Skip to content

Commit

Permalink
fixing up for release
Browse files Browse the repository at this point in the history
  • Loading branch information
MLDERES committed Jul 8, 2020
1 parent 76c7dea commit 46f0fc4
Showing 1 changed file with 280 additions and 0 deletions.
280 changes: 280 additions & 0 deletions cc-example.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%run ./src/challenge_tests.py"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Coding Challenges\n",
"Now it's time to put all you have learned together to try some unique coding puzzles. The coding challenges presented here range from very easy to challenging. For each one there are few hints that you can use if you get stuck. Each challenge has a space for you to write the answer and a cell following which can be executed to check your answer against a few test cases."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Challenge 1 - What day of the year is it?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def day_of_year(day, month, year):\n",
" ''' A function to determine what day of the year it is\n",
" Parameters\n",
" ----------\n",
" day : int\n",
" The day of the month\n",
" month : int\n",
" The month of the year\n",
" year : int\n",
" The year which the day is being calculated\n",
" '''\n",
" days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
" total_days = day\n",
" for m in range(month -1):\n",
" total_days = total_days+days_in_month[m]\n",
" if (month > 2) and is_leap_year(year):\n",
" total_days = total_days + 1\n",
" \n",
" return total_days\n",
"\n",
"def is_leap_year(y):\n",
" return (y % 4 == 0) and ((y % 100 != 0) or (y % 400 == 0))\n",
"\n",
"assert_equal(day_of_year(1,1,2000),1,'Jan 1, 2000')\n",
"assert_equal(day_of_year(15,2,2015),46,'Feb 2, 2015')\n",
"assert_equal(day_of_year(30,6,2020),182,'June 30, 2020', 'Did you check for leap year?')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Challenge 2 - Create me a monogram"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def monogram(full_name):\n",
" '''\n",
" Creates a traditional monogram from a supplied full name\n",
" '''\n",
" first, middle, last = full_name.split(' ',2)\n",
" return f'{first[0].lower()}.{last[0].upper()}.{middle[0].lower()}'\n",
"\n",
"# Basic tests\n",
"assert_equal(monogram('Dwight Kevin Shrute'),'d.S.k')\n",
"assert_equal(monogram('Eye see deadpeople'),'e.D.s', hint='Did you check the case?')\n",
"assert_equal(monogram('mers sadees benz II'),'m.B.s',hint='Did the extra suffix throw you off?')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Challenge 3 - Are you my mother?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def relations(family_tree, relationship):\n",
" ''' Determine the relationship between two people in a given family\n",
" \n",
" Parameters\n",
" ----------\n",
" family_tree : list of tuple of str\n",
" The family tree is defined by tuples of mother/daughter pairs \n",
" where the first item in the tuple is the mother of the second name in the tuple.\n",
" relationship: tuple of str\n",
" The relationship to be determined of the second person in the tuple to the first person in the tuple\n",
" \n",
" Returns\n",
" -------\n",
" str : {'Grandmother','Granddaughter','Mother','Daughter','Sister','Cousin','Aunt','Niece'}\n",
" The relationship of the second person in the `relationship` tuple to the first person in the tuple\n",
" \n",
" '''\n",
" # Replace pass with your code\n",
" parents = {}\n",
" for parent, child in family_tree:\n",
" # Build a list of children by specifying the parent\n",
" parents[child] = parent\n",
" \n",
" # Now get the targets\n",
" gen_1 = relationship[0]\n",
" gen_2 = relationship[1]\n",
" \n",
" gen_1_parent = parents.get(gen_1)\n",
" gen_1_parent_parent = parents.get(gen_1_parent)\n",
" gen_2_parent = parents.get(gen_2)\n",
" gen_2_parent_parent = parents.get(gen_2_parent)\n",
" \n",
" if gen_2 == gen_1_parent : return 'Mother'\n",
" if gen_2 == gen_1_parent_parent : return 'Grandmother'\n",
" if gen_1 == gen_2_parent : return 'Daughter'\n",
" if gen_1 == gen_2_parent_parent : return 'Granddaughter'\n",
" if gen_1_parent == gen_2_parent : return 'Sister'\n",
" if gen_1_parent_parent == gen_2_parent_parent : return 'Cousin'\n",
" if gen_1_parent_parent == gen_2_parent : return 'Aunt'\n",
" if gen_1_parent == gen_2_parent_parent : return 'Niece'\n",
" \n",
"# Run this cell to test your work\n",
"family_a = [(\"Enid\", \"Susan\"), (\"Susan\", \"Deborah\")]\n",
"family_b = [('Enid', 'Susan'), ('Susan', 'Deborah'), ('Enid', 'Dianne'), ('Dianne', 'Judy'), ('Dianne', 'Fern')]\n",
"\n",
"assert_equal(relations(family_a,('Enid','Susan')),'Daughter')\n",
"assert_equal(relations(family_b,('Enid','Judy')),'Sister')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Challenge 4 - Money in the bank"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def dispense_cash(amount):\n",
" ''' Determine the minimum number of ATM bills to meet the requested amount to dispense\n",
" \n",
" Parameters\n",
" ----------\n",
" amount : int\n",
" The amount of money requested from the ATM\n",
" \n",
" Returns\n",
" -------\n",
" int\n",
" The number of bills needed, -1 if it can't be done\n",
" '''\n",
" total_bills = 0\n",
" if amount % 10 != 0:\n",
" return -1 # Can't be done, because it has to be a multiple of 10\n",
" \n",
" b_500 = (amount // 500) # The // operator does integer only division - such that 4 // 3 = 1\n",
" b_100 = (amount % 500) // 100\n",
" b_50 = (amount - (b_500*500) - (b_100 *100)) // 50\n",
" b_20 = (amount - (b_500*500) - (b_100 *100) - (b_50 * 50)) // 20\n",
" b_10 = (amount - (b_500*500) - (b_100 *100) - (b_50 * 50) - (b_20 * 20)) // 10\n",
" \n",
" total_bills = b_500 + b_100 + b_50 + b_20 + b_10\n",
" return total_bills\n",
"\n",
"assert_equal(dispense_cash(1120), 4)\n",
"assert_equal(dispense_cash(492), -1)\n",
"assert_equal(dispense_cash(440), 6)\n",
"assert_equal(dispense_cash(370), 5)\n",
"assert_equal(dispense_cash(80), 3)\n",
"assert_equal(dispense_cash(8720), 20)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Challenge 5 - Fruit Calculator"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def fruit_calculator(question):\n",
" # Set the quantity to zero\n",
" # For each word in the string\n",
" # If the word is 'loses', then we are going to be subtracting\n",
" # If the word is a number, then convert it to an integer, if the \n",
" # If the previous word was a number (the value of number is not zero),\n",
" # then add this word to a dictionary as a key and add the number to the value\n",
" # Find the last word, remove the '?' and look it up in dictionary - this is the value\n",
" \n",
" # Set the qty to zero\n",
" qty = 0 \n",
" lose = False\n",
" fruits = {}\n",
" words = question.split(' ')\n",
" # For each word in the string\n",
" for w in words:\n",
" \n",
" # Since the split may include `.` or `,` we need to take these off\n",
" w = w.rstrip('.').rstrip(',')\n",
" if w == 'loses':\n",
" lose = True\n",
" \n",
" # If the word is a number, then figure out if this is a gain, loss or just has\n",
" if w.isdigit():\n",
" qty = int(w)\n",
" # Gains and Loses is 0 if there hasn't been a gain or loss\n",
" if lose:\n",
" qty = qty * -1\n",
" # Need to reset the lose value to False\n",
" lose = False\n",
" \n",
" else:\n",
" # If the previous word was a number (the value of qty is not zero),\n",
" # then add this word to a dictionary as a key and add the number to the value\n",
" if qty != 0:\n",
" fruits[w] = fruits.get(w,0) + qty\n",
" qty = 0\n",
" \n",
" # Find the last word, remove the '?' and look it up in dictionary - this is the value\n",
" target_fruit = words[-1].rstrip('?')\n",
" return fruits.get(target_fruit,0)\n",
"\n",
"assert_equal(fruit_calculator('Panda has 8 apples and loses 2 apples. How many apples?'), 6)\n",
"assert_equal(fruit_calculator('Panda has 8 apples, 2 bananas and gains 3 bananas. How many bananas?'), 5)\n",
"assert_equal(fruit_calculator('Panda has 8 apples, 2 bananas and gains 3 bananas. How many apples?'), 8) \n",
"assert_equal(fruit_calculator('Jim has 12 bananas. He loses 2 apples. Then he gains 1 apple. How many bananas?'),12)\n",
"assert_equal(fruit_calculator('Jim has 2 bananas and gains 3 bananas. How many watermelons?'),0)"
]
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

0 comments on commit 46f0fc4

Please sign in to comment.