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...
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.
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?"
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.
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...
PYTHON PROBLEM: Goal: Design and implement a Python program to solve the following problem. Scientists measure...
PYTHON PROBLEM: Goal: Design and implement a Python program to solve the following problem. Scientists measure an object’s mass in kilograms and weight in newtons. If you know the amount of mass of an object in kilograms, you can calculate its weight in newtons with the following formula: [Note: Use or test any random numbers to check whether the weight is heavy, can be lifted or light] ????h? = ???? × 9.8 Write a program that asks the user to...
Customer Order Example ORDER NO: 61384                                    &
Customer Order Example ORDER NO: 61384                                                            ORDER DATE: 9/24/2014 CUSTOMER NO: 1273 CUSTOMER NAME: CONTEMPORARY DESIGNS CUSTOMER ADDRESS: 123 OAK ST. CITY STATE ZIP: AUSTIN, TX 28384 PRODUCT                                        QUANTITY    UNIT                          EXTENDED NO                DESCRIPTION         ORDERED    PRICE                        PRICE M128              BOOKCASE                4                   200.00                        800.00 B381               CABINET                      2                   150.00                        300.00            R210              TABLE                          1                   500.00                        500.00                                                                                     TOTAL                  1600.00 Normalize this user view. Make sure to show your work for each view – you should have 4 answers (e.g....
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT