diff options
| author | Conor Hoekstra <codereport@outlook.com> | 2021-12-24 22:16:15 -0500 |
|---|---|---|
| committer | Conor Hoekstra <codereport@outlook.com> | 2021-12-24 22:16:15 -0500 |
| commit | c804b128cf740d95b244cd3fe15e86d0820ab51e (patch) | |
| tree | b9db7c3a5f93e6a83640f56bfd5decb9bbe8dbfe /challenge-032/paulo-custodio/python/ch-2.py | |
| parent | 116add27d0d74360c7d4ad26b12d972657e51afa (diff) | |
| parent | 6f518c687f743b68d3eeddedcf3d831aca20d4ec (diff) | |
| download | perlweeklychallenge-club-c804b128cf740d95b244cd3fe15e86d0820ab51e.tar.gz perlweeklychallenge-club-c804b128cf740d95b244cd3fe15e86d0820ab51e.tar.bz2 perlweeklychallenge-club-c804b128cf740d95b244cd3fe15e86d0820ab51e.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-032/paulo-custodio/python/ch-2.py')
| -rw-r--r-- | challenge-032/paulo-custodio/python/ch-2.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/challenge-032/paulo-custodio/python/ch-2.py b/challenge-032/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..6a0b7385c7 --- /dev/null +++ b/challenge-032/paulo-custodio/python/ch-2.py @@ -0,0 +1,35 @@ +#!/usr/bin/python3 + +# Challenge 032 +# +# Task #2 +# Contributed by Neil Bowers +# ASCII bar chart +# Write a function that takes a hashref where the keys are labels and the +# values are integer or floating point values. Generate a bar graph of the +# data and display it to stdout. +# +# The input could be something like: +# +# $data = { apple => 3, cherry => 2, banana => 1 }; +# generate_bar_graph($data); +# And would then generate something like this: +# +# apple | ############ +# cherry | ######## +# banana | #### +# If you fancy then please try this as well: (a) the function could let you +# specify whether the chart should be ordered by (1) the labels, or (2) the +# values. + +data = { 'apple':3, 'cherry':2, 'banana':1 } + +def chart(data): + # get size of keys + width = max([len(x) for x in data]) + + # output data + for key in sorted(data): + print(("{:"+str(width)+"s} | {}").format(key, "##"*data[key])) + +chart(data) |
