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%202%20-%20SOLUTION.ipynb

On-Site Question 2 - SOLUTION


Question

Given a dice which rolls from 1 to 5, simulate a uniform 7 sided dice!

Requirements

You MUST do this on pen and paper or on a whiteboard. No actual coding is allowed until you've come up with a solution by hand!

 

 

SOLUTION

Because the 5 sided dice can not produce 7 possible outcomes on a single roll, we immediately know that we need to roll the dice at least twice.

If we roll the dice twice we have 25 possible combinations of the results of the two rolls. While 25 is not divisible by 7, 21 is. This means we can implement our previous strategy of throwing out rolls not in our intended range.

It's also important to note that we can't expand the solution to implement more rolls in order to not throw any out, because 5 and 7 are both prime which means that no exponent of 5 will be divisible by 7 no matter how high you go.

We will define our range as a section of the 25 possible combinations of rolls. A good way to do this is by converting the two rolls into a unique outcome number in the range 1 through 25.

We will create this number by taking the rolls, then we take the first roll and after subtracting 1 from it we multiply it by 4. Now the first roll corresponds with a value of 1 - 20.

Then we take the second roll and add it to the result of the first manipulation. Giving us a range of 1-25.

So our final solution is to roll the dice twice. Check the manipulated range from 1 to 25, if its greater than 21, do a reroll. Let's see it in action:

 
from random import randint

def dice5():
return randint(1, 5)
 

Now for our conversion function:

 
 
def convert5to7():

    # For constant re-roll purposes
while True: # Roll the dice twice
roll_1 = dice5()
roll_2 = dice5() #print 'The rolls were {} and {}'.format(roll_1,roll_2) # Convert the combination to the range 1 to 25
num = ( (roll_1-1) * 5 ) + ( roll_2 ) #print 'The converted range number was:',num
if num > 21: # re-roll if we are out of range
continue return num %7 + 1
 
 
 
In [25]:
convert5to7()
Out[25]:
2
 

Good Job!

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

  1. Dice 7 ==> dice 5

    https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...

  2. UVA 10759 Dice Throwing

    题意为抛n个骰子凑成的点数和大于或等于x的概率,刚开始用暴力枚举,虽然AC了,但时间为2.227s,然后百度了下别人的做法,交了一遍,靠,0.000s,然后看了下思路,原来是dp,在暴力的基础上记忆化 ...

  3. poj 4014 Dice 贪心

    //poj 4014 //sep9 #include <iostream> #include <algorithm> using namespace std; int n; s ...

  4. 图像分割必备知识点 | Dice损失 理论+代码

    本文包含代码案例和讲解,建议收藏,也顺便点个赞吧.欢迎各路朋友爱好者加我的微信讨论问题:cyx645016617. 在很多关于医学图像分割的竞赛.论文和项目中,发现 Dice 系数(Dice coef ...

  5. 使用Dice loss实现清晰的边界检测

    ​ 前言: 在深度学习和计算机视觉中,人们正在努力提取特征,为各种视觉任务输出有意义的表示.在一些任务中,我们只关注对象的几何形状,而不管颜色.纹理和照明等.这就是边界检测的作用所在. 关注公众号CV ...

  6. HDOJ 4652 Dice

      期望DP +数学推导 Dice Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  7. 三种renderman规范引擎的dice对比

    次表面做的有些烦躁,既然如此,索性先记一下前一阵比较的PIXIE.3delight.prman的dice方式. 研究过reyes的人都知道dice,简而言之,就是为了生成高质量高精度的图片(电影CG) ...

  8. LightOJ 1248 Dice (III) 概率

    Description Given a dice with n sides, you have to find the expected number of times you have to thr ...

  9. hdu 4586 Play the Dice 概率推导题

    A - Play the DiceTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...

随机推荐

  1. 黑盒测试用例设计——PICT

    一.简单用法   在PICT安装目录下新建一个txt文本.把参数填入txt文本中.[内容包括(注意格式<ParamName> : <Value1>, <Value2> ...

  2. Kafka集群扩展以及重新分布分区

    我们往已经部署好的Kafka集群里面添加机器是最正常不过的需求,而且添加起来非常地方便,我们需要做的事是从已经部署好的Kafka节点中复制相应的配置文件,然后把里面的broker id修改成全局唯一的 ...

  3. EXCEL 数组公式

    数组里的元素,同一行内的各元素用英文逗号“,”分开,用英文分号“;”将各行分开 3.单列数组与单行数组的计算 两个数组相加,查看结果是几行几列:在任意单元格输入公式“=A80:A83+B87:E87” ...

  4. Spring MVC 视图及视图解析器

    org.springframework.web.servlet.view.InternalResoureceViewResolve 把逻辑视图改为物理视图 可混用多种视图 不进过Handler直接进入 ...

  5. as3 根据鼠标移动方向

    gz 为一个影片 import flash.display.Sprite; stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove ...

  6. 前端开发-3-HTML-body标签

    body标签 h.p.a.ul.ol.div.img. 想要在网页上展示出来的内容一定要放在body标签中. 把我们之前海燕那一段HTML代码贴过来,保存到一个HTML格式的文件中. <!DOC ...

  7. websocket使用

    兼容性介绍 : https://caniuse.com/#search=websockets var websocket = null; //判断当前浏览器是否支持WebSocket if ('Web ...

  8. 从底层谈WebGIS 原理设计与实现(二):探究本质,WebGIS前端地图显示之地图比例尺换算原理

    从底层谈WebGIS 原理设计与实现(二):探究本质,WebGIS前端地图显示之地图比例尺换算原理 作者:naaoveGI…    文章来源:http://www.cnblogs.com/naaove ...

  9. drop user和drop user cascade的区别

    SQL> delete user itp2;delete user itp2       *第 1 行出现错误:ORA-00903: 表名无效 SQL> drop user itp2;dr ...

  10. Petya and Array (权值线段树+逆序对)

    Petya and Array http://codeforces.com/problemset/problem/1042/D time limit per test 2 seconds memory ...