interview-preparation / what-we-do

0 stars 8 forks source link

[Moderate] interview questions #23 #150

Closed rygh4775 closed 5 years ago

rygh4775 commented 5 years ago

image

RoyMoon commented 5 years ago

Solution 1

(rand5() + rand5()) % 7

Screen Shot 2019-11-03 at 8 28 16 PM

Solution 2

import random
def Rand7():
    rand = [[0,1,2,3,4], \
            [5,6,0,1,2], \
            [3,4,5,6,0], \
            [1,2,3,4,5], \
                   [6,-1,-1,-1,-1]]

    ret = rand[Rand5()][Rand5()]
    if ret < 0 :
        return Rand7()
    else :
        return ret

def Rand5():
    return random.randrange(0,5) 

print(Rand7())