diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-12-24 01:03:26 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-24 01:03:26 +0000 |
| commit | f13cc76081c7af1d173ee01166c8e725d3843f3a (patch) | |
| tree | e0e79b1bdc232483dbb5f97a650f38b88700583b /challenge-032/paulo-custodio/python | |
| parent | 776396430ae1e9e0aa85d2e78bb92e38fd1556f7 (diff) | |
| parent | b2bda687c42cfc1229440dfb0d392e5fbad8f1d1 (diff) | |
| download | perlweeklychallenge-club-f13cc76081c7af1d173ee01166c8e725d3843f3a.tar.gz perlweeklychallenge-club-f13cc76081c7af1d173ee01166c8e725d3843f3a.tar.bz2 perlweeklychallenge-club-f13cc76081c7af1d173ee01166c8e725d3843f3a.zip | |
Merge pull request #5407 from pauloscustodio/devel
Add Perl and Python solutions to challenge 032
Diffstat (limited to 'challenge-032/paulo-custodio/python')
| -rw-r--r-- | challenge-032/paulo-custodio/python/ch-1.py | 51 | ||||
| -rw-r--r-- | challenge-032/paulo-custodio/python/ch-2.py | 35 |
2 files changed, 86 insertions, 0 deletions
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..717d0f8ded --- /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..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) |
