-
Notifications
You must be signed in to change notification settings - Fork 4
/
exam.py
134 lines (108 loc) · 4.09 KB
/
exam.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
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
# To print out the text as shown below, what code should we execute? I love to take a ride in his sport's car!
print("I love to take a ride in his sport's car!")
# my_list = [1,3,5,7,9,11] Which of the following code will produce the same contents for my_list_2 as that stored in my_list?
my_list = [1,3,5,7,9,11]
my_list_2 = range(1,12,2)
print(my_list)
print(my_list_2)
# ("Dave", "Jamie", "Jill", "Brenda") Which answer prints out Jamie? Pick Two
names = ("Dave", "Jamie", "Jill", "Brenda")
print(names[1])
print(names[-3])
print(names[-4])
# Create a class function that inherits from the child class.
class Parent():
def __init__(self, last_name, eye_color):
print("Parent Constructor Called")
self.last_name = last_name
self.eye_color = eye_color
def show_info(self):
print("Last Name - " + self.last_name)
print("Eye Color - " + self.eye_color)
class Child(Parent):
def __init__(self, last_name, eye_color, number_of_toys):
print("Child Constructor Called")
Parent.__init__(self, last_name, eye_color)
self.number_of_toys = number_of_toys
def show_info(self):
print("Last Name - " + self.last_name)
print("Eye Color - " + self.eye_color)
print("Number of Toys - " + str(self.number_of_toys))
class GrandChild(Child):
def __init__(self, last_name, eye_color, number_of_toys, number_of_cars):
print("GrandChild Constructor Called")
Child.__init__(self, last_name, eye_color, number_of_toys)
self.number_of_cars = number_of_cars
def show_info(self):
print("Last Name - " + self.last_name)
print("Eye Color - " + self.eye_color)
print("Number of Toys - " + str(self.number_of_toys))
print("Number of Cars - " + str(self.number_of_cars))
# Write a program to take a number as input, and output a list of all the numbers below that number, that are a multiple of both, 2 and 6.
def multiples_of_2_and_6(number):
return [i for i in range(number) if i % 2 == 0 and i % 6 == 0]
multiples = multiples_of_2_and_6(int(input("Enter a number: ")))
print(multiples)
# We have a report on the number of flu vaccinations in a class of 20 people. It has the following numbers: never:5 once: 8 twice:4 Threetimes:3 What is the mean number of the times those people have been vaccinated?
class_len = 20
never = 5
once = 8
twice = 4
threetimes = 3
def mean_vaccinations(class_len, never, once, twice, threetimes):
return ((never + once + twice + threetimes)) / class_len
print(mean_vaccinations(class_len, never, once, twice, threetimes))
# Create a program that prints out odd numbers using the while loop.
def odd_numbers():
i = 1
while i <= 100:
if i % 2 == 1:
print(i)
i += 1
odd_numbers()
# create a file
f = open("test.txt", "w")
# phone number regex
import re
# UG phone format
# MTN: 077, 078, 076
# AIRTEL: 070, 075
# length: 10
def phone_number_validator(phone_number):
# add length 10 in regex
airtel_regex = re.compile(r'^(070|075)\d{7}$')
mtn_regex = re.compile(r'^(077|078|076)\d{7}$')
# check phone number
if airtel_regex.search(phone_number) or mtn_regex.search(phone_number):
return True
else:
return False
# optimize phone number validator
def phone_number_validator(phone_number):
if len(phone_number) == 10:
if phone_number[0:3] in ('077', '078', '076'):
return True
elif phone_number[0:3] in ('070', '075'):
return True
else:
return False
else:
return False
# phone number validator
def phone_number_validator(phone_number):
if len(phone_number) == 10:
if phone_number[0:3] in ('077', '078', '076') or phone_number[0:3] in ('070', '075'):
return True
else:
return False
else:
return False
# phone number validator
def phone_number_validator(phone_number):
if len(phone_number) == 10:
if phone_number[0:3] in ('077', '078', '076') or phone_number[0:3] in ('070', '075'):
return True
else:
return False
else:
return False