[Python] exercises to achieve reverse a string

Title four string achieve reversal
1  # manner a cycle 
2 temStr = ' adbeorj1305 ' 
. 3 newStr = '' 
. 4 I = 0
 . 5  the while I < len (temStr):
 . 6      newStr + = newStr temStr [len (temStr)-l- I]
 . 7      I = I + . 1
 . 8  Print (newStr, ' string inversion ' )
 . 9  
10  # way two sections 
. 11 temStr = ' adbeorj1305 ' 
12 is  Print (temStr [:: -. 1], ' slice ' )
 13 is  
14  #Three ways to use the reverse method list 
15 temStr = ' adbeorj1305 ' 
16 Ll = List (temStr)
 . 17  L1.reverse ()
 18 is S2 = '' .join (Ll)
 . 19  Print (S2, " borrowing inversion method list ' )

 

1  # Four ways the lambda using the reduce 
2  #lambda used to write a simple function, but def more powerful to handle tasks. Which way it clearer which way use, do not blindly use lambda expressions.
. 3  
. 4  # manner using five stacks 
. 5 temStr = ' adbeorj1305 ' 
. 6 Ll = List (temStr)
 . 7 Sl = '' 
. 8  the while len (Ll)> 0:
 . 9      Sl = Sl + L1.pop ()
 10  Print (Sl, ' using stack mode using pop ' )
 11  # ###################### following is an example of failure 
12 is temStr = ' adbeorj1305 ' 
13 is Ll = List (temStr)
 14 = Sl '' 
15  for I in Ll:
16      Sl = Sl + L1.pop () # also after sequentially pop, stitching 
. 17      Print (Sl, ' Ll ' , Ll)
 18 is  # Print (Sl, 'the use of the stack, using pop mode', Ll) 
. 19  # # output They are: 5031jr why the latter is not performed, (⊙ o ⊙) ..., 
20 is  ' '' 
21 is  Causes: pop after execution, the variable length L1, and the index value i in L1 accessed in increasing i,
 22  when i = r, index = 6 enters the next cycle,
 23  but this time L1 = [ 'a', ' d', 'b', 'e', 'o'] =. 5 the maximum index,
 24  thus cycle end, subsequent elements can not be added.
25  '' '

 



Guess you like

Origin www.cnblogs.com/ww-xiaowei/p/11310704.html