I tried to use a different forum but I was completely out of my league with the responses. I am extrememly new to python. As a project, I'm supposed to ask the user to input a file name, check that the file exists, output the numbers in the file, and sort them for biggest to smallest. The trick is, I can't use built in functions to do the sorting. I have to use this algorithm:

for each i from range(n)
    for each j from range(n-i)
        if A[j-1]<A[j]
            swap(A[j], A[j-1])

Here's what I've been playing with:

def main():
    try:
        file=open(input("Please enter the name of the file you wish to open:" ))
        A =file.read().split()
        n=len(A)
        print ("These following", n,"numbers are in the inputted file:\n", A)

        new_list=[]

        while A:
            minimum=A[1]
            for i in range(n):
                for j in range(n-1):
                    if A[1]<A[2]:
                        minimum=A[1]
                        new_list.append(minimum)
                        A.remove(minimum)

            minimum=A[2]
            for i in range(n):
                for j in range(1,3):
                    if A[2]<A[1]:
                        minimum=A[2]
                        new_list.append(minimum)
                        A.remove(minimum)

            minimum=A[2]
            for i in range(n):
                for j in range(1,2):
                    if A[3]<A[4]:
                        minimum=A[3]
                        new_list.append(minimum)
                        A.remove(minimum)

            minimum=A[3]
            for i in range(n):
                for j in range(1,1):
                    if A[4] < A[0]:
                        minimum=A[4]
                        new_list.append(minimum)
                        A.remove(minimum)
            #minimum=A[4]
            #for i in range(n):
                #for j in range(n-i):
                    #if A[4]<A[0]:
                        #minimum=A[4]
                        #new_list.append(A[4])



                #if A < minimum:
                   # minimum = A


            print (new_list)
            break



        file.close()

    except IOError as e:
        print("({})".format(e))


    #new_list = []

    #while A:
        #minimum = A[0]  
    #for x in A: 
        #if x < minimum:
            #minimum = x
    #new_list.append(minimum)
    #data_list.remove(minimum)    

#print (new_list)



main()

Recommended Answers

All 4 Replies

You have the pseudo code for your sort algorithm, so simply expand it to Python and test it ...

mylist = [5, 3, 7, 2, 8, 4]
print(mylist)
n = len(mylist)

for i in range(n):
    for j in range(1, n-i):
        # swap if prev value is less than current value
        # change < to > to reverse the order
        if mylist[j-1] < mylist[j]:
            # do a tuple swap
            (mylist[j-1], mylist[j]) = (mylist[j], mylist[j-1])

print(mylist)

''' result ...
[5, 3, 7, 2, 8, 4]
[8, 7, 5, 4, 3, 2]
'''

You have no idea how much you've helped! thank you so much. I just have another question, how do I turn my list into strings?
Heres what I did but for some reason it only sorts every 3 numbers and not the whole thing. I figure if I make A into a string it would fix my problem!

def main():
    try:

        file=open(input("Please enter the name of the file you wish to open:" ))
        A =file.read().split()
        file.close()
        n = len(A)
        print ("These following", n,"numbers are in the inputted file:\n", A)

        for i in range(n):
            for j in range(1,n-i):
                if A[j-1] < A[j]:
                    (A[j-1], A[j]) = (A[j],A[j-1])
        print("We can now organize it in descending order:\n", A)

    except IOError as e:
        print("({})".format(e))


    Output_File = input("Where would you like to save this data?")
    fileObject = open(Output_File, 'a')
    fileObject.write(str(Output_File)+'\n')
    print("Your file is now saved as",Output_File,".\nHave a nice day!")
    fileObject.close()

if __name__ == '__main__':
    main()

Without using join() you can do it this way ...

mylist = [8, 7, 5, 4, 3, 2]

mystr = ""
space = " "
for n in mylist:
    mystr += str(n) + space

print(mystr)

'''
8 7 5 4 3 2 
'''

If you are allowed to use join(), you can do this ...

mylist = [8, 7, 5, 4, 3, 2]
mystr = " ".join(str(n) for n in mylist)
print(mystr)

'''
8 7 5 4 3 2
'''
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.