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.
#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 """
0 Comments