Skip to content

Instantly share code, notes, and snippets.

@miku
Last active August 11, 2017 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miku/9166bdd2b8c29971e9a24a760ae952a5 to your computer and use it in GitHub Desktop.
Save miku/9166bdd2b8c29971e9a24a760ae952a5 to your computer and use it in GitHub Desktop.
Python exercises.
.ipynb_checkpoints/
.vscode/
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Max of three, without max."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def max_without_max(x, y):\n",
" return (x + y + abs(x - y)) / 2"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10.0"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"max_without_max(1, 10)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def max_of_three(a, b, c):\n",
" if a > b and a > c:\n",
" return a\n",
" if b > a and b > c:\n",
" return b\n",
" if c > a and c > b:\n",
" return c\n",
" else:\n",
" return \"all are equal\"\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def max_of_three_abs(a, b, c):\n",
" return max_without_max(a, max_without_max(b, c))"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def max_of_three_sort(a, b, c):\n",
" return sorted([a, b, c])[-1]\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def max_of_three_bin(a, b, c):\n",
" d = a if a > b else b\n",
" return c if c > d else d\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def max_of_three_cond(a, b, c):\n",
" m = a\n",
" if m < b:\n",
" m = b\n",
" if m < c:\n",
" m = c\n",
" return m\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"a, b, c = 5, 10, 8\n"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The slowest run took 5.72 times longer than the fastest. This could mean that an intermediate result is being cached.\n",
"1000000 loops, best of 3: 344 ns per loop\n"
]
}
],
"source": [
"%timeit max_of_three(a, b, c)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The slowest run took 5.73 times longer than the fastest. This could mean that an intermediate result is being cached.\n",
"1000000 loops, best of 3: 1.01 µs per loop\n"
]
}
],
"source": [
"%timeit max_of_three_abs(a, b, c)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The slowest run took 5.00 times longer than the fastest. This could mean that an intermediate result is being cached.\n",
"1000000 loops, best of 3: 831 ns per loop\n"
]
}
],
"source": [
"%timeit max_of_three_sort(a, b, c)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The slowest run took 6.33 times longer than the fastest. This could mean that an intermediate result is being cached.\n",
"1000000 loops, best of 3: 332 ns per loop\n"
]
}
],
"source": [
"%timeit max_of_three_bin(a, b, c)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The slowest run took 6.12 times longer than the fastest. This could mean that an intermediate result is being cached.\n",
"1000000 loops, best of 3: 284 ns per loop\n"
]
}
],
"source": [
"%timeit max_of_three_cond(a, b, c)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Length of a string or list. Don't use length."
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def length(object):\n",
" i = 0\n",
" for element in obj:\n",
" i += 1\n",
" return i"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def length_enum(obj):\n",
" for i, _ in enumerate(obj, start=1):\n",
" pass\n",
" return i"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Is vowel."
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def is_vowel(c):\n",
" return c in list('aeiou')"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Sum and multiply"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from functools import reduce"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def sum_reduce(numbers):\n",
" return reduce(lambda a, b: a + b, numbers)"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def multiply_reduce(numbers):\n",
" return reduce(lambda a, b: a * b, numbers)"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Translate."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def translate(msg):\n",
" cipher, consonants = '', 'bcdfghjklmnpqrstvwxyz'\n",
" for c in msg:\n",
" if c in consonants:\n",
" cipher += c + \"o\" + c\n",
" else:\n",
" cipher += c\n",
" return cipher\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'tothohisos isos fofunon'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"translate(\"this is fun\")"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Reverse a string."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def reverse(s):\n",
" return ''.join([s[len(s) - 1 - i] for i, _ in enumerate(s)])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'olleH'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"reverse(\"Hello\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Recognize palindromes."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def is_palindrome(s):\n",
" for i, c in enumerate(s[:len(s) // 2]):\n",
" if s[len(s) - 1 - i] != c:\n",
" return False\n",
" return True"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_palindrome(\"raddxar\")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Is member (without \"in\")."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def is_member(value, alist):\n",
" for v in alist:\n",
" if v == value:\n",
" return True\n",
" return False"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Overlapping lists."
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def overlapping(a, b):\n",
" for v in a:\n",
" for w in b:\n",
" if v == w:\n",
" return True\n",
" return False"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"overlapping([1, 2, 3], [7, 4, 5])"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Generate n chars."
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def generate_n_chars(c, n):\n",
" return c * n"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'xxxxxxxxxx'"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"generate_n_chars(\"x\", 10)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Histogram."
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def histogram(numbers):\n",
" for n in numbers:\n",
" print('*' * n)\n"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"*\n",
"**\n",
"***\n"
]
}
],
"source": [
"histogram(range(1, 4))"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Max in list (without max)."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def max_in_list(alist):\n",
" if len(alist) == 0:\n",
" return None\n",
" m = alist[0]\n",
" for n in alist[1:]:\n",
" if n > m:\n",
" m = n\n",
" return m"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"100"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"max_in_list([1, 2, 3, 8, 4, 12, 19, 32, 66, 3, 7, 100, 0])"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Map words to their lengths."
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def map_words_length(words):\n",
" return dict([(w, len(w)) for w in words])\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'a': 1, 'ab': 2, 'abc': 3}"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"map_words_length([\"a\", \"ab\", \"abc\"])"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Longest word."
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def longest_word(words):\n",
" return len(max(words, key=lambda w: len(w)))"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"longest_word([\"hello\", \"workd\", \"as\", \"asdasdasd\"])"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Filter long words."
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def filter_long_words(words, n):\n",
" return [w for w in words if len(w) > n]"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"words = [\"cat\", \"dog\", \"alligator\", \"bird\", \"mouse\", \"sheep\"]"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['alligator', 'mouse', 'sheep']"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"filter_long_words(words, 4)"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Pangram."
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def is_pangram(s):\n",
" return all([l in s for l in list(\"abcdefghijklmopqrstuvwxyz\")])"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_pangram(\"asd\")"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_pangram(\"the quick brown fox jumps over the lazy dog\")"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Char frequency."
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import collections"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def char_frequency(s):\n",
" freq = collections.Counter()\n",
" for c in s:\n",
" freq[c] += 1\n",
" return freq"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Counter({'b': 1, 'o': 2, 'r': 1, 't': 1})"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"char_frequency(\"robot\")"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Average word lengths in a file."
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import io"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def average_word_length(file):\n",
" content = file.read()\n",
" words = content.split()\n",
" return sum([len(w) for w in words]) / len(words)"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"file = io.StringIO(\"Hello World One Two Three.\") # Simulate file api on a string."
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4.4"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"average_word_length(file)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"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.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment