Dice 7 ==> dice 5

https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Mock%20Interviews/Large%20Search%20Engine%20Company%20/Search%20Engine%20Company%20-%20Interview%20Problems%20-%20SOLUTIONS/On-Site%20Question%201%20-%20SOLUTION.ipynb

On-Site Question 1 - SOLUTION


Question

Given a dice which rolls 1 to 7 (with uniform probability), simulate a 5 sided dice. Preferably, write your solution as a function.

Requirements

You MUST do this on pen and paper or on a whiteboard. No actual coding is allowed until you‘ve solved it on pen and paper!


SOLUTION

This is a new problem we haven‘t seen directly before! Many times this question is asked in the form of functions e.g. your given a function random_7() and you have to take it as an input and create random_5()

The key to solving this problem is to make sure you focus on the requirement that the final distribution of the rolls be uniform, also you were not given any requirements on Time and Space, so the solution is actually very simple, just keep re-rolling if you get a number greater than 5!

We can code this out:

from random import randint

def dice7():
    return randint(1, 7)

# Our Solution
def convert7to5():

    # Starting roll (just needs to be larger than 5)
    roll = 7

    while roll > 5:

        roll = dice7()
        print ‘dice7() produced a roll of ‘,roll
    print ‘ Your final returned roll is below:‘
    return roll

In [6]:

convert7to5()
dice7() produced a roll of  7
dice7() produced a roll of  5
 Your final returned roll is below:

Out[6]:

5

Now, the next problem (On-Site Question 2) will be harder, the reverse conversion of rolls! This question should serve as a reminder not to overthink the solution to a question! Keep in mind that our solution has the potential to run for infinity if we keep rolling 6s and 7s (although this is highly unlikely).

Good Job!

时间: 2024-10-05 08:01:10

Dice 7 ==> dice 5的相关文章

HDOJ 5012 Dice

BFS爆搜 Dice Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 284    Accepted Submission(s): 166 Problem Description There are 2 special dices on the table. On each face of the dice, a distinct nu

UVA 10759 Dice Throwing

题意为抛n个骰子凑成的点数和大于或等于x的概率,刚开始用暴力枚举,虽然AC了,但时间为2.227s,然后百度了下别人的做法,交了一遍,靠,0.000s,然后看了下思路,原来是dp,在暴力的基础上记忆化搜索,把所有可能枚举出来再累加,然后自己也打了一遍,0.000sA了,做法是开一个二维数组,第一维是骰子个数,第二维是和x,然后一个只有可能是1,2,3,4,5,6,倒两个骰子时是1+1,2,3,4,5,6     2+1,2,3,4,5,6  3+...累加向上推即可 1 //暴力做法,2.227

poj 4014 Dice 贪心

//poj 4014 //sep9 #include <iostream> #include <algorithm> using namespace std; int n; struct DICE { int ids; int num; int a[128]; }d[1024]; int cmp1(DICE x,DICE y) { return x.num<y.num; } int cmp2(DICE x,DICE y) { return x.ids<y.ids; }

ACM学习历程—HDU 5012 Dice(ACM西安网赛)(bfs)

Problem Description There are 2 special dices on the table. On each face of the dice, a distinct number was written. Consider a1.a2,a3,a4,a5,a6 to be numbers written on top face, bottom face, left face, right face, front face and back face of dice A.

HDU 4652 Dice (概率DP)

Dice Problem Description You have a dice with m faces, each face contains a distinct number. We assume when we tossing the dice, each face will occur randomly and uniformly. Now you have T query to answer, each query has one of the following form: 0

CodeForcesGym 100502D Dice Game

Dice Game Time Limit: 1000ms Memory Limit: 524288KB This problem will be judged on CodeForcesGym. Original ID: 100502D64-bit integer IO format: %I64d      Java class name: (Any) Gunnar and Emma play a lot of board games at home, so they own many dice

Spring-1-F Dice(HDU 5012)解题报告及测试数据

Dice Time Limit:1000MS     Memory Limit:65536KB Description There are 2 special dices on the table. On each face of the dice, a distinct number was written. Consider a 1.a 2,a 3,a 4,a 5,a 6 to be numbers written on top face, bottom face, left face, r

hdu 5012 Dice

Dice Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 440    Accepted Submission(s): 259 Problem Description There are 2 special dices on the table. On each face of the dice, a distinct number w

HDU - 5012 Dice(BFS)

Problem Description There are 2 special dices on the table. On each face of the dice, a distinct number was written. Consider a1.a2,a3,a4,a5,a6 to be numbers written on top face, bottom face, left face, right face, front face and back face of dice A.