migrate to git.charlotte.sh
This commit is contained in:
commit
fbd588721e
412 changed files with 13750 additions and 0 deletions
1768
python-csi160/week10/alice.py
Normal file
1768
python-csi160/week10/alice.py
Normal file
File diff suppressed because it is too large
Load diff
39
python-csi160/week10/part1.py
Normal file
39
python-csi160/week10/part1.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
|
||||
# Step 1. Create dictionary called 'user'
|
||||
# with the following keys: 'name' and 'location'
|
||||
# Set the name to 'John Doe'
|
||||
# Set the location to 'Burlington, VT'
|
||||
|
||||
user = {
|
||||
'name': 'John Doe',
|
||||
'location': 'Burlington, VT'
|
||||
}
|
||||
|
||||
# Step 2. Ask the user for the user's age
|
||||
# and create a key/value pair in user to store this
|
||||
# called 'age'
|
||||
try:
|
||||
age = input(f"How old is {user['name']}? ")
|
||||
except KeyError:
|
||||
print('"name" not defined as a key in Step 1')
|
||||
else:
|
||||
user['age'] = int(age)
|
||||
print(f"{user['name']} is {user['age']} years old.")
|
||||
|
||||
|
||||
# Step 3
|
||||
# Create keys "first_name" and "last_name"
|
||||
# Use .split() to seperate name into firstname and last_name
|
||||
# Remove the key "name" from the dictionary
|
||||
|
||||
first_name, last_name = user['name'].split()
|
||||
user['first_name'] = first_name
|
||||
user['last_name'] = last_name
|
||||
del user['name']
|
||||
print(f"First name: {user['first_name']}")
|
||||
print(f"Last name: {user['last_name']}")
|
||||
|
||||
|
||||
# For testing (do not delete)
|
||||
print('Printing "user":')
|
||||
print(user)
|
25
python-csi160/week10/part2.py
Normal file
25
python-csi160/week10/part2.py
Normal file
|
@ -0,0 +1,25 @@
|
|||
def synonym_lookup(word_pairs, word_to_lookup):
|
||||
"""Returns synonym for the word to word_to_lookup
|
||||
|
||||
Complete this function by using a dictionary. For each synonym pair,
|
||||
you should add both words as keys to your dictionary with the value being the other word.
|
||||
|
||||
params:
|
||||
word_pairs (tuple of tuples): Example: (('Hello', 'Hi'), ('Goodbye','Bye'))
|
||||
word_to_lookup (string): The word to find a synonym for
|
||||
return (string): The synonym for the word_to_lookup
|
||||
"""
|
||||
synonym_dict = {}
|
||||
|
||||
# Create a dictionary with both words in each pair
|
||||
for word1, word2 in word_pairs:
|
||||
synonym_dict[word1] = word2
|
||||
synonym_dict[word2] = word1
|
||||
|
||||
# Return the synonym
|
||||
return synonym_dict.get(word_to_lookup, None)
|
||||
|
||||
|
||||
example_word_pairs = (('Hello', 'Hi'), ('Goodbye','Bye'), ('Snake', 'Serpent'))
|
||||
|
||||
print(f'A synonym for "Serpent" is {synonym_lookup(example_word_pairs, "Serpent")}. (Snake expected)')
|
38
python-csi160/week10/part3.py
Normal file
38
python-csi160/week10/part3.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
final_election_results = {}
|
||||
|
||||
def record_candidate_votes(election_results, candidate, num_votes, precinct):
|
||||
"""Records votes for a particular candidate in one precinct in the election_results.
|
||||
|
||||
The precinct is not used for the function currently.
|
||||
|
||||
Optional tip: Using method .get() can allow you to avoid needing a if or try statement
|
||||
|
||||
params:
|
||||
election_results (dict): Dictionary to be modified by recording the votes for a candidate in a precinct
|
||||
cadidate (string): The name of a candidate. Used as the 'key' in the election_results dictionary.
|
||||
num_votes (int): The number of votes to add to the candidate's total
|
||||
"""
|
||||
if candidate in election_results:
|
||||
# If candidate exists, add to the existing total
|
||||
election_results[candidate] += num_votes
|
||||
else:
|
||||
# If candidate doesn't exist, create a new entry
|
||||
election_results[candidate] = num_votes
|
||||
|
||||
|
||||
|
||||
# This section is for testing purposes, do not modify
|
||||
# FYI: These numbers are made up for example purposes only
|
||||
record_candidate_votes(final_election_results,'Miro Weinberger', 100, 'Ward 1')
|
||||
record_candidate_votes(final_election_results,'Max Tracy', 140, 'Ward 1')
|
||||
record_candidate_votes(final_election_results,'Ali Dieng', 27, 'Ward 1')
|
||||
record_candidate_votes(final_election_results,'Miro Weinberger', 50, 'Ward 2')
|
||||
record_candidate_votes(final_election_results,'Max Tracy', 150, 'Ward 2')
|
||||
record_candidate_votes(final_election_results,'Ali Dieng', 35, 'Ward 2')
|
||||
record_candidate_votes(final_election_results,'Miro Weinberger', 100, 'Ward 3')
|
||||
record_candidate_votes(final_election_results,'Max Tracy', 100, 'Ward 3')
|
||||
record_candidate_votes(final_election_results,'Ali Dieng', 56, 'Ward 3')
|
||||
record_candidate_votes(final_election_results,'Miro Weinberger', 320, 'Ward 4')
|
||||
record_candidate_votes(final_election_results,'Max Tracy', 213, 'Ward 4')
|
||||
record_candidate_votes(final_election_results,'Ali Dieng', 175, 'Ward 4')
|
||||
print(final_election_results)
|
45
python-csi160/week10/part4.py
Normal file
45
python-csi160/week10/part4.py
Normal file
|
@ -0,0 +1,45 @@
|
|||
import string
|
||||
from alice import alice_text
|
||||
|
||||
def word_frequency(text):
|
||||
"""Generates a word frequency dictionary for the supplied text. All punctuation is stripped and the case is converted to lowercase.
|
||||
|
||||
Example dictionary: {'the': 2, 'cat': 1, 'bit': 1, 'dog': 1}
|
||||
|
||||
param text: (string) The text to be analyzed
|
||||
return: (dict) Word frequency dictionary
|
||||
"""
|
||||
# Strip Punctuation
|
||||
for character in string.punctuation:
|
||||
text = text.replace(character, "")
|
||||
|
||||
# Convert to lower case
|
||||
text = text.lower()
|
||||
|
||||
# Generate a list of all of the words
|
||||
words = text.split()
|
||||
|
||||
word_frequency = {}
|
||||
for word in words:
|
||||
if word in word_frequency:
|
||||
word_frequency[word] += 1
|
||||
else:
|
||||
word_frequency[word] = 1
|
||||
return word_frequency
|
||||
|
||||
|
||||
# Code for testing purposes. Do not modify
|
||||
short_sentence = "The silly cat is the silly face."
|
||||
print(f'Analyzing word frequency of "{short_sentence}"')
|
||||
print(word_frequency("The silly cat is the silly face."))
|
||||
|
||||
print()
|
||||
try:
|
||||
print('In Alice and Wonderland,')
|
||||
alice_freq = word_frequency(alice_text)
|
||||
print(f'The word Alice occurs {alice_freq["alice"]} times')
|
||||
print(f'The word melancholy occurs {alice_freq["melancholy"]} times')
|
||||
print(f'{len(alice_freq)} different words are used in the book.')
|
||||
except Exception as e:
|
||||
print('Exception raised when analyzing Alice in Wonderland:')
|
||||
print(e)
|
Loading…
Add table
Add a link
Reference in a new issue