String Problem
In a certain encrypted message which has information about the location(area, city), the characters are jumbled such that first character of the first word is followed by the first character of the second word, then it is followed by second character of the first word and so on
In other words, let’s say the location is bandra,mumbai
The encrypted message says ‘bmaunmdbraai’.
Sample Input:
bmaunmdbraai
Sample Output:
bandra,mumbai
Let’s say the size or length of the two words wouldn’t match then the smaller word is appended with # and then encrypted in the above format.
With this in mind write a code to identify the right location and print it as place,city.
Solution Code
#code
import ast,sys
input_str = sys.stdin.read()
message1 = input_str[0:-1:2]
message2 = input_str[1:len(input_str):2]
print(message1.strip('#') + "," + message2.strip('#'))
-----------------------------------------------------------------------------------------------------------------------------
Tuple
Add the element ‘Python’ to a tuple input_tuple = ('Monty Python', 'British', 1969). Since tuples are immutable, one way to do this is to convert the tuple to a list, add the element, and convert it back to a tuple.
Sample Input:
('Monty Python', 'British', 1969)
Sample Output:
('Monty Python', 'British', 1969, 'Python')
Solution Code
#code
import ast,sys
input_str = sys.stdin.read()
input_tuple = ast.literal_eval(input_str)
list_1 = list(input_tuple)
list_1.append('Python')
tuple_2 = tuple(list_1)
print(tuple_2)
-----------------------------------------------------------------------------------------------------------------------------
STRING SPLIT
DescriptionSplit the string input_str = 'Kumar_Ravi_003' to the person's second name, first name and unique customer code. In this example, second_name= 'Kumar', first_name= 'Ravi', customer_code = '003'.
A sample output of the input 'Kumar_Ravi_003' is:
Ravi Kumar 003
Note that you need to print in the order first name, last name and customer code.
#code
import ast,sys
input_str = sys.stdin.read()
name = input_str.split('_')
first_name = name[1]
second_name = name[0]
customer_code = name[2]
print(first_name)
print(second_name)
print(customer_code)
Split the string input_str = 'Kumar_Ravi_003' to the person's second name, first name and unique customer code. In this example, second_name= 'Kumar', first_name= 'Ravi', customer_code = '003'.
A sample output of the input 'Kumar_Ravi_003' is:
Ravi Kumar 003
Note that you need to print in the order first name, last name and customer code.
#code
import ast,sys
input_str = sys.stdin.read()
name = input_str.split('_')
first_name = name[1]
second_name = name[0]
customer_code = name[2]
print(first_name)
print(second_name)
print(customer_code)
Comments
Post a Comment