Permalink
Browse files

first commit

  • Loading branch information...
afeder17 committed Dec 2, 2019
0 parents commit 053b23d68a164750c370ad54c272b6cfd0fc6437
Showing with 381 additions and 0 deletions.
  1. +381 −0 Projects/project-code-challenge.ipynb
@@ -0,0 +1,381 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"http://imgur.com/1ZcRyrc.png\" style=\"float: left; margin: 20px; height: 55px\">\n",
"\n",
"# Project 1: Python Coding Exercises\n",
"\n",
"_Authors: Joseph Nelson (DC) _\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following code challenges are drawn from common exercises used in technical interviews.\n",
"\n",
"Please note that there may be several ways to approach each challenge. If you get stuck, try mapping out your approach in pseudocode first. Finally, while solutions to problems like these may be found online, remember that if you copy/paste code that you can't explain, you'll be missing out on the point of the project. The only way to truly learn a new skill is through practice, trial, and error - we can only help you improve by understanding where you are having trouble."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Challenge 1: Largest Palindrome\n",
"A palindromic number reads the same both ways. For example, 1234321 is a palindrome. The largest palindrome made from the product of two two-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two three-digit numbers. Afterward, write a brief explanation walking through your code's logic in markdown."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"906609\n"
]
}
],
"source": [
"def is_palindrome(num):\n",
" if (str(num) == str(num)[::-1]):\n",
" return True\n",
" else:\n",
" return False\n",
"\n",
"largest = 0\n",
"for i in range(100, 999):\n",
" for j in range(100, 999):\n",
" product = i * j\n",
" if (is_palindrome(product) and product > largest):\n",
" largest = product\n",
"\n",
"print(largest)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Simply, I made a function to test whether a number is a pallindrome. Then, I iterated through\n",
"all the possible numbers in the range (multiples of two 3 digit numbers), checking for the \n",
"largest one meeting the palindrome condition"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"### Challenge 2: Summation of Primes\n",
"The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below 2,000. Afterward, write a brief explanation walking through your code's logic in markdown."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"277051\n"
]
}
],
"source": [
"def is_prime(num):\n",
" for i in range(2, num):\n",
" if ((num / i) % 1 == 0):\n",
" return False\n",
" return True\n",
"\n",
"sum = 0\n",
"for i in range(0, 2000):\n",
" if (is_prime(i)):\n",
" sum += i\n",
"print(sum)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This was the same. I wrote a function to check for primes, then I added together all the \n",
"primes I found in the range"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Challenge 3: Multiples of 3 and 5\n",
"If we list all of the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6, and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 and 5 below 1,000. Afterward, write a brief explanation walking through your code's logic in markdown."
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"233168\n"
]
}
],
"source": [
"def is_mult_3_or_5(num):\n",
" if ((num / 3) % 1 == 0):\n",
" return True\n",
" if ((num / 5) % 1 == 0):\n",
" return True\n",
" return False\n",
"\n",
"sum = 0\n",
"for i in range(0, 1000):\n",
" if (is_mult_3_or_5(i)):\n",
" sum += i\n",
"print(sum)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Again, the same basic strategy. A functio to check for multiples of 3 and 5, then a loop \n",
"that loops over the range looking for them, and sums them all together."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Challenge 4: String Compressor\n",
"Implement a method to perform basic string compression using the counts of repeated characters. (This is called run-length encoding.) For example, the string \"aabcccccaaa\" would become a2b1c5a3. If the “compressed” string would not become smaller than the original string, your method should return the original string. You can assume the string has only uppercase and lowercase letters (a–z). Specify whether your solution is case sensitive or case insensitive and what you would need to change to make it the other. Afterward, write a brief explanation walking through your code's logic in markdown."
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'c1a6t4'"
]
},
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def compress(word):\n",
" new_word = \"\"\n",
" i = 0\n",
" while (i < len(word)):\n",
" count = 1;\n",
" while (i < len(word) - 1 and word[i] == word[i + 1]): \n",
" count += 1 \n",
" i += 1 \n",
"\n",
" new_word += word[i] + str(count)\n",
" i += 1\n",
" return new_word\n",
"\n",
"compress(\"caaaaaatttt\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This one was really quite tricky. Basically, I go through the word, running a counter \n",
"each time we hit a repetition, and counting until we find a letter thats different, and \n",
"concatenating our subloop result. The main complication here was that a regular for loop would\n",
"not work, since unlike in C++, python insists on doing every value in the range without \n",
"skipping, so I had to switch to an awkward while loop with a counter."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### *BONUS* Challenge: FizzBuzz\n",
"Write a program that prints all of the numbers from 1 to 100. For multiples of 3, instead of the number, print \"Fizz;\" for multiples of 5, print \"Buzz.\" For numbers that are multiples of both 3 and 5, print \"FizzBuzz.\" Afterward, write a brief explanation walking through your code's logic in markdown."
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"Fizz\n",
"4\n",
"Buzz\n",
"Fizz\n",
"7\n",
"8\n",
"Fizz\n",
"Buzz\n",
"11\n",
"Fizz\n",
"13\n",
"14\n",
"FizzBuzz\n",
"16\n",
"17\n",
"Fizz\n",
"19\n",
"Buzz\n",
"Fizz\n",
"22\n",
"23\n",
"Fizz\n",
"Buzz\n",
"26\n",
"Fizz\n",
"28\n",
"29\n",
"FizzBuzz\n",
"31\n",
"32\n",
"Fizz\n",
"34\n",
"Buzz\n",
"Fizz\n",
"37\n",
"38\n",
"Fizz\n",
"Buzz\n",
"41\n",
"Fizz\n",
"43\n",
"44\n",
"FizzBuzz\n",
"46\n",
"47\n",
"Fizz\n",
"49\n",
"Buzz\n",
"Fizz\n",
"52\n",
"53\n",
"Fizz\n",
"Buzz\n",
"56\n",
"Fizz\n",
"58\n",
"59\n",
"FizzBuzz\n",
"61\n",
"62\n",
"Fizz\n",
"64\n",
"Buzz\n",
"Fizz\n",
"67\n",
"68\n",
"Fizz\n",
"Buzz\n",
"71\n",
"Fizz\n",
"73\n",
"74\n",
"FizzBuzz\n",
"76\n",
"77\n",
"Fizz\n",
"79\n",
"Buzz\n",
"Fizz\n",
"82\n",
"83\n",
"Fizz\n",
"Buzz\n",
"86\n",
"Fizz\n",
"88\n",
"89\n",
"FizzBuzz\n",
"91\n",
"92\n",
"Fizz\n",
"94\n",
"Buzz\n",
"Fizz\n",
"97\n",
"98\n",
"Fizz\n",
"Buzz\n"
]
}
],
"source": [
"for i in range(1, 101):\n",
" if ((i / 3) % 1 == 0 and (i / 5) % 1 == 0):\n",
" print(\"FizzBuzz\")\n",
" elif ((i / 3) % 1 == 0):\n",
" print(\"Fizz\")\n",
" elif ((i / 5) % 1 == 0):\n",
" print(\"Buzz\")\n",
" else:\n",
" print(str(i))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What we're being asked for here, essentially, is an exercise in control flow. The key thing is the order of the if/elif statements. The double condition has to come first, otherwise the others will accidentally trigger, and in the same way the default condition must come last."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"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 053b23d

Please sign in to comment.