Dice 7 ==> dice 5
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
convert7to5()
dice7() produced a roll of 7
dice7() produced a roll of 5
Your final returned roll is below:
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的更多相关文章
- Dice 5 ==> dice 7
https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...
- UVA 10759 Dice Throwing
题意为抛n个骰子凑成的点数和大于或等于x的概率,刚开始用暴力枚举,虽然AC了,但时间为2.227s,然后百度了下别人的做法,交了一遍,靠,0.000s,然后看了下思路,原来是dp,在暴力的基础上记忆化 ...
- poj 4014 Dice 贪心
//poj 4014 //sep9 #include <iostream> #include <algorithm> using namespace std; int n; s ...
- 图像分割必备知识点 | Dice损失 理论+代码
本文包含代码案例和讲解,建议收藏,也顺便点个赞吧.欢迎各路朋友爱好者加我的微信讨论问题:cyx645016617. 在很多关于医学图像分割的竞赛.论文和项目中,发现 Dice 系数(Dice coef ...
- 使用Dice loss实现清晰的边界检测
前言: 在深度学习和计算机视觉中,人们正在努力提取特征,为各种视觉任务输出有意义的表示.在一些任务中,我们只关注对象的几何形状,而不管颜色.纹理和照明等.这就是边界检测的作用所在. 关注公众号CV ...
- HDOJ 4652 Dice
期望DP +数学推导 Dice Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- 三种renderman规范引擎的dice对比
次表面做的有些烦躁,既然如此,索性先记一下前一阵比较的PIXIE.3delight.prman的dice方式. 研究过reyes的人都知道dice,简而言之,就是为了生成高质量高精度的图片(电影CG) ...
- LightOJ 1248 Dice (III) 概率
Description Given a dice with n sides, you have to find the expected number of times you have to thr ...
- hdu 4586 Play the Dice 概率推导题
A - Play the DiceTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...
随机推荐
- hadoop命令帮助
安装完hadoop后,在hadoop的bin目录下有一系列命令: container-executor hadoop hadoop.cmd hdfs hdfs.cmd mapred mapred.cm ...
- DataSanp App与Rest, WebBroker App的区别
DataSanp App与Rest, WebBroker App的区别 datasnap server :选择这一项,我们得到的将是一个独立EXE的三层服务器应用程序(TCP及HTTP两种模式) To ...
- Maven的几个常用plugin
出自:https://www.cnblogs.com/zhangxh20/p/6298062.html maven-compiler-plugin 编译Java源码,一般只需设置编译的jdk版本 &l ...
- as3 有趣现象 关于声明与变量
当使用了一个变量,并且前后期都没有在有效域内对此声明,不管有没有赋值,都会报错. 但先使用了一个变量,后期在有效域内对此声明,那么此变量不报错,但在声明之前没有赋值,那么赋值默认值:如果使用变量时,赋 ...
- struts2 模拟令牌机制防止表单重复提交
web.xml: <?xml version="1.0" encoding="UTF-8"?><web-app version="3 ...
- conductor Workflow Metrics
Server Metrics conductor使用spectator收集指标.https://github.com/Netflix/spectator 名称 目的 标签 workflow_serve ...
- Maven(一)——Maven入门
一.Maven的基本概念 Maven(翻译为"专家","内行")是跨平台的项目管理工具.主要服务于基于Java平台的项目构建,依赖管理和项目信息管理. 1.1. ...
- pip & Jinja2
[pip&Jinja2] 2.7.9 以及上版本的 python 安装程序会自动安装 pip. 升级:pip install --upgrade pip. 使用pip安装Jinja2:sudo ...
- JDA 8.0.0.0小版本升级
一.升级前关服务和进行备份 二.开始升级 三. 开以下四个服务 1237 四个服务开启后需重新执行SSIS中的startingFP(去掉backupdata 05 importFP) 当以下值为0,代 ...
- (动态规划)有 n 个学生站成一排,每个学生有一个能力值,从这 n 个学生中按照顺序选取kk 名学生,要求相邻两个学生的位置编号的差不超过 d,使得这 kk 个学生的能力值的乘积最大,返回最大的乘积
第2关:最强战队 挑战任务 绿盟和各大名企合作,举办编程能力大赛,需要选拔一支参赛队伍.队伍成员全部来自“绿盟杯”中表现优秀的同学,每个同学都根据在比赛中的表现被赋予了一个能力值.现在被召集的N个同学 ...