Skip to main content

PYTHON 3 PRACTICE PROGRAM

String Problem

Description

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

Description

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


Description

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)



List Indexing

Suppose list_1 is [2, 33, 222, 14, 25]. What is list_1[-5]?

Solution
             option 1 (2)

List_remove_append

Description

Remove SPSS from input_list=['SAS', 'R', 'PYTHON', 'SPSS'] and add 'SPARK' in its place.


Sample Input:

['SAS', 'R', 'PYTHON', 'SPSS']


Sample Output:

['SAS', 'R', 'PYTHON', 'SPARK']

Execution Time Limit

15 seconds



SOLUTION CODE

#CODE

import ast,sys

input_list = (sys.stdin.read()).split(',')

input_list.remove('SPSS')

input_list.append('SPARK')

print(input_list)


List Manipulation

word = ['1','2','3','4']
word[ : ] = [ ] 
print(word)

What would be the output of the code above?


SOLUTION 

ANSWER IS   "EMPTY LIST " (option 3)


Nested List

How will I extract Python from the nested list

input_list =  [['SAS','R'],['Tableau','SQL'],['Python','Java']]

 


ANSWER IS OPTION A (


input_list[2][0])




List Sorting

Consider the following list:

L = ['one','two','three', 'four', 'five', 'six']
print(sorted(L))
print (L)

 



Comments

Popular posts from this blog

SETS Practice Problems

Sets Practice Description Let’s say you have two lists A and B. Identify the elements which are common in the two lists A and B and return them in a sorted manner. For example  Sample Input : A = [5,1,3,4,4,5,6,7] B = [3,3,5,5, 1 ,7 ,2] Sample Output: [1,3,5] If you observe the sample output here you can see that  Though value 5 is repeated twice in both lists, in the final output it is present only once. The values are returned in a sorted manner with values increasing. Execution Time Limit 10 seconds SOLUTION CODE #code import ast,sys input_str = sys.stdin.read() input_list = ast.literal_eval(input_str) list_1 = input_list[0] list_2 = input_list[1] A = set(list_1) B = set(list_2) answer = list(A.intersection(B)) print(answer) 2) Sets What gets printed with the following set of instructions? nums = set ([ 1 , 1 , 2 , 3 , 3 , 3 , 4 ]) print ( len (nums))   7 4 ✓ Correct Feedback: Set() removes duplicates and returns the unique values. Hence, nums would be updated as...

VARIABLES AND DATA TYPES IN PYTHON

  Variables In  [1]: name = "mike" print ( name ) mike In  [3]: name = input ( "Enter your name : " ) print ( "Welcome" , name ) Enter your name : Jane Welcome Jane Data Types in Python Here in the below piece of code you can see that we have assigned each variable with a particular value and use the type method we can check the datatype of the variable Variables In  [1]: name = "mike" print ( name ) mike In  [3]: name = input ( "Enter your name : " ) print ( "Welcome" , name ) Enter your name : Jane Welcome Jane Data Types in Python Here in the below piece of code you can see that we have assigned each variable with a particular value and use the type method we can check the datatype of the variable In  [15]: integer_num = 1 floating_num = 1.3 string = 'Mike' boolean = True ​ print ( type ( integer_num )) print ( type ( floating_num )) print ( type ( string )) print ( type ( boolean )) <clas...