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-1.py | 51 +++++++++++++++++++++++++++++ challenge-032/paulo-custodio/python/ch-2.py | 35 ++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 challenge-032/paulo-custodio/python/ch-1.py create mode 100644 challenge-032/paulo-custodio/python/ch-2.py (limited to 'challenge-032/paulo-custodio/python') diff --git a/challenge-032/paulo-custodio/python/ch-1.py b/challenge-032/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..2a600a8112 --- /dev/null +++ b/challenge-032/paulo-custodio/python/ch-1.py @@ -0,0 +1,51 @@ +#!/usr/bin/python3 + +# Challenge 032 +# +# Task #1 +# Contributed by Neil Bowers +# Count instances +# Create a script that either reads standard input or one or more files +# specified on the command-line. Count the number of times and then print a +# summary, sorted by the count of each entry. +# +# So with the following input in file example.txt +# +# apple +# banana +# apple +# cherry +# cherry +# apple +# the script would display something like: +# +# apple 3 +# cherry 2 +# banana 1 +# For extra credit, add a -csv option to your script, which would generate: +# +# apple,3 +# banana,1 +# cherry,2 + +import fileinput +import sys + +# command line options +sep = "\t" +if len(sys.argv)>1 and sys.argv[1]=="-csv": + sys.argv.pop(1) + sep = "," + +# count instances +count = {} +for line in fileinput.input(): + word = line.strip() + if word in count: + count[word] += 1 + else: + count[word] = 1 + +# output +for key in sorted(count): + print(f"{key}{sep}{count[key]}") 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-1.py | 8 ++++---- challenge-032/paulo-custodio/python/ch-2.py | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'challenge-032/paulo-custodio/python') diff --git a/challenge-032/paulo-custodio/python/ch-1.py b/challenge-032/paulo-custodio/python/ch-1.py index 2a600a8112..717d0f8ded 100644 --- a/challenge-032/paulo-custodio/python/ch-1.py +++ b/challenge-032/paulo-custodio/python/ch-1.py @@ -8,9 +8,9 @@ # Create a script that either reads standard input or one or more files # specified on the command-line. Count the number of times and then print a # summary, sorted by the count of each entry. -# +# # So with the following input in file example.txt -# +# # apple # banana # apple @@ -18,12 +18,12 @@ # cherry # apple # the script would display something like: -# +# # apple 3 # cherry 2 # banana 1 # For extra credit, add a -csv option to your script, which would generate: -# +# # apple,3 # banana,1 # cherry,2 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