1

I have a txt file that contains data in the following fashion:

13
56
9
32
99
74
2

each value in a different file. I created three function:

the first one is to swap the values

def swap(lst,x,y):
    temp = lst[x]
    lst[x] = lst[y]
    lst[y] = temp

and the second function is to sort the values:

def selection_sort(lst):
    for x in range(0,len(lst)-1):
        print(lst)
        swap(lst,x,findMinFrom(lst[x:])+ x)

the third function is to find the minimum value from the list:

def findMinFrom(lst):
    minIndex = -1
    for m in range(0,len(lst)):
        if minIndex == -1:
            minIndex = m
        elif lst[m] < lst[minIndex]:
            minIndex = m
    return minIndex

Now, how can I read from the file that contains the numbers and print them sorted?

Thanks in advance!


I used:

def main():
    f = []
    filename = input("Enter the file name: ")
    for line in open(filename):
        for eachElement in line:
            f += eachElement
    print(f)
    selectionSort(f)
    print(f)
main()

but still not working! any help?

4
  • Please read and apply the help->tour when you ost here. It states "There is no chit-chat" and despite that your post contains "Thanks in advance" Commented Sep 29, 2015 at 22:23
  • Your swap function has a superfluous temp variable and its body can be written in a single line; lst[x], lst[y] = lst[y], lst[x], why make it so complicated as you do? Commented Sep 29, 2015 at 22:26
  • Anthon, I know about this line but it's not recommended as it is only existing in python, so it's actually a bad practice for me as a programmer. Commented Sep 29, 2015 at 22:33
  • 2
    Where do have that misinformation from? I used parallel assignment in Occam2 before Python was even created. And wikipedia lists 10 programming languages that can do that by name, in a non-exhaustive list. Commented Sep 29, 2015 at 22:41

2 Answers 2

3

Good programmers don't reinvent the wheel and use sorting routines that are standard in most modern languages. You can do:

with open('input.txt') as fp:
    for line in sorted(fp):
        print(line, end='')

to print the lines sorted alphabetically (as strings). And

with open('input.txt') as fp:
    for val in sorted(map(int, fp)):
        print(val)

to sort numerically.

Sign up to request clarification or add additional context in comments.

3 Comments

Monica, I can't use any build-in python sort function for this assignment. The idea is to create a function to do so, and I made one. now how can I sort the numbers in a file using my sorting function?
@r4b: there are two independent tasks: read numbers from a file, sort the numbers. Solve one problem at a time (divide-and-conquer). If you want to know how to read numbers from a file; forget about the sorting for a moment and learn how to read numbers. Your selection_sort() doesn't look like it sorts anything; to fix it, forget about reading numbers from a file and learn how to sort a list of integers instead. Once you've done the independent tasks; you could combine them (sometimes it may simplify things or allow to make the code more efficient but you can ignore it for now)
My selection_sort() does sort and I test it myself!
0

To read all the lines in a file:

f = open('test.txt')
your_listname = list(f)

To sort and print

selection_sort(output_listname)
print(output_listname)

You may need to strip newline characters before sorting/printing

stripped_listname=[]
for i in your_listname:
    i = i.strip('\n')
    stripped_listname.append(i)

You probably also want to take the print statement out of your sort function so it doesn't print the list many times while sorting it.

2 Comments

Knells, should I do that in the main function? and I also used the input function so the user choose the file they want, so : f = input("Name of the file: ")
I you only need to do it once, the main function is perfectly fine. Don't put it in any o the functions you posted. f = open(input("Name of the file: ")) will get data from a file with input filename.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.