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!

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

  1. Dice 5 ==> dice 7

    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. bootstrap修改勾选样式

    小对勾需要引入awesome插件. css部分: .bella-checkbox{ position: relative;}/** 将初始的checkbox的样式改变 */.bella-checkbo ...

  2. 基于OpenGL编写一个简易的2D渲染框架-03 渲染基本几何图形

    阅读文章前需要了解的知识,你好,三角形:https://learnopengl-cn.github.io/01%20Getting%20started/04%20Hello%20Triangle/ 要 ...

  3. 前端-CSS-7-标准文档流&行内元素和块级元素转换

    1.什么是标准文档流 <!--  什么是标准文档流 宏观的将,我们的web页面和ps等设计软件有本质的区别 web 网页的制作 是个“流” 从上而下 ,像 “织毛衣” 而设计软件 ,想往哪里画东 ...

  4. WDA-FPM-3-SEARCH(OIF)

    转载:https://www.cnblogs.com/sapSB/p/10097830.html   FPM三:简单的SEARCH(OIF) 这里是使用FPM Workbench自动生成的,没有去SE ...

  5. fabric应用

    安装: easy_install fabric 或 pip install fabric 验证: #python >>> import  fabric 有时候我们可以直接使用命令行的 ...

  6. HQL中的Like查询需要注意的地方

    public List getOrgan(String organCode, String organName) {    String hsql;    List list; if (organCo ...

  7. Spoon新建repository的时候

    Spoon新建repository的时候,下面选项选择‘否’,不要选择‘是’,不然可能会出错.

  8. Rust语言学习笔记(4)

    Variables and Mutability(变量和可变性) 变量声明有三种:不变量(运行期的常量),变量以及(编译期的)常量. 变量可以重复绑定,后声明的变量覆盖前面声明的同名变量,重复绑定时可 ...

  9. Kotlin语言学习笔记(6)

    运算符重载(Operator overloading) 一元运算符 Expression Translated to +a a.unaryPlus() -a a.unaryMinus() !a a.n ...

  10. ArcGIS模型构建器案例学习-批量删除空要素类地理模型

    ArcGIS模型构建器案例学习笔记-批量删除空要素类地理模型 联系方式:谢老师,135-4855-4328,xiexiaokui@qq.com 目的:批量删除记录个数为0的矢量文件 优点:逻辑清晰,不 ...