Skip to content

Commit 5eb45d4

Browse files
author
Jessica Yung
committed
feat(icake): add p37 rand5() from rand7()
1 parent 8aaddc1 commit 5eb45d4

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

interview-cake/p37.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
You have a function rand7() that generates a random integer from 1 to 7. Use it to write a function rand5() that
3+
generates a random integer from 1 to 5.
4+
"""
5+
import numpy as np
6+
def rand7():
7+
return int(np.random.random()*7+1)
8+
9+
def rand5():
10+
result = 7
11+
while result > 5:
12+
result = rand7()
13+
return result
14+
15+
def rand5_v1():
16+
stop = False
17+
18+
while stop == False:
19+
num = rand7()
20+
if num <= 5:
21+
stop = True
22+
return num
23+
24+
# Test rand7():
25+
"""
26+
for i in range(10):
27+
print(rand7())
28+
"""

0 commit comments

Comments
 (0)