Skip to content
Snippets Groups Projects

Übung 2 - Zoltan

Merged Zoltan Csik requested to merge Ubeung2 into master
4 unresolved threads

Merge request reports

Merged by Zoltan CsikZoltan Csik 3 years ago (Nov 27, 2021 8:32am UTC)

Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
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."""
  • 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():
  • Zoltan Csik added 1 commit

    added 1 commit

    Compare with previous version

  • Zoltan Csik added 2 commits

    added 2 commits

    Compare with previous version

  • 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
          """
    • Author Owner

      Im using Visual Studio Code but it should have the same option. I'll look into that. Thank you! :smiley_cat:

      Edited by Zoltan Csik
    • Please register or sign in to reply
  • 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 put alphabet = list(string.ascii_lowercase) ... if you wanted to save some writing :)

      Edited by Zoltan Csik
    • Author Owner

      Yes, i thought about that but i think you need the string library for that? I wasn't sure if it's okay to import it :thinking:

    • Ah yes, we were not 100% sure if this library was allowed or not - true that there was some comment about the use of libraries in the task. However, if you're aware of this option, all good anyway - that's what matters! :)

    • Please register or sign in to reply
  • merged

  • Zoltan Csik mentioned in commit 3f3d784c

    mentioned in commit 3f3d784c

  • Please register or sign in to reply
    Loading