#06 - Lambda problems
Task one
v=lambda a:a+15
w=v(int(input('Please input a number: ')))
x=int(input('Please input the 1st number: '))
y=int(input('Please input the 2nd number: '))
z=lambda a,b:a*b
print(w)
print(z(x,y))
Task two
multiples=['Double','Triple','Quadruple','Quintuple']
w=int(input('Please input a number'))
for x in range(len(multiples)):
v=lambda a:a*(x+2)
print(multiples[x],"the number of",w,"=",v(w))
Task three
scores=[('English',88),('Science',90),('Maths',97),('Social sciences',82)]
print("Original list of tuples:\n",scores)
scores.sort(key=lambda x:x[1])
print("Sorting the List of Tuples:\n",scores)
Task four
phones=[{'make': 'Nokia', 'model': 216, 'colour': 'Black'}, {'make': 'Mi Max', 'model': '2', 'colour': 'Gold'}, {'make': 'Samsung', 'model': 7, 'colour': 'Blue'}]
print('Original list of dictionaries:\n',phones)
phones.sort(key=lambda x:x['colour'])
print('Sorting the List of dictionaries :\n',phones)
Task five
numbers=[1,2,3,4,5,6,7,8,9,10]
print('Original list of integers:\n',numbers)
even=list(filter(lambda x:x%2==0,numbers))
print('Even numbers from the said list:\n',even)
odd=list(filter(lambda x:x%2!=0,numbers))
print('Odd numbers from the said list:\n',odd)
Task six
numbers=[1,2,3,4,5,6,7,8,9,10]
print('Original list of integers:\n',numbers)
square=list(map(lambda x: x ** 2, numbers))
print('Square every number of the said list:\n',square)
cube=list(map(lambda x: x ** 3, numbers))
print('Cube every number of the said list:\n',cube)
Task Seven
string_one = "Hello"
string_two = "Elo"
charachter = "H"
starts_with = lambda x: True if x.startswith(charachter) else False
print(starts_with(string_one))
print(starts_with(string_two))
Task Eight
import datetime
x=datetime.datetime.now()
year=lambda a:x.year
month=lambda a:x.month
day=lambda a:x.day
time=lambda a:x.time()
print('',x,'\n' ,year(x),'\n' ,month(x),'\n' ,day(x),'\n' ,time(x))
Task Nine
num_1=['3','d','1','f','2','s']
for x in range(len(num_1)):
if_number=lambda x:x.isnumeric()
print(if_number(num_1[x]))
print('Print checking numbers:')
num_2=['-123','-87519']
for x in range(len(num_2)):
if_number_2=lambda x:if_number(x[1:]) if x[0]=='-' else if_number(x)
print(if_number_2(num_2[x]))
Task Ten
from functools import reduce
fib_series=lambda n:reduce(lambda x, _: x+[x[-1]+x[-2]],range(n-2),[0,1])
print("Fibonacci series upto 2:")
print(fib_series(2))
print("\nFibonacci series upto 5:")
print(fib_series(5))
print("\nFibonacci series upto 6:")
print(fib_series(6))
print("\nFibonacci series upto 9:")
print(fib_series(9))
Task Eleven
arr_1=[1,2,3,4,5]
arr_2=[3,1,6,7,2]
intersection=list(filter(lambda x:x in arr_1,arr_2))
print('Original arrays\n',arr_1,'\n',arr_2)
print('Intersection of the said arrays:',intersection)