Skip to main content
WueCampus
  • Nützliche Links
    Veranstaltungssuche
    Veranstaltungssuche
    Rechenzentrum
    Rechenzentrum
    Häufige Fragen
    Häufige Fragen
    Lehre digital
    Lehre digital
    Forschung digital
    Forschung digital
    Lecture - Videoupload
    Lecture - Videoupload
    CaseTrain
    CaseTrain
    Toolbox
    Toolbox
  • Calendar
  • More
English ‎(en)‎
Català ‎(ca)‎ Deutsch ‎(de_kids)‎ Deutsch ‎(de_wp)‎ Deutsch ‎(de)‎ Deutsch (du) ‎(de_du)‎ English ‎(en)‎ Español - Internacional ‎(es)‎ Français ‎(fr)‎ Italiano ‎(it)‎ Português - Portugal ‎(pt)‎ Svenska ‎(sv)‎ Türkçe ‎(tr)‎ Русский ‎(ru)‎ العربية ‎(ar)‎
You are currently using guest access
Log in
WueCampus
Calendar Nützliche Links Collapse Expand
Veranstaltungssuche Rechenzentrum Häufige Fragen Lehre digital Forschung digital Lecture - Videoupload CaseTrain Toolbox
Kursantrag Collapse Expand
Kursantrag: WS 25/26 Kursantrag: SS 25 Semesterübergreifend
Expand all Collapse all
  1. Home
  2. WS23_DS4DH1
  3. Part 2: (Re-)introduction to Python
  4. Exercise 1: Python basics

Exercise 1: Python basics

Completion requirements

Exercise 1: Python basics


from typing import List, Tuple, Union, Dict

with open("names.txt") as f:
    names = [line.strip() for line in f if line]
print(f"Number of names: {len(names)}")
print(names[::1000])

class NamesDataset:
    """
    Implement all methods in this class,
    such that the output type fulfills the type annotations
    and their behavior follows the description in the doc-strings. 
    """
    def __init__(self, names: List[str]):
        """
        The constructor must save the names as an attribute.
        """
    
    def query(self, prefix: str) -> List[str]:
        """
        The query method takes in a prefix and returns a list 
        of all names in the dataset that start with the given prefix.
        """
    
    def longest_name(self, prefix: str = None, return_len: bool = False) -> Union[str, Tuple[str, int]]:
        """
        The longest_name method returns the longest name in the dataset.
        
        It also must support filtering the names by the prefix.
        If a prefix is given, than it should return the longest name, starting with the prefix
        
        Additionally, if the return_len argument is True, it should output the longest name. 
        AND its length (as tuple). 
        """
    
    def mean_name_length(self) -> float:
        """
        This method computes the mean length of all names in the dataset.
        """
    
    def ngrams(self, size: int, prefix: str = None) -> List[str]:
        """
        This method returns a list of all character n_grams of all names (or those starting with a given prefix).
        The size determines the size of the n_grams.
        """
        
    def count_ngrams(self, size: int, prefix: str = None) -> List[Tuple[str, int]]:
        """
        This method returns a list of tuples containing all n_grams created with the given the arguments
        and their frequency within all n_grams found under the parameters.
        The list should be sorted in ascending order.
        """
  • names.txt names.txt
    12 October 2023, 1:56 PM
◄ Kick-off slides
Notebook: Basics 2 (HTML-Version) ►

Impressum  |  Kontakt  |  Datenschutzerklärung - WueCampus  |  Erklärung zur Barrierefreiheit  |  Bildnachweise

Navigationsleiste - WueStudy: University icons created by justicon - Flaticon
Navigationsleiste - Rechenzentrum: Data center icons created by Eucalyp - Flaticon
Navigationsleiste - Website Support: Consultant icons created by Vitaly Gorbachev - Flaticon
Navigationsleiste - Häufige Fragen: Files and folders icons created by Freepik - Flaticon
Navigationsleiste - Lehre Digital: Training icons created by vectorspoint - Flaticon
Navigationsleiste - Forschung Digital: Research icons created by Eucalyp - Flaticon
Navigationsleiste - Lecture: Video icons created by Freepik - Flaticon
Navigationsleiste - Toolbox: Toolbox icons created by Freepik - Flaticon