From 7351bbbc019e9474f9157aabf00b2d3c76dc56c8 Mon Sep 17 00:00:00 2001 From: Yohaan Date: Fri, 23 Oct 2020 04:13:51 +0000 Subject: [PATCH] day 3 reverse each word --- day3_reverse_each_word.py | 14 +++++++++++--- main.py | 38 ++++++++++++++++++++++++++------------ 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/day3_reverse_each_word.py b/day3_reverse_each_word.py index d11932d..50939a4 100644 --- a/day3_reverse_each_word.py +++ b/day3_reverse_each_word.py @@ -1,10 +1,19 @@ -# Required concepts: https://www.w3schools.com/python/python_intro.asp Everything till for loops +# Required concepts: https://www.w3schools.com/python/python_intro.asp strings and slicing print('Question: reverse each word of string') sentence = 'hello world how are you' res = '' +for word in sentence.split(): + # get each word from the split function + res += word[::-1]+" " + #add the reverse of it + +print('Answer:') +print(res) + +""" # what to reverse? # reverse upto the space left = 0 @@ -18,7 +27,6 @@ res += word[::-1] left = right #now start from here +""" -print('Answer:') -print(res) diff --git a/main.py b/main.py index a7be56f..50939a4 100644 --- a/main.py +++ b/main.py @@ -1,18 +1,32 @@ -# Required concepts: https://www.w3schools.com/python/python_intro.asp strings +# Required concepts: https://www.w3schools.com/python/python_intro.asp strings and slicing -print('Question: for sets A and B return all pairs {A(i) , B(-i)}') +print('Question: reverse each word of string') +sentence = 'hello world how are you' +res = '' -l1 = ["hi","hello","bye"] -l2 = ["shivam",'raj','roy'] +for word in sentence.split(): + # get each word from the split function + res += word[::-1]+" " + #add the reverse of it -for i in range(len(l1)): - print(l1[i]+" "+l2[-(i+1)]) - # match first element of l1, - # with last element of l2 +print('Answer:') +print(res) + +""" +# what to reverse? +# reverse upto the space +left = 0 +while left < len(sentence): + right = sentence.find(' ',left) + if right == -1: + right = len(sentence) + word = sentence[left:right] + # now add this reversed word to result! + print(res) + res += word[::-1] + left = right + #now start from here +""" -#single line -res = [(l1[i],l2[-i-1]) for i in range(len(l1))] -print('Answer:') -print(res) \ No newline at end of file