user1906407
user1906407

Reputation: 1

Putting elements from file in descending order without built in functions

I re-did the program following the bubble sort.

def main():
try:
    array=[]
    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,". \n Have a nice day!")
fileObject.close()

if name == 'main': main()

The problem is, it sorts every 3 numbers in the list. so if i have 9 numbers it will have 3 different ones. for example, 1 -3 10 6 5 0 3 -5 20, will be, ['6', '5', '3', '20', '10', '1', '0', '-5', '-3']. What could be wrong now? And did I do the output file right?

Upvotes: 0

Views: 886

Answers (2)

user1906407
user1906407

Reputation: 1

I finished it! I just had to figure out a different way to turn my list into integers. Here it is:

def main():
try:
    file=open(input("Please enter the name of the file you wish to open:" ))
    A = []
    #Here I convert the list to integers to separate as numbers in order to sort later
    for val in file.read().split():
        A.append(int(val))
    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]) #swap
    print("We can now organize it in descending order:\n", A)

    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()

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




if __name__ == '__main__':
main()

Upvotes: 0

Hunter McMillen
Hunter McMillen

Reputation: 61512

Where you have this line:

x = minimum

I think you meant:

minimum = x

Seems like you just got the assignment order incorrect. Assigning to the variable x during your iteration of A wont have any side effects.

EDIT

Your problem as I discovered in the comments is that you are using the readlines() function, but only have a single line in your file. What you really want to do is read that line then use split() to generate a list:

A = file.read().split()

Keep in mind though that since you are using strings when comparing with '<', you will not get numerical order after your code running you will get lexicographic order.

example:

inputs:

5 4 14 6 -1 2 0 9 8 7 3 4 -10 200

output:

['-1']
['-1', '-10']
['-1', '-10', '0']
['-1', '-10', '0', '14']
['-1', '-10', '0', '14', '2']
['-1', '-10', '0', '14', '2', '200']
['-1', '-10', '0', '14', '2', '200', '3']
['-1', '-10', '0', '14', '2', '200', '3', '4']
['-1', '-10', '0', '14', '2', '200', '3', '4', '4']
['-1', '-10', '0', '14', '2', '200', '3', '4', '4', '5']
['-1', '-10', '0', '14', '2', '200', '3', '4', '4', '5', '6']
['-1', '-10', '0', '14', '2', '200', '3', '4', '4', '5', '6', '7']
['-1', '-10', '0', '14', '2', '200', '3', '4', '4', '5', '6', '7', '8']
['-1', '-10', '0', '14', '2', '200', '3', '4', '4', '5', '6', '7', '8', '9']

Notice above how 200 is not at the end but comes after 2, to get numerical order you need to coerce the strings into a numeric data type, probably an int. You can easily do this when you read the numbers from the file using the map function:

A = map(int, file.read().split())

This will call the int cast function on every element returned by split before storing the element in A. After this change this is the output that I see from your program:

inputs:

5 4 14 6 -1 2 0 9 8 7 3 4 -10 200

output:

[-10]
[-10, -1]
[-10, -1, 0]
[-10, -1, 0, 2]
[-10, -1, 0, 2, 3]
[-10, -1, 0, 2, 3, 4]
[-10, -1, 0, 2, 3, 4, 4]
[-10, -1, 0, 2, 3, 4, 4, 5]
[-10, -1, 0, 2, 3, 4, 4, 5, 6]
[-10, -1, 0, 2, 3, 4, 4, 5, 6, 7]
[-10, -1, 0, 2, 3, 4, 4, 5, 6, 7, 8]
[-10, -1, 0, 2, 3, 4, 4, 5, 6, 7, 8, 9]
[-10, -1, 0, 2, 3, 4, 4, 5, 6, 7, 8, 9, 14]
[-10, -1, 0, 2, 3, 4, 4, 5, 6, 7, 8, 9, 14, 200]

Upvotes: 3

Related Questions