submission

py

School

San Jose State University *

*We aren’t endorsed by this school

Course

256

Subject

Computer Science

Date

Dec 6, 2023

Type

py

Pages

3

Uploaded by MagistrateProtonKookabura32

Report
import collections import math from typing import Any, DefaultDict, List, Set, Tuple ############################################################ # Custom Types # NOTE: You do not need to modify these. """ You can think of the keys of the defaultdict as representing the positions in the sparse vector, while the values represent the elements at those positions. Any key which is absent from the dict means that that element in the sparse vector is absent (is zero). Note that the type of the key used should not affect the algorithm. You can imagine the keys to be integer indices (e.g., 0, 1, 2) in the sparse vectors, but it should work the same way with arbitrary keys (e.g., "red", "blue", "green"). """ SparseVector = DefaultDict[Any, float] Position = Tuple[int, int] ############################################################ # Problem 4a def find_alphabetically_first_word(text: str) -> str: """ Given a string |text|, return the word in |text| that comes first lexicographically (i.e., the word that would come first after sorting). A word is defined by a maximal sequence of characters without whitespaces. You might find min() handy here. If the input text is an empty string, it is acceptable to either return an empty string or throw an error. """ # BEGIN_YOUR_CODE words = text.split() if not words: return "" lex_first_word = min(words) return lex_first_word # END_YOUR_CODE ############################################################ # Problem 4b def euclidean_distance(loc1: Position, loc2: Position) -> float: """ Return the Euclidean distance between two locations, where the locations are pairs of numbers (e.g., (3, 5)). """ # BEGIN_YOUR_CODE return ((loc1[0] - loc2[0])**2 + (loc1[1] - loc2[1])**2)**0.5 # END_YOUR_CODE
############################################################ # Problem 4c def sparse_vector_dot_product(v1: SparseVector, v2: SparseVector) -> float: """ Given two sparse vectors (vectors where most of the elements are zeros) |v1| and |v2|, each represented as collections.defaultdict(float), return their dot product. You might find it useful to use sum() and a list comprehension. This function will be useful later for linear classifiers. Note: A sparse vector has most of its entries as 0. """ # BEGIN_YOUR_CODE return sum(v1[i] * v2[i] for i in v1) # END_YOUR_CODE ############################################################ # Problem 4d def increment_sparse_vector(v1: SparseVector, scale: float, v2: SparseVector, ) -> None: """ Given two sparse vectors |v1| and |v2|, perform v1 += scale * v2. If the scale is zero, you are allowed to modify v1 to include any additional keys in v2, or just not add the new keys at all. NOTE: This function should MODIFY v1 in-place, but not return it. Do not modify v2 in your implementation. This function will be useful later for linear classifiers. """ # BEGIN_YOUR_CODE if scale == 0: return for key in v2: if key in v1: v1[key] += scale * v2[key] else: v1[key] = scale * v2[key] # END_YOUR_CODE ############################################################ # Problem 4e def find_nonsingleton_words(text: str) -> Set[str]: """ Split the string |text| by whitespace and return the set of words that occur more than once. You might find it useful to use collections.defaultdict(int). """ # BEGIN_YOUR_CODE c = collections.Counter(text.split(' ')) return set(word for word in c if c[word] > 1)
# END_YOUR_CODE
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help