Search

SETS IN PYTHON

by 10:40:00 0 comments

  Introduction to Sets                        

Task
        Now, let's use our knowledge of sets and help Mickey.Ms. Gabriel Williams is a botany professor at District College. One day, she asked her student Mickey to compute the average of all the plants with distinct heights in her greenhouse.
Input Format
The first line contains the integer, , the total number of plants.
The second line contains the space separated heights of the plants.
Output Format
Output the average height value on a single line.
Sample Input
10
161 182 161 154 176 170 167 171 170 174
Sample Output
169.375
SOURCE CODE:
num_plants = int(raw_input())
heights =[int(x) for x in raw_input().split()]
heights_list = list(set(heights))
print sum(heights_list) / float(len(heights_list))                

  Symmetric Difference

 

Task
Given sets of integers, and , print their symmetric difference in ascending order. The term symmetric difference indicates those values that exist in either or but do not exist in both.

Input Format
The first line of input contains an integer, .
The second line contains space-separated integers.
The third line contains an integer, .
The fourth line contains space-separated integers.
Output Format
Output the symmetric difference integers in ascending order, one per line.
Sample Input
2 4 5 9
4
2 4 11 12
Sample Output
11 
12  
SOURCE CODE:
m = int(raw_input())
a =[int(x) for x in raw_input().split()]
s=set(a)
n = int(raw_input())
b =[int(x) for x in raw_input().split()]
c=s.symmetric_difference(set(b))
l=[]
for i in c:
    l.append(i)
    l.sort()
for i in l:
    print i

  Set .discard(), .remove() & .pop()

 

Task

You have a non-empty set , and you have to execute commands given in lines.
The commands will be pop, remove and discard.

Input Format
The first line contains integer , the number of elements in the set .
The second line contains space separated elements of set . All of the elements are non-negative integers, less than or equal to 9.
The third line contains integer , the number of commands.
The next lines contains either pop, remove and/or discard commands followed by their associated value.

Output Format
Print the sum of the elements of set on a single line.
Sample Input
1 2 3 4 5 6 7 8 9 
10 
pop 
remove 9 
discard 9 
discard 8 
remove 7 
pop 
discard 6 
remove 5 
pop 
discard 5
Sample Output
4
SOURCE CODE:
n = input()
s = set(map(int, raw_input().split()))
m = int(raw_input())
for _ in xrange(m):
    pair = raw_input().split()
    arg = pair[1] if len(pair) == 2 else ""
    eval("s." + pair[0] + "(" + arg + ")")
print sum(s)

  Set .union() Operation  

   

Task
The students of District College have subscriptions to English and French newspapers. Some students have subscribed only to English, some have subscribed to only French and some have subscribed to both newspapers.You are given two sets of student roll numbers. One set has subscribed to the English newspaper, and the other set is subscribed to the French newspaper. The same student could be in both sets. Your task is to find the total number of students who have subscribed to at least one newspaper.

Input Format
The first line contains an integer, , the number of students who have subscribed to the English newspaper.
The second line contains space separated roll numbers of those students.
The third line contains , the number of students who have subscribed to the French newspaper.
The fourth line contains space separated roll numbers of those students.
Output Format
Output the total number of students who have at least one subscription.
Sample Input
1 2 3 4 5 6 7 8 9 
10 1 2 3 11 21 55 6 8
Sample Output
13 
SOURCE CODE:
m = int(raw_input())
a =[int(x) for x in raw_input().split()]
s=set(a)
n = int(raw_input())
b =[int(x) for x in raw_input().split()]
c=s.union(set(b))
l=[]
count=0
for i in c:
    l.append(i)
for i in l:
    count+=1
print count

  Set .intersection() Operation 

Task
The students of District College have subscriptions to English and French newspapers. Some students have subscribed only to English, some have subscribed only to French, and some have subscribed to both newspapers.You are given two sets of student roll numbers. One set has subscribed to the English newspaper, one set has subscribed to the French newspaper. Your task is to find the total number of students who have subscribed to both newspapers.
Input Format
The first line contains , the number of students who have subscribed to the English newspaper.
The second line contains space separated roll numbers of those students.
The third line contains , the number of students who have subscribed to the French newspaper.
The fourth line contains space separated roll numbers of those students.
Output Format
Output the total number of students who have subscriptions to both English and French newspapers.
Sample Input
1 2 3 4 5 6 7 8 9 
10 1 2 3 11 21 55 6 8
Sample Output
5
SOURCE CODE: 
m = int(raw_input())
a =[int(x) for x in raw_input().split()]
s=set(a)
n = int(raw_input())
b =[int(x) for x in raw_input().split()]
c=s.intersection(set(b))
l=[]
count=0
for i in c:
    l.append(i)
for i in l:
    count+=1
print count

  Set .difference() Operation 

   

Task
Students of District College have a subscription to English and French newspapers. Some students have subscribed to only the English newspaper, some have subscribed to only the French newspaper, and some have subscribed to both newspapers.
You are given two sets of student roll numbers. One set has subscribed to the English newspaper, and one set has subscribed to the French newspaper. Your task is to find the total number of students who have subscribed to only English newspapers.
Input Format
The first line contains the number of students who have subscribed to the English newspaper.
The second line contains the space separated list of student roll numbers who have subscribed to the English newspaper.
The third line contains the number of students who have subscribed to the French newspaper.
The fourth line contains the space separated list of student roll numbers who have subscribed to the French newspaper.
Output Format
Output the total number of students who are subscribed to the English newspaper only.
Sample Input
1 2 3 4 5 6 7 8 9 
10 1 2 3 11 21 55 6 8
Sample Output
4
SOURCE CODE:
m = int(raw_input())
a =[int(x) for x in raw_input().split()]
s=set(a)
n = int(raw_input())
b =[int(x) for x in raw_input().split()]
c=s.difference(set(b))
l=[]
count=0
for i in c:
    l.append(i) 
for i in l:
    count+=1
print count

  Set Mutations

TASK
You are given a set and number of other sets. These number of sets have to perform some specific mutation operations on set .Your task is to execute those operations and print the sum of elements from set . 
Input Format
The first line contains the number of elements in set .The second line contains the space separated list of elements in set .The third line contains integer , the number of other sets.The next lines are divided into parts containing two lines each.The first line of each part contains the space separated entries of the operation name and the length of the other set.The second line of each part contains space separated list of elements in the other set. 
Output Format
Output the sum of elements in set. 
Sample Input 
16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 24 52
4
intersection_update 10
2 3 5 6 8 9 1 4 7 11 update 2
55 66
symmetric_difference_update 5
22 7 35 62 58
difference_update 7
11 22 35 55 58 62 66
Sample Output
38

SOURCE CODE:

n=input()
s = set(raw_input().split())
for _ in range(input()):
    s1 = raw_input().split()
    getattr(s,s1[0])(set(raw_input().split()))
print sum(map(int,s))         

                                                                               

  Check Subset  

              

You are given two sets, and Your job is to find whether set is a subset of set .If set is subset of set , print True.If set is not a subset of set , print False.
Input Format
The first line will contain the number of test cases, .
The first line of each test case contains the number of elements in set .
The second line of each test case contains the space separated elements of set .
The third line of each test case contains the number of elements in set .
The fourth line of each test case contains the space separated elements of set .

Output Format
Output True or False for each test case on separate lines.
Sample Input
1 2 3 5 6 
9 8 5 6 3 2 1 4 7 
3 6 5 4 1 
1 2 3 5 6 8 9 
9 8 2
Sample Output
True 
False 
False
SOURCE CODE: 
for i in range(int(raw_input())):
    a = int(raw_input()); A = set(raw_input().split())
    b = int(raw_input()); B = set(raw_input().split())
    print not bool(A.difference(B))

 Check Strict Superset 


You are given one set and a number of other sets, .Your job is to find whether set is a strict superset of all the sets.Print True, if is a strict superset of all of the sets. Otherwise, print False.A strict superset has at least one element that does not exist in its subset. 
Example:
Set is a strict superset of set.Set is not a strict superset of set.Set is not a strict superset of set.

Input Format
The first line contains the space separated elements of set .
The second line contains integer , the number of other sets.
The next lines contains the space separated elements of the other sets.


Output Format
Print True if set is a strict superset of all other sets. Otherwise, print False.
Sample Input
1 2 3 4 5 6 7 8 9 10 11 12 23 45 84 78 
1 2 3 4 5 
100 11 12
Sample Output
False
SOURCE CODE:
A, n = set(raw_input().split()), int(raw_input())
flag = True
for i in range(n):
    newSet = set(raw_input().split())
    if not (A > newSet):
        flag = False
        break
print flag


 



                                                                
 

Anonymous

Developer

This is created by GAVASKAR .

0 comments:

Post a Comment