\\\\$)...ced>(?a:[...\n",
"\n",
"FUNCTIONS\n",
" capwords(s, sep=None)\n",
" capwords(s [,sep]) -> string\n",
" \n",
" Split the argument into words using split, capitalize each\n",
" word using capitalize, and join the capitalized words using\n",
" join. If the optional second argument sep is absent or None,\n",
" runs of whitespace characters are replaced by a single space\n",
" and leading and trailing whitespace are removed, otherwise\n",
" sep is used to split and join the words.\n",
"\n",
"DATA\n",
" __all__ = ['ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'cap...\n",
" ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'\n",
" ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'\n",
" ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'\n",
" digits = '0123456789'\n",
" hexdigits = '0123456789abcdefABCDEF'\n",
" octdigits = '01234567'\n",
" printable = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTU...\n",
" punctuation = '!\"#$%&\\'()*+,-./:;<=>?@[\\\\]^_`{|}~'\n",
" whitespace = ' \\t\\n\\r\\x0b\\x0c'\n",
"\n",
"FILE\n",
" /Users/rbasnet/anaconda3/lib/python3.7/string.py\n",
"\n",
"\n"
]
}
],
"source": [
"# next solution using string library\n",
"# string library provides range of different types of characters as data\n",
"import string\n",
"help(string)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'!\"#$%&\\'()*+,-./:;<=>?@[\\\\]^_`{|}~'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# string library has data that can be useful, e.g.\n",
"string.punctuation"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'\"Well, I never did!\", said Alice.'"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ss"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Well I never did said Alice\n"
]
}
],
"source": [
"newStr = ''\n",
"for c in ss:\n",
" if c in string.ascii_lowercase:\n",
" newStr += c\n",
" elif c in string.ascii_uppercase:\n",
" newStr += c\n",
" elif c == ' ':\n",
" newStr += ' '\n",
" \n",
"print(newStr)"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [],
"source": [
"# write a function that removes all the punctations except for space\n",
"# returns new cleaned up string\n",
"def cleanUp(someStr):\n",
" newStr = ''\n",
" for c in someStr:\n",
" if c.islower():\n",
" newStr += c\n",
" elif c.isupper():\n",
" newStr += c\n",
" elif c.isspace():\n",
" newStr += c\n",
" return newStr"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Well I never did said Alice\n"
]
}
],
"source": [
"s = cleanUp(ss)\n",
"print(s)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 6.10 string formatting\n",
"- format method\n",
"- use {} as replacement field\n",
"- numbers in curly braces are optional; determine which argument gets substituted\n",
"- each of the replace fields can also contain a format specification\n",
" - < left alignment, > right, and ^ center, e.g. {1:<10}\n",
" - 10 is width\n",
" - type conversion such as f for float (.2f two decimal places), x for hex, etc."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"name = \"Arthur\"\n",
"age = 25"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"His name is Arthur!\n"
]
}
],
"source": [
"s1 = \"His name is {}!\".format(name)\n",
"print(s1)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"His name is Arthur!\n"
]
}
],
"source": [
"# newer syntax\n",
"print(f\"His name is {name}!\")"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"His name is Arthur and Arthur is 25 years old.\n"
]
}
],
"source": [
"# note age and name are provided in reverse order\n",
"print(\"His name is {1} and {1} is {0} years old.\".format(age, name))"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4 x 5.5 = 22.0 and 4 ^ 5.5 = 2048.00\n"
]
}
],
"source": [
"n1 = 4\n",
"n2 = 5.5\n",
"s3 = \"{0} x {1} = {2} and {0} ^ {1} = {3:.2f}\".format(n1, n2, n1*n2, n1**n2)\n",
"print(s3)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Pi to three decimal places is 3.142\n"
]
}
],
"source": [
"# formating decimal/float values to certian decimal points\n",
"print(\"Pi to three decimal places is {0:.3f}\".format(3.1415926))"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"123456789 123456789 123456789 123456789 123456789 123456789\n",
"|||Paris ||| Whitney ||| Hilton|||Born in 1981|||\n"
]
}
],
"source": [
"n1 = \"Paris\"\n",
"n2 = \"Whitney\"\n",
"n3 = \"Hilton\"\n",
"print(\"123456789 123456789 123456789 123456789 123456789 123456789\")\n",
"print(\"|||{0:<15}|||{1:^15}|||{2:>15}|||Born in {3}|||\"\n",
" .format(n1,n2,n3,1981))"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The decimal value 16 converts to hex value 0x10\n"
]
}
],
"source": [
"# formatting decimal int to hexadecimal number\n",
"print(\"The decimal value {0} converts to hex value 0x{0:x}\".format(16))"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The decimal value 8 converts to hex value 0b1000\n"
]
}
],
"source": [
"# formatting decimal int to binary number\n",
"print(\"The decimal value {0} converts to binary value 0b{0:b}\".format(8))"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The decimal value 8 converts to octal value 0o10\n"
]
}
],
"source": [
"# formatting decimal int to octal number\n",
"print(\"The decimal value {0} converts to octal value 0o{0:o}\".format(8))"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Dear Paris Hilton.\n",
" Paris, I have an interesting money-making proposition for you!\n",
" If you deposit $10 million into my bank account, I can\n",
" double your money ...\n",
"\n",
"\n",
"Dear Bill Jeff.\n",
" Bill, I have an interesting money-making proposition for you!\n",
" If you deposit $10 million into my bank account, I can\n",
" double your money ...\n",
"\n"
]
}
],
"source": [
"letter = \"\"\"\n",
"Dear {0} {2}.\n",
" {0}, I have an interesting money-making proposition for you!\n",
" If you deposit $10 million into my bank account, I can\n",
" double your money ...\n",
"\"\"\"\n",
"print(letter.format(\"Paris\", \"Whitney\", \"Hilton\"))\n",
"print(letter.format(\"Bill\", \"Warren\", \"Jeff\"))"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" i i**2 i**3 i**5 i**10 i**20\n",
" 1 1 1 1 1 1\n",
" 2 4 8 32 1024 1048576\n",
" 3 9 27 243 59049 3486784401\n",
" 4 16 64 1024 1048576 1099511627776\n",
" 5 25 125 3125 9765625 95367431640625\n",
" 6 36 216 7776 60466176 3656158440062976\n",
" 7 49 343 16807 282475249 79792266297612001\n",
" 8 64 512 32768 1073741824 1152921504606846976\n",
" 9 81 729 59049 3486784401 12157665459056928801\n",
" 10 100 1000 100000 10000000000 100000000000000000000\n"
]
}
],
"source": [
"layout = \"{0:>4}{1:>6}{2:>6}{3:>8}{4:>13}{5:>24}\"\n",
"\n",
"print(layout.format(\"i\", \"i**2\", \"i**3\", \"i**5\", \"i**10\", \"i**20\"))\n",
"for i in range(1, 11):\n",
" print(layout.format(i, i**2, i**3, i**5, i**10, i**20))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 6.11 Exercises\n",
"\n",
"1. print a neat looking multiplication table like this:\n",
"\n",
" 1 2 3 4 5 6 7 8 9 10 11 12\n",
" :--------------------------------------------------\n",
" 1: 1 2 3 4 5 6 7 8 9 10 11 12\n",
" 2: 2 4 6 8 10 12 14 16 18 20 22 24\n",
" 3: 3 6 9 12 15 18 21 24 27 30 33 36\n",
" 4: 4 8 12 16 20 24 28 32 36 40 44 48\n",
" 5: 5 10 15 20 25 30 35 40 45 50 55 60\n",
" 6: 6 12 18 24 30 36 42 48 54 60 66 72\n",
" 7: 7 14 21 28 35 42 49 56 63 70 77 84\n",
" 8: 8 16 24 32 40 48 56 64 72 80 88 96\n",
" 9: 9 18 27 36 45 54 63 72 81 90 99 108\n",
"10: 10 20 30 40 50 60 70 80 90 100 110 120\n",
"11: 11 22 33 44 55 66 77 88 99 110 121 132\n",
"12: 12 24 36 48 60 72 84 96 108 120 132 144\n",
"
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. Write a program that determines whether a given string is palindrome. Palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run or race car."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2.1 Convert Exercise 2 into a function and write at least two test cases."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3. Write a program that calculates number of trials required to guess a 3 digit pass code 777 (starting from 000, 001, 002, 003, 004, 005..., 010, etc.) using some brute force technique."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3.1. Convert Exercise 3 into a function and write at least 3 test cases."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4. Write a function that calculates the [run-length encoding](https://en.wikipedia.org/wiki/Run-length_encoding) of a given string. Run-length is a lossless data compression in which runs of data are stored as a single data value and count, rather than the original run. Assume that the data contains alphabets (upper and lowercase) only and are case insisitive.\n",
"E.g.: \n",
" - aaaabbc -> 4a2b1c\n",
" - Abcd -> 1a1b1c1d"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"# 4 solution\n",
"# Algorithm:\n",
"# for each character:\n",
"# if the current character is same as the previous one\n",
"# increment count\n",
"# else \n",
"# print the count and the previous character\n",
"# reset count and previous character\n",
"# \n",
"\n",
"def run_length_encoding(text):\n",
" # check for corner case\n",
" if not text: # if text is empty!\n",
" return ''\n",
" \n",
" encoding = ''\n",
" # FIXME: implement the algorithm\n",
" \n",
" \n",
" return encoding\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"ename": "AssertionError",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# unit testing for run_length_encoding\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0mrun_length_encoding\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m''\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m''\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0mrun_length_encoding\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'aaaabbc'\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'4a2b1c'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0mrun_length_encoding\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'abcd'\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'1a2b3c4d'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0mrun_length_encoding\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'zzaazyyyYY'\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'2z2a1z5y'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAssertionError\u001b[0m: "
]
}
],
"source": [
"# unit testing for run_length_encoding\n",
"assert run_length_encoding('') == ''\n",
"assert run_length_encoding('aaaabbc') == '4a2b1c'\n",
"assert run_length_encoding('abcd') == '1a2b3c4d'\n",
"assert run_length_encoding('zzaazyyyYY') == '2z2a1z5y'\n",
"# FIXME: Write few more test cases; what corner cases can you think of \n",
"# that would break run_length_encoding function?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"5. Write a function that decodes the given run-length encoded compressed data, i.e. decompresses the compressed data to the original string.\n",
"e.g. \n",
" - '' -> ''\n",
" - '1a2b3c' -> 'abbccc'\n",
" - '10a' -> 'aaaaaaaaaa'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Kattis problems\n",
"1. Avion - https://open.kattis.com/problems/avion\n",
"- Apaxiaans - https://open.kattis.com/problems/apaxiaaans\n",
"- Hissing Microphone - https://open.kattis.com/problems/hissingmicrophone\n",
"- Reversed Binary Numbers - https://open.kattis.com/problems/reversebinary\n",
"- Kemija - https://open.kattis.com/problems/kemija08\n",
"- Simon Says - https://open.kattis.com/problems/simonsays\n",
"- Simon Says - https://open.kattis.com/problems/simon\n",
"- Quite a Problem - https://open.kattis.com/problems/quiteaproblem\n",
"- Eligibility - https://open.kattis.com/problems/eligibility\n",
"- Charting Progress - https://open.kattis.com/problems/chartingprogress\n",
"- Pig Latin - https://open.kattis.com/problems/piglatin\n",
"- Battle Simulation - https://open.kattis.com/problems/battlesimulation\n",
"- Palindromic Password - https://open.kattis.com/problems/palindromicpassword\n",
"- Image Decoding - https://open.kattis.com/problems/imagedecoding"
]
},
{
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}