{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Denison CS181/DA210 Homework\n", "\n", "Before you turn this problem in, make sure everything runs as expected. This is a combination of **restarting the kernel** and then **running all cells** (in the menubar, select Kernel$\\rightarrow$Restart And Run All).\n", "\n", "Make sure you fill in any place that says `YOUR CODE HERE` or \"YOUR ANSWER HERE\"." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Encoding Homework Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q1** One of the reasons for the existence of *Unicode* is its ability to use strings that go beyond the limitations of the keyboard. Relative to the discussion in the chapter, *Unicode* is about the *strings* we can use in our programs, and the issue of how they translate/map to a sequence of *bytes* (i.e. their *encoding*) is a separate concept.\n", "\n", "When we have the *code-point* (generally a hex digit sequence identifying an index into the set of characters) for a Unicode character that is beyond our normal keyboard characters, we can include them in our strings by using the `\\u` escape prefix followed by the hex digits for the code-point. Consider the Python string s:\n", "\n", " s = \"Unicode examples: \\u2B2C and \\u266A and \\u1F60 and \" \\\n", " \"\\u265E and \\u0394 and \\u0402\"\n", " \n", "Write code to print `s`, then assign to `b8` the UTF-8 encoding of `s`, and `b16` the UTF-16BE encoding of `s`. For each, use the `hex()` method of the `bytes` data type to see a hex version of the encoded values. Use the following code cell for your Python sequence. Then, in the subsequent Markdown cell, answer the following questions: \n", "\n", "- which of the hex representations is longer?\n", "- give explicit lengths for `b8`, `b16`, and for the two `hex()` transformations.\n", "- how does this compare to the length of `s`?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "193e8b2d0fc60555acb596cbf67a56ce", "grade": true, "grade_id": "cell-e2e0441cf1a37b54", "locked": false, "points": 2, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "nbgrader": { "cell_type": "markdown", "checksum": "f5ee94d56f170f314fc406d9adcbbf37", "grade": true, "grade_id": "cell-607a3db88789bed0", "locked": false, "points": 1, "schema_version": 3, "solution": true, "task": false } }, "source": [ "YOUR ANSWER HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q2** Write a function\n", "\n", " shiftLetter(letter, n)\n", "\n", "whose parameter, `letter` should be a single character. If the character is between `\"A\"` and `\"Z\"`, the function returns an upper case character $n$ positions further along, and \"wrapping\" if the + $n$ mapping goes past `\"Z\"`. Likewise, it should map the lower case characters between `\"a\"` and `\"z\"`. If the parameter `letter` is anything else, or not of length 1, the function should return `letter`.\n", "\n", "Hint: review functions `ord()` and `chr()` from the section, as well as the modulus operator `%`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "daf9985ae05f0badf024ae7a36a63a11", "grade": false, "grade_id": "cell-fb6f443b7ea95ec8", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "e3ba619249352e935e81ea5c9ffe84d6", "grade": true, "grade_id": "cell-8d8fb21572039cbd", "locked": true, "points": 3, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "assert True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q3** Building on the previous exercise, write a function\n", "\n", " encrypt(plaintext, n)\n", " \n", "that performs a `shiftLetter` for each of the letters in `plaintext` and accumulates and returns the resultant string." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "fe314ed29f5c7d84324dec46d1662205", "grade": false, "grade_id": "cell-32d65f646b2da222", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "d376e44fdcd84070890bfeba343d2825", "grade": true, "grade_id": "cell-fef1d4cb2dc5b399", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "assert True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q4** Write a function \n", "\n", " singleByteChars(s)\n", " \n", "that takes its argument, `s`, and determines whether or not all the characters in `s` can be encoded by a single byte. The function. should return the Boolean `True` if so, and `False` otherwise." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "e8e199d3f16e2769e7033e1051ebb78b", "grade": false, "grade_id": "cell-938f90290ee40c69", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()\n", "print(singleByteChars(\"Hello \\u2B2C\"))\n", "singleByteChars(\"Hello\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "f0e876babe175258089c87a6649f8952", "grade": true, "grade_id": "cell-a02544e577b74f0a", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "assert True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q5** Suppose you have, in your Python program, a variable that refers to a `bytes` data type, like `mystery` refers to the `bytes` constant literal as given here:\n", "\n", " mystery = b'\\xc9\\xa2\\x95}\\xa3@\\x89\\xa3@\\x87\\x99\\x85'\\\n", " b'\\x81\\xa3@\\xa3\\x96@\\x82\\x85@\\xa2\\x96\\x93'\\\n", " b'\\xa5\\x89\\x95\\x87@\\x97\\x99\\x96\\x82\\x93\\x85'\\\n", " b'\\x94\\xa2o@@\\xe8\\x96\\xa4@\\x82\\x85\\xa3Z'\n", " \n", "Perhaps this value came from a network message, or from a file. But you suspect that it, in fact, holds the bytes for a character string, and you need to figure out how it is encoding. Assume that you have narrowed the encodings down to one of the following:\n", "\n", "- 'UTF-8'\n", "- 'UTF-16BE'\n", "- 'cp037'\n", "- 'latin_1'\n", "\n", "Write code to convert the byte sequence to a character string, and determine the correct encoding. By the end of your code, assign to `s` the \"correct\" decoding translation." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "09164073244c9af85c30f001b541724f", "grade": false, "grade_id": "cell-6ce4980e23e78176", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "e0a1488490c71df394a0d2858b181327", "grade": true, "grade_id": "cell-8600351c695570fd", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "assert True" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }