Preliminary tasks a. Prepare one example of each of the following data types: St
ID: 3885366 • Letter: P
Question
Preliminary tasks
a. Prepare one example of each of the following data types:
Strings; Lists; Tuples; Sets.
b. Learn the principles of functional programming. Write a code to check the congruency of two given numbers a, and b with modulo m.
c. Write aa code to select all numbers from a given range that are congruent to a given number. Use list, filter and range with the following function definition
def f(x): return x%m
(m is supposed to be set).
d. Consider your own Student ID number. Which of the following numbers (0, 1, 2) is it congruent to?
You will be using this number to select your question from the list below.
Exercises
Task B.1 Write an expression whose value is
the average of the elements of the list [20, 10, 15, 75].
the max of the list [2, 5, 1, 4, 6]
the min of the list [9, 1, 4, 3]
Task B.2 Write a double list comprehension over the lists
['A','B','C'] and [1,2,3]
[3, 4, 1] and [“my”, “task”]
[‘1’,’2’] and [2,3,4]
whose value is the list of all possible two-element lists.
Task B.3: Suppose LofL has been assigned a list whose elements are themselves lists of numbers. Write an expression that evaluates to
the sum of all the numbers in all the lists;
the sum of the cubes of all the numbers in the list;
the sum of the squares of all the numbers in the list.
The expression has the form sum([sum(... and includes one comprehension. 1
Test your expression after assigning
[[.25, .75, .1], [-1, 0], [4, 4, 4, 4]]
[[.50, .25, .1], [-1, -1], [4, 4, -4, 4]]
[[-.25, .75, .1], [1, 0], [4, -4, 4, 4]]
to LofL. Note that your expression should work for a list of any length. Task B.4. Suppose S is a set of integers, e.g.
{-4, -2,1,2,5,0},
{4, 2,1,2,5,0},
{1,-1,2,-3,-2,3}.
Write a triple comprehension whose value is a list of all three-element tuples (i,j,k) such that i,j,k are elements of S whose sum is zero.
Task B.5. Modify the comprehension of the previous task so that the resulting list does not include (0,0,0). Hint: add a filter
Task B.6: Further modify the expression so that its value is not the list of all such tuples but is the first such tuple.
Is the order of elements preserved? If not, find out why?
Task B.6: Write a comprehension over a range of the form range(n) such that the value of the comprehension is the set of
odd numbers from 1 to 99,
even numbers from 4 to 50,
numbers congruent to 1 mod 3.
Task B.7. Starting from the lists
[10, 25, 40] and [1, 15, 20],,
[-10, -25, 40] and [15, 1, 20],
[25, 10, 40] and [20, 15, 10].
write a comprehension whose value is the three-element list in which the each element is the sum of the corresponding elements of the two given sets. Your expression should use zip but not list.
Explanation / Answer
A) example of string, list, tuple and set are below.
String example:
mystring='This is example of single quote string'
mystring="This is example of double quote string"
mystring="""This is
example of line break string"""
print (mystring[0])-will print 'T'(similarly you can print any index string).
test = 'This is a test'
t2 = mystring+t
t3 = mystring*3
List Example:(list values can be modified/changed)
mylist = [1, 2, 3]
mylist=['a',"example",45]
mylist=[[1,2,3],"a","b"]
mylist[0] (it will print first index string)
mylist.append(1)(appends mylist with 1 value)
mylist[1:](it will print all values from 1st index)
mylist[1:3](it will print values between 1 to 3)
Tuple Example:(Cannot be modified or changed)
tuple=(1,2,3)
tuple=['a',"example",45]
all the list operations can be done on tuple except deletion and insertion or appending.
Set Example:(Sets are constructed from a sequence (or some other iterable object). Since sets cannot have duplicated.)
a = set([1, 2, 3, 4])
b = set([3, 4, 5, 6])
a | b # Union
{1, 2, 3, 4, 5, 6}
a & b # Intersection
{3, 4}
a < b # Subset
False
a - b # Difference
{1, 2}
a ^ b # Symmetric Difference
{1, 2, 5, 6}
B) Write a code to check the congruency of two given numbers a, and b with modulo m.
def check(x, y):
result1=(x%5)
result2=(y%5)
if(result1==result2):
print (x," and",y," are congruence")
else:
print (val1," and",val2," are not congruence")
num1 = 54
num2 = 24
check(num1,num2)
C)
def fun(x):
input1=[1,2] #change list values to test possible ways
input2=[6,8] #change list values to test possible ways
for x in input1:
for y in input2:
for i in range(x, y):
if((i%5)==congruent_number):
print (i," and",congruent_number," are congruence")
else:
print (i," and",congruent_number," are not congruence")
congruent_number=5
fun(congruent_number)
D)
student_id = 123456
b = str(student_id)
c = []
for digit in b:
c.append (int(digit))
list= [20, 10, 15, 75] #given list for comparision
for i in c:
for x in list:
if((i%5)==(x%5)):
print (i," and",x," are congruence")
else:
print (i," and",x," are not congruence")