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. maven下载与配置

    转自:https://www.cnblogs.com/jdys/p/3770534.html 1.访问官网:从maven官网下载maven http://maven.apache.org/downlo ...

  2. mysql数据库复制

    核心命令是 myssqldump mysqldump --host=host1 --opt sourceDb| mysql --host=host2 -C targetDb 详情参考: MySQL数据 ...

  3. Xtrabackup安装及使用

    官方安装步骤:https://www.percona.com/doc/percona-xtrabackup/2.4/installation/yum_repo.html 安装percona repo源 ...

  4. Mysql 5.8安装报错

    1 mysql安装过程中报下面这个错 解决方法:只要将目录改成反斜杠即可. 2 修改密码错误:ERROR 1064 (42000): You have an error in your SQL syn ...

  5. neo4j 常用命令查询,以及更新 节点 的 label 名 和 property 名

    常用命令查询 https://neo4j.com/docs/cypher-refcard/current/ 更新节点的 labels 有时候 发现节点的 label 名字起错了怎么修改呢?!一个节点是 ...

  6. 读取数据库信息并生成表设计文档Word版本

    1.参考C#代码 using Help.DBAccessLayer.Business; using Help.DBAccessLayer.Model.SqlGenerator; using Newto ...

  7. 多线程-threading

    多线程-threading python的thread模块是比较底层的模块,python的threading模块是对thread做了一些包装的,可以更加方便的被使用 1. 使用threading模块 ...

  8. tag-SMASS-1

    SMASS 是在vasp的DFTMD中决定着系综的类型,在手册中给出的该参数具体信息如下: SMASS = -3 | -2 | -1 | [real] ≥ 0 Default: SMASS = -3 ...

  9. C++中的浅拷贝和深拷贝

    浅拷贝(shallow copy)与深拷贝(deep copy)对于值拷贝的处理相同,都是创建新对象,但对于引用拷贝的处理不同,深拷贝将会重新创建新对象,返回新对象的引用字.浅拷贝不会创建新引用类型. ...

  10. leetcode 196. Delete Duplicate Emails

    # 慢,内连接delete p1 from Person p1, Person p2 where p1.Email=p2.Email and p1.Id>p2.Id delete from Pe ...