Jul-28-2018, 02:16 PM
Hi everyone, I came across this problem(which is quite interesting):
input: a sentence
output: that sentence in reverse
E.g: input: 'abc 123 abcd'
output:'abcd 123 abc'
I succeeded in solving this problem but my code is too long.
input: a sentence
output: that sentence in reverse
E.g: input: 'abc 123 abcd'
output:'abcd 123 abc'
I succeeded in solving this problem but my code is too long.
sentence=input('enter a sentence: ') new_sen=[] def split(sen): if len(sen)==0: return new_sen else: if sen.find(' ')!= -1: for i in range(0,len(sen)): if sen[len(sen)-i-1]!=' ': pass else: first=sen[:(len(sen)-i)] second=sen[(len(sen)-i):] new_sen.append(second) break count=0 for i in range(0,len(first)): if first[len(first)-i-1]==' ': count+=1 else: break new_sen.append(' '*count) global third third=first[:(len(first)-count)] split(third) else: new_sen.append(third) return new_sen final_list=split(sentence) emp_list='' for i in range(0,len(final_list)): emp_list+=final_list[i] print(emp_list)I'm trying to find many others ways to solve this. I'm looking forward to listening to your ideas and algorithm.