大早上起来逛微博,看见@西瓜大丸子汤Po的一个逻辑题,遂点开看之...

原文链接:http://nbviewer.ipython.org/url/norvig.com/ipython/Cheryl.ipynb

然后发现这不是TM的猜卡牌的那个题么,某次面试的时候还跪了来着。。接着就魔性大发,试着用JAVA来学习一下

 package cn.edu.bipt.hcol;

 import java.util.ArrayList;
import java.util.List; /**
* This logic puzzle has been making the rounds:
*
* Albert and Bernard just became friends with Cheryl, and they want to know
* when her birtxhday is. Cheryl gave them a list of 10 possible dates:
*
* May 15 May 16 May 19 June 17 June 18 July 14 July 16 August 14 August 15
* August 17 Cheryl then tells Albert and Bernard separately the month and the
* day of the birthday respectively.
*
* Albert: I don't know when Cheryl's birthday is, but I know that Bernard does
* not know too.
*
* Bernard: At first I don't know when Cheryl's birthday is, but I know now.
*
* Albert: Then I also know when Cheryl's birthday is.
*
* So when is Cheryl's birthday?
*
* @author Colin
*
*/
public class WhenIsCherylsBirthday { public static final String DATES[] = { "May 15", "May 16", "May 19",
"June 17", "June 18", "July 14", "July 16", "August 14",
"August 15", "August 17" }; /**
* Albert : I don't know when Cheryl's birthday is, but I know that Bernard
* does not know too.
*
* That means: Albert : After Cheryl told me the month of her birthdate, I
* didn't know her birthday, I don't know which day Cheryl told Bernard, but
* I know that for all of the possible dates, if Bernard is told that day ,
* he wouldn't know the birthdate.
*
*/
private boolean statement3(String date) {
// When Cheryl told the month to Albert
// Albert : I know if Bernard doesn't knows Cheryl birthday when Cheryl
// told him the day
// that means the day is not unique in DATES
// that means I know the month and I know the days but Bernard doesn't
// konws
boolean temper = true; List<String> possible_dates = tell(getMonth(date)); for (int i = 0; i < possible_dates.size(); i++) {
if (know(tell(getDay(possible_dates.get(i))))) {
temper = false;
break;
}
} return !know(possible_dates) && temper;
} /**
* Bernard: At first I don't know when Cheryl's birthday is, but I know now.
*
* Bernard: At first time Cheryl told me the day,and i didn't know. Then I
* considered just the dates for which Albert's statement3 is true,and now I
* know.
*
* @param date
* @return
*/
private boolean statement4(String date) {
List<String> atFirst = tell(getDay(date)); List<String> filterItem = new ArrayList<String>();
for (String item : atFirst) {
if (statement3(item) == true)
filterItem.add(item);
} return !know(atFirst) && know(filterItem);
} /**
* Albert: Then I also know when Cheryl's birthday is.
*
* @param date
* @return
*/
private boolean statement5(String date) {
List<String> mList = tell(getMonth(date)); List<String> filterItem = new ArrayList<String>();
for (String item : mList) {
if (true == statement4(item))
filterItem.add(item);
} return know(filterItem);
} /**
* filter(statement4 , filter(statement3 , DATES)) filter(statement4 ,
* tell(getMonth(date)))
*
* @param date
* @return
*/
private boolean statement3to5(String date) {
return statement3(date) && statement4(date) && statement5(date);
} private String getCherylsBirthday(String dates[]) {
String birthday = "";
for (String date : dates) {
if (true == statement3to5(date))
birthday = date;
}
return birthday;
} /**
* Cheryl tells a part of birthday to someone; Return a new list of possible
* dates that match the part.
*
* @param part
* @param return
*/
private List<String> tell(String part) {
List<String> mList = new ArrayList<String>();
char c = part.charAt(0); // 匹配“月”
if (c >= 'A' && c <= 'Z') {
for (String date : DATES) {
if (part.equals(getMonth(date))) {
mList.add(date);
}
}
} else {// 匹配“日”
for (String date : DATES) {
if (part.equals(getDay(date))) {
mList.add(date);
}
}
}
return mList;
} /**
* A person knows the birthday if they have exactly one possible date
*
* @return
*/
private boolean know(List<String> mList) {
return mList.size() == 1;
} /**
* 获取data中的“月”的数据
*
* @param data
* getMonth("May 15")
* @return May
*/
private String getMonth(String date) {
return date.split(" ")[0];
} /**
* 获取data中的“日”的数据
*
* @param data
* getMonth("May 15")
* @return 15
*/
private String getDay(String date) {
return date.split(" ")[1];
} public static void main(String[] args) {
WhenIsCherylsBirthday whenIsCherylsBirthday = new WhenIsCherylsBirthday(); System.out.println(whenIsCherylsBirthday.getCherylsBirthday(DATES));
}
}

顺着原文中的思路和Python代码修改的...话说Python还真是叼炸天啊...all()和filter()这俩函数简直叼...膜拜之...

疑惑:还是不太明白statement3的用处。。

When Is Cheryl's Birthday的更多相关文章

  1. ASP.NET Core 中文文档 第四章 MVC(4.2)控制器操作的路由

    原文:Routing to Controller Actions 作者:Ryan Nowak.Rick Anderson 翻译:娄宇(Lyrics) 校对:何镇汐.姚阿勇(Dr.Yao) ASP.NE ...

  2. DevExpress ComboBoxEdit 添加值

    今天在使用ComboBoxEdit 这个控件的时候,不知道怎么添加值. 在官网上找到代码.在这里做个记录 ComboBoxEdit combo = new ComboBoxEdit(); ComboB ...

  3. sql server中常用方法函数

    SQL SERVER常用函数 1.DATEADD在向指定日期加上一段时间的基础上,返回新的 datetime 值. (1)语法: DATEADD ( datepart , number, date ) ...

  4. SQL like 模糊查询

    SQL 模糊查询 在进行数据库查询时,有完整查询和模糊查询之分. 一般模糊查询语句如下: SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式: ...

  5. ACRush 楼天成回忆录

    楼教主回忆录: 利用假期空闲之时,将这几年 GCJ , ACM , TopCoder 参加的一些重要比赛作个回顾.首先是 GCJ2006 的回忆. Google Code Jam 2006 一波三折: ...

  6. hibernate的like用法(用占位符解决)

    原本我的写法:Query repeatClientQuery=querysession.createQuery("from ClientInfo as a " +"whe ...

  7. R提高篇(四): 数据管理二

    目录: 数学函数 统计函数 应用示例 控制流 数学函数 ceiling(x):  大于等于 x  的最小整数, 如:  ceiling(3.213)  --> 4 floor(x):     小 ...

  8. [SQL]SQL语言入门级教材_SQL数据操作基础(二)

    SQL数据操作基础(初级) netnova 于 -- :: 加贴在 数据库探讨: 为了建立交互站点,你需要使用数据库来存储来自访问者的信息.例如,你要建立一个职业介绍服务的站点,你就需要存储诸如个人简 ...

  9. sql模糊查询

    SQL 模糊查询 在进行数据库查询时,有完整查询和模糊查询之分. 一般模糊查询语句如下: SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式: ...

随机推荐

  1. JavaWeb核心编程之(三.1)ServletHello

    Servlet简介Java Servlet是和平台无关的服务器端组件,它运行在Servlet容器中. Servlet容器负责Servlet和客户的通信以及调用Servlet方法, 在这里Servlet ...

  2. JQuery使用on绑定动态生成元素时碰到的问题

    今天在写界面时,希望对一些由JS代码动态添加的html标签进行事件绑定,使用了jquery 1.6+才提供的on函数.我的JQ版本是1.9.2. 以下这段代码是动态生成的. <div class ...

  3. Android minHeight/Width,maxHeight/Width

    在layout文件中,设置IamgeView的最大(最小)高度(宽度)时,需要同时设置android:adjustViewBounds="true",这样设置才会生效.在代码中设置 ...

  4. Silverlight代码编写对控件的PlaneProjection.RotationY属性控制动画

    Canvas c; void btnDraw_Click(object sender, RoutedEventArgs e) { Storyboard story = new Storyboard() ...

  5. OpenCV学习 7:图像形态学:腐蚀、膨胀

    原创文章,欢迎转载,转载请注明出处 首先什么是图像形态学?额,这个抄下百度到的答案.基本思想:    用具有一定形态的结构元素去度量和提取图像中的对应形状已达到对图像分析和识别的目的,形态学图像处理表 ...

  6. PostCss 从0开始

    PostCss 摘自 http://ju.outofmemory.cn/entry/215105 http://www.w3cplus.com/PostCSS/postcss-deep-dive-pr ...

  7. Orchard 源码探索(Log)

    简单工厂模式.抽象工厂模式和适配器模式 依赖倒置原则也叫依赖倒转原则,Dependence Inversion Principle,对抽象进行编程,不要对实现进行编程. A.高层次的模块不应该依赖于低 ...

  8. ORACLE视图添加备注

    ORACLE视图添加备注 版权声明:本文为博主原创文章,未经博主允许不得转载. create or replace view oes_material_series_ref as select t.p ...

  9. android ViewHolder 使用

    android中使用ListView   ExpandableListView  数据适配器adapter很多都是自己定义,自己定义数据适配器时,要重写getView.重写getView为了不让每次调 ...

  10. J2SE知识点摘记(八)

    1.        多线程指的是在单个进程中可以同时运行多个不同的线程,执行不用的任务.多线程意味着一个程序的多行语句可以看上去几乎同时进行. 同样作为基本的执行单元,线程是划分得比进程更小的执行单位 ...