csc-665-homework/hw0/hw0.py
2024-02-09 00:43:11 -08:00

59 lines
1.6 KiB
Python

import collections
import math
from typing import List, Set, Tuple
############################################################
# Custom Type
# NOTE: You should not modify this.
Position = Tuple[int, int]
############################################################
# Problem 5a
def find_alphabetically_first_word(text: str) -> str:
return min(text.split())
############################################################
# Problem 5b
def euclidean_distance(loc1: Position, loc2: Position) -> float:
x1, y1 = loc1
x2, y2 = loc2
return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
############################################################
# Problem 5c
def find_non_singleton_words(text: str) -> Set[str]:
word_count_lol = collections.Counter(text.split())
return {word for word, count in word_count_lol.items() if count > 1}
############################################################
# Problem 5d
def mutate_sentences(sentence: str) -> List[str]:
words_in_sentence = sentence.split()
similar_sentences = []
if len(words_in_sentence) < 2:
return similar_sentences
for i in range(len(words_in_sentence) - 1):
for j in range(i + 1, len(words_in_sentence)):
new_sentence = words_in_sentence[:i] + [words_in_sentence[i], words_in_sentence[j]] + words_in_sentence[j + 1:]
similar_sentences.append(' '.join(new_sentence))
return similar_sentences
if __name__ == "__main__":
# feel free to modify this for testing purposes
# print(mutate_sentences('the cat and the mouse'))
print(find_alphabetically_first_word('hello how are you my apple mango banana'))