Skip to content

Instantly share code, notes, and snippets.

@SebDeclercq
Last active June 27, 2019 13:45
Show Gist options
  • Select an option

  • Save SebDeclercq/d2c66827944148f97174d2cec62fc401 to your computer and use it in GitHub Desktop.

Select an option

Save SebDeclercq/d2c66827944148f97174d2cec62fc401 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from typing import List\n",
"def scale(string: str, width: int, height: int) -> str:\n",
" chunks: List[str] = string.split('\\n')\n",
" output: str = ''\n",
" for i, chunk in enumerate(chunks.copy()):\n",
" chunks[i] = ''.join([letter * width for letter in chunk])\n",
" for chunk in chunks:\n",
" for _ in range(height):\n",
" output += f'{chunk}\\n'\n",
" return output.rstrip()\n",
"s: str = \"abcd\\nefgh\\nijkl\\nmnop\"\n",
"assert scale(s, 2, 3) == \"aabbccdd\\naabbccdd\\naabbccdd\\neeffgghh\\neeffgghh\\neeffgghh\\niijjkkll\\niijjkkll\\niijjkkll\\nmmnnoopp\\nmmnnoopp\\nmmnnoopp\""
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from datetime import datetime\n",
"def us_time_to_military_time(ustime: str) -> str:\n",
" return datetime.strptime(ustime, '%I:%m %p').strftime('%H:%m')\n",
"assert us_time_to_military_time('11:11 AM') == '11:11'\n",
"assert us_time_to_military_time('11:11 PM') == '23:11'"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"def power_of_two(num: int) -> bool:\n",
" return math.sqrt(num) % 2 == 0\n",
"\n",
"assert power_of_two(256) == True\n",
"assert power_of_two(257) == False"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"from typing import List\n",
"def sort_guests(string: str) -> str:\n",
" guests: List[str] = []\n",
" for guest in string.upper().split(';'):\n",
" firstname, name = guest.split(':')\n",
" guests.append((name, firstname))\n",
" guests = sorted(guests, key=lambda x: (x[0], x[1]))\n",
" output: List[str] = []\n",
" for name, firstname in guests:\n",
" output.append(f'({name}:{firstname})')\n",
" return ','.join(output)\n",
" \n",
"s: str = \"Fred:Corwill;Wilfred:Corwill;Barney:Tornbull;Betty:Tornbull;Bjon:Tornbull;Raphael:Corwill;Alfred:Corwill\"\n",
"assert sort_guests(s) == '(CORWILL:ALFRED),(CORWILL:FRED),(CORWILL:RAPHAEL),(CORWILL:WILFRED),(TORNBULL:BARNEY),(TORNBULL:BETTY),(TORNBULL:BJON)'"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def alarm(employed: bool, vacation: bool) -> bool:\n",
" if not employed:\n",
" return False\n",
" return not vacation\n",
"\n",
"assert alarm(True, False) == True\n",
"assert alarm(True, True) == False\n",
"assert alarm(False, False) == False\n",
"assert alarm(False, True) == False"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"from functools import reduce\n",
"def persistence(num: int) -> int:\n",
" counter: int = 0\n",
" while len(str(num)) > 1:\n",
" counter += 1\n",
" num = reduce(lambda a, b: a * b, [int(e) for e in str(num)])\n",
" return counter\n",
"\n",
"assert persistence(39) == 3\n",
"assert persistence(999) == 4"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"from typing import List\n",
"def number(bus_stops: List[List[int]]) -> int:\n",
" return sum(i - o for i, o in bus_stops)\n",
"\n",
"assert number([[10,0],[3,5],[5,8]]) == 5\n",
"assert number([[3,0],[9,1],[4,10],[12,2],[6,1],[7,10]]) == 17\n",
"assert number([[3,0],[9,1],[4,8],[12,2],[6,1],[7,8]]) == 21"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"from typing import List\n",
"from collections import defaultdict\n",
"def get_outlier(num_list) -> int:\n",
" even_odd: defaultdict = defaultdict(int)\n",
" for i in num_list[:5]:\n",
" even_odd[i % 2] += 1\n",
" if 0 in even_odd and even_odd[0] == 5:\n",
" return next((i for i in num_list if i % 2))\n",
" else:\n",
" return next((i for i in num_list if i % 2 == 0))\n",
"\n",
"\n",
"assert get_outlier([2, 4, 0, 100, 4, 11, 2602, 36]) == 11\n",
"assert get_outlier([160, 3, 1719, 19, 11, 13, -21]) == 160\n",
"assert get_outlier([-668751, -6729823, -757305, 924025, 3443292, 4943173, 4342469, 4165055,\n",
" -1789457, -3480255, 2298533, -9549351, 3146467, 5849267, -6717409, 8696991,\n",
" -9067269, -4504379, 1085743, 7741419, -7272243, -4336999, 8960085, -5036667,\n",
" 5048431, 7515973, -7550127, -8454781]) == 3443292"
]
},
{
"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.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment