In: Computer Science
1. Write a python program to create a list of integers using random function. Use map function to process the list on the expression: 3x2+4x+5 and store the mapped elements in another list. Now use filter to do sum of all the elements in another list.
2. Write a function that takes n as input and creates a list of n lists such that ith list contains first 10 multiples of i.
3. Write a function that takes a number as input parameter and returns the corresponding text in word. For example, on input 458, the function should return ‘Four Five Eight’. Use dictionary for mapping digits to their string representation.
4. Write a program to create two sets of integer type using random function. Perform following operations on these two sets:
a) Union
b) Intersection
c) Difference
d) Symmetric difference
Problem 1:
import random
def myfunc(x):
return 3*2+4*x+5
x = random.sample(range(10, 30), 5)
y = map(myfunc, x)
#convert the map into a list, for readability:
print(list(x))
print(list(y))
Output:
Poblem 2:
def myfunc():
n = int(input('Enter n: '))
requiredlist = []
for i in range(n):
templist = []
for j in range(1,11):
templist.append(i*j)
requiredlist.append(templist)
print(requiredlist)
myfunc()
Output:
Problem 3:
def myfunc(n):
numdict = {'1':'One','2':'Two','3':'Three','4':'Four',
'5':'Five','6':'Six','7':'Seven','8':'Eight','9':'Nine','0':'Zero'}
x = str(n)
ret = ""
for i in x:
ret = ret + numdict[i]+" "
return ret[0:len(ret)-1]
print(myfunc(458))
Output:
Problem 4:
import random
x = set(random.sample(range(10, 50), 10))
y = set(random.sample(range(10, 50), 10))
union = x.union(y)
intersection = x.intersection(y)
difference = x-y
symmetric = union-intersection
Output vaiables:
Hope this helps.