I cannot find a boolean error in this bubble sorting code
I'm trying to do some simple bubble sorting code to familiarize myself with the list / string and method usage, but for some reason, when I try to iterate through each value in the list to remove the space and values that are non ints, it misses some. I didn't even get into the sorting part of the bubble.
#test data: 45,5j, f,e,s , , , 45,q,
if __name__ == "__main__":
getList = input("Enter numbers separated by commas:\n").strip()
listOfBubbles = getList.split(',')
print (listOfBubbles)
i = 0
for k in listOfBubbles:
listOfBubbles[i] = k.strip()
print ("i = {0} -- Checking '{1}'".format(i,listOfBubbles[i]))
if listOfBubbles[i] == '' or listOfBubbles[i] == ' ':
del listOfBubbles[i]
i -= 1
else:
try:
listOfBubbles[i] = int(listOfBubbles[i])
except ValueError as ex:
#print ("{0}\nCan only use real numbers, deleting '{1}'".format(ex, listOfBubbles[i]))
print ("deleting '{0}', i -= 1".format(listOfBubbles[i]))
del listOfBubbles[i]
i -= 1
else:
print ("{0} is okay!".format(listOfBubbles[i]))
i += 1
print(repr(listOfBubbles))
Output:
Enter numbers separated by commas:
45,5j, f,e,s , , , 45,q,
['45', '5j', ' f', 'e', ', ' ', ' ', ' 45', 'q', '']
i = 0 -- Checking '45'
45 is okay!
i = 1 -- Checking '5j'
deleting '5j', i -= 1
i = 1 -- Checking 'e'
deleting 'e', i -= 1
i = 1 -- Checking ''
i = 1 -- Checking '45'
45 is okay!
i = 2 -- Checking 'q'
deleting 'q', i -= 1
[45, 45, ' ', ' 45', 'q', '']
a source to share
How about a more pythonic way?
#input
listOfBubbles = ['45', '5j', ' f', 'e', ', ' ', ' ', ' 45', 'q', '']
#Copy input, strip leading / trailing spaces. Remove empty items
stripped = [x.strip() for x in listOfBubbles if x.strip()]
# list(filtered) is ['45', '5j', 'f', 'e', 's', '45', 'q']
out = []
for val in filtered:
try:
out.append(int(val))
except:
# don't do anything here, but need pass because python expects at least one line
pass
# out is [45, 45]
Finally, to get to the correct answer
out.sort()
Update To clarify the pass
>>> for i in range(0,5):
pass
print i
0
1
2
3
4
a source to share
Never change the list itself that you are looping - inside the loop for k in listOfBubbles:
you delete some elements of this very list and break the logic of the inner loop. There are many alternative approaches, but the simplest solution is to cycle on the list of copies that you want to change: for k in list(listOfBubbles):
. There may be more problems, but this is the first one.
a source to share
Nevermind, fixed it. I change the loop to a for .. for a time.
if __name__ == "__main__":
getList = input("Enter numbers separated by commas:\n").strip()
listOfBubbles = getList.split(',')
print (listOfBubbles)
i = 0
while i < len(listOfBubbles):
listOfBubbles[i] = listOfBubbles[i].strip()
print ("i = {0} -- Checking '{1}'".format(i,listOfBubbles[i]))
if listOfBubbles[i] == '' or listOfBubbles[i] == ' ':
del listOfBubbles[i]
i -= 1
else:
try:
listOfBubbles[i] = int(listOfBubbles[i])
except ValueError as ex:
#print ("{0}\nCan only use real numbers, deleting '{1}'".format(ex, listOfBubbles[i]))
print ("deleting '{0}', i -= 1".format(listOfBubbles[i]))
del listOfBubbles[i]
i -= 1
else:
print ("{0} is okay!".format(listOfBubbles[i]))
i += 1
print(repr(listOfBubbles))
a source to share
You cannot use an iterator to remove from a list, because the length changes.
Instead, you should use the index in a for loop (or in a while loop).
Once you have removed the item, you need to scroll through the list again.
pseudo code:
again:
for i = 0 to list.count - 1
{
if condition then
delete list [i]
goto again;
}
If you are going to remove from a list, iterate through it, go to the list in reverse order:
for( i = myList.length - 1; i >= 0; i-- ) {
// do something
if( some_condition ) {
myList.deleteItem( i );
}
}
This way you won't skip the elements of the list, since the reduction of the list does not affect future iterations. Of course, the above snippet assumes that the deleteItem method is supported in the list / array class and takes action accordingly.
a source to share