diff --git a/ipynb/Cheryl.ipynb b/ipynb/Cheryl.ipynb
index dfd77d1..a0635fc 100644
--- a/ipynb/Cheryl.ipynb
+++ b/ipynb/Cheryl.ipynb
@@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "
Peter Norvig
April 2015
Python 3: Feb 2019
\n",
+ "Peter Norvig
April 2015
Python 3: Feb 2019
Steve's bus: Apr 2020
\n",
"\n",
"# When is Cheryl's Birthday?\n",
"\n",
@@ -55,7 +55,6 @@
"outputs": [],
"source": [
"def Month(date): return date.split()[0]\n",
- "\n",
"def Day(date): return date.split()[1]"
]
},
@@ -119,7 +118,7 @@
"BeliefSet = set\n",
"\n",
"def tell(part, dates=dates) -> BeliefSet:\n",
- " \"Cheryl tells a part of her birthdate to someone; return a set of possible dates.\"\n",
+ " \"Cheryl tells a part of her birthdate to someone; return a belief set of possible dates.\"\n",
" return {date for date in dates if part in date}\n",
"\n",
"def know(beliefs) -> bool:\n",
@@ -231,7 +230,7 @@
" \"Return a subset of the dates for which all three statements are true.\"\n",
" return satisfy(dates, statement3, statement4, statement5)\n",
"\n",
- "def satisfy(items, *predicates):\n",
+ "def satisfy(items, *predicates) -> BeliefSet:\n",
" \"Return the subset of items that satisfy all the predicates.\"\n",
" return {item for item in items\n",
" if all(pred(item) for pred in predicates)}\n",
@@ -314,7 +313,7 @@
"metadata": {},
"outputs": [],
"source": [
- "def statement4(date):\n",
+ "def statement4(date) -> bool:\n",
" \"Bernard: At first I don't know when Cheryl's birthday is, but I know now.\"\n",
" dates = tell(Day(date))\n",
" return (not know(dates) and know(satisfy(dates, statement3)))"
@@ -364,7 +363,7 @@
"metadata": {},
"outputs": [],
"source": [
- "def statement5(date):\n",
+ "def statement5(date) -> bool:\n",
" \"Albert: Then I also know when Cheryl's birthday is.\"\n",
" return know(satisfy(tell(Month(date)), statement4))"
]
@@ -423,6 +422,97 @@
"source": [
"know(cheryls_birthday())"
]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "___\n",
+ "\n",
+ "# New Puzzle: Steve's Bus\n",
+ "\n",
+ "Here's [another puzzle](https://www.reddit.com/r/riddles/comments/fw7h42/a_riddle_i_couldnt_solve/) with a very similar format:\n",
+ "\n",
+ "> 1. Steve tells Alice the hour of his bus departure and he tells Annie at which minute it leaves. He also tells them both that the bus leaves between 0600 and 1000.\n",
+ "> 2. Alice and Annie consult the timetable and find the following services between those two time: 06:32 06:43 06:50 07:17 07:46 08:19 08:32 09:17 09:19 09:50.\n",
+ "> 3. Alice then says “I don’t know when Steve’s bus leaves but I am sure that neither does Annie”\n",
+ "> 4. Annie Replies “I didn’t know his bus, but now i do”\n",
+ "> 5. Alice responds “Now I do as well!”\n",
+ "> 6. When is Steve’s bus?\n",
+ "\n",
+ "Everything works pretty much the same as with Cheryl's birthday, except we're dealing with hours and minutes of times, not days and months of dates. So it is a quick copy and edit. Note that I need to explicitly pass the second argument to `tell`, because it relied on having the default being the birthday dates, not the bus times."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 17,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "bus_times = '06:32 06:43 06:50 07:17 07:46 08:19 08:32 09:17 09:19 09:50'.split()\n",
+ "\n",
+ "def hour(time): return time[:3] # First three characters\n",
+ "def minutes(time): return time[-3:] # Last three characters\n",
+ "\n",
+ "def steves_bus(times) -> BeliefSet:\n",
+ " \"Return a subset of the times for which all three statements are true.\"\n",
+ " return satisfy(times, statement3b, statement4b, statement5b)\n",
+ "\n",
+ "def statement3b(time) -> bool:\n",
+ " \"Alice then says: I don’t know when Steve’s bus leaves but I am sure that neither does Annie\"\n",
+ " times = tell(hour(time), bus_times)\n",
+ " return (not know(times) \n",
+ " and all(not know(tell(minutes(d), bus_times)) for d in times))\n",
+ "\n",
+ "def statement4b(time) -> bool:\n",
+ " \"Annie Replies: I didn’t know his bus, but now I do\"\n",
+ " times = tell(minutes(time), bus_times)\n",
+ " return (not know(times) and know(satisfy(times, statement3b)))\n",
+ "\n",
+ "def statement5b(time) -> bool:\n",
+ " \"Alice responds: Now I do as well!\"\n",
+ " return know(satisfy(tell(hour(time), bus_times), statement3b, statement4b))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 18,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "{'08:32'}"
+ ]
+ },
+ "execution_count": 18,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "steves_bus(bus_times)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 19,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 19,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "know(steves_bus(bus_times))"
+ ]
}
],
"metadata": {
@@ -441,7 +531,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.7.0"
+ "version": "3.7.7"
}
},
"nbformat": 4,