-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
1410 lines (1185 loc) · 54 KB
/
functions.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -----------------------------------------------------------
# This file contains all the functions for the API.
# output of each file is sending to app.py to generate
# JSON output and route to desired path.
# Difficulty of questions can be changed from here.
# Data used in sets such as country names are selected from datasets.py
# -----------------------------------------------------------
# ---------- Imports ----------
import random
import math
import datasets as ds
import jsonify as js
import uuid
import venn_diagram as venn
import itertools
# ---------- Template ----------
def template():
"""
Template for making questions
:return:
"""
# Making a list for answer answer_choices for multiple choice question.
answer_choices = []
# Making 4 question and adding the answer of first three to
# the answer options and choose the last question as the main question
while len(answer_choices) < 4:
# TODO
question = 'question in string'
answer = 'whatever the answer is'
# END TODO
# for making a Venn diagram
# venn.ven2(set1, set2)
# To prevent presence duplicate answer answer_choices.
if answer not in answer_choices:
answer_choices.append(answer)
# end while
# Shuffle answer_choices
answer_choices = random.sample(answer_choices, len(answer_choices))
# Put the question in json format using jsonify.py
return js.question_json_maker(uuid.uuid1().hex, question, answer_choices, answer_choices.index(answer) + 1, difficulty=4)
# ---------- Set Operations ----------
def random_set(integer=5, floats=0, char=0, country_name=0, city_name=0, male_name=0
, female_name=0, integer_min=0, integer_max=20, integer_type='mix'
, float_min=0, float_max=20, float_dec=2, heterogeneous=False):
"""
Generates set with given arguments.
:param integer: number of integers in the set.
:param floats: number of floats in the set.
:param char: number of characters in the set.
:param country_name: number of country names in the set.
:param city_name: number of city names in the set.
:param male_name: number of different male names in the set.
:param female_name: number of different female names in the set.
:param integer_min: minimum to be selected for integers.
:param integer_max: maximum to be selected for integers.
:param integer_type: defines if the integers should be odd, even or mix.
:param float_min: minimum to be selected for floats.
:param float_max: maximum to be selected for floats.
:param float_dec: number of decimal points for floats.
:param heterogeneous: if we want repeated elements in the set or not.
:return: main_list
"""
main_list = []
# Integers
int_temp = []
while len(int_temp) < integer:
if heterogeneous:
if integer_type == 'even':
rand_int = random.randrange(integer_min, integer_max, 2)
if rand_int not in int_temp:
int_temp.append(rand_int)
main_list.append(rand_int)
elif integer_type == 'odd':
rand_int = random.randrange(integer_min, integer_max, 2) + 1
if rand_int not in int_temp:
int_temp.append(rand_int)
main_list.append(rand_int)
else:
rand_int = random.randrange(integer_min, integer_max)
if rand_int not in int_temp:
int_temp.append(rand_int)
main_list.append(rand_int)
if heterogeneous is False:
if integer_type == 'even':
rand_int = random.randrange(integer_min, integer_max, 2)
int_temp.append(rand_int)
main_list.append(rand_int)
elif integer_type == 'odd':
rand_int = random.randrange(integer_min, integer_max, 2) + 1
int_temp.append(rand_int)
main_list.append(rand_int)
else:
rand_int = random.randrange(integer_min, integer_max)
int_temp.append(rand_int)
main_list.append(rand_int)
# floats
float_temp = []
while len(float_temp) < floats:
if heterogeneous:
rand_float = round(random.uniform(float_min, float_max), float_dec)
if rand_float not in float_temp:
float_temp.append(rand_float)
main_list.append(rand_float)
if heterogeneous is False:
rand_float = round(random.uniform(float_min, float_max), float_dec)
float_temp.append(rand_float)
main_list.append(rand_float)
# strings
string_ls = [(ds.country_names, country_name), (ds.city_names, city_name),
(ds.male_names, male_name), (ds.female_names, female_name), (ds.characters, char)]
string_temp = []
for i in string_ls:
if heterogeneous:
while len(string_temp) < i[1]:
temp1 = random.choice(i[0])
if temp1 not in string_temp:
string_temp.append(temp1)
main_list.append(temp1)
string_temp = []
if heterogeneous is False:
while len(string_temp) < i[1]:
temp1 = random.choice(i[0])
string_temp.append(temp1)
main_list.append(temp1)
string_temp = []
return main_list
def sub_sets(sset):
"""
Function to generate subset of a given set.
:param sset: the set that we want to get subsets.
:return: subsets
"""
return subsetsRecur([], sorted(sset))
def subsetsRecur(current, sset):
if sset:
return subsetsRecur(current, sset[1:]) + subsetsRecur(current + [sset[0]], sset[1:])
return [current]
def set_operation(op='set-union', set_1=set(random_set()), set_2=set(random_set())):
"""
Implements different set operations on two given sets.
:param op: type of operation we want to implement, options are:
'union': returns union of two set.
'intersection': returns intersection of two sets.
'cartesian': returns cartesian product of two sets.
'difference': returns difference of two sets.
'symmetric_difference': returns symmetric difference of two sets.
'partition': returns all subsets of the given set.(only gets set_1)
'complement': returns complement from a given set. (only gets set_1 and it considers as universal set.)
:param set_1: first set.
:param set_2: second set.
:return: output
"""
global output
# list of symbols used in the questions in HTML unicode format
# intersection_symbol = '∩'
# union_symbol = '∪'
empty_symbol = '∅'
# delta = 'Δ'
if op == 'set-union':
output = str(set(set_1).union(set(set_2))).replace("'", '')
elif op == 'set-intersection':
output = str(
set(set_1).intersection(set(set_2))).replace('set()', empty_symbol).replace("'", '')
elif op == 'set-difference':
output = str(set(set_1).difference(set(set_2))).replace('set()',
empty_symbol).replace(
"'", '')
elif op == 'cartesian-product':
output = str([(obj1, obj2) for obj1 in set_1 for obj2 in set_2]).replace(
'[', '').replace(']', '')
elif op == 'set-symmetric-difference':
output = str(
set(set_1).difference(set(set_2)).union(set(set_2).difference(set(set_1)))).replace('set()',
empty_symbol).replace(
"'", '')
elif op == 'set-partition':
# make second set empty
set_2 = []
output = str(sub_sets(set_1)).replace('[', '{').replace(']', '}').replace('B=', '')
return output
def set_complement():
"""
function to generate random questions for set complement.
:return: Questions.(JSON)
"""
answer_choices = []
while len(answer_choices) < 4:
set_1 = random_set()
subset_1 = []
for i in range(len(set_1) - 2):
subset_1.append(random.choice(set_1))
question = (
f'What is the complement of set A= {str(set(subset_1))} considering U= {str(set(set_1))} is universal set? ').replace(
'[]',
'').replace(
'[', '{').replace(']', '}').replace('_', ' ').replace('[', '{').replace(
']', '}')
answer = str(set(set_1) - set(subset_1)).replace('[', '{').replace(']', '}')
if answer not in answer_choices:
answer_choices.append(answer)
answer_choices = random.sample(answer_choices, len(answer_choices))
return js.question_json_maker(uuid.uuid1().hex, question, answer_choices, answer_choices.index(answer) + 1, difficulty=3)
def set_theory_choices(par, operation):
"""
Generates choices for set operation multiple answer questions.
:param par: type of items in each list.(str)
:param operation: type of operation on sets.(str)
:return: Questions.(JSON)
"""
answer_choices = []
while len(answer_choices) < 4:
num2 = str(par)
if operation == 'set-partition':
set2 = []
if num2[0] == '1':
set1 = random_set(integer=3)
elif num2[0] == '2':
set1 = random_set(integer=0, floats=3)
elif num2[0] == '3':
set1 = random_set(integer=0, char=3)
elif num2[0] == '4':
set1 = random_set(integer=0, country_name=3)
elif num2[0] == '5':
set1 = random_set(integer=0, city_name=3)
elif num2[0] == '6':
set1 = random_set(integer=0, male_name=3)
elif num2[0] == '7':
set1 = random_set(integer=0, female_name=3)
elif operation == 'cartesian':
if num2[0] == '1':
set1 = random_set(integer=3)
elif num2[0] == '2':
set1 = random_set(integer=0, floats=3)
elif num2[0] == '3':
set1 = random_set(integer=0, char=3)
elif num2[0] == '4':
set1 = random_set(integer=0, country_name=3)
elif num2[0] == '5':
set1 = random_set(integer=0, city_name=3)
elif num2[0] == '6':
set1 = random_set(integer=0, male_name=3)
elif num2[0] == '7':
set1 = random_set(integer=0, female_name=3)
if num2[1] == '1':
set2 = random_set(integer=3)
elif num2[1] == '2':
set2 = random_set(integer=0, floats=3)
elif num2[1] == '3':
set2 = random_set(integer=0, char=3)
elif num2[1] == '4':
set2 = random_set(integer=0, country_name=3)
elif num2[1] == '5':
set2 = random_set(integer=0, city_name=3)
elif num2[1] == '6':
set2 = random_set(integer=0, male_name=3)
elif num2[1] == '7':
set2 = random_set(integer=0, female_name=3)
else:
if num2[0] == '1':
set1 = random_set()
elif num2[0] == '2':
set1 = random_set(integer=0, floats=5)
elif num2[0] == '3':
set1 = random_set(integer=0, char=5)
elif num2[0] == '4':
set1 = random_set(integer=0, country_name=5)
elif num2[0] == '5':
set1 = random_set(integer=0, city_name=5)
elif num2[0] == '6':
set1 = random_set(integer=0, male_name=5)
elif num2[0] == '7':
set1 = random_set(integer=0, female_name=5)
if num2[1] == '1':
set2 = random_set()
elif num2[1] == '2':
set2 = random_set(integer=0, floats=5)
elif num2[1] == '3':
set2 = random_set(integer=0, char=5)
elif num2[1] == '4':
set2 = random_set(integer=0, country_name=5)
elif num2[1] == '5':
set2 = random_set(integer=0, city_name=5)
elif num2[1] == '6':
set2 = random_set(integer=0, male_name=5)
elif num2[1] == '7':
set2 = random_set(integer=0, female_name=5)
if operation == 'set-complement':
answer = set_operation(op=operation, set_1=set1, set_2=set2)
else:
answer = set_operation(op=operation, set_1=set(set1), set_2=set(set2))
if answer not in answer_choices:
answer_choices.append(answer)
else:
pass
if operation == 'set-complement':
question = ('What is the ' + operation.replace('set-',
'') + ' of set A considering U is universal set? ').replace('[]',
'').replace(
'[', '{').replace(']', '}').replace('_', ' ').replace('[', '{').replace(
']', '}')
elif operation == 'set-partition':
question = ('What is the ' + operation.replace('set-', '') + ' of this set? ').replace('[]', '').replace('[',
'{').replace(
']', '}') + ' A= ' + str(set(set1)).replace('[]', '').replace('[', '{').replace(
']', '}')
else:
question = ('What is the ' + operation.replace('set-', '').replace('cartesian-product',
'cartesian product').replace(
'set-symmetric-difference', 'symmetric difference') + ' of these two sets? ').replace('_', ' ').replace('[',
'{').replace(
']', '}') + ' A= ' + str(set(set1)).replace('[]', '').replace('[', '{').replace(
']', '}') + ' B= ' + str(set(set2)).replace(
'[]', '').replace('[]', '').replace('[', '{').replace(']', '}')
answer_choices = random.sample(answer_choices, len(answer_choices))
output_json = js.question_json_maker(uuid.uuid1().hex, str(question), answer_choices, answer_choices.index(answer) + 1,
difficulty=2)
return output_json
def intersection_venn():
"""
Generate question for sets using Venn diagram.
:return: Question in JSON format. Venn diagram in img tag for use in HTML.
"""
answer_choices = []
while len(answer_choices) < 4:
set1 = set(random_set(integer=8, floats=0, char=0, country_name=0, city_name=0, male_name=0
, female_name=0, integer_min=5, integer_max=20, integer_type='mix'
, float_min=0, float_max=20, float_dec=2, heterogeneous=False))
set2 = set(random_set(integer=9, floats=0, char=0, country_name=0, city_name=0, male_name=0
, female_name=0, integer_min=1, integer_max=30, integer_type='mix'
, float_min=0, float_max=20, float_dec=2, heterogeneous=False))
question = f'Which one is correct for two sets of A = {set1} B = {set2} ?'
answer = venn.ven2(set1, set2)
if answer not in answer_choices:
answer_choices.append(answer)
answer_choices = random.sample(answer_choices, len(answer_choices))
return js.question_json_maker(uuid.uuid1().hex, question, answer_choices, answer_choices.index(answer) + 1, difficulty=3)
# ---------- Functions ----------
def function(domain_set, target_set):
"""
Determine if a relation is a function and tupe of the function.
:return: Function or not and type of the function.
"""
if len(domain_set) == len(set(domain_set)):
function_type = 'General function'
if len(target_set) != len(set(target_set)):
if len(target_set) < len(domain_set) or len(target_set) == len(domain_set):
function_type = 'Surjective function'
elif len(target_set) > len(domain_set) or len(target_set) == len(domain_set):
function_type = 'Injective function'
elif len(target_set) == len(set(target_set)) and len(domain_set) == len(target_set):
function_type = 'Bijective function'
else:
function_type = 'Not function'
return function_type
def general_function():
"""
Generates questions for determining if a set is function or not and if so what kind.
:param num: number of questions.
:return: questions in JSON format.
"""
answer_choices = []
global zip
while len(answer_choices) < 4:
A = []
B = []
# question_json = 'question ' + str(i + 1)
for i in range(random.randint(3, 6)):
A.append(random.randint(1, 10))
for i in range(random.randint(3, 6)):
B.append(random.randint(1, 10))
zip1 = set(zip(A, B))
question = f'Is f(x) from ' \
f'A = {str(set(A)).replace("[", "{").replace("]", "}")} to ' \
f'B = {str(set(B)).replace("[", "{").replace("]", "}")} a function ? ' \
f'If so what kind of function it is ? f(x)=' + str(zip1).replace("[", "{").replace("]", "}")
answer = function(A, B)
if answer not in answer_choices:
answer_choices.append(answer)
answer_choices = random.sample(answer_choices, len(answer_choices))
return js.question_json_maker(uuid.uuid1().hex, question, answer_choices, answer_choices.index(answer) + 1,
difficulty=3)
def ceiling_function():
"""
Generates ceiling function with random floats and asks for the answer.
:return: questions in JSON format.
"""
answer_choices = []
while len(answer_choices) < 4:
r_ceiling_symbol = '⌉'
l_ceiling_symbol = '⌈'
A = round(random.uniform(-40, 40), 2)
question = l_ceiling_symbol + str(A) + r_ceiling_symbol + ' ?'
answer = str(math.ceil(A))
if answer not in answer_choices:
answer_choices.append(answer)
answer_choices = random.sample(answer_choices, len(answer_choices))
return js.question_json_maker(uuid.uuid1().hex, question, answer_choices, answer_choices.index(answer) + 1, difficulty=3)
def floor_function():
"""
Generates floor function with random floats and asks for the answer.
:return: questions in JSON format.
"""
answer_choices = []
while len(answer_choices) < 4:
r_floor_symbol = '⌋'
l_floor_symbol = '⌊'
A = round(random.uniform(-40, 40), 2)
question = l_floor_symbol + str(A) + r_floor_symbol + ' ?'
answer = str(math.floor(A))
if answer not in answer_choices:
answer_choices.append(answer)
answer_choices = random.sample(answer_choices, len(answer_choices))
return js.question_json_maker(uuid.uuid1().hex, question, answer_choices, answer_choices.index(answer) + 1, difficulty=3)
def inverse_function():
"""
Generates a question for inverse of a function.
:param num: number of questions.
:return: questions in JSON format.
"""
global zip
answer_choices = []
while len(answer_choices) < 4:
A = []
B = []
set_len = random.randint(3, 6)
while len(A) != set_len:
rand_int = random.randint(1, 15)
if rand_int not in A:
A.append(rand_int)
while len(B) != set_len:
rand_int = random.randint(15, 30)
if rand_int not in B:
B.append(rand_int)
zip1 = list(zip(A, B))
zip2 = list(zip(B, A))
question = 'What is the inverse of function f=' + str(zip1) + ' ?'
answer = str(zip2)
if answer not in answer_choices:
answer_choices.append(answer)
answer_choices = random.sample(answer_choices, len(answer_choices))
return js.question_json_maker(uuid.uuid1().hex, question, answer_choices, answer_choices.index(answer) + 1, difficulty=4)
def function_domain():
"""
Generates a question for domain of a function.
:return: questions in JSON format.
"""
global zip
answer_choices = []
while len(answer_choices) < 4:
A = []
B = []
for i in range(random.randint(3, 6)):
A.append(random.randint(1, 10))
for i in range(random.randint(3, 6)):
B.append(random.randint(11, 21))
zip1 = list(zip(A, B))
question = 'what is the domain of this function ? f(x)=' + str(zip1).replace('[', '{').replace(']', '}')
zip2 = []
for i in zip1:
zip2.append(i[0])
answer = str(set(zip2))
answer_choices.append(answer)
answer_choices = random.sample(answer_choices, len(answer_choices))
return js.question_json_maker(uuid.uuid1().hex, question, answer_choices, answer_choices.index(answer) + 1, difficulty=3)
def function_target():
"""
Generates a question for target of a function.
:return: questions in JSON format.
"""
global zip
answer_choices = []
while len(answer_choices) < 4:
A = []
B = []
# question_json = 'question ' + str(i + 1)
for i in range(random.randint(3, 6)):
A.append(random.randint(1, 10))
for i in range(random.randint(3, 6)):
B.append(random.randint(11, 21))
zip1 = list(zip(A, B))
question = 'what is the target of this function ? f(x)=' + str(zip1).replace('[', '{').replace(']', '}')
zip2 = []
for i in zip1:
zip2.append(i[1])
answer = str(set(zip2))
answer_choices.append(answer)
answer_choices = random.sample(answer_choices, len(answer_choices))
return js.question_json_maker(uuid.uuid1().hex, question, answer_choices, answer_choices.index(answer) + 1, difficulty=3)
def one_to_one_function():
"""
Generates a question for one to one function using equation with LaTex.
:return: questions in JSON format, equations are in LaTex format and wrapped in $$ in order to be rendered with Mathjax.
"""
answer = f'\(x+{random.randint(2, 20)}\)'
answer_choices = [f'\(x^{random.randint(2, 6)}\)', '$${\sqrt{x} \over 2}$$', '$${\sqrt{x}}$$', answer]
question = 'Which one of these functions is one-to-one?'
answer_choices = random.sample(answer_choices, len(answer_choices))
return js.question_json_maker(uuid.uuid1().hex, question, answer_choices, answer_choices.index(answer) + 1, difficulty=3)
# ---------- Probabilities ----------
def event_probability_1():
"""
Generates a question for event probability.
:return: question, answer choices, correct answer.(JSON)
"""
answer_choices = []
while len(answer_choices) < 4:
roll_number = random.randint(2, 6)
outcome_num = random.randint(1, 6)
times = random.randint(1, roll_number)
question = f'We throw a dice for {roll_number} times what is the probability of having number {outcome_num},' \
f' {times} times?'
answer = f'{times}/{6 * roll_number}'
if answer not in answer_choices:
answer_choices.append(answer)
answer_choices = random.sample(answer_choices, len(answer_choices))
return js.question_json_maker(uuid.uuid1().hex, question, answer_choices, answer_choices.index(answer) + 1, difficulty=3)
def event_probability_2():
"""
Generates a question for event probability.
:return: question, answer choices, correct answer.(JSON)
"""
answer_choices = []
while len(answer_choices) < 4:
symbols = ['spades', 'hearts', 'clubs', 'diamonds']
cards = ['Jack', 'King', 'Queen', 'Ace']
cards_num = random.randint(2, 8)
goal_card1 = random.randint(2, 10)
goal_card2 = random.choice(cards)
question = f'{cards_num} cards are randomly drawn from a deck of normal' \
f' playing cards (52 cards) and each time ' \
f'drawn card replaced to the deck what is the probability' \
f' of drawing {goal_card1} of {random.choice(symbols)} and ' \
f' {goal_card2} of {random.choice(symbols)}?'
answer = f'1/52<sup>{cards_num}</sup>'
if answer not in answer_choices:
answer_choices.append(answer)
answer_choices = random.sample(answer_choices, len(answer_choices))
return js.question_json_maker(uuid.uuid1().hex, question, answer_choices, answer_choices.index(answer) + 1, difficulty=4)
def event_probability_3():
"""
Generates a question for event probability.
:return: question, answer choices, correct answer.(JSON)
"""
answer_choices = []
while len(answer_choices) < 4:
a = random.randint(5, 9)
b = random.randint(8, 12)
question = ('While picking out a tie, you have the choice between ' + str(a) + ' satin ties or ' + str(b) +
' cotton. How many tie choices do you have?')
answer = str(a + b)
if answer not in answer_choices:
answer_choices.append(answer)
answer_choices = random.sample(answer_choices, len(answer_choices))
return js.question_json_maker(uuid.uuid1().hex, question, answer_choices, answer_choices.index(answer) + 1, difficulty=4)
def multiplication_rule_1():
"""
Generates a question for multiplication.
:return: question, answer choices, correct answer.(JSON)
"""
answer_choices = []
while len(answer_choices) < 4:
a = random.randint(3, 8)
question = ('You need to create a password ' + str(
a) + ' characters long. You can choose letters and numbers. Repetition of letters and numbers are allowed, '
'and letters can be capital or lowercase. How many passwords are possible?')
answer = str(62 ** a)
if answer not in answer_choices:
answer_choices.append(answer)
answer_choices = random.sample(answer_choices, len(answer_choices))
return js.question_json_maker(uuid.uuid1().hex, question, answer_choices, answer_choices.index(answer) + 1, difficulty=3)
def multiplication_rule_2():
"""
Generates a question for multiplication.
:return: question, answer choices, correct answer.(JSON)
"""
answer_choices = []
while len(answer_choices) < 4:
a = random.randint(5, 10)
question = ('You decide to flip a coin ' + str(a) + ' times, resulting in a sequence of heads (H) '
'or tails (T). How many difference sequences'
' of heads and tails are possible?')
answer = str(2 ** a)
if answer not in answer_choices:
answer_choices.append(answer)
answer_choices = random.sample(answer_choices, len(answer_choices))
return js.question_json_maker(uuid.uuid1().hex, question, answer_choices, answer_choices.index(answer) + 1, difficulty=4)
def multiplication_rule_3():
"""
Generates a question for multiplication.
:return: question, answer choices, correct answer.(JSON)
"""
answer_choices = []
while len(answer_choices) < 4:
a = random.randint(5, 10)
question = ('Rolling a 3-sided die results in a 1, 2, or 3 appearing. If you roll the die ' + str(
a) + ' times, how many different sequences are possible?')
answer = str(3 ** a)
if answer not in answer_choices:
answer_choices.append(answer)
answer_choices = random.sample(answer_choices, len(answer_choices))
return js.question_json_maker(uuid.uuid1().hex, question, answer_choices, answer_choices.index(answer) + 1, difficulty=4)
def multiplication_rule_4():
"""
Generates a question for multiplication.
:return: question, answer choices, correct answer.(JSON)
"""
answer_choices = []
while len(answer_choices) < 4:
a = random.randint(4, 8)
b = random.randint(3, 6)
c = random.randint(4, 9)
question = ('You have ' + str(a) + ' shirts, ' + str(b) + ' pairs of shoes, and ' + str(
c) + ' pairs of pants. How many outfits can you make?')
answer = str(a * b * c)
if answer not in answer_choices:
answer_choices.append(answer)
answer_choices = random.sample(answer_choices, len(answer_choices))
return js.question_json_maker(uuid.uuid1().hex, question, answer_choices, answer_choices.index(answer) + 1, difficulty=4)
def permutation_1():
"""
Generates a question for event permutation.
:return: question, answer choices, correct answer.(JSON)
"""
answer_choices = []
while len(answer_choices) < 4:
athletes_num = random.randint(6, 10)
question = f'{athletes_num} athletes are competing in a tournament, the first winner ' \
f'is going to get a golden medal, ' \
f'second is going to get a silver and third is going to get bronze. considering ' \
f'the chances of all athletes for all three medals are same in how many possible' \
f' way we can distribute the medals?'
answer = str(math.factorial(athletes_num) / math.factorial(athletes_num - 3))
if answer not in answer_choices:
answer_choices.append(answer)
answer_choices = random.sample(answer_choices, len(answer_choices))
return js.question_json_maker(uuid.uuid1().hex, question, answer_choices, answer_choices.index(answer) + 1, difficulty=4)
def permutation_2():
"""
Generates a question for event permutation.
:return: question, answer choices, correct answer.(JSON)
"""
answer_choices = []
while len(answer_choices) < 4:
a = random.randint(1, 5)
word_list = ['phone', 'truck', 'beach', 'ideal', 'spare', 'smart', 'tower', 'times', 'today']
selected_word = random.choice(word_list)
question = f'How many different {str(a)} letter words can be made' \
f' from {selected_word} if letters cannot be repeated?'
answer = str((math.factorial(5)) / (math.factorial(5-a)))
if answer not in answer_choices:
answer_choices.append(answer)
answer_choices = random.sample(answer_choices, len(answer_choices))
return js.question_json_maker(uuid.uuid1().hex, question, answer_choices, answer_choices.index(answer) + 1, difficulty=3)
def combination_1():
"""
Generates a question for combination.
:return: question, answer choices, correct answer.(JSON)
"""
answer_choices = []
while len(answer_choices) < 4:
athletes_num = random.randint(3, 6)
total_num = athletes_num + random.randint(4, 8)
question = f'{athletes_num} are going to be selected out of a team of {total_num}' \
f' how many posisible combinations' \
f' exist?'
answer = str(
math.factorial(total_num) / (math.factorial(athletes_num - athletes_num) * math.factorial(athletes_num)))
if answer not in answer_choices:
answer_choices.append(answer)
answer_choices = random.sample(answer_choices, len(answer_choices))
return js.question_json_maker(uuid.uuid1().hex, question, answer_choices, answer_choices.index(answer) + 1, difficulty=4)
def combination_2():
"""
Generates a question for combination.
:return: question, answer choices, correct answer.(JSON)
"""
answer_choices = []
while len(answer_choices) < 4:
a = random.randint(2, 7)
b = random.randint(3, 8)
question = (str(a) + ' marbles are drawn from a bag containing ' + str(a) + ' red and ' + str(
b) + ' white marbles. How many different draws are there?')
answer = str((math.factorial(a + b)) / (math.factorial(a) * math.factorial(b)))
if answer not in answer_choices:
answer_choices.append(answer)
answer_choices = random.sample(answer_choices, len(answer_choices))
return js.question_json_maker(uuid.uuid1().hex, question, answer_choices, answer_choices.index(answer) + 1, difficulty=4)
def combination_3():
"""
Generates a question for combination.
:return: question, answer choices, correct answer.(JSON)
"""
answer_choices = []
while len(answer_choices) < 4:
a = random.randint(3, 6)
b = random.randint(20, 40)
question = ('Drawing ' + str(a) + ' cards from a standard deck of ' + str(
b) + ' cards, how many different card hands are there?')
answer = str((math.factorial(b)) / (math.factorial(b - a)))
if answer not in answer_choices:
answer_choices.append(answer)
answer_choices = random.sample(answer_choices, len(answer_choices))
return js.question_json_maker(uuid.uuid1().hex, question, answer_choices, answer_choices.index(answer) + 1, difficulty=4)
def conditional_probability_1():
"""
Generates a question for conditional probability.
:return: question, answer choices, correct answer.(JSON)
"""
answer_choices = []
while len(answer_choices) < 4:
allergic_percent = round(random.uniform(2, 8), 2)
question = f'The {allergic_percent} percent of adults are female are allergic to sesame, ' \
f'what is the probability of being allergic to sesame, given being a female? round your answer to nearest hundredth'
raw_result = ((allergic_percent / 100) / (0.5)) * 100
answer = str(round(raw_result, 2)) + '%'
if answer not in answer_choices:
answer_choices.append(answer)
answer_choices = random.sample(answer_choices, len(answer_choices))
return js.question_json_maker(uuid.uuid1().hex, question, answer_choices, answer_choices.index(answer) + 1, difficulty=4)
def probability_union():
"""
Generates a question for union of events.
:return: question, answer choices, correct answer.(JSON)
"""
answer_choices = []
while len(answer_choices) < 4:
python = (random.randint(10, 60))
java = (random.randint(10, 50))
both = (random.randint(10, 20))
python_str = str(python) + '%'
java_str = str(java) + '%'
both_str = str(both) + '%'
question = f'In a certain population of class: {python_str} are familiar with Python. {java_str} are familiar with Java. ' \
f'{both_str} are familiar with both Python and Java. What percent of students are familiar with either Python or Java?'
raw_result = str((python + java) - both)
answer = raw_result + '%'
if answer not in answer_choices:
answer_choices.append(answer)
answer_choices = random.sample(answer_choices, len(answer_choices))
return js.question_json_maker(uuid.uuid1().hex, question, answer_choices, answer_choices.index(answer) + 1, difficulty=4)
def probability_complement():
"""
Generates a question for complement of events.
:return: question, answer choices, correct answer.(JSON)
"""
answer_choices = []
while len(answer_choices) < 4:
possible_outcomes = [1, 2, 3, 4, 5, 6]
chosen_ones = []
for i in range(random.randint(1, 5)):
chosen_ones.append(random.choice(possible_outcomes))
excluded_numebrs = str(set(chosen_ones)).replace('[', '{').replace(']', '}')
question = f'We are rolling a normal dice, What is the probability that out come would not be' \
f' one of these numbers ? {excluded_numebrs} round your answer to nearest hundredth'
chosen_ones = set(chosen_ones)
raw_answer = (1 - (len(chosen_ones) / 6)) * 100
answer = str(round(raw_answer, 2)) + '%'
if answer not in answer_choices:
answer_choices.append(answer)
answer_choices = random.sample(answer_choices, len(answer_choices))
return js.question_json_maker(uuid.uuid1().hex, question, answer_choices, answer_choices.index(answer) + 1, difficulty=4)
def bayes_theorem():
"""
Generates a question for Bayes theorem.
:return: question, answer choices, correct answer.(JSON)
"""
answer_choices = []
while len(answer_choices) < 4:
A_blue = random.randint(2, 10)
A_red = random.randint(2, 10)
B_blue = random.randint(2, 10)
B_red = random.randint(2, 10)
P_blue_from_A = A_blue / (A_blue + A_red)
P_blue_from_B = B_blue / (B_blue + B_red)
P_bucket = 1 / 2
P_blue = (P_blue_from_A * P_bucket) + (P_blue_from_B * P_bucket)
question = f'We have two buckets A and B. There are {A_blue} blue pencils and {A_red} red pencils in bucket A. And ' \
f'there are {B_blue} blue pencils and {B_red} red pencils in bucket B. If we randomly draw a blue pencil,' \
f' what is the probability' \
f'that pencil was from bucket A ? '
raw_answer = ((P_blue_from_A * P_bucket) / P_blue) * 100
answer = str(round(raw_answer, 2)) + '%'
if answer not in answer_choices:
answer_choices.append(answer)
answer_choices = random.sample(answer_choices, len(answer_choices))
return js.question_json_maker(uuid.uuid1().hex, question, answer_choices, answer_choices.index(answer) + 1, difficulty=4)
# ---------- Relations ----------
def is_reflexive(universe, relation):
"""
Function to determine if a relation of a set is reflexive
:param universe: a set
:param relation: a relation on set universe
return: True if relation is a reflexiver relation of set universe
"""
new_set = {(a, b) for a in universe for b in universe if a == b}
if relation >= new_set:
return True
return False
def inverse_of_relation(r):
"""
Function to determine the inverse of a relation
:param r: a relation
:return: inverse of the relation
"""
return [(y, x) for (x, y) in r]
def is_symmetric(r):
"""
Function to determine if a realtion is symmetric
:param r: a relation
:return: True if relation is symmetric, False otherwise
"""
return set(r) == set([pair for pair in r if pair in inverse_of_relation(r)])
def is_asymmetric(r):
"""
Function to determine if a realtion is asymmetric
:param r: a relation
:return: True if relation is asymmetric, False otherwise
"""
return [pair for pair in r if pair in inverse_of_relation(r)] == []
def is_antisymmetric(r):
"""
Function to determine if a realtion is antisymmetric
:param r: a relation
:return: True if relation is antisymmetric, False otherwise
"""
for (x, y) in set([pair for pair in r if pair in inverse_of_relation(r)]):
if x != y:
return False
return True
def binary_relations(n):
'''Function to find all binary relations of a set n
:param n: a python list of elements
:return: all binary relations of set n
'''
cartesian = []
for element in itertools.product(n, n):
cartesian.append(element)