In: Computer Science
Python:
The goal is to reverse the order of month and date.
# For example,
# output: I was born on 24 June
print(reverse("I was born on June 24"))
# output: I was born on June two
print(reverse("I was born on June two"))
#output: I was born on 1 January, and today is 9 Feb.
print(reverse("I was born on January 1, and today is Feb 9."))
My code (so far, works for first two not the last):
def reverseOrder(string):
newString = []
count = 0
for word in string.split():
count = count+1
if word.isdigit():
count = count+1
newString.append(word)
newString[count-3], newString[count-2] = newString[count-2],
newString[count-3]
else:
newString.append(word)
return ' '.join(newString)
Here iam providing the code and the explantion in the comments.
We have fixed the order in string i.e date is followed by month.
Code:-
Code in text format:-
def reverseMonth(string): #function to reverse the order
new_string = []
#array
new_string = string.split() # taking the list of
words in given string
for word in string.split(): # for each word in
string
if
word.isdigit(): # we check whether it is digit or
not
swap = new_string.index(word) # if word in digit we take the
index
number = new_string[swap] # taking the date
new_string[swap] = new_string[swap+1] # we swap the date with next
index which is month
new_string[swap+1] = number # swapping
else:
continue
return ' '.join(new_string) #finally join the
words
print(reverseMonth(" I was born on 1 January and today is 9
Feb"))
output:-
if you have any doubts please commnet.Thanking you