-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathex_01_solution.py
More file actions
132 lines (93 loc) · 3.16 KB
/
Copy pathex_01_solution.py
File metadata and controls
132 lines (93 loc) · 3.16 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""
Script for the first laboratory.
Add code by following instructions of the teacher.
"""
from typing import Any, List
def add(a: Any, b: Any) -> Any:
"""Adds parameters."""
return a + b
def what_number(number: int|float) -> str:
"""Returns string positive/zero/negative specifying
value of the number."""
if number > 0:
return "positive"
elif number == 0:
return "zero"
else:
return "negative"
def sum_of_numbers(list_of_numbers: List[int|float]) -> int|float:
"""Returns sum of the numbers in the list."""
total = 0
for n in list_of_numbers:
total += n
return total
def ship_name(fleet: dict, designated_no: Any) -> Any:
"""Return ship's name for specified designated number
from the fleet."""
# has dictionary a key? Use syntax: key in dictionary
if designated_no in fleet:
return fleet[designated_no]
return None
def dividable_by(lst: List[Any], divisor: int|float) -> None:
"""Prints first integer in the lst that is
dividable by specified divisor."""
# types: int, float, str, list, tuble, dict
# get type of a variable: type(variable_name)
for item in lst:
if type(item) == int and item % divisor == 0:
print(item)
return
def how_many_5(numbers: List[int|float]) -> int:
"""Returns number of numbers greater than 5."""
# Modify example to take argument that specifies threshold
count = 0
for n in numbers:
if n > 5:
count += 1
return count
# OR
return sum(1 for n in numbers if n > 5)
def gen_list_gt(lst: List[int|float], no: int|float) -> List[int|float]:
"""Returns list with numbers greater than no."""
#syntax: [ item for item in lst if_condition ]
return [item for item in lst if item > no]
def fact(n: int) -> int:
"""Returns factorial of n using recursion."""
if n <= 1:
return 1
return n * fact(n - 1)
def fact_while(n: int) -> int:
"""Returns factorial of the n using while loop."""
result = 1
while n > 1:
result *= n
n -= 1
return result
print(add(1, 3))
#print(add([1, 2, 3], [4, 5, 6]))
# Try addition of strings or different data type and see what happens
#if statement example
#n = 5
#print("Number", n, "is:", what_number(n))
#for example: sum of the list example
#lst = [1, 2, 3, 6, 7, 8]
#print("Sum is:", sum_of_numbers(lst))
#dictionary example
#fleet = {'BS62': 'Pegasus', "BS75": "Galactica", 36: 'Valkirie'}
#designated_no = "BS62"
#print("We've got {} in the fleet".format(ship_name(fleet, designated_no)))
#for example: find first number dividable by ...
#lst = [3, 'string1', 23, 14.0, "string2", 49, 64, 70 ]
#divisor = 8
#dividable_by(lst, divisor)
#function to count how many numbers > 5 are in the list
#lst = [1, 2, 5, 6, 7, 10, 12, 40, 3]
#print("There are {} numbers greater than 5".format(how_many_5(lst))
#generating list example
#lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]
#no = 5
#print("List with numbers > {}: {}".format(no, gen_list_gt(lst, no)))
#recursion example
#print("Factorial of number", n, "is:", fact(n))
#while loop example
#print("Factorial of number", n, "is:", fact_while(n))