From a8da579c53ff1196c91163cd0da7424d0971c299 Mon Sep 17 00:00:00 2001 From: Noam Daloya <90097801+nananoam123@users.noreply.github.com> Date: Mon, 11 Apr 2022 20:40:27 +0300 Subject: [PATCH 1/3] added students added project members --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 92644d7..163a117 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,6 @@ # final-project final-project python code challenge + +Elad ben david +Noam Daloya +Avi Gross From c60a2011825584fed8994cf53a07d0f33a0848c2 Mon Sep 17 00:00:00 2001 From: Noam Daloya <90097801+nananoam123@users.noreply.github.com> Date: Mon, 11 Apr 2022 21:18:27 +0300 Subject: [PATCH 2/3] fixed code for Statecap python script fixed the code for the script inside the script there are explanations for every added line of code --- StateCap.py | 111 ++++++++++++++++++++++------------------------------ 1 file changed, 46 insertions(+), 65 deletions(-) diff --git a/StateCap.py b/StateCap.py index 73fcf7f..80564ec 100644 --- a/StateCap.py +++ b/StateCap.py @@ -1,20 +1,3 @@ -"""We have an existing dictionary that maps US states to their capitals. -1. Print the state capital of Idaho -2. Print all states. -3. Print all capitals. -4. Create a single string 'Alabama -> Montgomery, Alaska -> Juneau, ...' -5. Ensure the string you created in 4. is alphabetically sorted by state -7. Now we want to add the reverse look up, given the name of a capital what state -is it in? -Implement the function def get_state(capital): below so it returns the state. -GOTCHAS: What happens if two states have the same capital name, how do you -handle that? - -""" -import sys - -import pytest - STATES_CAPITALS = { 'Alabama' : 'Montgomery', 'Alaska' : 'Juneau', @@ -68,51 +51,49 @@ 'Wyoming' : 'Cheyenne', } - -def capital_of_Idaho(): - # Your code here - pass - -def all_states(): - # Your code here - pass - -def all_capitals(): - # Your code here - pass - -def states_capitals_string(): - # Your code here - pass - - - -def get_state(capital): - pass - - - -def test_state_to_capital(): - assert 'Cheyenne' == STATES_CAPITALS['Wyoming'] - - -def test_state_to_capital_unknown(): - with pytest.raises(KeyError): - STATES_CAPITALS[''] - - -def test_capital_to_state(): - assert 'Wyoming' == get_state('Cheyenne') - - -def test_capital_to_state_unknown(): - with pytest.raises(KeyError): - get_state('') - - -def main(): - return pytest.main(__file__) - - -if __name__ == '__main__': - sys.exit(main()) +# adds the city idaho +def capital_of_Idaho(STATES_CAPITALS): + print(STATES_CAPITALS["Idaho"]) +# prints all the states in the dict +def states_capitals_string(STATES_CAPITALS): + for i in STATES_CAPITALS: + print(i) +# prints all the cities in the dict +def all_capitals(STATES_CAPITALS): + for i in STATES_CAPITALS: + print(STATES_CAPITALS[i]) +# prints the items in dict in the above order using the above format +def state_capital(STATES_CAPITALS): + for i in STATES_CAPITALS: + print(f'{i} -> {STATES_CAPITALS[i]}', end=' , ') +# prints the items in dict in the above order using the above format in sorted order +def state_capital_sorted(STATES_CAPITALS): + sorted_states = sorted(STATES_CAPITALS.items()) + for i in sorted_states: + print(f'{i[0]} -> {i[1]}', end=' , ') +# function that asks the user for input of a city the function scans the dictionary items and returns the value of the key +# Also there is an if condition that checks for duplicates and if there is a duplicate prints an error +def reverse(STATES_CAPITALS): + city=input('enter the city:') + counter=0 + for key,value in STATES_CAPITALS.items(): + if value == city: + counter+=1 + state=key + if counter > 1: + print('Error - more than one state with the same capital') + elif counter==1: + print(f'The state of {city} is',state) + +# +capital_of_Idaho(STATES_CAPITALS) +print() +states_capitals_string(STATES_CAPITALS) +print() +all_capitals(STATES_CAPITALS) +print() +state_capital(STATES_CAPITALS) +print() +state_capital_sorted(STATES_CAPITALS) +print() +reverse(STATES_CAPITALS) From 351926f4432cbfbbcaef82d9652b113d054529ec Mon Sep 17 00:00:00 2001 From: Noam Daloya <90097801+nananoam123@users.noreply.github.com> Date: Tue, 12 Apr 2022 19:26:38 +0300 Subject: [PATCH 3/3] Fixed code for strings this is the fixed python script explanations inside the script for every added line of code --- strings.py | 57 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/strings.py b/strings.py index 7915338..a19231e 100644 --- a/strings.py +++ b/strings.py @@ -1,26 +1,36 @@ -"""With this string: -'monty pythons flying circus' -Create a function that returns a sorted string with no duplicate characters -(keep any whitespace): -Example: ' cfghilmnoprstuy' -Create a function that returns the words in reverse order: -Example: ['circus', 'flying', 'pythons', 'monty'] -Create a function that returns a list of 4 character strings: -Example: ['mont', 'y py', 'thon', 's fl', 'ying', ' cir', 'cus'] -### git comment -""" -import pytest -def no_duplicates(a_string): - pass +# This function removes character duplicates and creates a new sorted string +msting='monty pythons flying circus' +duplicates = [] +tstring = "" +def no_duplicates(msting): + + tstring = "" + for i in msting : + if i not in tstring and i != " " : + tstring=tstring + i + sortlist = sorted(tstring) + print("".join(sortlist)) + + + + +# A function that creates a list from the string in reverse order +def reversed_words(msting): + tlist = msting.split(" ") + tlist=tlist[::-1] + print(tlist) + + +# A function that creates a list and adds an element to the list every 4 characters +def four_char_strings(msting): + + print([msting[i:i+4] for i in range(0,len(msting),4)]) + -def reversed_words(a_string): - pass -def four_char_strings(a_string): - pass def test_no_duplicates(): @@ -38,10 +48,9 @@ def test_four_char_strings(): assert four_char_strings(s) == ['mont', 'y py', 'thon', 's fl', 'ying', ' cir', 'cus'] -def main(): - return pytest.main(__file__) - -if __name__ == '__main__': - main() - +no_duplicates(msting) +print("") +reversed_words(msting) +print("") +four_char_strings(msting)