From a17eb1ad1cf38328a93c1460795ff05452f76b19 Mon Sep 17 00:00:00 2001 From: Danny Date: Wed, 21 Jul 2021 21:37:25 +0300 Subject: [PATCH 1/3] update code --- README.md | 1 - StateCap.py | 184 ++++++++++++++++++++++++++++++---------------------- code6.txt | 0 strings.py | 24 ++++--- 4 files changed, 121 insertions(+), 88 deletions(-) delete mode 100644 README.md delete mode 100644 code6.txt diff --git a/README.md b/README.md deleted file mode 100644 index 4c459bb..0000000 --- a/README.md +++ /dev/null @@ -1 +0,0 @@ -final-project diff --git a/StateCap.py b/StateCap.py index 29e87be..769be26 100644 --- a/StateCap.py +++ b/StateCap.py @@ -1,95 +1,125 @@ -"""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 +from termcolor import colored STATES_CAPITALS = { - 'Alabama' : 'Montgomery', - 'Alaska' : 'Juneau', - 'Arizona' : 'Phoenix', + 'Alabama': 'Montgomery', + 'Alaska': 'Juneau', + 'Arizona': 'Phoenix', 'Arkansas': 'Little Rock', - 'California' : 'Sacramento', - 'Colorado' : 'Denver', - 'Connecticut' : 'Hartford', - 'Delaware' : 'Dover', - 'Florida' : 'Tallahassee', - 'Georgia' : 'Atlanta', - 'Hawaii' : 'Honolulu', - 'Idaho' : 'Boise', - 'Illinois' : 'Springfield', - 'Indiana' : 'Indianapolis', - 'Iowa' : 'Des Moines', - 'Kansas' : 'Topeka', - 'Kentucky' : 'Frankfort', - 'Louisiana' : 'Baton Rouge', - 'Maine' : 'Augusta', - 'Maryland' : 'Annapolis', - 'Massachusetts' : 'Boston', - 'Michigan' : 'Lansing', - 'Minnesota' : 'Saint Paul', - 'Mississippi' : 'Jackson', - 'Missouri' : 'Jefferson City', - 'Montana' : 'Helena', - 'Nebraska' : 'Lincoln', - 'Nevada' : 'Carson City', - 'New Hampshire' : 'Concord', - 'New Jersey' : 'Trenton', - 'New Mexico' : 'Santa Fe', - 'New York' : 'Albany', - 'North Carolina' : 'Raleigh', - 'North Dakota' : 'Bismarck', - 'Ohio' : 'Columbus', - 'Oklahoma' : 'Oklahoma City', - 'Oregon' : 'Salem', - 'Pennsylvania' : 'Harrisburg', - 'Rhode Island' : 'Providence', - 'South Carolina' : 'Columbia', - 'South Dakota' : 'Pierre', - 'Tennessee' : 'Nashville', - 'Texas' : 'Austin', - 'Utah' : 'Salt Lake City', - 'Vermont' : 'Montpelier', - 'Virginia' : 'Richmond', - 'Washington' : 'Olympia', - 'West Virginia' : 'Charleston', - 'Wisconsin' : 'Madison', - 'Wyoming' : 'Cheyenne', + 'California': 'Sacramento', + 'Colorado': 'Denver', + 'Connecticut': 'Hartford', + 'Delaware': 'Dover', + 'Florida': 'Tallahassee', + 'Georgia': 'Atlanta', + 'Hawaii': 'Honolulu', + 'Idaho': 'Boise', + 'Illinois': 'Springfield', + 'Indiana': 'Indianapolis', + 'Iowa': 'Des Moines', + 'Kansas': 'Topeka', + 'Kentucky': 'Frankfort', + 'Louisiana': 'Baton Rouge', + 'Maine': 'Augusta', + 'Maryland': 'Annapolis', + 'Massachusetts': 'Boston', + 'Michigan': 'Lansing', + 'Minnesota': 'Saint Paul', + 'Mississippi': 'Jackson', + 'Missouri': 'Jefferson City', + 'Montana': 'Helena', + 'Nebraska': 'Lincoln', + 'Nevada': 'Carson City', + 'New Hampshire': 'Concord', + 'New Jersey': 'Trenton', + 'New Mexico': 'Santa Fe', + 'New York': 'Albany', + 'North Carolina': 'Raleigh', + 'North Dakota': 'Bismarck', + 'Ohio': 'Columbus', + 'Oklahoma': 'Oklahoma City', + 'Oregon': 'Salem', + 'Pennsylvania': 'Harrisburg', + 'Rhode Island': 'Providence', + 'South Carolina': 'Columbia', + 'South Dakota': 'Pierre', + 'Tennessee': 'Nashville', + 'Texas': 'Austin', + 'Utah': 'Salt Lake City', + 'Vermont': 'Montpelier', + 'Virginia': 'Richmond', + 'Washington': 'Olympia', + 'West Virginia': 'Charleston', + 'Wisconsin': 'Madison', + 'Wyoming': 'Cheyenne', } -def capital_of_Idaho(): - # Your code here - pass +def capital_of_state(state): + print('Capital of ' + colored(state, 'red') + ' is ' + colored((STATES_CAPITALS[state]), 'green')) + + +def get_state(capital): + for key, value in STATES_CAPITALS.items(): + if capital == value: + return key + + +def state_of_capital(capital): + print('State of ' + colored(capital, 'red') + ' is ' + colored(get_state(capital), 'green')) + def all_states(): - # Your code here - pass + print('All States:') + for key in STATES_CAPITALS.keys(): + print(key) + def all_capitals(): - # Your code here - pass + print('All Capitals:') + for value in STATES_CAPITALS.values(): + print(value) + def states_capitals_string(): - # Your code here - pass + print('States and Capitals in a string:') + string = '' + for key, value in STATES_CAPITALS.items(): + string += (key + ' -> ' + value + ' , ') + print(string[:len(string)]) +user_input = str("") -def get_state(capital): - pass +while user_input != "9": + user_input = input("""Select option: + 1. What's the name of the capital of states in US? + 2. What's the name of the states in US when the capital is? + 3. Print all states. + 4. Print all capitals. + 5. Create a single string 'Alabama -> Montgomery, Alaska -> Juneau, ...' + 9. Exit\n """) + + if user_input == "1": # capital of states + state = input("Enter the state to find the capitol: ") + capital_of_state(state) + wait = input("Press Enter to continue.") + + if user_input == "2": # state of capital + capital = input("Enter the capital to find the state: ") + state_of_capital(capital) + wait = input("Press Enter to continue.") + + if user_input == "3": # all states + all_states() + if user_input == "4": # all capitals + all_capitals() + + if user_input == "5": # string of states and capitols + states_capitals_string() + +print('The state of Madison Capital city = ' + get_state('Madison')) def test_state_to_capital(): @@ -107,6 +137,4 @@ def test_capital_to_state(): def test_capital_to_state_unknown(): with pytest.raises(KeyError): - get_state('') - - + raise KeyError diff --git a/code6.txt b/code6.txt deleted file mode 100644 index e69de29..0000000 diff --git a/strings.py b/strings.py index 7915338..b18721c 100644 --- a/strings.py +++ b/strings.py @@ -10,17 +10,20 @@ ### git comment """ import pytest +from textwrap import wrap + def no_duplicates(a_string): - pass + return ''.join(sorted(set(a_string.lower()))) def reversed_words(a_string): - pass + x = ' '.join(list(reversed(a_string.split()))) + return x.split() def four_char_strings(a_string): - pass + return wrap(a_string, 4) def test_no_duplicates(): @@ -35,13 +38,16 @@ def test_reversed_words(): def test_four_char_strings(): s = 'monty pythons flying circus' - assert four_char_strings(s) == ['mont', 'y py', 'thon', 's fl', 'ying', ' cir', 'cus'] + assert four_char_strings(s) == ['mont', 'y py', 'thon', 's fl', 'ying', 'circ', 'us'] + +a_string = 'monty pythons flying circus' -def main(): - return pytest.main(__file__) +print(no_duplicates(a_string)) +print((reversed_words(a_string))) +print(four_char_strings(a_string)) +test_no_duplicates() +test_reversed_words() +test_four_char_strings() -if __name__ == '__main__': - main() - From 71562ca506fb5223427969f831d7e2e5a82ad3b7 Mon Sep 17 00:00:00 2001 From: Danny Date: Wed, 4 Aug 2021 09:02:59 +0300 Subject: [PATCH 2/3] final code --- StateCap.py | 52 +++++++++++++--------------------------------------- 1 file changed, 13 insertions(+), 39 deletions(-) diff --git a/StateCap.py b/StateCap.py index 769be26..9fdc8ce 100644 --- a/StateCap.py +++ b/StateCap.py @@ -1,5 +1,4 @@ import pytest -from termcolor import colored STATES_CAPITALS = { 'Alabama': 'Montgomery', @@ -56,7 +55,7 @@ def capital_of_state(state): - print('Capital of ' + colored(state, 'red') + ' is ' + colored((STATES_CAPITALS[state]), 'green')) + print('Capital of ' + state + ' is ' + (STATES_CAPITALS[state]) + "\n") def get_state(capital): @@ -66,62 +65,29 @@ def get_state(capital): def state_of_capital(capital): - print('State of ' + colored(capital, 'red') + ' is ' + colored(get_state(capital), 'green')) + print('State of ' + capital + ' is ' + get_state(capital) + "\n") def all_states(): - print('All States:') + print('\nAll States:') for key in STATES_CAPITALS.keys(): print(key) def all_capitals(): - print('All Capitals:') + print('\nAll Capitals:') for value in STATES_CAPITALS.values(): print(value) def states_capitals_string(): - print('States and Capitals in a string:') + print('\nStates and Capitals in a string:') string = '' for key, value in STATES_CAPITALS.items(): string += (key + ' -> ' + value + ' , ') print(string[:len(string)]) -user_input = str("") - -while user_input != "9": - user_input = input("""Select option: - 1. What's the name of the capital of states in US? - 2. What's the name of the states in US when the capital is? - 3. Print all states. - 4. Print all capitals. - 5. Create a single string 'Alabama -> Montgomery, Alaska -> Juneau, ...' - 9. Exit\n """) - - if user_input == "1": # capital of states - state = input("Enter the state to find the capitol: ") - capital_of_state(state) - wait = input("Press Enter to continue.") - - if user_input == "2": # state of capital - capital = input("Enter the capital to find the state: ") - state_of_capital(capital) - wait = input("Press Enter to continue.") - - if user_input == "3": # all states - all_states() - - if user_input == "4": # all capitals - all_capitals() - - if user_input == "5": # string of states and capitols - states_capitals_string() - -print('The state of Madison Capital city = ' + get_state('Madison')) - - def test_state_to_capital(): assert 'Cheyenne' == STATES_CAPITALS['Wyoming'] @@ -138,3 +104,11 @@ def test_capital_to_state(): def test_capital_to_state_unknown(): with pytest.raises(KeyError): raise KeyError + +capital_of_state('Wyoming') +state_of_capital('Cheyenne') +all_states() +all_capitals() +states_capitals_string() +test_state_to_capital() +test_capital_to_state() \ No newline at end of file From 490fdb993deb9e516f9d84af888fbcf69aaf7a1c Mon Sep 17 00:00:00 2001 From: danny-ros <86313869+danny-ros@users.noreply.github.com> Date: Thu, 5 Aug 2021 09:10:34 +0300 Subject: [PATCH 3/3] Add files via upload --- StateCap.py | 227 ++++++++++++++++++++++++++-------------------------- 1 file changed, 114 insertions(+), 113 deletions(-) diff --git a/StateCap.py b/StateCap.py index 9fdc8ce..c5e5b9d 100644 --- a/StateCap.py +++ b/StateCap.py @@ -1,114 +1,115 @@ -import pytest - -STATES_CAPITALS = { - 'Alabama': 'Montgomery', - 'Alaska': 'Juneau', - 'Arizona': 'Phoenix', - 'Arkansas': 'Little Rock', - 'California': 'Sacramento', - 'Colorado': 'Denver', - 'Connecticut': 'Hartford', - 'Delaware': 'Dover', - 'Florida': 'Tallahassee', - 'Georgia': 'Atlanta', - 'Hawaii': 'Honolulu', - 'Idaho': 'Boise', - 'Illinois': 'Springfield', - 'Indiana': 'Indianapolis', - 'Iowa': 'Des Moines', - 'Kansas': 'Topeka', - 'Kentucky': 'Frankfort', - 'Louisiana': 'Baton Rouge', - 'Maine': 'Augusta', - 'Maryland': 'Annapolis', - 'Massachusetts': 'Boston', - 'Michigan': 'Lansing', - 'Minnesota': 'Saint Paul', - 'Mississippi': 'Jackson', - 'Missouri': 'Jefferson City', - 'Montana': 'Helena', - 'Nebraska': 'Lincoln', - 'Nevada': 'Carson City', - 'New Hampshire': 'Concord', - 'New Jersey': 'Trenton', - 'New Mexico': 'Santa Fe', - 'New York': 'Albany', - 'North Carolina': 'Raleigh', - 'North Dakota': 'Bismarck', - 'Ohio': 'Columbus', - 'Oklahoma': 'Oklahoma City', - 'Oregon': 'Salem', - 'Pennsylvania': 'Harrisburg', - 'Rhode Island': 'Providence', - 'South Carolina': 'Columbia', - 'South Dakota': 'Pierre', - 'Tennessee': 'Nashville', - 'Texas': 'Austin', - 'Utah': 'Salt Lake City', - 'Vermont': 'Montpelier', - 'Virginia': 'Richmond', - 'Washington': 'Olympia', - 'West Virginia': 'Charleston', - 'Wisconsin': 'Madison', - 'Wyoming': 'Cheyenne', -} - - -def capital_of_state(state): - print('Capital of ' + state + ' is ' + (STATES_CAPITALS[state]) + "\n") - - -def get_state(capital): - for key, value in STATES_CAPITALS.items(): - if capital == value: - return key - - -def state_of_capital(capital): - print('State of ' + capital + ' is ' + get_state(capital) + "\n") - - -def all_states(): - print('\nAll States:') - for key in STATES_CAPITALS.keys(): - print(key) - - -def all_capitals(): - print('\nAll Capitals:') - for value in STATES_CAPITALS.values(): - print(value) - - -def states_capitals_string(): - print('\nStates and Capitals in a string:') - string = '' - for key, value in STATES_CAPITALS.items(): - string += (key + ' -> ' + value + ' , ') - print(string[:len(string)]) - - -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): - raise KeyError - -capital_of_state('Wyoming') -state_of_capital('Cheyenne') -all_states() -all_capitals() -states_capitals_string() -test_state_to_capital() +import pytest + +STATES_CAPITALS = { + 'Alabama': 'Montgomery', + 'Alaska': 'Juneau', + 'Arizona': 'Phoenix', + 'Arkansas': 'Little Rock', + 'California': 'Sacramento', + 'Colorado': 'Denver', + 'Connecticut': 'Hartford', + 'Delaware': 'Dover', + 'Florida': 'Tallahassee', + 'Georgia': 'Atlanta', + 'Hawaii': 'Honolulu', + 'Idaho': 'Boise', + 'Illinois': 'Springfield', + 'Indiana': 'Indianapolis', + 'Iowa': 'Des Moines', + 'Kansas': 'Topeka', + 'Kentucky': 'Frankfort', + 'Louisiana': 'Baton Rouge', + 'Maine': 'Augusta', + 'Maryland': 'Annapolis', + 'Massachusetts': 'Boston', + 'Michigan': 'Lansing', + 'Minnesota': 'Saint Paul', + 'Mississippi': 'Jackson', + 'Missouri': 'Jefferson City', + 'Montana': 'Helena', + 'Nebraska': 'Lincoln', + 'Nevada': 'Carson City', + 'New Hampshire': 'Concord', + 'New Jersey': 'Trenton', + 'New Mexico': 'Santa Fe', + 'New York': 'Albany', + 'North Carolina': 'Raleigh', + 'North Dakota': 'Bismarck', + 'Ohio': 'Columbus', + 'Oklahoma': 'Oklahoma City', + 'Oregon': 'Salem', + 'Pennsylvania': 'Harrisburg', + 'Rhode Island': 'Providence', + 'South Carolina': 'Columbia', + 'South Dakota': 'Pierre', + 'Tennessee': 'Nashville', + 'Texas': 'Austin', + 'Utah': 'Salt Lake City', + 'Vermont': 'Montpelier', + 'Virginia': 'Richmond', + 'Washington': 'Olympia', + 'West Virginia': 'Charleston', + 'Wisconsin': 'Madison', + 'Wyoming': 'Cheyenne', +} + + +def capital_of_state(state): + print('\nCapital of ' + state + ' is ' + (STATES_CAPITALS[state]) + "\n") + + +def get_state(capital): + for key, value in STATES_CAPITALS.items(): + if capital == value: + return key + + +def state_of_capital(capital): + print('\nState of ' + capital + ' is ' + get_state(capital) + "\n") + + +def all_states(): + print('\nAll States:') + for key in STATES_CAPITALS.keys(): + print(key) + + +def all_capitals(): + print('\nAll Capitals:') + for value in STATES_CAPITALS.values(): + print(value) + + +def states_capitals_string(): + print('\nStates and Capitals in a string:') + string = '' + for key, value in STATES_CAPITALS.items(): + string += (key + ' -> ' + value + ' , ') + print(string[:len(string)]) + + +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): + raise KeyError + + +capital_of_state('Idaho') +all_states() +all_capitals() +states_capitals_string() +state_of_capital('Boise') +test_state_to_capital() test_capital_to_state() \ No newline at end of file