Skip to content

Commit

Permalink
solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
cab938 committed Sep 10, 2019
1 parent b6a5f43 commit 23d8984
Showing 1 changed file with 290 additions and 0 deletions.
290 changes: 290 additions & 0 deletions 190909_programming_assessment_solutions.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Programming Assessment\n",
"Answer the following questions using Python. You have 30 minutes, try and write something for each question. **There are no grades associated with this assessment**, this is only to help me better understand people's skills coming into this course! However, there is a prize for the best set of solutions. And this is all open network, go look things up if you need to, just no direct asking these questions or communication amongst yourselves."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Question 1\n",
"What is the difference between a class and an object?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A class is a definition of an object, and object is an instance of a class. The first is conceptually the code you are writing which describes how the latter will be interpreted by the Python interpreter."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Question 2\n",
"Calculate the average of the list `[34,53,21,32,56,323,23,53,62,34,12,64,85,35,87,95]`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lst=[34,53,21,32,56,323,23,53,62,34,12,64,85,35,87,95]\n",
"j=0\n",
"for i in lst:\n",
" j=j+i\n",
"print(j/len(lst))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Question 3\n",
"Write a function called `is_palindrome` which takes in one parameter as a string and returns `True` if the string is a palindrome and `False` otherwise. A palindrome is a string which is the same backwards as it is forwards, e.g. radar, wakaw, madam"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def is_palindrome(s):\n",
" for i in range(0,len(s)):\n",
" if s[i]!=s[-1*(i+1)]:\n",
" return False\n",
" return True\n",
"\n",
"is_palindrome(\"test\")\n",
"#is_palindrome(\"madam\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Question 4\n",
"Update the function to `is_any_palindrome` to also be able to take in any integer or floating point value and treat them the same (ignoring the decimal place in the case of floats). E.g. 12721 would be a palindrome."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def is_any_palindrome(i):\n",
" return is_palindrome(str(i).replace(\".\",\"\"))\n",
"\n",
"is_any_palindrome(1271)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Question 5\n",
"Fix the following code."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"classroom1=[\"Ollie\",\"Raven\",\"Jameson\",\"Puppy\",\"Sparky\",\"Blue\",\"Prince\",\"Sydney\",\"Ella\",\"Jake\",\"Gucci\",\"Lola\"]\n",
"classroom2=[\"Shiner\",\"Thor\",\"Bentley\",\"Raven\",\"Cody\",\"Piper\",\"Lexi\",\"Raven\",\"Astro\",\"Oreo\",\"Lucky\",\"Lucy\"]\n",
"\n",
"conflicts=[]\n",
"def detect_class_conflict(1, 2):\n",
" '''Returns the names of people who are in both classes and thus have a time conflict\n",
" '''\n",
" for name in range(0,len(1)):\n",
" for another in range(0,len(2))\n",
" if 1[name]==2[another]:\n",
" conflicts.append(name)\n",
" return conflicts\n",
"\n",
"detect_class_conflict(classroom1, classroom2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"classroom1=[\"Ollie\",\"Raven\",\"Jameson\",\"Puppy\",\"Sparky\",\"Blue\",\"Prince\",\"Sydney\",\"Ella\",\"Jake\",\"Gucci\",\"Lola\"]\n",
"classroom2=[\"Shiner\",\"Thor\",\"Bentley\",\"Raven\",\"Cody\",\"Piper\",\"Lexi\",\"Raven\",\"Astro\",\"Oreo\",\"Lucky\",\"Lucy\"]\n",
"\n",
"def detect_class_conflict(one, two):\n",
" '''Returns the names of people who are in both classes and thus have a time conflict\n",
" '''\n",
" conflicts=[]\n",
" for name in range(0,len(one)):\n",
" for another in range(0,len(two)):\n",
" if one[name]==two[another]:\n",
" conflicts.append(one[name])\n",
" return conflicts\n",
"\n",
"detect_class_conflict(classroom1, classroom2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Question 6\n",
"Generate a list of all even numbers up to 10000"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"[i for i in range(2,10000,2)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Question 7\n",
"Write a function called `whole_divisions` which takes two required parameters, `lower` and `upper` which represent two bounds as positive numbers (where `lower <= upper`), and an optional third parameter `divisor`, and returns a list of all numbers between (but not including) the two bounds which are exactly dividable by divisor. If divisor is not provided use a divisor of 2.\n",
"\n",
"`whole_divisions(3,9)` should return `[4, 6, 8]`\n",
"\n",
"`whole_divisions(2,10)` should return `[4, 6, 8]`\n",
"\n",
"`whole_divisions(3,9,divisor=3)` should return `[6]`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def whole_divisions(lower, upper, divisor=2):\n",
" results=[]\n",
" for i in range(lower+1, upper):\n",
" if i%divisor==0:\n",
" results.append(i)\n",
" return results"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Question 8\n",
"Write some code which downloads the wikipedia page on saskatchewn at `https://en.wikipedia.org/wiki/Saskatchewan` and prints out the capital city (you can see it listed on the right hand side of the page)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from bs4 import BeautifulSoup\n",
"import requests\n",
"\n",
"request= requests.get(\"https://en.wikipedia.org/wiki/Saskatchewan\")\n",
"soup = BeautifulSoup(request.text)\n",
"\n",
"ths=soup.find_all(\"th\")\n",
"for th in ths:\n",
" if th.text==\"Capital\":\n",
" print(th.nextSibling.text)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Question 9\n",
"\n",
"Imagine you are creating a list of all possible usernames from aaa, aab, aac, etc. through to zzz. How would you do that using the code below? Can you do it by adding only one line?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import string\n",
"string.ascii_lowercase\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"names=[a+b+c for a in string.ascii_lowercase for b in string.ascii_lowercase for c in string.ascii_lowercase]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Question 10\n",
"Given the code `source_dict={1:10, 2:20, 3:30, 4:40}`, turn this into a dictionary where all of the keys are strings and all of the values associate with those keys are twice the current value. E.g. the output dictionary should be `{'1': 20, '2': 40, '3': 60, '4': 80}`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"source_dict={1:10, 2:20, 3:30, 4:40}\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"double_dict = {str(k):v*2 for (k,v) in source_dict.items()}"
]
}
],
"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
}

0 comments on commit 23d8984

Please sign in to comment.