3. Form a palindrome string from the input (Which is missed in last set) * Do not use any inbuilt functionality i/o : aabbc o/p : abcba (any of the possible palindrome) i/o : sdab o/p : can not form palindrome Ans:: Sol 1:: a = input("Enter the string:") b = [] for i in range (len(a)): if a[i] not in b: b.append(a[i]) #print("add",b) else: b.remove(a[i]) #print("remove",b) if (len(a)% 2 == 0 and len(b) == 0 or (len(a) % 2 == 1 and len(b) == 1)): print(a) else: print('can not form palindrome') o/p: Enter the string:abab abab
Finding a Friend with Longest Name Write an algorithm and the subsequent Python program to store the names of your friends, count the number of friends, identify the longest name and the number of letters in the longest name. Get the names of friends till 'stop' is entered. For example, if the input sequence is Ravi, Raj, Puela, stop, then the output is 3, Puela and 5. When you are coding in the online judge (SkillRack), use rstrip() function to remove carriage return from the input string. Input Format: First line is the name of the first friend Second line is the name of the second friend Third line is the name of the third friend …. stop Output Format: Number of friends Friend’s name with longest length Number of characters in that longest name list_of_names=[] name=input().rstrip() while(name!="Stop"): list_of_names.append(name) name=input().rstrip() print(len(list_of_names)) temp="" m=0 for name in list_of_names: if(len(name)>m): m=(len(name)) temp=name print(temp) print(m)
bcdef abcdefg bcde bcdef Sample Output 3 2 1 1 Explanation There are distinct words. Here, "bcdef" appears twice in the input at the first and last positions. The other words appear once each. The order of the first appearances are "bcdef", "abcdefg" and "bcde" which corresponds to the output. #solution from collections import OrderedDict n = int(input()) inputs = OrderedDict() for i in range(n): word = input() if word in inputs: inputs[word] += 1 else: inputs[word] =1 print (len(inputs)) for key, value in inputs.items(): print(value, end = ' ')
if name == 'main': n = int(input()) d = {} for _ in range(n): s = input().split() d[s[0]]=[float(s[1]),float(s[2]),float(s[3])] sr=input() f = 0 for i in range(len(s)-1): f += d[sr][i] f = f/(len(s)-1) print("%.2f" % round(f,2)) #my code n = int(input()) my_dict = {} for line in range(n): info = input().split(" ") score = list(map(float, info[1:])) my_dict.update({info[0] : sum(score)/float(len(score))}) print("%.2f" % round(my_dict[input()],2)) ######################################### # pattern arrnage ########################################### #Replace all ______ with rjust, ljust or center. Sample Input 5 Sample Output H
3?:1 New:1 Python:5 Read:1 and:1 between:1 choosing:1 or:2 to:1 Created on Thu Mar 7 19:36:49 2019 @author: mahtab faraji www.onlinebme.com "" from string import Template sen=input("Enter your sentence: ") new_sen=sen.split(' ') out=[] b=[] a=[] for i in range(len(new_sen)): #out.append(new_sen.count(new_sen[i])) b=new_sen.count(new_sen[i]) print(new_sen[i],':',b) #out_m=list(zip(a,b,out)) #o=[] #for i in range(0,len(out_m)): # ' '.join(list(out_m[i]))
return askContinue() else: return askContinue() def requestInputFile(): """Asks the user for a (valid) input image that is returned. Generates a loop in waiting of a valid file""" inputfile = input("Which file do you want as input?\n") try: return Image.open(inputfile) except FileNotFoundError: print("Error, the file doesn't exists!") return requestInputFile() def main(): """Program main loop""" close = False while close == False: close = not askContinue() if close == False: image = requestInputFile() outfile = input("Which file do you want as output?\n") image = applyEffect(image) print("Saving file...") image.save(outfile) if __name__ == "__main__": import doctest doctest.testmod() main()
itertools.combinations_with_replacement(iterable, r) This tool returns r length subsequences of elements from the input iterable allowing individual elements to be repeated more than once. Combinations are emitted in lexicographic sorted order. So, if the input iterable is sorted, the combination tuples will be produced in sorted order. from itertools import combinations_with_replacement s, r = input().split(' ') t = list(combinations_with_replacement(sorted(s),int(r))) for j in range(len(t)): print("".join(t[j]))
DOWN 3 LEFT 3 RIGHT 2 Then, the output of the program should be: 2 Hints: In case of input data being supplied to the question, it should be assumed to be a console input. Solution: ```python import math pos = [0,0] while True: s = input() if not s: break movement = s.split(" ") direction = movement[0] steps = int(movement[1]) if direction=="UP": pos[0]+=steps elif direction=="DOWN": pos[0]-=steps elif direction=="LEFT": pos[1]-=steps elif direction=="RIGHT": pos[1]+=steps else: pass
Enter input string: Jill, Allen First word: Jill Second word: Allen Enter input string: Golden , Monkey First word: Golden Second word: Monkey Enter input string: Washington,DC First word: Washington Second word: DC Enter input string: q # Type your code here myinput=input('Enter input string:\n') mylist = [] while myinput != 'q': if ',' in myinput: mylist = myinput.split(',') print('First word: %s' % mylist[0].rstrip()) print('Second word: %s\n' % mylist[1].lstrip()) else: print('Error: No comma in string.\n') myinput=input('Enter input string:\n')
Verify that your program works correctly, and hand it in. Below is an example of output from running the program. Input your text: Python and Perl are both programming languages Python Perl and are both programming languages ############################################################################################################################################################################# quote = input("Please type your text here: ") words = quote.split() UC = [] LC=[] for word in words: isUpperCaseExist=0 for letter in word: if letter.isupper(): isUpperCaseExist=1 break else: isUpperCaseExist=0 if isUpperCaseExist==1: UC.append(word) else: