Python selection sort

2024/11/15 15:48:24

Question: The code is supposed to take a file (that contains one integer value per line), print the (unsorted) integer values, sort them, and then print the sorted values.

Is there anything that doesn't look right? I know I could test it and I did test the selectionSort, which worked fine. But I don't really know how I could test whether it successfully takes the file and does what its supposed to do.

Thank you

filename=input('Enter file path:')
file = open(filename, 'r')
alist = [int(line) for line in file.readlines()]
print(alist)def selectionSort(alist):for index in range(0, len(alist)):ismall = indexfor i in range(index,len(alist)):if alist[ismall] > alist[i]:ismall = ialist[index], alist[ismall] = alist[ismall], alist[index]return alist 
Answer

Your selection sort seems to be correct but the part before it has issues:

(I am assuming this is Python 2.X, if it isn't ignore my answer)

  • You need to use raw_input not input.
  • file.readlines() doesn't get rid of the \n at the end of every line. You need to strip it away.

The corrected code:

filename=raw_input('Enter file path:')
file = open(filename, 'r')
alist = [int(line.strip()) for line in file.readlines()]
print(alist)
https://en.xdnf.cn/q/119405.html

Related Q&A

Simple inheritance issue with Django templates

just getting started in Django, and I have some problems with the inheritances. It just seems that the loop for doesnt work when inheriting other template. Heres my code in base.html:<!DOCTYPE html&…

Replacing values in a list [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Azure Release Pipeline - Environment variables on python script

Lately Ive been requested to run a python script on my Azure Release Pipeline. This script needs some environment variables for being executed, as Ive seen that in the build pipeline, the task include …

Problem with python prepared stmt parameter passing

File C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector\cursor.py, line 1149, in execute elif len(self._prepared[parameters]) != len(params): TypeError: object of ty…

list of lists to list of tuples without loops or list comprehensions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 6 years ago.Improve…

How can I merge CSV rows that have the same value in the first cell?

This is the file: https://drive.google.com/file/d/0B5v-nJeoVouHc25wTGdqaDV1WW8/view?usp=sharingAs you can see, there are duplicates in the first column, but if I were to combine the duplicate rows, no…

i usually get this error : ValueError: invalid literal for int() with base 10

I have loaded a csv file and as i try to print it i get this error Traceback (most recent call last):File "C:\Users\FSTC\Downloads\spaceproject\main.py", line 389, in <module>world_data…

How to Draw a triangle shape in python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 1…

DataFrame from list of string dicts with array() values

So I have a list where each entry looks something like this: "{A: array([1]), B: array([2]), C: array([3])}"I am trying to get a dataframe that looks like thisA B C 0 1 2 3 1 4 …

Need Help Making Buttons to perform for loops when you input a number

I am trying to make a button in Maya using Python that when you type in a number the for loop would loop for that many times. For example, I would put 5 in the box so the for loop would loop 5 times re…