-
Notifications
You must be signed in to change notification settings - Fork 0
/
tradertest.py
121 lines (119 loc) · 3.85 KB
/
tradertest.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
# Orko Sarkar
# Trader Test
# tests mental addition, multiplication, square root estimation, and previous
# answer recall to help prepare for interviews
# Will repeat questions until correct answer except for recall.
# Sqrt estimation assumes user is using linear approximation.
import os
import math as m
import random
from decimal import Decimal,DecimalException
lasttwo = [] #stores the last two integers for recall
rand = -1 #
twoint = 0
while True:
# stops repeat recall questions
if rand >= .8:
while rand >= .8:
rand = random.random()
else:
rand = random.random()
# addition
if rand < .3:
# generate random numbers and sum
a,b = random.randint(1,100),random.randint(1,100)
c = a+b
sol = -1
# test sum
while sol != c:
print(str(a) + '+' + str(b))
os.system('sleep 5')
os.system('clear')
# catch input errors
while True:
try:
sol = int(input('solution:\n'))
os.system('clear')
break
except ValueError:
os.system('clear')
print('Invalid input, try again.')
lasttwo.append(sol)
print('Correct!')
twoint = twoint + 1
# multiplication
elif rand < .6:
# generate random numbers and product
a,b = random.randint(1,50),random.randint(1,50)
c = a*b
sol = -1
# test product
while sol != c:
print(str(a) + '*' + str(b))
os.system('sleep 5')
os.system('clear')
# catch input errors
while True:
try:
sol = int(input('solution:\n'))
os.system('clear')
break
except ValueError:
os.system('clear')
print('Invalid input, try again.')
lasttwo.append(sol)
print('Correct!')
twoint = twoint + 1
# square root estimation
elif rand < .75:
# generate random number, squareroot, and linear approximation
a = random.randint(1,100)
c = m.sqrt(a)
upper = m.ceil(c)**2
lower = m.floor(c)**2
while (upper - lower)==0:
a = random.randint(1,100)
c = m.sqrt(a)
upper = m.ceil(c)**2
lower = m.floor(c)**2
est = Decimal(m.floor(c) + (a-lower)/(upper-lower))
sol = -1
# test square root
# since linear approximation will always be under sqrt, truncate at 2 decimal
# places and use that as the lower bound - should never estimate over
while sol < m.floor(est*100)/100 or sol > c:
print('square root of ' + str(a))
os.system('sleep 5')
os.system('clear')
# catch input errors
while True:
try:
sol = Decimal(float(input('solution:\n')))
os.system('clear')
break
except (ValueError, DecimalException):
os.system('clear')
print('Invalid input, try again.')
print('Correct!')
twoint = 0
# recall last integer
elif len(lasttwo) == 2 and twoint > 1:
# test last integer, and catch input errors
while True:
try:
if int(input('What was the solution before the last \n')) == lasttwo[0]:
os.system('clear')
print('Correct!')
break
else:
print('Incorrect, correct answer is ' + str(lasttwo[0]))
os.system('sleep 5')
os.system('clear')
break
break
except ValueError:
os.system('clear')
print('Invalid input, try again.')
twoint = 0
if len(lasttwo) == 3:
lasttwo.pop(0)