Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • master
1 result

Target

Select target project
  • cp/02002students
  • cp/02002-students-internal
2 results
Select Git revision
  • master
1 result
Show changes
Showing
with 265 additions and 0 deletions
"""DO NOT EDIT/ADD TO THIS FILE."""
"""Exercise 6.5: Languages."""
def get_people_by_language(language: str, name_languages: dict)-> list:
"""Return the names of people who speak a specific language.
:param language: A string containing the desired language.
:param name_languages: A dictionary containing the names of people along with their spoken languages
:return: The names of people that speak the desired language.
"""
# TODO: Code has been removed from here.
if __name__ == "__main__":
name_languages = {
'Peter': ['Danish', 'English'],
'Alice': ['English', 'French'],
'John': ['Spanish', 'English'],
'Maria': ['Danish', 'Spanish'],
'Anna': ['German', 'English']
}
print(get_people_by_language('English', name_languages))
"""Exercise 6.2: Letter histogram."""
def letter_histogram(input_string : str) -> dict:
"""Return the histogram of letter occurrences.
:param input_string: The word based on which the letter histogram is calculated.
:return: The alphabet characters as keys with their corresponding occurrences as values.
"""
# TODO: Code has been removed from here.
if __name__ == "__main__":
# here you can try out your functions
print("What is the letter histogram of the word banana?", letter_histogram('banana'))
"""Exercise 6.9: Multi-tap."""
def multi_tap(keys : list, times : list) -> str:
"""Return the string corresponding to the multi-tap key presses.
:param keys: The list of keys pressed.
:param times: The list of times of when the keys were pressed.
:return: The string corresponding to the key presses.
"""
# TODO: Code has been removed from here.
if __name__ == "__main__":
keys = [7, 7, 7, 7, 6, 6, 6]
times = [0, 0.7, 0.8, 0.9, 1, 1.1, 1.2]
print(multi_tap(keys, times), '==', 'PRO')
"""Exercise 6.1: The NATO alphabet."""
def text_to_nato(plaintext : str) -> str:
"""Return the NATO version of a word separated by dashes.
:param plaintext: The word to replace with its phrase according to the NATO alphabet.
:return: The NATO representation of the input word.
"""
# TODO: Code has been removed from here.
if __name__ == "__main__":
# here you can try out your functions
print("What is the NATO representation of hello?", text_to_nato('hello'))
"""Exercise 6.7: Sentiment analysis."""
def sentiment_analysis(text: str) -> int:
"""Return the sentence sentiment score, according to the rules of words scoring, as described in the text above.
:param text: The sentence to check for sentiment scoring.
:return: The total sentiment scoring of the sentence.
"""
# TODO: Code has been removed from here.
if __name__ == "__main__":
# here you can try out your functions
text='I think the food was excellent and great, but the service was horrible '
sentiment_analysis(text)
"""Exercise 6.8: Spell check."""
def spell_check(text: str, corrections: dict) -> str:
"""Return the corrected text for spelling errors according to a set of rules.
:param text: The sentence to check for spelling.
:param corrections: The dictionary of wrongly spelled words and their equivalent corrected version.
:return: The correctly spelled sentence.
"""
# TODO: Code has been removed from here.
if __name__ == "__main__":
# here you can try out your functions
corrections = {
'apsolute': 'absolute',
'teh': 'the',
'acess': 'access',
'occured': 'occurred',
'exampel': 'example'
}
text = "The apsolute acsess to teh data occured in this exampel."
print(spell_check(text, corrections))
"""Exercise 6.6: Truncate and normalize."""
def truncate_values(float_list : list, settings: dict) -> list:
"""Return a truncated list of floats given the initial list and the settings for truncating. If normalize is True, the values are first normalized to the [0,1] range and then truncated.
:param float_list: list of floats
:param settings: Dictionary containing the keys vmin, vmax and normalize with their corresponding values.
:return: Truncated/Normalized+Truncated values.
"""
# TODO: Code has been removed from here.
if __name__ == "__main__":
# here you can try out your functions
settings = {'vmin': 0, 'vmax': 2, 'normalize': False}
float_list = [0.5,0.4,-0.3, 1.5, 2.5, 3.5]
result=truncate_values(float_list=float_list,settings=settings)
print(result)
"""Exercise 6.3-6.4."""
def word_histogram(lines : list) -> dict:
"""Return the word count histogram from the input lines.
:param lines: The lines that are analyzed for word count.
:return: The histogram of word occurrences.
"""
# TODO: Code has been removed from here.
def extract_keyword(lines : str, ignore_list : list) -> dict:
"""Return the five most frequent words that are not on the ignore list and their occurrences.
:param lines: The sentence to extract keywords from.
:param ignore_list: The words that should ignored.
:return: The five most frequent words in the sentence as keys with their count as values.
"""
# TODO: Code has been removed from here.
if __name__ == "__main__":
# here you can try out your functions
lines = ['This is the first sentence of text for you', 'This is the second sentence of text', 'This is for you']
print("word_histogram")
print(word_histogram(lines))
# Ignore list of common words
ignore_list = ['the', 'be', 'to', 'of', 'and', 'a', 'in', 'is', 'have', 'I']
# Print the 5 most occurring keywords
print("extract_keywords")
print(extract_keyword(lines, ignore_list))
"""DO NOT EDIT/ADD TO THIS FILE."""
"""Exercise 7.9: Astronomical season."""
def astronomical_season(date : tuple) -> str:
"""Return the astronomical season of the given date.
:param date: Tuple with the given date.
:return: String with astronomical season
"""
# TODO: Code has been removed from here.
"""Exercise 7.5: Box packing."""
def box_packing(object : tuple, box : tuple)-> tuple:
"""Return the amount of object sticking in each dimension, or zero if sufficient space.
:param object: Tuple (h,w) the dimensions of the object
:param box: Tuple (H, W) the dimensions of the box.
:return: Tuple
"""
# TODO: Code has been removed from here.
"""Exercise 7.7: Code shift."""
def code_shift(code : tuple, turn : tuple) -> tuple:
"""Return the updated code pattern.
:param code: Tuple with the initial code in the lock
:param turn: Tuple with the turn on each lock dial
:return: Updated lock code.
"""
# TODO: Code has been removed from here.
"""Exercise 7.1: Have equal length."""
def have_equal_length(a : tuple, b : tuple) -> bool:
"""Check whether two tuples have equal length.
:param a: Tuple.
:param b: Tuple.
:return: A boolean value.
"""
# TODO: Code has been removed from here.
"""Exercise 7.3: Last difference."""
def last_difference(a : tuple, b : tuple) -> float:
"""Return the difference between last elements regardless of length.
:param a: The first tuple.
:param b: The second tuple.
:return: The difference between the last elements of the two tuples.
"""
# TODO: Code has been removed from here.
"""Exercise 7.8: Morse code."""
def morse_to_text(morse_code : str) -> str:
"""Return the extracted message from its Morse code.
:param morse_code: String with the initial message encoded in Morse.
:return: The decoded message.
"""
# TODO: Code has been removed from here.
"""Exercise 7.4: Returning multiple values."""
def returning_multiple_values(values: list, threshold: int) -> tuple:
"""Return a tuple containing a list of all elements in the list that are greater than the threshold and the minimum of the values.
:param values: A list of integers.
:param threshold: An integer.
:return: A tuple containing the a list of elements that are greater than the threshold and the minimum of values
"""
# TODO: Code has been removed from here.
"""Exercise 7.4: Color Hue."""
def rgb_to_hue(RGB: tuple) -> float:
"""Return the hue given RGB values.
:param RGB: Tuple containing three RGB values.
:return: Hue in degrees.
"""
# TODO: Code has been removed from here.
"""DO NOT EDIT/ADD TO THIS FILE."""
"""Exercise 8.4: Counting."""
from cp.ex08.loading import load_txt2str
def count_words(string) -> int:
"""Count the number of words in a string.
:param string: A string.
:return: The number of words in the string.
"""
# TODO: Code has been removed from here.
def count_letters(string) -> int:
"""Count the number of letters in a string.
:param string: A string.
:return: The number of letters in the string without spaces.
"""
# TODO: Code has been removed from here.
def count_words_letters(path: str) -> tuple:
"""Count the number of words and letters in a text file.
This function takes a file path as input, reads the content of the text file
located at the specified path, and returns the number of words and letters in
the text file as a tuple.
:param path: A string representing the path to the text file.
:return: A tuple containing the number of words and letters in the text file.
"""
# TODO: Code has been removed from here.