Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

2. Place a value on the outcomes.

Answered: 1 week ago