Übung 2 - Zoltan
4 unresolved threads
4 unresolved threads
Merge request reports
Activity
Filter activity
Output für
granatapfel
:Das Wort granatapfel enthält 11 Zeichen Granatapfel [103, 114, 97, 110, 97, 116, 97, 112, 102, 101, 108] {'g': 1, 'r': 1, 'a': 3, 'n': 1, 't': 1, 'p': 1, 'f': 1, 'e': 1, 'l': 1}
Output für
Lauch
:Das Wort lauch enthält 5 Zeichen Lauch [108, 97, 117, 99, 104] {'l': 1, 'a': 1, 'u': 1, 'c': 1, 'h': 1}
- Uebung_2/word_impl.py 0 → 100644
1 """Aufgabe 2 - Implementierung.""" 2 3 4 class Word: 5 """In this case, actually implement the functions.""" 6 7 string_representation = "" 8 9 def __init__(self, input_string): 10 """Let the string be determined by input.""" 11 self.string_representation = input_string 12 13 def number_of_characters(self, string_representation): 14 """Iterate through the string and add one value/letter to counter.""" changed this line in version 10 of the diff
- Uebung_2/word_impl.py 0 → 100644
40 sentence = sentence.replace("ß", "ss").replace("ä", "ae") 41 if count_all: 42 alphabet = "abcdefghijklmnopqrstuvwxyz " 43 for letter in alphabet: 44 letters_output[letter] = 0 45 else: 46 for n in sentence: 47 keys = letters_output.keys() 48 if n in keys: 49 letters_output[n] += 1 50 else: 51 letters_output[n] = 1 52 return letters_output 53 54 55 def main(): - Resolved by Zoltan Csik
added 2 commits
- Uebung_2/word_impl.py 0 → 100644
1 """Aufgabe 2 - Implementierung.""" 2 3 4 class Word: 5 """Implement the following in-class functions. 6 7 --> number_of_characters to count the number of characters. 8 --> capitalize(): return the input string with the first letter capitalized 9 --> get_ascii(): return the ASCII realization of each character. 10 --> histogram(): return the number of occurrences for each letter. 11 """ 12 13 string_representation = "" 14 15 def __init__(self, input_string): 16 """Determine string based on input.""" for the docstrings, pycharm has a nice built-in function that automatically creates a skeleton like this, with the parameter names according to the PEP8 guidelines - not sure which editor you are using, maybe there is a similar functionality?
""" bla bla description :param count_all: default mode is False, if set to True, all characters of the alphabet will be included :return: histogram containing the number of occurrences of each letter """
Im using Visual Studio Code but it should have the same option. I'll look into that. Thank you!
Edited by Zoltan Csik
- Uebung_2/word_impl.py 0 → 100644
50 51 --> special_chars TRUE: ount special_chars [ö,ä,ü,ß] 52 --> special_chars FALSE: replace special characters with ae/oe/ue/ss 53 --> create letters_output{}: dictionary containing the results. 54 --> count_all TRUE: Use the predefined alphabet and update it. 55 --> count_all FALSE: only add letters of the given string to dict. 56 --> return value: letters_output dictionary, 57 - With updated key/value pairs 58 """ 59 sentence = self.string_representation.lower() 60 letters_output = {} 61 if not special_chars: 62 sentence = sentence.replace("ö", "oe").replace("ü", "ue") 63 sentence = sentence.replace("ß", "ss").replace("ä", "ae") 64 if count_all: 65 alphabet = "abcdefghijklmnopqrstuvwxyz " There is a nice method which could be used to create a list containig the letters of the alphabet:
string.ascii_lowercase
, so you could putalphabet = list(string.ascii_lowercase)
... if you wanted to save some writing :)Edited by Zoltan Csik
mentioned in commit 3f3d784c