{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 1" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello World'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"\\x48\\x65\\x6c\\x6c\\x6f\\x20\\x57\\x6f\\x72\\x6c\\x64\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 2" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"She wrote a long letter but didn't read it\"" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"she wROTe a LoNG lETter bUt diDn'T ReAD it\".capitalize()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 3" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "def wrap_with_html_tag(inp, tag):\n", " print(\"<{1}>{0}{1}>\".format(inp, tag))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "
some text
\n" ] } ], "source": [ "wrap_with_html_tag(\"some text\", \"p\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 4" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "def inject_string(inp, shim_text):\n", " print(shim_text.join(inp.split(\" \")))" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "She-wrote-a-long-letter-but-didn't-read-it\n" ] } ], "source": [ "inject_string(\"She wrote a long letter but didn't read it\", \"-\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 5" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [], "source": [ "def strip_new_lines(inp):\n", " print(inp.replace(\"\\n\", \" \"))" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "There once was a man named John. One day, he went to the circus.\n" ] } ], "source": [ "strip_new_lines(\"There once was a man named John.\\n\"\n", " \"One day, he went to the circus.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 8" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [], "source": [ "def add_prefix(inp, prefix):\n", " print(prefix + inp.replace(\"\\n\", \"\\n\"+prefix))" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "INFO:There once was a man named John.\n", "INFO:One day, he went to the circus.\n" ] } ], "source": [ "add_prefix(\"There once was a man named John.\\n\"\n", " \"One day, he went to the circus.\", \"INFO:\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 7" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [], "source": [ "def draw_box(width, height):\n", " \n", " # Draw the top border\n", " print(\"-\" * width)\n", " # Draw interior rows\n", " for idx in range(0, height-2): \n", " print(\"|{}|\".format(\" \" * (width-2)))\n", " # Draw the bottom border\n", " print(\"-\" * width)" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "----------\n", "| |\n", "| |\n", "| |\n", "| |\n", "| |\n", "----------\n" ] } ], "source": [ "draw_box(10,7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 8" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [], "source": [ "def calc_vol(width, height, length):\n", " vol = float(width * height * length)\n", " print(\"Volume: {0:.3f} {1}\\u00B3\".format(vol, units))" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Volume: 124.928 m³\n" ] } ], "source": [ "calc_vol(4, 5.12, 6.1, \"m\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 9" ] }, { "cell_type": "code", "execution_count": 185, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------\n", "| Mode | Alpha | Beta |\n", "------------------------------------\n", "| 0X0000 | -5,546.55| 2.56e+04 |\n", "------------------------------------\n", "| 0X0001 | 22,081.72| 1.05e+04 |\n", "------------------------------------\n", "| 0X0002 | -5,965.93| -8.94e+03 |\n", "------------------------------------\n", "| 0X0003 | -2,450.81| -7.14e+03 |\n", "------------------------------------\n", "| 0X0004 | 28,925.01| 2.89e+04 |\n", "------------------------------------\n", "| 0X0005 | 18,628.49| 4.87e+03 |\n", "------------------------------------\n", "| 0X0006 | 23,188.22| 2.99e+04 |\n", "------------------------------------\n", "| 0X0007 | 5,054.67| 1.16e+03 |\n", "------------------------------------\n", "| 0X0008 | 6,091.46| 2.05e+04 |\n", "------------------------------------\n", "| 0X0009 | -6,351.53| -1.27e+04 |\n", "------------------------------------\n", "| 0X000A | -2,146.08| 2.61e+03 |\n", "------------------------------------\n", "| 0X000B | -971.36| 1.24e+04 |\n", "------------------------------------\n", "| 0X000C | 5,866.07| 2.43e+04 |\n", "------------------------------------\n", "| 0X000D | 30,746.76| 1.55e+04 |\n", "------------------------------------\n", "| 0X000E | -7,623.54| 8.22e+03 |\n", "------------------------------------\n", "| 0X000F | -10,874.77| 1.72e+04 |\n", "------------------------------------\n" ] } ], "source": [ "NUM_COLS = 3\n", "COL0_WIDTH = 8\n", "COL1_WIDTH = 12\n", "COL2_WIDTH = 12\n", "\n", "# Create helper functions so that we don't repeat these line of code\n", "def print_separator():\n", " print(\"-\" * (COL0_WIDTH + COL1_WIDTH + COL2_WIDTH + NUM_COLS + 1)) \n", " \n", "def print_data_row(val0, val1, val2):\n", " print(\"|{0:^8}|{1:>12,.2f}|{2:^12,.2e}|\".format(\"{0:#06X}\".format(val0), val1, val2))\n", "\n", "\n", "# Print the table\n", "# NOTE: Using loops and tuples stored in a dictionary, this would be simpler.\n", "# But for this exercise, since these haven't been covered yet,\n", "# 'brute force' will have to suffice.\n", "print_separator()\n", "print(\"|{0:^8}|{1:^12}|{2:^12}|\".format(\"Mode\", \"Alpha\", \"Beta\"))\n", "print_separator()\n", "print_data_row(0, -5546.54725, 25586.73057)\n", "print_separator()\n", "print_data_row(1, 22081.71617, 10538.63947)\n", "print_separator()\n", "print_data_row(2, -5965.92742, -8944.90781)\n", "print_separator()\n", "print_data_row(3, -2450.81368, -7135.44111)\n", "print_separator()\n", "print_data_row(4, 28925.01114, 28879.88609)\n", "print_separator()\n", "print_data_row(5, 18628.48674, 4874.96768)\n", "print_separator()\n", "print_data_row(6, 23188.21829, 29880.82047)\n", "print_separator()\n", "print_data_row(7, 5054.67254, 1163.15909)\n", "print_separator()\n", "print_data_row(8, 6091.46452, 20483.97234)\n", "print_separator()\n", "print_data_row(9, -6351.53182, -12741.09698)\n", "print_separator()\n", "print_data_row(10, -2146.08381, 2610.09901)\n", "print_separator()\n", "print_data_row(11, -971.36459, 12375.65034)\n", "print_separator()\n", "print_data_row(12, 5866.07492, 24253.62213)\n", "print_separator()\n", "print_data_row(13, 30746.75865, 15498.02591)\n", "print_separator()\n", "print_data_row(14, -7623.53765, 8220.57827)\n", "print_separator()\n", "print_data_row(15, -10874.77380, 17235.47176)\n", "print_separator()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }