3

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 ?

Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82
R.Yamada
  • 43
  • 5
  • do you mean replace way you accessing item of list ( access as `i-1,i` and not `i,i+1` ) ? – Andriy Ivaneyko Mar 13 '16 at 22:45
  • Yes. That is what I want to do, but I don't how to do it. I tried different ways but it doesn't seem like it goes through the entire list. – R.Yamada Mar 13 '16 at 22:54

2 Answers2

1

Print it after the inner loop gets over.

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

For the second part of your answer, you can use your inner loop to be

for ii in range(n-1,i,-1). // This runs the loop in reverse order.

      for i in range(1,n):
            for ii in range(n-1,0,-1):
                if L[ii-1].upper()<L[ii].upper():
                    tmp=L[ii-1]  
                    L[ii-1]=L[ii]
                    L[ii]=tmp

You meant this, right?

Priyansh Goel
  • 2,660
  • 1
  • 13
  • 37
1

Use feature of range method which allow you to iterate in reverse order

print range(1,3) # [1,2]
print range(3-1, 0, -1) # [1,2]

By the way you can avoid code duplication of loops ( if asc == False and if asc == True remove conditions).

To do it use function below(i've added comments):

def bubble(L, asc=False):
   n = len(L)
   # passing -1 as lest argument of range is allow to iterate in reverse order
   for i in range(n-1, 0, -1):
       for ii in range(n-1-i, 0, -1):
           # you assign boolean value which depends on `asc` param to comparison_result
           comparison_result = L[ii-1].upper() > L[ii].upper() if asc else L[ii-1].upper() < L[ii].upper()
           # check bool of comparison_result 
           if comparison_result:
               tmp=L[ii-1]
               L[ii-1]=L[ii]
               L[ii]=tmp
   print L


print bubble(['1', 'a', 's'])

Line

comparison_result =  if asc else L[ii-1].upper() < L[ii].upper()

is equivalent of:

if asc:
   comparison_result = L[ii-1].upper() > L[ii].upper()
else:
   comparison_result = L[ii-1].upper() < L[ii].upper()
Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82
  • Thank you. Your description is really easy to understand. I have learn more about python programming. – R.Yamada Mar 14 '16 at 01:50
  • @R.Yamada You are welcome :) Hope that was use full for you ! Also, you can take a look on my answer there: http://stackoverflow.com/questions/7286365/print-a-list-in-reverse-order-with-range-in-python/35977481#35977481 , it contain description particularly for getting reversed array with `range` method :) Have i nice day! – Andriy Ivaneyko Mar 14 '16 at 02:02
  • When I try to run the program, I have a error. UnboundLocalError: local variable 'comparison_result' referenced before assignment. Do you know why this happens? – R.Yamada Mar 14 '16 at 02:17
  • @R.Yamada that'a mean you are trying to use variable, before some value assigned to it. You can take a look on http://stackoverflow.com/questions/855493/referenced-before-assignment-error-in-python it has more explantions ... – Andriy Ivaneyko Mar 14 '16 at 02:29