Skip to main content

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 

  1. Though value 5 is repeated twice in both lists, in the final output it is present only once.
  2. 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))

 

 ANSWER OPTION 2(4)





Comments

Popular posts from this blog

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('#')) --------------------------------------------------------------------------------------------...

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...