aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-08-28 17:25:19 +0100
committerGitHub <noreply@github.com>2022-08-28 17:25:19 +0100
commit44e8bf302e6e0bc570f9c45ac3a8dcf315a5d7eb (patch)
treebe8327db1fc5bbf8660ac7b9577c5be0e7062c11
parentf964821d8d3459c2078f85d4c5b5387e75c6d79c (diff)
parentebd856ad7f9be363073598af45a200d448cef9c8 (diff)
downloadperlweeklychallenge-club-44e8bf302e6e0bc570f9c45ac3a8dcf315a5d7eb.tar.gz
perlweeklychallenge-club-44e8bf302e6e0bc570f9c45ac3a8dcf315a5d7eb.tar.bz2
perlweeklychallenge-club-44e8bf302e6e0bc570f9c45ac3a8dcf315a5d7eb.zip
Merge pull request #6656 from simongreen-net/master
sgreen solutions to challenge 179
-rw-r--r--challenge-179/sgreen/README.md4
-rw-r--r--challenge-179/sgreen/blog.txt1
-rwxr-xr-xchallenge-179/sgreen/perl/ch-1.pl101
-rwxr-xr-xchallenge-179/sgreen/perl/ch-2.pl25
-rwxr-xr-xchallenge-179/sgreen/python/ch-1.py77
-rwxr-xr-xchallenge-179/sgreen/python/ch-2.py20
6 files changed, 226 insertions, 2 deletions
diff --git a/challenge-179/sgreen/README.md b/challenge-179/sgreen/README.md
index d637d292a2..65ea77fbce 100644
--- a/challenge-179/sgreen/README.md
+++ b/challenge-179/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 178
+# The Weekly Challenge 179
-Blog [Quater-imaginary Business Date](https://dev.to/simongreennet/quater-imaginary-business-date-58ln)
+Blog [The Ordinal Sparkline](https://dev.to/simongreennet/the-ordinal-sparkline-dn5)
diff --git a/challenge-179/sgreen/blog.txt b/challenge-179/sgreen/blog.txt
new file mode 100644
index 0000000000..0841550863
--- /dev/null
+++ b/challenge-179/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/the-ordinal-sparkline-dn5 \ No newline at end of file
diff --git a/challenge-179/sgreen/perl/ch-1.pl b/challenge-179/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..18cffa3704
--- /dev/null
+++ b/challenge-179/sgreen/perl/ch-1.pl
@@ -0,0 +1,101 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main ($n) {
+ if ( $n >= 10**12 ) {
+ die 'Number too large\n';
+ }
+ my $hundred = $n % 100 == 0;
+
+ my @numbers = (
+ undef, 'one', 'two', 'three',
+ 'four', 'five', 'six', 'seven',
+ 'eight', 'nine', 'ten', 'eleven',
+ 'twelve', 'thirteen', 'fourteen', 'fifteen',
+ 'sixteen', 'seventeen', 'eighteen', 'nineteen'
+ );
+ my @tens = (
+ undef, undef, 'twenty', 'thirty', 'fourty', 'fifty',
+ 'sixty', 'seventy', 'eighty', 'ninety'
+ );
+ my @ordinal = (
+ undef, 'first', 'second', 'third',
+ 'fourth', 'fifth', 'sixth', 'seventh',
+ 'eighth', 'ninth', 'tenth', 'eleventh',
+ 'twelfth', 'thirteenth', 'fourteenth', 'fifteenth',
+ 'sixteenth', 'seventeenth', 'eighteenth', 'nineteenth'
+ );
+ my @thousands = ( undef, 'thousand', 'million', 'billion' );
+
+ # Split the string into blocks of three
+ my @blocks = ();
+ while ( length($n) > 3 ) {
+ # Take off the last three numbers
+ push @blocks, substr( $n, -3 );
+ $n = substr( $n, 0, -3 );
+ }
+
+ push @blocks, $n;
+
+ my @words = ();
+ while ( my ( $count, $value ) = each @blocks ) {
+ my $w = '';
+
+ # Maybe this block has no numbers
+ next if $value == 0;
+
+ # Deal with the hundreds first
+ if ( $value >= 100 ) {
+ $w = $numbers[ int( $value / 100 ) ] . ' hundred';
+
+ if ( $value % 100 == 0 ) {
+ # Deal with numbers evenly divisable by 100
+ if ( $count > 0 ) {
+ $w .= ' ' . $thousands[$count];
+ }
+ unshift @words, $w;
+ next;
+ }
+
+ $w .= ' and ';
+ $value = $value % 100;
+ }
+
+ # Then the tens
+ if ( $value >= 20 ) {
+ $w .= $tens[ int( $value / 10 ) ] . ' ';
+ $value = $value % 10;
+
+ if ($value == 0 and $count == 0) {
+ # We need to add the ordinal word here
+ substr($w, -2) = 'ieth';
+ }
+ }
+
+ # And then the number
+ if ($value != 0) {
+ $w .= $count == 0 ? $ordinal[$value] : $numbers[$value];
+ }
+
+ # And maybe the thousandth word
+ if ( $count > 0 ) {
+ $w .= ' ' . $thousands[$count];
+ }
+
+ unshift @words, $w;
+ }
+
+# Edge case, if the number is evenly divisible by 100, add 'th' to the last value
+ if ($hundred) {
+ $words[-1] .= 'th';
+ }
+
+ # Print the ordinal number
+ say join ' ', @words;
+}
+
+main( $ARGV[0] ); \ No newline at end of file
diff --git a/challenge-179/sgreen/perl/ch-2.pl b/challenge-179/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..808e3e8662
--- /dev/null
+++ b/challenge-179/sgreen/perl/ch-2.pl
@@ -0,0 +1,25 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+use List::Util qw(min max);
+
+sub main (@numbers) {
+ my @bar = (qw'▁ ▂ ▃ ▄ ▅ ▆ ▇ █');
+ my $barcount = scalar(@bar);
+
+ my $mn = min(@numbers);
+ my $mx = max(@numbers);
+ my $extent = $mx - $mn;
+ my $sparkline = '';
+
+ foreach my $n (@numbers) {
+ $sparkline .= $bar[ min( $barcount - 1, int( ( $n - $mn ) / $extent * $barcount ) ) ];
+ }
+
+ say $sparkline;
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-179/sgreen/python/ch-1.py b/challenge-179/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..9ff2f822a7
--- /dev/null
+++ b/challenge-179/sgreen/python/ch-1.py
@@ -0,0 +1,77 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def main(s):
+ n = int(s)
+ if n >= 10 ** 12:
+ raise ValueError('Number too large')
+
+ numbers = [None, 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten',
+ 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']
+ tens = [None, None, 'twenty', 'thirty', 'fourty',
+ 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
+ ordinal = [None, 'first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth', 'tenth',
+ 'eleventh', 'twelfth', 'thirteenth', 'fourteenth', 'fifteenth', 'sixteenth', 'seventeenth', 'eighteenth', 'nineteenth']
+ thousands = [None, 'thousand', 'million', 'billion']
+
+ # Split the string into blocks of three
+ blocks = []
+ while len(s) > 3:
+ # Take off the last three numbers
+ blocks.append(int(s[-3:]))
+ s = s[:-3]
+ blocks.append(int(s))
+
+ words = []
+ for count, value in enumerate(blocks):
+ w = ''
+
+ # Maybe this block has no numbers
+ if value == 0:
+ continue
+
+ # Deal with the hundreds first
+ if value >= 100:
+ w = numbers[int(value/100)] + ' hundred'
+
+ if value % 100 == 0:
+ # Deal with numbers evenly divisable by 100
+ if count > 0:
+ w += ' ' + thousands[count]
+ words.insert(0, w)
+ continue
+
+ w += ' and '
+ value = value % 100
+
+ # Then the tens
+ if value >= 20:
+ w += tens[int(value/10)] + ' '
+ value = value % 10
+ if value == 0 and count == 0:
+ # We need to add the ordinal word here
+ w = w[:-2] + 'ieth'
+
+ # And then the number
+ if value != 0:
+ t = ordinal if count == 0 else numbers
+ w += t[value]
+
+ # And maybe the thousandth word
+ if count > 0:
+ w += ' ' + thousands[count]
+
+ words.insert(0, str(w))
+
+ # Edge case, if the number is evenly divisible by 100, add 'th' to the last value
+ if int(n) % 100 == 0:
+ words[-1] += 'th'
+
+ # Print the ordinal number
+ print(*words, sep=' ')
+
+
+if __name__ == '__main__':
+ main(sys.argv[1])
diff --git a/challenge-179/sgreen/python/ch-2.py b/challenge-179/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..7647b00b1e
--- /dev/null
+++ b/challenge-179/sgreen/python/ch-2.py
@@ -0,0 +1,20 @@
+#!/usr/bin/python3
+
+import sys
+
+
+def main(strings):
+ numbers = [float(x) for x in strings]
+ bar = '▁▂▃▄▅▆▇█'
+ barcount = len(bar)
+
+ mn, mx = min(numbers), max(numbers)
+ extent = mx - mn
+ sparkline = ''.join(bar[min([barcount - 1,
+ int((n - mn) / extent * barcount)])]
+ for n in numbers)
+ print(sparkline)
+
+
+if __name__ == '__main__':
+ main(sys.argv[1:])