Question

Python code for reverse: Reverse from: ['row row row your boat', 'gently down the stream.', 'happily...

Python code for reverse:

Reverse from:

['row row row your boat',
'gently down the stream.',
'happily happily happily happily',
'life is but a dream' ],

Output should be:

['boat your row row row',

'stream the down gently.',

'happily happily happily happily',

'dream a but is life']

How can I achieve this?

Homework Answers

Answer #1

Code Explanation-

Given a list containing strings ,we have to reverse the strings with out changing the original words of the string.

We can do it by using reversed() method in python but in the given input it contains characters such as ',' and it is not interchanged i.e it is in same place evn after the reversing the string ,inorder to handle these cases we are using regular expressions and finding if there is a character such as '.' if the character is there we do not move it we place it in the same original place and reverse the other words in the string ,not only '.' it ma contain characters such as , ?; like these we will handle these cases too.

Note- This program needs regular expressions to handle '.' and other characters such as ?;,

Below is the code with clear comments and explanation-

#importing regular expression to acheive the testcases such as .,? 
import re



#r'([,:;?!.]+)$'
#our function to reverse words in a given list of strings
def myreverse(listinput):
    #creating a string of ,:?!. to handle these in the strings 
    handlingother= ',:;?!.'
    #creating a empty list to return the result
    z=[]
    #iterating through the list of strings
    for i in range(len(listinput)):
        #checking if each string in the list has .,:?!. 
        checkother=re.search(r'([,:;?!.]+)$',listinput[i])
        #if the value of checkother is none  means if the string does'nt contain any character like .?!; 
        #we keep it as empty string and store in a variable called finalvalue
        if(checkother is None):
            finalvalue=''
        #if the value of checkother is not none
        else:
            #the group method returns the characters which match the character such as ,?. in the string 
            #and store in the variable finalvalue
            finalvalue=checkother.group(True)
        #creating a variable which stores our reversed string result
        #we are using rstrip method to remove character such as .,? by finding the characters like .,? using
        #findall method as a regular expression and reversing the whole string and appending with spaces using join
        #method and at last concatenating the finalvalue at end using + symbol
        new= ' '.join(reversed(re.findall(r'[\w,:;?!.]+',listinput[i].rstrip(handlingother))))+finalvalue
        
        #appending the result to a list called z
        z.append(new)
        
    #returning oyr result
    return z 


#our input 
listinput=['row row row your boat',
'gently down the stream.',
'happily happily happily happily',
'life is but a dream']
#callling our function myreverse by passing a parameter listinput which contains our input as per the question.
print(myreverse(listinput))
        

Code with output screens-

(Hope you like the work if you have any doubt please comment I will Defininately help you.)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
For the given table, write a code in python to delete the row if the row...
For the given table, write a code in python to delete the row if the row has more than 70% of it's values as 0 or Nan In the table there are 11 values in a row without considering ID and Name, so if there more than 7 values are null values then the row should be deleted. **code should be in python using pandas library ID Name a b c d e f g h i j k 7...
Using loop logic in a python code snippet, create a loop that counts down from 15...
Using loop logic in a python code snippet, create a loop that counts down from 15 to 5 by 3's printing the count as it is processing. example output: Counter Example count =  15 count =  10 count =  5
How can i make this lunix code print 3 numbers in reverse it must be in...
How can i make this lunix code print 3 numbers in reverse it must be in printStars format and no loops Here is the code i have but can figure out how to reverse the numbers #include<stdio.h> void printStars(int n) { if (n>0){ printf(""); printStars(n-1); } } int main() { int n; int b; int c; printf("Enter 3 numbers to reverse "); scanf("%d",&n,&b,&c); printf("your reversed numbers are %d",n); printStars(n); return 0;
python question lets say you had a string that was returned from a block of code...
python question lets say you had a string that was returned from a block of code that converted it from letters to numbers. for example, the user input was "hi", the code converted it into "89" as a string with a dictionary how could you take that "89" and go back into hi again? lets say the dictionary was keys and values of keys = alphabet and values = numbers the output would be back at "hi" again Convert into...
I need python code for this. Write a program that inputs a text file. The program...
I need python code for this. Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order. Uppercase words should take precedence over lowercase words. For example, 'Z' comes before 'a'. The input file can contain one or more sentences, or be a multiline list of words. An example input file is shown below: example.txt the quick brown fox jumps over the lazy dog An example of the program's output...
1) A man is in a boat 2 miles from the nearest point on the coast....
1) A man is in a boat 2 miles from the nearest point on the coast. He is to go to point Q, located 3 miles down the coast and 1 mile inland (see figure). He can row at a rate of 4 miles per hour and walk at 4 miles per hour. Toward what point on the coast should he row in order to reach point Q in the least time? (Round your answer to two decimal places. 2)...
WRITE USING PYTHON PROGRAMMING THE CODE GIVEN BELOW HAS SOME ERRORS WHICH NEED TO BE SOLVED....
WRITE USING PYTHON PROGRAMMING THE CODE GIVEN BELOW HAS SOME ERRORS WHICH NEED TO BE SOLVED. ALSO THE 2 POINTS MENTIONED BELOW SHOULD BE PRESENT IN THE CODE Write a script that calculates the 3 longest words of a text stored in a file and print them from the longest to the smaller of the 3. Please note: 1. The name of the file is word_list.csv and it doesn’t need to be asked to the user (meaning the name will...
Use the Pythone programming language. see the #bold hashtags in the code for instructions. Also use...
Use the Pythone programming language. see the #bold hashtags in the code for instructions. Also use the website provided for an idea on how the code should be approached. please provide the code along with a screen shot of the code working with the outcome. s1 = str("abc text xyz") print("String s1 =", s1) print("First letter of s1 = ", s1[0]) print("length of s1= ", len(s1))#len for length length = len(s1) last = s1[length-1] print("Last letter = ", last) print("First...
How to code this in python Write a program that computes and prints the first 1000...
How to code this in python Write a program that computes and prints the first 1000 prime numbers - simply write out each prime number on a new line. In implementing this solution I want you to define 2 functions: is_prime which can be used to test whether a number is a prime (e.g. is_prime(17) returns True but is_prime(9) returns False) and compute_primes which will return an array (or Python list) of the first n primes where n is passed...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...