1) Given an input string and a dictionary of words, find out if the input string can be segmented into a space-separated sequence of dictionary words. Please ignore the case.
2) write a program to sort the (name, age, score) tuples by ascending order where name is string, age and score are numbers. The tuples are input by console. The sort criteria is:
a) Sort based on name;
b) Then sort based on age;
c) Then sort by score.
The priority is that name > age > score.
If the following tuples are given as input to the program:
Tom,19,80
John,20,90
Jony,17,91
Jony,17,93
Json,21,85
Then, the output of the program should be:
[('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')]
3) For the given string return 1 if it is a Palindrome else 0.
4) For the given string remove the white spaces.
d = ["emc", "great", "place", "is", "to", "work", "at", "ace", "tow"]
s = "EMCIsGreatPlaceToWork"
s = s.lower()
segment = []
while s:
if s in d:
segment.append(s)
s = ""
for i in range(1, len(s)):
if s[:i] in d:
segment.append(s[:i])
s = s[i:]
break
if i == len(s)-1:
s = ""
print(segment)
>>> ['emc', 'is', 'great', 'place', 'to', 'work']
print(" ".join(segment))
>>> emc is great place to work
s = "EMCIsGreatPlaceToWork"
s = s.lower()
segment = []
while s:
if s in d:
segment.append(s)
s = ""
for i in range(1, len(s)):
if s[:i] in d:
segment.append(s[:i])
s = s[i:]
break
if i == len(s)-1:
s = ""
print(segment)
>>> ['emc', 'is', 'great', 'place', 'to', 'work']
print(" ".join(segment))
>>> emc is great place to work
2) write a program to sort the (name, age, score) tuples by ascending order where name is string, age and score are numbers. The tuples are input by console. The sort criteria is:
a) Sort based on name;
b) Then sort based on age;
c) Then sort by score.
The priority is that name > age > score.
If the following tuples are given as input to the program:
Tom,19,80
John,20,90
Jony,17,91
Jony,17,93
Json,21,85
Then, the output of the program should be:
[('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')]
from operator import itemgetter
persons = []
while True:
line = input(">")
if not line:
break
persons.append(tuple(line.split(",")))
print(persons)
>>> [('Tom', '19', '80'), ('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85')]
pesrsons = sorted(persons, key=itemgetter(0,1,2))
print(persons)
>>> [('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')]
persons = []
while True:
line = input(">")
if not line:
break
persons.append(tuple(line.split(",")))
print(persons)
>>> [('Tom', '19', '80'), ('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85')]
pesrsons = sorted(persons, key=itemgetter(0,1,2))
print(persons)
>>> [('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')]
3) For the given string return 1 if it is a Palindrome else 0.
def checkpalindrome():
s = input("Enter the string:")
if s == s[::-1]:
return 1
else:
return 0
checkpalindrome()
s = input("Enter the string:")
if s == s[::-1]:
return 1
else:
return 0
checkpalindrome()
4) For the given string remove the white spaces.
s = "EMC is great place to work"
s = s.split()
s = "".join(s)
print(s)
>>> EMCisgreatplacetowork
s = s.split()
s = "".join(s)
print(s)
>>> EMCisgreatplacetowork
Comments
Post a Comment