forked from Fezz0419/Final-Python-Practice-Test
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall.py
More file actions
150 lines (133 loc) · 3.35 KB
/
Copy pathall.py
File metadata and controls
150 lines (133 loc) · 3.35 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#Question 1
def get_date_of_birth(id_number:str):
"""
STEP 2: Extract the date of birth from the ID number and return it as a string
return format: DD/MM/YY:
"""
new_id = id_number[0:6]
new_id = new_id
int_id = new_id
day = int_id[4:]
month = int_id[2:4]
year = int_id[0:2]
return (f"{day}/{month}/{year}")
#Question 2
def get_gender(id_number):
"""
STEP 3: Extract the gender from the ID number using the formula below and return
it as a string
Formula: 1 if the ID number's 7th to 10th digit is less than 5000, the person is
female and if it is greater than 4999, the person is male.
"""
sequence = int(id_number[6:10])
if sequence < 5000:
return "Female"
elif sequence >= 5000:
return "Male"
else:
return "Invalid number"
#Question 3
# TODO: return shape from user input (it can't be blank and must be a valid shape!)
def get_shape()-> str:
# drawing different typees of shapes
shapeList = ["circle", "triangle", "square", "rectangle","Pentagon"]
shape = input("Please enter a shape")
if shape not in shapeList:
return "Invalid shape"
else:
return shape
#Question 4
def draw_triangle_multiplication(n: int):
"""
STEP 10: Draw a triangle of multiplication table up to n
Example for n = 5:
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
"""
#Question 5
def draw_pyramid(n: int):
"""
STEP 12: Draw a pyramid of height n using '*'
Example for n = 4:
*
***
*****
*******
"""
#Question 6
def fibonacci(n: int) -> int:
"""
STEP 13: Return the nth Fibonacci number using recursion
The Fibonacci sequence is defined as follows:
F(0) = 0
F(1) = 1
F(n) = F(n-1) + F(n-2) for n > 1
"""
if (n == 0):
return 0
elif (n == 1):
return 1
else:
return fibonacci(n - 1) + fibonacci(n - 2)
#Question 7
def fizz_buzz(n: int):
"""
STEP 14: Return "Fizz" if n is divisible by 3, "Buzz" if n is divisible by 5,
"FizzBuzz" if n is divisible by both 3 and 5, and n itself if none of those
conditions are met.
"""
if (n % 3) == 0:
return "Fizz"
elif (n % 5) == 0:
return "Buzz"
elif (n % 3) == 0 and (n % 5) == 0:
return "FizzBuzz"
else:
return n
#Question 8
def is_prime(n: int) -> bool:
"""
STEP 15: Return True if n is a prime number, False otherwise.
A prime number is a natural number greater than 1 that cannot be formed by
multiplying two smaller natural numbers.
"""
if n % 1 == 0:
return True
else:
return False
#Question 9
def factorial(n: int) -> int:
"""
STEP 16: Return the factorial of n using recursion.
The factorial of a non-negative integer n is the product of all positive
integers less than or equal to n.
"""
factorial(n) = n * factorial(n-1)
return factorial(n)
#Question 10
def prime_triangle(n: int):
"""
STEP 17: Draw a triangle of prime numbers up to n rows.
Example for n = 5:
2
3 5
7 11 13
17 19 23 29
31 37 41 43 47
"""
pass
#Question 11
def pascal_triangle(n: int):
"""
STEP 19: Draw Pascal's triangle up to n rows.
Example for n = 5:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
"""
pass