Tasks. For every task apply all appropriate methods as shown in the above exampl
ID: 3890803 • Letter: T
Question
Tasks.
For every task apply all appropriate methods as shown in the above example. Use any values as parameters to illustrate your solutions.
Write a Python code in order to…
1. Find the squares of all even numbers taken from a given range.
2. Generate the Fibonacci series (where F(n+2)=F(n) + F(n+1) ) for all numbers between two given integer values.
3. Given a list of names find their lengths. Look up the needed strings related functions.
4. Given two lists – one of foods and another one of drinks – create a list of all possible food-drink combinations.
5. A Pythagorean triple consists of three positive integers a, b, and c, such that a 2 + b2 = c2 . Such a triple is commonly written (a, b, c), and the best known example is (3, 4, 5). For a given range find all the triples (a, b, c) that have the same property. Example: (6, 8, 10)
principles of functional programming in Python (Appendix). I suggest you follow the steps as they explained in the description Perform the tasks similar to the following example. Example. Write a 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 Step 1: Define a function that returns x congruent to m >>>def f(x): return x Step 2: Set up some arbitrary parameters: modulo m and the range of numbers: >>> m=5 >y range (20) Step 3: Call a filter to select the numbers from the range y being subject to the function f >>> z=filter(f,y) Step 4: Since z is not the list we are looking for but rather a pointer (iterator) to it we have to apply the function list to z in order to obtain all numbers from the range (0..20) congruent to 5: >>> list (z) [5, 10, 15] Solution 2 We can use a list comprehension like the following one: >>> list (K for X In range (20) if x%n«-01) [0, 5, 10, 15] This solution corresponds to a mathematical formulation such as the definition of the set of all squares of the natural numbers:Explanation / Answer
A)
def evens(n):
for i in range(n + 1):
if i % 2 == 0: # Condition to check whether even or not.
print(i)
n is the range specified by user.
B)
def fibo(n):
num = []
for i in range(n + 1):
if i == 0:
num.append(1)
elif i == 1:
num.append(1)
else:
num.append(num[i - 1] + num[i - 2])
return num
start = 12
end = 30
fiboNumbers = fibo(end)
y = list(range(start, end + 1))
for i in fiboNumbers:
if i in y:
print(i)
C)
names = ["rob", "jack", "michael", "robot", "Darek"]
for i in names:
print(len(i))
D)
drinks = ["cock", "lemonade", "beer", "vodka"]
food = ["burger", "pizza", "pasta"]
combo = []
for i in food:
for j in drinks:
combo.append((i, j))
print(combo)
E)
def check(n):
y = range(1, n + 1)
squareY = [i**2 for i in y]
l = []
for i in y:
for j in y:
sum = i ** 2 + j ** 2
if sum in squareY:
l.append((i, j, sum))
return l
# N is the range
combo = check(10)
print(combo)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.