From 66a35b10a22aea879827eec30959b0a2136eb132 Mon Sep 17 00:00:00 2001 From: Ostrowski Date: Tue, 9 Jan 2018 11:23:27 -0600 Subject: [PATCH 1/4] My answers --- __pycache__/exercise.cpython-36.pyc | Bin 0 -> 2011 bytes exercise.py | 58 ++++++++++++++++++++++------ 2 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 __pycache__/exercise.cpython-36.pyc diff --git a/__pycache__/exercise.cpython-36.pyc b/__pycache__/exercise.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..20ba48b23646458608f168dbaa17890cd9868cad GIT binary patch literal 2011 zcma)7OK;mo5ayE9TW)Q~c3U6_7H|Pss2oU+oZ6}@G_lb`+WHXn0njoCO1qX>@u4g! zS5oDZeCVY=q`#;DuRZA>6uGoBOGghoHA-k#!`a<=eDm$_*~&_G@b-J-x1^%{r7S#Y zkiUWv{0I|M422P+hCipmS$TGrTEe4v+V1>SIbg(dc*M2_vo%mncoh15YpW%$_HGu?SMb+ z4YQ-nOTR@Mt{q;EGT(5IzVbaDjPfrWKA-gnLlvos4OT9_q@FN>)b&Wkcmv~;$- za`)KW;JG}qgpvl}G~S^!R(^(JGcr+``U$9u+UXRboc;q(kwi%m^c*_`i46WwL;{xH zR*!4V9|A6dChv~g`Pw2|G52rKGQLgpOlm{df%5xKfs(#H?QNueYGR$j?%kUSBa zAWe~>Q&dh(Q~#lwbo0|lO_6LL*L6?7WYqz+0xArmaG zKKfH8g14~orf&%-55I!ubY(exkoz9vf>z2g__%AKuXug81@~rVU{=JS?_w*FhJx7Y z+`xY{61mO9BWCQ|gu0tx$&@jemGaKneu+LW(a%ftpoI0Plwnk0ELQJXwJHOefrIxS z0g_DuZv-~dErqFFY@^U1aB&JF?gwxoiFc=P0>PKy;juU>XBz46$N_b|$Gm=!u?v_4s beb#q)9nH%xoUUiJ^jel=iLRzMH?)5N$hP(_ literal 0 HcmV?d00001 diff --git a/exercise.py b/exercise.py index 0ae90ce..eb11d35 100644 --- a/exercise.py +++ b/exercise.py @@ -1,32 +1,68 @@ +""" +Run: + python -m doctest -v exercise.py +""" + # problem 1 # ------------------------------------------------------------------- # people = ['Bob Smith', 'Ken Jones', 'Alex Bradino'] -def sort_by_last_name(people, order): - # return full names sorted by last name in either ascending or descending order - # add doctests make sure it passes - pass +def sort_by_last_name(people): + """ + Sort by last names + + Agruments: First Last names + Returns: sorted list of names by last name + + Doctests: + >>> sort_by_last_name(['Bob Smith', 'Ken Jones', 'Alex Bradino']) + ['Alex Bradino', 'Ken Jones', 'Bob Smith'] + """ + return sorted(people, key=lambda x: x.rsplit(None,1)[-1]) # problem 2 # ------------------------------------------------------------------- # + names = ['James', 'Susan', 'Maggie'] ages = [4, 9, 12] - +d = {} def create_dictionary_from_lists(names, ages): - # {'James':4, 'Susan':9, 'Maggie':12} - # add doctests make sure it passes - pass + """ + Create dictionary from lists + + Agruments: names = list of names; ages = list of ages + Returns: Dictionary of name and age + + Doctests: + >>> create_dictionary_from_lists(['James', 'Susan', 'Maggie'], [4,9,12]) + {'James': 4, 'Susan': 9, 'Maggie': 12} + """ + for k, v in zip(names, ages): + d[k] = v + return d + + # problem 3 # ------------------------------------------------------------------- # + numbers = [5, 6, 7, 8, 9, 10, 11, 12] def square_even_values_and_sum_under_10(numbers): - # 6^2 + 8^2] - # add doctests make sure it passes - pass + """ + Squares even values under 10 + + Agruments: numbers + Returns: sum of even squares + + Doctests: + >>> square_even_values_and_sum_under_10([5, 6, 7, 8, 9, 10, 11, 12]) + 344 + """ + evens = list(filter(lambda n: n % 2 == 0 , numbers)) + return sum(list(map(lambda n: n**2, evens))) From 8c3b3857a4bcf16f1fb5e637cd53d369038175a7 Mon Sep 17 00:00:00 2001 From: Ostrowski Date: Tue, 9 Jan 2018 11:38:58 -0600 Subject: [PATCH 2/4] updated answers --- __pycache__/exercise.cpython-36.pyc | Bin 2011 -> 2013 bytes exercise.py | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/__pycache__/exercise.cpython-36.pyc b/__pycache__/exercise.cpython-36.pyc index 20ba48b23646458608f168dbaa17890cd9868cad..47030d1e6c0a3d4e2059e30915e72d0e815373a3 100644 GIT binary patch delta 277 zcmcc3f0v)rn3tDJ_FqU;+eS`qCSAr9re>xTW=V!%22B>fUyKSb85tNDUj7FHO~zZS z#RWN;B|rva#Y%=Ew#oKPz1(a-`64jEKKVM65+l#%Urb9GQG{2q7&3}&zQa<)C0?`LDfn#z8Tc)Bc%rH&HB6%1aYC1&KCpJqIv(4DeJXL{Gewv)O*yH0<@{{A^ ii?~5*c|e2^h!6%5f*?W+M5urWkRBvAKs~UPT@3*HH#cYi delta 275 zcmcc1f197vn3tEU;#Wvi$3{+WCT)fk#%87zCP{{122Ezamy8Sy3@`r!fhOZEwxZ&K zoXip+i?L!QLlNs_N2XqGR-l3+Fu^wYCX*5)_vSxLOBqpw*RU8eif+EgQp6|)QV7zY z2hjr22Qqrq72M1O~xWQ7#nIjMAa8IOBAy$*v&jufKq;%oVVEH<5TjJ>> sort_by_last_name(['Bob Smith', 'Ken Jones', 'Alex Bradino']) ['Alex Bradino', 'Ken Jones', 'Bob Smith'] """ - return sorted(people, key=lambda x: x.rsplit(None,1)[-1]) + return sorted(people, key=lambda x: x.split(' ',1)[-1]) # problem 2 From 4c1d963c359ede454c218aafd2eea2bddebdd0ec Mon Sep 17 00:00:00 2001 From: Ostrowski Date: Tue, 9 Jan 2018 11:42:24 -0600 Subject: [PATCH 3/4] Updated again --- exercise.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercise.py b/exercise.py index 67d14eb..803dd13 100644 --- a/exercise.py +++ b/exercise.py @@ -19,7 +19,7 @@ def sort_by_last_name(people): >>> sort_by_last_name(['Bob Smith', 'Ken Jones', 'Alex Bradino']) ['Alex Bradino', 'Ken Jones', 'Bob Smith'] """ - return sorted(people, key=lambda x: x.split(' ',1)[-1]) + return sorted(people, key=lambda x: x.split(' ')[-1]) # problem 2 From 6d5a918a4b781a27861b954b83dd68e0586212fb Mon Sep 17 00:00:00 2001 From: Ostrowski Date: Tue, 9 Jan 2018 12:08:20 -0600 Subject: [PATCH 4/4] corrected during review session --- __pycache__/exercise.cpython-36.pyc | Bin 2013 -> 2011 bytes exercise.py | 6 ++++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/__pycache__/exercise.cpython-36.pyc b/__pycache__/exercise.cpython-36.pyc index 47030d1e6c0a3d4e2059e30915e72d0e815373a3..793f8ceb1ab0d9c3c1485c8ac4322fc11dc97aa9 100644 GIT binary patch delta 45 ycmcc1f197vn3tF9*}ssemW`Z08AX{G7#MGaX?C03W;x A^#A|> diff --git a/exercise.py b/exercise.py index 803dd13..9747dfe 100644 --- a/exercise.py +++ b/exercise.py @@ -8,7 +8,7 @@ people = ['Bob Smith', 'Ken Jones', 'Alex Bradino'] -def sort_by_last_name(people): +def sort_by_last_name(people, order): """ Sort by last names @@ -19,7 +19,7 @@ def sort_by_last_name(people): >>> sort_by_last_name(['Bob Smith', 'Ken Jones', 'Alex Bradino']) ['Alex Bradino', 'Ken Jones', 'Bob Smith'] """ - return sorted(people, key=lambda x: x.split(' ')[-1]) + return sorted(people, key=lambda person: person.split(' ')[-1], reverse = order) # problem 2 @@ -44,6 +44,8 @@ def create_dictionary_from_lists(names, ages): d[k] = v return d + #{name:age for name, age in zip(names, ages)} +