Advertisement
SimeonTs

SUPyF2 Dict-Exercise - 01. Count Chars in a String

Oct 24th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. """
  2. Dictionaries - Exercise
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1737#0
  4.  
  5. SUPyF2 Dict-Exercise - 01. Count Chars in a String
  6.  
  7. Problem:
  8. Write a program that counts all characters in a string except for space (' ').
  9. Print all the occurrences in the following format:
  10. {char} -> {occurrences}
  11. Examples
  12. Input:              Output:
  13. text                t -> 2
  14.                    e -> 1
  15.                    x -> 1
  16.  
  17. text text text      t -> 6
  18.                    e -> 3
  19.                    x -> 3
  20. """
  21. characters = {}
  22.  
  23. list_of_characters = [symbol for symbol in input() if symbol != " "]
  24. for symbol in list_of_characters:
  25.     if symbol not in characters:
  26.         characters[symbol] = 1
  27.     else:
  28.         characters[symbol] += 1
  29.  
  30. for character, occurrence in characters.items():
  31.     print(f"{character} -> {occurrence}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement