From 9f83fe38e24a005d07ee31c0c53582f3eefbc9e5 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Thu, 23 Dec 2021 14:31:24 +0000 Subject: Add Perl and Python solutions to challenge 032 --- challenge-032/paulo-custodio/python/ch-2.py | 35 +++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 challenge-032/paulo-custodio/python/ch-2.py (limited to 'challenge-032/paulo-custodio/python/ch-2.py') 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..e8956935b6 --- /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) -- cgit From ce5bd2dd1a3159107febe4b9eb488a071bc601f5 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Thu, 23 Dec 2021 14:33:04 +0000 Subject: Whitespace --- challenge-032/paulo-custodio/python/ch-2.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'challenge-032/paulo-custodio/python/ch-2.py') diff --git a/challenge-032/paulo-custodio/python/ch-2.py b/challenge-032/paulo-custodio/python/ch-2.py index e8956935b6..6a0b7385c7 100644 --- a/challenge-032/paulo-custodio/python/ch-2.py +++ b/challenge-032/paulo-custodio/python/ch-2.py @@ -8,13 +8,13 @@ # 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 | #### @@ -25,10 +25,10 @@ data = { 'apple':3, 'cherry':2, 'banana':1 } def chart(data): - # get size of keys + # get size of keys width = max([len(x) for x in data]) - # output data + # output data for key in sorted(data): print(("{:"+str(width)+"s} | {}").format(key, "##"*data[key])) -- cgit