Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jrjames83/3ff9e73aa0006dc47473095071416512 to your computer and use it in GitHub Desktop.
Save jrjames83/3ff9e73aa0006dc47473095071416512 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": [
"#A write a function that returns True if a number is odd, else False\n",
"#B write a function that takes a string and returns the count of words\n",
"#C write a function that accepts a list of numbers and returns them into a string eg: [1,2,3] --> \"123\"\n",
"#D write a function that determines if an input word, is a palindrome (eg, civic is the same backward and forward)\n",
"#E write a function that returns an NxN grid of some character, which is also a function agrument \n",
" #eg passing N=5 and char=\"*\" would yield\n",
"# [['*', '*', '*', '*', '*'],\n",
"# ['*', '*', '*', '*', '*'],\n",
"# ['*', '*', '*', '*', '*'],\n",
"# ['*', '*', '*', '*', '*'],\n",
"# ['*', '*', '*', '*', '*']]\n",
" \n",
"#F write a function that counts the number of occurrences of a target pattern, within a string\n",
" # eg, pattern = \"dog\" and string = \"you dog, that dog was running and dogged\" --> return 3\n",
" \n",
"#G write a function that returns the type of its input parameter\n",
"#H write a function that accepts keyword arguments only, and returns them in a list (google **kwargs python for more)\n",
"#I write a function that returns elements which are common to two lists\n",
"#J write a function that makes an email private, eg [email protected] --> ****@****.com\n",
"#K write a function that returns an error if someone has numbers in a string"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"#A\n",
"\n",
"def even_odd(nbr):\n",
" if nbr % 2 == 0:\n",
" return False\n",
" else:\n",
" return True\n",
" \n",
"assert(even_odd(3) == True)\n",
"assert(even_odd(100000) == False)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# B\n",
"\n",
"def word_count(input_str):\n",
" return len(input_str.split())\n",
"\n",
"assert(word_count(' here are 4 words ') == 4)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# C - map applies a function, to each element of an iterable, eg, we want to convert each \n",
"# integer in the input_nbr_list to a str, then join 'em\n",
"\n",
"def nbr_to_list(input_nbr_list):\n",
" return \"\".join(map(str, input_nbr_list))\n",
"\n",
"assert(nbr_to_list([1,2,3,4]) == '1234')\n",
"assert(nbr_to_list([333]) == '333')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# D - edge case of one letter word passes\n",
"\n",
"def is_pal(input_word):\n",
" return input_word == input_word[::-1]\n",
"\n",
"assert(is_pal('civic') == True)\n",
"assert(is_pal('civics') == False)\n",
"assert(is_pal('a') == True)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[['¯\\\\_(ツ)_/¯', '¯\\\\_(ツ)_/¯', '¯\\\\_(ツ)_/¯', '¯\\\\_(ツ)_/¯', '¯\\\\_(ツ)_/¯'],\n",
" ['¯\\\\_(ツ)_/¯', '¯\\\\_(ツ)_/¯', '¯\\\\_(ツ)_/¯', '¯\\\\_(ツ)_/¯', '¯\\\\_(ツ)_/¯'],\n",
" ['¯\\\\_(ツ)_/¯', '¯\\\\_(ツ)_/¯', '¯\\\\_(ツ)_/¯', '¯\\\\_(ツ)_/¯', '¯\\\\_(ツ)_/¯'],\n",
" ['¯\\\\_(ツ)_/¯', '¯\\\\_(ツ)_/¯', '¯\\\\_(ツ)_/¯', '¯\\\\_(ツ)_/¯', '¯\\\\_(ツ)_/¯'],\n",
" ['¯\\\\_(ツ)_/¯', '¯\\\\_(ツ)_/¯', '¯\\\\_(ツ)_/¯', '¯\\\\_(ツ)_/¯', '¯\\\\_(ツ)_/¯']]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# E - many approaches to this, could append lists to a master list, then print the master list as well\n",
"# I am using a nested list comprehension\n",
"\n",
"def n_n_grid(n, char):\n",
" return [ [char for x in range(n)] for x in range(n)]\n",
"\n",
"n_n_grid(5, \"¯\\_(ツ)_/¯\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# F - target pattern count\n",
"# this highlights a useful string method, but you could have used a regexp as well\n",
"\n",
"import re\n",
"\n",
"def target_pattern_count(pattern, input_string):\n",
" return input_string.count(pattern) # :)\n",
"\n",
"def target_pattern_using_regex(pattern, input_string):\n",
" return len(re.findall(pattern, input_string))\n",
"\n",
"assert(target_pattern_count('dog', \"The dog, is a dogged dog\") == 3)\n",
"assert(target_pattern_count('moose', \"The dog, is a dogged dog\") == 0)\n",
"\n",
"assert(target_pattern_using_regex('dog', \"The dog, is a dogged dog\") == 3)\n",
"assert(target_pattern_using_regex('moose', \"The dog, is a dogged dog\") == 0)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# G\n",
"\n",
"def param_type(input_param):\n",
" if callable(input_param):\n",
" return 'function'\n",
" else:\n",
" return type(input_param)\n",
"\n",
"assert(param_type(\"jeff\") == str)\n",
"assert(param_type([1,2,3]) == list)\n",
"assert(param_type(999) == int)\n",
"assert(param_type({}) == dict)\n",
"assert(param_type((1,)) == tuple)\n",
"assert(param_type(lambda x: x) == 'function')"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"dog scout\n",
"owner jeff\n",
"age 1.5\n"
]
}
],
"source": [
"# H\n",
"def print_out_kwargs(*args, **kwargs):\n",
" for key, value in kwargs.items():\n",
" print(key, value)\n",
" \n",
"print_out_kwargs(dog=\"scout\", owner=\"jeff\", age=\"1.5\")\n",
"\n",
"# More details here https://www.digitalocean.com/community/tutorials/how-to-use-args-and-kwargs-in-python-3\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{5, 6, 7, 8, 9}"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# I\n",
"\n",
"def common_between(list1, list2):\n",
" return set(list1) & set(list2)\n",
"\n",
"common_between(list(range(10)), list(range(5, 15))) # Could call list on a set object\n",
"# To return another list"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"# J eg [email protected] --> ****@****.com\n",
"# Probably could have done this many more elegant ways\n",
"\n",
"\n",
"def private_email(input_email):\n",
" part1, part2 = input_email.split(\"@\")\n",
" tld = part2.split(\".\")[-1]\n",
" temp1 = re.sub(\"[a-zA-z1-9]\", \"*\", part1)\n",
" temp2 = re.sub(\"[a-zA-z1-9]\", \"*\", part2).split(\".\")[0]\n",
" return temp1 +\"@\" + temp2 + \".\" + tld\n",
"\n",
"assert(private_email('[email protected]') == '**@****.com')\n",
"assert(private_email('[email protected]') == '***@****.com')"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"# K\n",
"def nbr_in_str_warn(input_string):\n",
" return any(char.isdigit() for char in input_string)\n",
"\n",
"assert(nbr_in_str_warn('this is a the #1 exercise') == True)\n",
"assert(nbr_in_str_warn('No numbers hanging around here, just us letters') == False)\n",
"\n",
"# Discussion on the use of any https://stackoverflow.com/a/19389957/3182843"
]
},
{
"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.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