I have a question on how to get the following output in python 2.7.
>> bubble(['abe','Ada','bak','bAr'], False)
['Ada', 'bak', 'bAr', 'abe']
['bak', 'bAr', 'Ada', 'abe']
['bAr', 'bak', 'Ada', 'abe']
>> bubble(['Adm','abe','bAr','bak'], False)
['Ada','bAr','bak','abe']
['bArt','bak','Ada','abe']
The input for the functions are the following. A list of strings (L) and a Boolean value which represent ascending alphabetical order (asc=True) or descending alphabetical order (asc=False). In both cases, it is in dictionary order. I want to print out the state of the list every time there is a pass.
def bubble(L, asc):
n = len(L)
if asc == False:
for i in range(1,n):
for ii in range(n-i):
if L[ii].upper()<L[ii+1].upper():
tmp=L[ii]
L[ii]=L[ii+1]
L[ii+1]=tmp
print L
if asc == True:
for i in range(1,n):
for ii in range(n-i):
if L[ii].upper()>L[ii+1].upper():
tmp=L[ii]
L[ii]=L[ii+1]
L[ii+1]=tmp
print L
In addition, can I replace the way I am currently accessing item of list to i-1
and i
and not i
,i+1
?