Question

In: Computer Science

Python: The goal is to reverse the order of month and date. # For example, #...

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)

Solutions

Expert Solution

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


Related Solutions

PYTHON 1. Write a for loop to iterate across a user entered string in reverse order...
PYTHON 1. Write a for loop to iterate across a user entered string in reverse order Use an input statement to ask the user for a string using the prompt: Cheer? [space after the ?] and assign to a variable Start the for loop to iterate across the elements in the string in reverse order Display each element of the string on screen After the for loop ends, display the entire string 2. Write a for loop with a range...
example of reverse offshoring
example of reverse offshoring
Date - month: int - day: int - year: int +Date() +Date(month: int, day: int, year:...
Date - month: int - day: int - year: int +Date() +Date(month: int, day: int, year: int) +setDate(month: int, day: int, year: int): void -setDay(day: int): void -setMonth(month: int): void -setYear(year: int): void +getMonth():int +getDay():int +getYear():int +isLeapYear(): boolean +determineSeason(): string +printDate():void Create the class Constructor with no arguments sets the date to be January 1, 1900 Constructor with arguments CALLS THE SET FUNCTIONS to set the Month, then set the Year, and then set the Day - IN THAT ORDER...
Python: What are the defintions of the three method below for a class Date? class Date(object):...
Python: What are the defintions of the three method below for a class Date? class Date(object): "Represents a Calendar date"    def __init__(self, day=0, month=0, year=0): "Initialize" pass def __str__(self): "Return a printable string representing the date: m/d/y" pass    def before(self, other): "Is this date before that?"
Example of a Long-Term Goal: Beginning January 2020, contribute $600 per month (2,000 per year) to...
Example of a Long-Term Goal: Beginning January 2020, contribute $600 per month (2,000 per year) to my Roth IRA for 40 years earning a rate of 8% per year for an ending balance of $518,113 on December 31, 2060. (This example used a time value of money calculation of N = 40, PMT = 2000, I = 8%) Based upon this example, create two other long term goals.
Question: Reverse engineering (Python if possible) In the FizzBuzz question, it's an exercise to calculate a...
Question: Reverse engineering (Python if possible) In the FizzBuzz question, it's an exercise to calculate a value based on whether the input was divisible by 3, 5, or both. If we looped that function through integers from one, the first 10 return values would be: None # For input 1 None # For input 2 '3' None '5' '3' None None '3' '5' In this exercise, we'll try to reverse engineer the output of a similar process. The sequence of...
Give an example of a financial goal and explain how this goal meets each of the...
Give an example of a financial goal and explain how this goal meets each of the SMART criteria.
Python The goal of this assignment is to build a simulation of a popular children’s dice...
Python The goal of this assignment is to build a simulation of a popular children’s dice game named Beat That. Beat That is an educational game in which children learn strategic thinking and the concept of place value. While the rules of the game can be flexible, we will be playing a basic version in which 2 players (Player A and Player B) are rolling 2 dice in a 5-round game. Game play is based on rounds. In each round,...
The following code was meant to print out the elements in an array in reverse order....
The following code was meant to print out the elements in an array in reverse order. However, it does not behave correctly. public static void reverse(int[] a, int index) {       if (index == (a.length - 1))         System.out.printf("%d%n", a[index]);       else {         reverse(a, index); What does it do? Explain why it behaves in this way and There is more than one error in the code. Correct the code so that it will recursively print out the elements of...
/* print_reverse function that prints the nodes in reverse order if the list is empty print...
/* print_reverse function that prints the nodes in reverse order if the list is empty print nothing */ include <bits/stdc++.h> using namespace std; class Node{     public:     int data;     Node *next;     Node *prev;     Node(int d, Node *p=NULL, Node *n=NULL){         data = d;         prev = p;         next = n;     } }; class DLL{     public:     Node *head;     DLL(){head=NULL;}     void push(int d){         Node *pNode = new Node(d,NULL,head);         if (head != NULL)             head->prev = pNode;         head = pNode;     }     void print_forward(){         Node *pCurr = head;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT