-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_function.py
71 lines (57 loc) · 1.9 KB
/
lambda_function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# I'm going to use Lambda Functions or Lambda Expressions
import math
# lambda expression
first_expression = (lambda argument: argument * 2)
second_expression = (lambda base, height: (base * height) / 2)
third_expression1 = (lambda seconds: "It's equivalent {0} minutes".format(seconds / 60))
third_expression2 = (lambda seconds: "It's equivalent to " +
str(seconds / 60) + " minutes or " + str(seconds / 3600) + " hours")
# lambda expression as anonymous function
def signOfTheSum(a, b, c):
sum_ = a + b + c
if sum_ < 0:
print("the result", sum_, "is negative")
return lambda x: "The sum " + str(sum_) + " multiplied by " + str(x) + " is: " + str(x * sum_)
else:
print("the result", sum_, "is positive")
return lambda x: "The sum " + str(sum_) + " multiplied by " + str(x) + " is: " + str(x * sum_)
# validation
def validation_int(message):
while True:
try:
value = int(input(message))
return value
except ValueError:
print("just integer number")
def validation_float(message):
while True:
try:
value = float(input(message))
return value
except ValueError:
print("Just number, try again")
def sumOfNumber():
lst = []
sum = 0
quantity = validation_int("quantity of number: ")
for i in range(quantity):
number = validation_float("type a number: ")
lst.append(number)
# sum of a list
sum = math.fsum(lst)
print("the sum is: ", sum)
return lambda x: math.sqrt(sum)*x
# RESULT
print(first_expression(12))
print(second_expression(12, 2))
print(third_expression1(3600))
print(third_expression2(100))
print("-"*40)
function_lambda = signOfTheSum(-1, -3, -4)
print(function_lambda(20))
print("-"*30)
sum_to = sumOfNumber()
print(sum_to(10))
print("-"*40)
sign = (lambda x, y: x+y)
print(sign(2, 3))