-
Notifications
You must be signed in to change notification settings - Fork 0
/
question_class.py
307 lines (271 loc) · 12.7 KB
/
question_class.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
from random import randint, randrange
from fractions import Fraction
from typing import Union
import names
def arrange_answer_choices(answer_choices: list, correct: Union[int, float, Fraction]) -> tuple:
asc_desc = randint(0, 1)
if asc_desc == 0:
answer_choices = sorted(answer_choices)
else:
answer_choices = (sorted(answer_choices))[::-1]
choices = ['A', 'B', 'C', 'D', 'E']
i = 0
while i < len(answer_choices):
print(f"{choices[i]}) {answer_choices[i]}")
i += 1
if answer_choices[0] == correct:
correct_letter = 'A'
elif answer_choices[1] == correct:
correct_letter = 'B'
elif answer_choices[2] == correct:
correct_letter = 'C'
elif answer_choices[3] == correct:
correct_letter = 'D'
else:
correct_letter = 'E'
print(correct_letter)
return answer_choices, correct_letter
class QuestionFactory:
def __init__(self, number):
self.number = number
def __repr__(self):
return f"Question number: {self.number}"
@staticmethod
def no_of_primes():
a = randint(5, 10)
b = randint(20, 50)
def prime_check(n: int) -> str:
# 0, 1, even numbers greater than 2 are NOT PRIME
if n == 1 or n == 0 or (n % 2 == 0 and n > 2):
return "Not prime"
else:
'''Not prime if divisible by another number less than
or equal to the square root of itself.
n**(1/2) returns square root of n'''
for i in range(3, int(n ** (1 / 2)) + 1, 2):
if n % i == 0:
return "Not prime"
return "Prime"
def total_primes(a: int, b: int) -> QuestionElements:
arr = range(a + 1, b)
wrongs = []
correct = 0
stem = f"How many prime numbers are less than {b} and greater than {a}?"
print(stem)
for num in arr:
if prime_check(num) == "Prime":
correct += 1
while len(wrongs) < 4:
wrong = correct + randint(-5, 10)
if wrong not in wrongs and wrong > 0 and wrong != correct:
wrongs.append(wrong)
wrongs.append(correct)
choices_and_correct = arrange_answer_choices(answer_choices=wrongs, correct=correct)
return QuestionElements(
question_stem=stem, ac_list=choices_and_correct[0], correct=choices_and_correct[1])
return total_primes(a, b)
@staticmethod
def sum_of_first_n_numbers():
n1 = randint(1, 10) * 25
n2 = n1 * randint(2, 5)
def gaussian_theorem_consecutive(n: int) -> int:
return int((n + 1) * n / 2)
def first_second(n1: int, n2: int) -> QuestionElements:
answer_choices = []
first = "{:,}".format(gaussian_theorem_consecutive(n1))
second = gaussian_theorem_consecutive(n2)
stem = f'''The sum of the first {n1} positive integers is {first}.
What is the sum of the first {n2} positive integers?'''
print(
f'''The sum of the first {n1} positive integers is {first}.
What is the sum of the first {n2} positive integers?''')
correct = second
answer_choices.append(correct)
while len(answer_choices) < 5:
wrongs = correct + (randint(-5, 10)) * 25
if wrongs not in answer_choices and wrongs > 0 and wrongs != correct:
answer_choices.append(wrongs)
choices_and_correct = arrange_answer_choices(answer_choices=answer_choices, correct=correct)
return QuestionElements(
question_stem=stem, ac_list=choices_and_correct[0], correct=choices_and_correct[1])
return first_second(n1, n2)
@staticmethod
def mf_rat_ratio():
n1 = randint(4, 6) * 10
n2 = randint(2, 4) * 10
def rat_ratio(n1: int, n2: int) -> QuestionElements:
answer_choices = []
stem = f"""{n1} percent of the mice included in an experiment were male mice. If some of the mice
died during the experiment and {n2} percent of the mice that died were male mice,
what was the ratio of death rate among the male mice to the death rate among the female mice?"""
print(stem)
num_of_num = int((n2 / 100) * 50)
denom_of_num = n1
num_of_denom = int(50 - num_of_num)
denom_of_denom = 100 - n1
numerator = Fraction(num_of_num / denom_of_num)
denominator = Fraction(num_of_denom / denom_of_denom)
correct = (numerator / denominator).limit_denominator()
answer_choices.append(correct)
while len(answer_choices) < 5:
wrongs = Fraction(randint(1, 9) / randint(1, 9)).limit_denominator()
if wrongs not in answer_choices and wrongs > 0 and wrongs != correct:
answer_choices.append(wrongs)
choices_and_correct = arrange_answer_choices(answer_choices=answer_choices, correct=correct)
return QuestionElements(
question_stem=stem, ac_list=choices_and_correct[0], correct=choices_and_correct[1])
return rat_ratio(n1, n2)
@staticmethod
def age_diff():
def al_age():
factor_dict = {2: 'twice', 3: 'three times', 4: 'four times', 5: 'five times', 6: 'six times',
7: 'seven times'}
randomizer1 = randrange(3, 7, 2)
randomizer2 = randint(2, randomizer1 - 1)
in_years = randrange(3, 15, 2)
less_years = randrange(1, 5, 2)
name_1 = names.get_first_name(gender='male')
name_2 = names.get_first_name(gender='female')
stem = f"""Today {name_1} is {factor_dict[randomizer1]} as old as {name_2}. In {in_years} year(s),
{name_1} will be {less_years} year(s) less than {factor_dict[randomizer2]} as old as {name_2} will be then.
How many years old is {name_1} today?"""
correct = int((randomizer2 * in_years - less_years - in_years) / (1 - (randomizer2 / randomizer1)))
answer_choices = []
if correct == 0:
al_age()
else:
print(stem)
answer_choices.append(correct)
while len(answer_choices) < 5:
wrongs = correct + randint(-10, 20)
if wrongs not in answer_choices and wrongs > 0 and wrongs != correct:
answer_choices.append(wrongs)
choices_and_correct = arrange_answer_choices(answer_choices=answer_choices, correct=correct)
return QuestionElements(
question_stem=stem, ac_list=choices_and_correct[0], correct=choices_and_correct[1])
return al_age()
@staticmethod
def jacket_profit():
mark_up = randint(1, 3) / 4
if mark_up == 1 / 4:
discount = 1 / 5
else:
discount = randint(1, 2) / 5
purchase_price = int((20 * randint(3, 6)))
sale_price = purchase_price / (1 - mark_up)
discounted_price = sale_price * (1 - discount)
correct = discounted_price - purchase_price
stem = f"""A merchant purchased a jacket for ${purchase_price} and
then determined a selling price that equalled the purchase price of
the jacket plus a markup that was {int(mark_up * 100)} percent of the selling
price. During a sale, the merchant discounted the selling price by
{int(discount * 100)} percent and sold the jacket. What was the merchant's
gross profit on the sale?"""
answer_choices = []
print(stem)
answer_choices.append(correct)
while len(answer_choices) < 5:
wrongs = correct + randint(-10, 20)
if wrongs not in answer_choices and wrongs > 0 and wrongs != correct:
answer_choices.append(wrongs)
choices_and_correct = arrange_answer_choices(answer_choices=answer_choices, correct=correct)
return QuestionElements(
question_stem=stem, ac_list=choices_and_correct[0], correct=choices_and_correct[1])
@staticmethod
def work_time():
random_base = randrange(1, 8, 2)
together = random_base * randint(2, 3)
alone = random_base * randrange(4, 8, 2)
correct = int(alone * together / (alone - together))
name_1 = names.get_first_name(gender='male')
name_2 = names.get_first_name(gender='female')
stem = f"""Working alone, {name_1} can complete a certain kind of job in {alone}
hours. {name_1} and {name_2}, working together at their respective rates, can complete one of
these jobs in {together} hours. In how many hours can {name_2}, working alone, complete one of
these jobs?"""
answer_choices = []
print(stem)
answer_choices.append(correct)
while len(answer_choices) < 5:
wrongs = correct + randint(-10, 20)
if wrongs not in answer_choices and wrongs > 0 and wrongs != correct:
answer_choices.append(wrongs)
choices_and_correct = arrange_answer_choices(answer_choices=answer_choices, correct=correct)
return QuestionElements(
question_stem=stem, ac_list=choices_and_correct[0], correct=choices_and_correct[1])
@staticmethod
def percent_solution():
percent_liquid_x = randrange(10, 40, 10)
percent_water = 100 - percent_liquid_x
starting_kg = randrange(6, 12, 2)
evaporated_kg = randrange(2, 4, 2)
correct = 100 * (starting_kg * percent_liquid_x / 100 + evaporated_kg * percent_liquid_x / 100) / starting_kg
if correct == int(correct):
correct = int(correct)
else:
correct = correct
stem = f"""Solution Y is {percent_liquid_x} percent liquid X and
{percent_water} percent water. If {evaporated_kg} kilograms of water
evaporate from {starting_kg} kilograms of solution Y and {evaporated_kg}
kilograms of solution Y are added to the remaining
{starting_kg - evaporated_kg} kilograms of liquid, what percent of this
new solution is liquid X?"""
answer_choices = []
print(stem)
answer_choices.append(correct)
while len(answer_choices) < 5:
wrongs = correct + randint(-10, 20)
if wrongs not in answer_choices and wrongs > 0 and wrongs != correct:
answer_choices.append(wrongs)
asc_desc = randint(0, 1)
if asc_desc == 0:
answer_choices = sorted(answer_choices)
else:
answer_choices = (sorted(answer_choices))[::-1]
choices = ['A', 'B', 'C', 'D', 'E']
i = 0
while i < len(answer_choices):
print(f"{choices[i]}) {answer_choices[i]}%")
i += 1
choices_and_correct = arrange_answer_choices(answer_choices=answer_choices, correct=correct)
choices = choices_and_correct[0]
if correct == int(correct):
choices_as_percents = [f"{choice}%" for choice in choices]
else:
rounded = ["{:,.2f}".format(choice) for choice in choices]
choices_as_percents = [f"{choice}%" for choice in rounded]
return QuestionElements(
question_stem=stem, ac_list=choices_as_percents, correct=choices_and_correct[1])
@staticmethod
def original_solution():
percent_1 = 5 * randint(1, 6)
percent_2 = 5 * randint(7, 12)
liter_increase = randint(1, 5)
numerator = (100 - percent_2) * liter_increase
denominator = percent_2 - percent_1
correct = Fraction(numerator / denominator).limit_denominator()
if round(float(correct)) == int(correct):
correct = int(correct)
else:
correct = correct
stem = f"""A certain tub originally contained a solution that was {percent_1}
percent ammonia by volume. After {liter_increase} liters of ammonia was added
to the tub, the new solution was {percent_2} percent ammonia by volume. How
many liters were in the original solution?"""
answer_choices = []
print(stem)
answer_choices.append(correct)
while len(answer_choices) < 5:
wrongs = correct + randint(-5, 10)
if wrongs not in answer_choices and wrongs > 0 and wrongs != correct:
answer_choices.append(wrongs)
choices_and_correct = arrange_answer_choices(answer_choices=answer_choices, correct=correct)
return QuestionElements(
question_stem=stem, ac_list=choices_and_correct[0], correct=choices_and_correct[1])
class QuestionElements:
def __init__(self, question_stem, ac_list, correct):
self.question_stem = question_stem
self.ac_list = ac_list
self.correct = correct
def __repr__(self):
return f"Question:"