Advertisement
Python253

ipp1_2_poor_mans_bar_chart

May 31st, 2024
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: ipp1_2_poor_mans_bar_chart.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    - This script demonstrates "Chapter 1: Practice Project #2 - Poor Man's Bar Chart" from the book "Impractical Python Projects" by Lee Vaughan.
  10.    - This script generates a "Poor Man’s Bar Chart" representing the frequency of each letter in a given text.
  11.    - It maps letters from the text into a dictionary and prints the frequency of each letter along with its occurrences.
  12.  
  13. Requirements:
  14.    - Python 3.x
  15.    - The following modules:
  16.        - sys:
  17.            Provides access to some variables used or maintained by the Python interpreter and to functions that interact strongly with the interpreter.
  18.        - pprint:
  19.            Provides a capability to “pretty-print” arbitrary Python data structures in a format that can be used as input to the interpreter.
  20.        - defaultdict:
  21.            Provides a subclass of the built-in dict class. It overrides one method and adds one writable instance variable. The remaining functionality is the same as for the dict class.
  22.          
  23. Usage:
  24.    - This script can be run directly from the command line or imported as a module.
  25.  
  26. Additional Notes:
  27.    - Ensure that the input text provided is relatively short to ensure proper formatting of the output.
  28.    - The script assumes the input text is in English and only considers lowercase letters for the frequency count.
  29. """
  30.  
  31. import sys
  32. import pprint
  33. from collections import defaultdict
  34.  
  35. # Note: text should be a short phrase for bars to fit in IDLE window
  36. text = 'Like the castle in its corner in a medieval game, I foresee terrible \
  37. trouble and I stay here just the same.'
  38. ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
  39.  
  40. # defaultdict module lets you build dictionary keys on the fly!
  41. mapped = defaultdict(list)
  42.  
  43. for character in text:
  44.     character = character.lower()
  45.     if character in ALPHABET:
  46.         mapped[character].append(character)
  47.  
  48. # Modify the list of appearances to include the count of appearances
  49. for letter, occurrences in mapped.items():
  50.     count = len(occurrences)
  51.     mapped[letter] = (count, occurrences)
  52.  
  53. # pprint lets you print stacked output
  54. print("Text: \n", end='')
  55. print("{}\n".format(text), file=sys.stderr)
  56. print("Letter Frequency:")
  57. for letter, (count, occurrences) in mapped.items():
  58.     frequency = f"{count}x"
  59.     if count < 10:
  60.         frequency = " " + frequency  # Add space for single-digit frequency
  61.     print(f"{frequency} '{letter}': {' '.join(occurrences)}")
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement