Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md

This file was deleted.

227 changes: 115 additions & 112 deletions StateCap.py
Original file line number Diff line number Diff line change
@@ -1,112 +1,115 @@
"""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',
'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_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('')


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()
Empty file removed code6.txt
Empty file.
24 changes: 15 additions & 9 deletions strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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()