{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Card guessing game" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" }, "tags": [ "remove-cell" ] }, "source": [ "**CS1302 Introduction to Computer Programming**\n", "___" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## Rules of the game" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Consider a deck of 52 cards:\n", "
Obviously, you should not ask whether the card is precisely certain card, e.g., Is it Diamond Ace? Is it Diamond 2? ... Why not? The card may be one of the remaining $52-6=46$ possibilities you did not ask." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Hint 2: Think of each **Yes/No** question as splitting the set of possible cards into two smaller groups of possible cards corresponding to each possible answer **Yes/No**." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Hint 3: How many questions is required to split the set of 52 cards into groups of size $1$, i.e., with only one possible card?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Challenge the computer" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Play the role of the dealer and test if the program below can guess the card correctly after 6 questions." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-08-24T06:34:11.494355Z", "start_time": "2020-08-24T06:32:33.101128Z" }, "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "suitIdx = 0\n", "number = 0\n", "\n", "if \"y\" == input(\n", " \"Is the suite either heart or spade? (y/[n]) \").strip().lower():\n", " suitIdx += 2\n", "\n", "if \"y\" == input(\"Is the suite either club or spade? (y/[n]) \").strip().lower():\n", " suitIdx += 1\n", "\n", "if \"y\" == input(\n", " f\"Is the number {number+8} or above? (y/[n]) \").strip().lower():\n", " number += 8\n", "\n", "if \"y\" == input(\n", " f\"Is the number {number+4} or above? (y/[n]) \").strip().lower():\n", " number += 4\n", "\n", "if \"y\" == input(\n", " f\"Is the number {number+2} or above? (y/[n]) \").strip().lower():\n", " number += 2\n", "\n", "if \"y\" == input(\n", " f\"Is the number {number+1} or above? (y/[n]) \").strip().lower():\n", " number += 1\n", "\n", "print(f\"The card is {suits[suitIdx]} {number}\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "**Exercise** Does the above program always win? Explain your answer?" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "nbgrader": { "cell_type": "markdown", "checksum": "ed044740d01dabcbf82c56ce0a744078", "grade": true, "grade_id": "cell-d020c0eb31353627", "locked": false, "points": 1, "schema_version": 3, "solution": true, "task": false }, "slideshow": { "slide_type": "-" } }, "source": [ "YOUR ANSWER HERE" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Challenge your understanding" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "The following table gives the binary representions of unsigned decimal integers from 0 to 7." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "Binary | Decimal |
---|---|
000 | 0 |
001 | 1 |
010 | 2 |
011 | 3 |
100 | 4 |
101 | 5 |
110 | 6 |
111 | 7 |