Virtusa(CEO-IT) 2022 OffCampus Normal Coding Questions And Answers:
1) Print Palindrome If YES if the given string is a palindrome, else print NO as output.
Input: string
Output: YES if the string is a palindrome else NO
Example:
Input: level
Output: YES
Input: Telugu
Output: NO
Code:
def isPalindrome(string):
if string == string[::-1]:
return 'YES'
else:
return 'NO'
print(isPalindrome(input()))
2) Print each word in the string in reverse for the given string which contains a number of words.
Input 1: The number of words in the string
Input 2: A string with input 1 number of words
Output: A string with each word in reverse order
Examples:
Input1: 11
Input2: Telugu Tech Learners Is a Great youtube channel for job seekers
Output: uguleT hceT srenreaL sI a taerG ebutuoy lennahc rof boj srekees
Code:
def stringWordsReverse(input1,input2):
input2 = list(input2.split()) #first we will convert the string into a list of words
string = "" # an empty string to append the reversed word of the input2 (string)
for i in input2: # we will iterate over each word
string += i[::-1] + " " # we will reverse the word and append it to a string
return string[:-1] # string will contain space at last, remove it we will slice the string
input1 = int(input()) # input1 to store the number of words in the string
input2 = input() # input to take the string with input1 number of words
print(stringWordsReverse(input1,input2))
3) Write A Program To Print The Number Of Palindromes In A Given String.
Input1: Input string containing words
Input2: The number of words in the string
Output: The number of palindromes in a string
Examples:
Input1: A level is completed
Input2: 4
Output: 1
Explanation: level is the only palindrome in the given string. So, the output will be one.
Input1: locol is upraisal werew youoy said
Input2: 6
Output: 3
Code:
def countPalindromes(input1,input2):
input1 = list(input1.split()) # first we will convert the string as list of words
count = 0 # count is initially zero
for i in input1: # iterating over the words in the list
if i == i[::-1]: # if word equal to reverse fo the word i.e., a palindrome
count+=1 # coutn = count + 1
return count
input1 = input() # input to take the string
input2 = int(input()) # input2 to store the number of words in the string
print(countPalindromes(input1,input2))
4) Two Words Are Given If the second word can be formed from the first word then print YES, else print No
Input1: A word
Input2: Another word
Output: YES if the input2 can be formed from Input1 by interchanging the letters in the word
Examples:
Input1: word
Input2: rowd
Output: YES
Explanation: rowd can be formed from word by interchanging the word word
Input1: ttl
Input2: llt
Output: No
Explanation: llt cannot be obtained by interchanging the letter of ttl
Code:
code:from itertools import permutations
def isWordFormed(input1,input2):
# using permutations to find the number of words can be formed by interchaning the letters
words = list(permutations(input1,len(input1)))
#changing input2 to a tuple of its letters because perumations returns the words as tuples
input2 = tuple(input2.strip())
if input2 in words: # if input2 in words
return 'YES' # return YES
exit(0) # and exit
return 'NO' # else return NO
input1 = input()
input2 = input()
print(isWordFormed(input1,input2))
5) Write A Program To Print A string by removing the duplicates in it.
Input1: A string containing words
Output: Uniques letters in the word in order
Examples:
Input: Good Morning
Output: God Mrni
Explanation: In the given string 'o', 'i', 'n' have duplicates, so the duplicates are removed. After removing duplicates we left with unique elements
Code:
code:
def removeDuplicates(string):
strs = ""
# iterating over the each letter in the string
for i in string:
''' if the letter is not in the strs we append it,
if it already there in the strs we do not append '''
if i not in strs:
strs+= i
return strs
string = input()
print(removeDuplicates(string))
No comments:
Post a Comment