generic code to reverse alternate words in a sentence using python -
can please on how reverse alternate words in sentence end using python.
note: not use reversed()
eg:
aspire systems in america
acirema in si systems eripsa
s = 'aspire systems in america'
l = len(s) - 1
rev = ''
for in range (0,l):
rev = rev + str(a[l]) l--
- i couldnt find way jump "in" , print it.
can use split/join ?
s = 'aspire systems in america' li = s.split(' ') #["aspire", "systems", "is", "in", "america"] #reverse alternate words index in range (0,len(li),2): #0, 2, 4 rev = '' ch in li[index]: #'a', 's', 'p' ... rev = ch + rev li[index] = rev #now reverse list of words revli = [] word in li: revli = word + revli print ' '.join(revli) #should print expected result
it should equivalent following code, uses reversed :
s = 'aspire systems in america' li = s.split(' ') #["aspire", "systems", "is", "in", "america"] #reverse alternate words index in range (0,len(li),2): #0, 2, 4 li[index] = reversed(li[index]) print ' '.join(reversed(li)) #should print expected result
i cannot test code currently, please tell me if missing or if have question concerning code. if learning programming, it's important understand it
edit: corrected join call