Love Letter in Python: Very simple code for Love Letter program of Nagarro's coding round question


Hello friends how are you all, Today in this blog i will teach you a very interesting program which is being asked today in coding round of a Multinational Software Company.  If you are preparing for job in a software company then this type of question will help you definitely. 

If you have only basic knowledge of programming still you can understand this program because i am giving a very simple code of this program.

Question: Love Letter in Python

Description: You write a love letter to your friend. However , before your friend can read it, someone else takes it and rotates the characters of each words left to right K times. Find the number of words that remain the same even after this shifting of letters.

Input Specification: 

Input1: String of words                                                                                                                  Input2: K, number of times rotation happens

Output Specification:

Your function should return the number of correct words.

Example 1:
String:loHel endFri
K:3
Output:0
Expalation:Here "loHel endFri" is a rotated string so the original string was "Hello Friend" which is not correct. Hence the output will be 0.

Example 2:
String:Hello dFrien
K:5
Output:1
Expalation:Here "Hello dFrien" is a rotated string so the original string was "Hello Friend" so here one word Hello is remain same after rotation so the final output will be 1.

If you want to understand this program easily then you must have knowledge of the following topics:
 

Program: 
Here is the complete code of this program if you want to run this just copy the code and paste it into an editor and run it to check output.

#define function to rotate word
def rotateWord(str,k):
    #splitting the string
    strRotate = str.split()
    #loop for number of rotation
    for i in range(0,k):
     #loop for number of words
     for j in range(0, len(strRotate)):
        #shifting 1 character from left to right
        strRotate[j] = strRotate[j][1:] + strRotate[j][0]
    #initializing counter
    count = 0
    #loop for getting rotating words
    for k in range(0, len(strRotate)):
        #checking rotating words in original string
        if strRotate[k] in str:
            #incrementing counter
            count+=1
    print(count)
#calling rotateword function
rotateWord("adaada", 3)
rotateWord("loHel endFri", 3)
rotateWord("Hello dFrien", 5)
"""
Output
1
0
1
"""

If you wan this program in any other language then  just comment name of programming language i will give you the code in your specified language.

Request:-If you found this post helpful then let me know by your comment and share it with your friend. 
 
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

Thanks.

Don't use my blog post without my permission.

Post a Comment

0 Comments