diff options
Diffstat (limited to 'challenge-002')
67 files changed, 321 insertions, 0 deletions
diff --git a/challenge-002/alex-daniel/README b/challenge-002/alex-daniel/README new file mode 100644 index 0000000000..a085722b3b --- /dev/null +++ b/challenge-002/alex-daniel/README @@ -0,0 +1 @@ +Solution by Alex Daniel diff --git a/challenge-002/alexander-karelas/README b/challenge-002/alexander-karelas/README new file mode 100644 index 0000000000..3de9fce8d9 --- /dev/null +++ b/challenge-002/alexander-karelas/README @@ -0,0 +1 @@ +Solution by Alexander Karelas diff --git a/challenge-002/alexey-melezhik/README b/challenge-002/alexey-melezhik/README new file mode 100644 index 0000000000..ca7008f224 --- /dev/null +++ b/challenge-002/alexey-melezhik/README @@ -0,0 +1 @@ +Solution by Alexey Melezhik diff --git a/challenge-002/antonio-gamiz/README b/challenge-002/antonio-gamiz/README new file mode 100644 index 0000000000..1f5ed5871a --- /dev/null +++ b/challenge-002/antonio-gamiz/README @@ -0,0 +1 @@ +Solution by Antonio Gamiz diff --git a/challenge-002/arpad-toth/README b/challenge-002/arpad-toth/README new file mode 100644 index 0000000000..6661dd82c1 --- /dev/null +++ b/challenge-002/arpad-toth/README @@ -0,0 +1 @@ +Solution by Arpad Toth diff --git a/challenge-002/athanasius/README b/challenge-002/athanasius/README new file mode 100644 index 0000000000..cc357fda4a --- /dev/null +++ b/challenge-002/athanasius/README @@ -0,0 +1 @@ +Solution by Athanasius diff --git a/challenge-002/aubrey-quarcoo/README b/challenge-002/aubrey-quarcoo/README new file mode 100644 index 0000000000..8e63a2c1f5 --- /dev/null +++ b/challenge-002/aubrey-quarcoo/README @@ -0,0 +1 @@ +Solution by Aubrey Quarcoo diff --git a/challenge-002/bob-kleemann/README b/challenge-002/bob-kleemann/README new file mode 100644 index 0000000000..51a81ba0b7 --- /dev/null +++ b/challenge-002/bob-kleemann/README @@ -0,0 +1 @@ +Solution by Bob Kleeman diff --git a/challenge-002/daniel-mantovani/README b/challenge-002/daniel-mantovani/README new file mode 100644 index 0000000000..ed538216f2 --- /dev/null +++ b/challenge-002/daniel-mantovani/README @@ -0,0 +1 @@ +Solution by Daniel Mantovani diff --git a/challenge-002/dave-cross/README b/challenge-002/dave-cross/README new file mode 100644 index 0000000000..04b1b0623b --- /dev/null +++ b/challenge-002/dave-cross/README @@ -0,0 +1 @@ +Solution by Dave Cross diff --git a/challenge-002/dave-jacoby/README b/challenge-002/dave-jacoby/README new file mode 100644 index 0000000000..7c06689f16 --- /dev/null +++ b/challenge-002/dave-jacoby/README @@ -0,0 +1 @@ +Solution by Dave Jacoby diff --git a/challenge-002/david-kayal/README b/challenge-002/david-kayal/README new file mode 100644 index 0000000000..66f8615260 --- /dev/null +++ b/challenge-002/david-kayal/README @@ -0,0 +1 @@ +Solution by David Kayal diff --git a/challenge-002/doug-schrag/README b/challenge-002/doug-schrag/README new file mode 100644 index 0000000000..bf8a10ffb4 --- /dev/null +++ b/challenge-002/doug-schrag/README @@ -0,0 +1 @@ +Solution by Doug Schrag diff --git a/challenge-002/duncan-c-white/README b/challenge-002/duncan-c-white/README new file mode 100644 index 0000000000..81f31dc7c0 --- /dev/null +++ b/challenge-002/duncan-c-white/README @@ -0,0 +1 @@ +Solution by Duncan C. White diff --git a/challenge-002/eddy-hs/README b/challenge-002/eddy-hs/README new file mode 100644 index 0000000000..0ed51d222d --- /dev/null +++ b/challenge-002/eddy-hs/README @@ -0,0 +1 @@ +Solution by Eddy HS diff --git a/challenge-002/finley/README b/challenge-002/finley/README new file mode 100644 index 0000000000..f08a121076 --- /dev/null +++ b/challenge-002/finley/README @@ -0,0 +1 @@ +Solution by Finley diff --git a/challenge-002/fred-zinn/README b/challenge-002/fred-zinn/README new file mode 100644 index 0000000000..17bac4f094 --- /dev/null +++ b/challenge-002/fred-zinn/README @@ -0,0 +1 @@ +Solution by Fred Zinn diff --git a/challenge-002/freddie-b/README b/challenge-002/freddie-b/README new file mode 100644 index 0000000000..823d7474b5 --- /dev/null +++ b/challenge-002/freddie-b/README @@ -0,0 +1 @@ +Solution by Freddie B diff --git a/challenge-002/gustavo-chaves/README b/challenge-002/gustavo-chaves/README new file mode 100644 index 0000000000..194781ee4d --- /dev/null +++ b/challenge-002/gustavo-chaves/README @@ -0,0 +1 @@ +Solution by Gustavo Chavez diff --git a/challenge-002/gustavo-chaves/blog.txt b/challenge-002/gustavo-chaves/blog.txt new file mode 100644 index 0000000000..be76eb1b9c --- /dev/null +++ b/challenge-002/gustavo-chaves/blog.txt @@ -0,0 +1 @@ +https://blog.gnustavo.com/2019/03/perl-weekly-chalenge-2.html diff --git a/challenge-002/gustavo-chaves/perl5/ch-2.pl b/challenge-002/gustavo-chaves/perl5/ch-2.pl new file mode 100644 index 0000000000..1b2da18b08 --- /dev/null +++ b/challenge-002/gustavo-chaves/perl5/ch-2.pl @@ -0,0 +1,46 @@ +#!/usr/bin/env perl + +use 5.026; +use strict; +use integer; +use warnings; + +my $usage = "Usage: $0 FROM_BASE TO_BASE NUMBER\n\nError"; + +die "$usage: invalid number of arguments\n" + unless @ARGV == 3; + +my @digits = ('0' .. '9', 'A' .. 'Z'); +my %digits = map {$digits[$_] => $_} 0 .. @digits-1; + +my ($from_base, $to_base, $number) = @ARGV; + +die "$usage: FROM_BASE must be between 2 and @digits\n" + if $from_base < 2 || $from_base > @digits; + +die "$usage: TO_BASE must be between 2 and @digits\n" + if $to_base < 2 || $to_base > @digits; + +sub from_base { + my ($base, $string) = @_; + my $n = 0; + $string =~ s/^0+//; + foreach my $char (reverse split //, $string) { + die "$usage: Invalid char in NUMBER: '$char'\n" + if $digits{$char} >= $base; + $n *= $base; + $n += $digits{$char}; + } + return $n; +} + +sub to_base { + my ($base, $n) = @_; + my $string = ''; + for (my $i = $n; $i; $i /= $base) { + $string .= $digits[$i % $base]; + } + return length $string ? reverse $string : '0'; +} + +say to_base($to_base, from_base($from_base, $number)); diff --git a/challenge-002/jaldhar-h-vyas/README b/challenge-002/jaldhar-h-vyas/README new file mode 100644 index 0000000000..7dce927eca --- /dev/null +++ b/challenge-002/jaldhar-h-vyas/README @@ -0,0 +1 @@ +Solution by Jaldhar H. Vyas diff --git a/challenge-002/jeff/README b/challenge-002/jeff/README new file mode 100644 index 0000000000..d5f15f4a8e --- /dev/null +++ b/challenge-002/jeff/README @@ -0,0 +1 @@ +Solution by Jeff diff --git a/challenge-002/jeremy-carman/README b/challenge-002/jeremy-carman/README new file mode 100644 index 0000000000..3d08510e49 --- /dev/null +++ b/challenge-002/jeremy-carman/README @@ -0,0 +1 @@ +Solution by Jeremy Carman diff --git a/challenge-002/jim-bacon/README b/challenge-002/jim-bacon/README new file mode 100644 index 0000000000..8dcfd637a3 --- /dev/null +++ b/challenge-002/jim-bacon/README @@ -0,0 +1 @@ +Solution by Jim Bacon diff --git a/challenge-002/jj-merelo/README b/challenge-002/jj-merelo/README new file mode 100644 index 0000000000..d09838676b --- /dev/null +++ b/challenge-002/jj-merelo/README @@ -0,0 +1 @@ +Solution by JJ Merelo diff --git a/challenge-002/jo-christian-oterhals/README b/challenge-002/jo-christian-oterhals/README new file mode 100644 index 0000000000..eafff999fb --- /dev/null +++ b/challenge-002/jo-christian-oterhals/README @@ -0,0 +1 @@ +Solution by Jo Christian Oterhals diff --git a/challenge-002/joelle-maslak/README b/challenge-002/joelle-maslak/README new file mode 100644 index 0000000000..d12c7940c3 --- /dev/null +++ b/challenge-002/joelle-maslak/README @@ -0,0 +1 @@ +Solution by Joelle Maslak diff --git a/challenge-002/joelle-maslak/perl6/ch-1.sh b/challenge-002/joelle-maslak/perl6/ch-1.sh new file mode 100644 index 0000000000..2d3f9b460b --- /dev/null +++ b/challenge-002/joelle-maslak/perl6/ch-1.sh @@ -0,0 +1,77 @@ +#!/bin/bash + +# Challenge: +# Write a script or one-liner to remove leading zeros from positive +# numbers. +# +# Assumptions: +# The first question was, "What do you do about non-positive numbers? +# +# I assumed we just wanted to print non-numbers or non-positive +# numbers "as-is", without removing anything. But we still wanted to +# handle them, otherwise this would be a shorter program. +# +# Zero is neither postiive or negative. +# +# Likewise, what is a number? +# +# I assumed any number that is provided in decimal format is a +# number. Numbers expressed in scientific notation, non-latan +# script, or fractions (1/2) are not treated as positive numbers, so +# they are printed without modification. +# +# Example input/output +# +# INPUT OUTPUT +# 0 0 +# 0.0 0.0 +# 00.00 00.00 +# -1 -1 +# -01 -01 +# 1.0 1.0 +# 1.0000 1.0000 +# 00. 00. (not a standard-formatted number) +# 0.0 .0 (leading zero removed) +# 001 1 +# abc abc +# 01a 01a +# 001 2 001 2 +# +# Implementation: +# "perl6 -e" simply executes the first argument, the script. We'll +# describe the script later. The final argument, "$*", is just the +# arguments passed to this shell script, passed as the first argument +# to the one-liner. +# +# The script used takes the first command line parameter (@*ARGS[0]) +# and does a non-destructive substitution on it (using the S/// +# operator followed by a "with" to reference the source). +# +# The regex basically removes zeros at the front of one of two +# different patterns - this pattern could be simplified as two +# regexes: +# +# S/ ^ 0+ ( <[0..9]>* \. <[0..9]>+ ) $ /$0/ +# +# - and - +# +# s/ ^ 0+ ( <[1..9]> <[0..9]>* ) $ /$0/ +# +# The first one handles numbers with a decimal point. It removes all +# leading zeros in front of the decimal point, in a pretty +# straightforward way. Note that 0.0 would become .0, removing the +# leading zero. +# +# The second regex above handles non-zero positive numbers without a +# decimal point. But we don't want to remove leading zeros for zero +# itself. I.E. 00 is not a positive number (it's zero, it's neither +# positive or negative), so it should stay 00. So any number starting +# with a zero must have at least one non-zero digit for leading zeros +# to be removed. +# +# These two regexes were then combined form what is in the one-liner +# below. +# +perl6 -e 'say S/ ^ 0+ ( ( <[0..9]>* \. <[0..9]>+ ) | ( <[1..9]> <[0..9]>* ) ) $ /$0/ with @*ARGS[0]' "$*" + + diff --git a/challenge-002/john-barrett/README b/challenge-002/john-barrett/README new file mode 100644 index 0000000000..a7ff0af641 --- /dev/null +++ b/challenge-002/john-barrett/README @@ -0,0 +1 @@ +Solution by John Barrett diff --git a/challenge-002/juan-caballero/README b/challenge-002/juan-caballero/README new file mode 100644 index 0000000000..bffe4b05c2 --- /dev/null +++ b/challenge-002/juan-caballero/README @@ -0,0 +1 @@ +Solution by Juan Caballero diff --git a/challenge-002/khalid/README b/challenge-002/khalid/README new file mode 100644 index 0000000000..1ca4211368 --- /dev/null +++ b/challenge-002/khalid/README @@ -0,0 +1 @@ +Solution by Khalid diff --git a/challenge-002/khalid/blog.txt b/challenge-002/khalid/blog.txt new file mode 100644 index 0000000000..7088e9680e --- /dev/null +++ b/challenge-002/khalid/blog.txt @@ -0,0 +1 @@ +https://github.com/khalidelboray/PerlWeeklyChallenge/blob/master/002/Perl6.md diff --git a/challenge-002/kian-meng-ang/README b/challenge-002/kian-meng-ang/README new file mode 100644 index 0000000000..a5f0359813 --- /dev/null +++ b/challenge-002/kian-meng-ang/README @@ -0,0 +1 @@ +Solution by Kian-Meng Ang diff --git a/challenge-002/kivanc-yazan/README b/challenge-002/kivanc-yazan/README new file mode 100644 index 0000000000..bc7f8ca509 --- /dev/null +++ b/challenge-002/kivanc-yazan/README @@ -0,0 +1 @@ +Solution by Kivanc Yazan diff --git a/challenge-002/lars-balker/README b/challenge-002/lars-balker/README new file mode 100644 index 0000000000..054649c9eb --- /dev/null +++ b/challenge-002/lars-balker/README @@ -0,0 +1 @@ +Solution by Lars Balker diff --git a/challenge-002/lars-balker/perl5/ch-1.pl b/challenge-002/lars-balker/perl5/ch-1.pl new file mode 100644 index 0000000000..6c86065ef8 --- /dev/null +++ b/challenge-002/lars-balker/perl5/ch-1.pl @@ -0,0 +1,44 @@ +# Write a script or one-liner to remove leading zeros from positive +# numbers. + +# I chose to solve this in the strictest interpretation of the challenge: +# Make sure it's a number +# Make sure it's positive +# Don't remove the last 0 before period. + +use v5.10; +use strict; +use warnings; +use Scalar::Util qw/looks_like_number/; # distributed with perl since 5.8 + +sub remove_leading_zeros { + my $num = shift; + # ask perl if it thinks input is a number, and make sure it's positive + if (looks_like_number($num) && $num > 0) { + $num =~ s/^ + ( + 0* # match all leading 0s followed by + (?=0\.) # 0. (not included in match) + | + 0* # or just all leading 0s + ) + //x; + } + $num; +} + +if (@ARGV) { + say remove_leading_zeros shift; +} +else { + eval "use Test::More"; + is(remove_leading_zeros("000000"), "000000"); + is(remove_leading_zeros("-0001"), "-0001"); + is(remove_leading_zeros("0001"), "1"); + is(remove_leading_zeros("0.001"), "0.001"); + is(remove_leading_zeros(".001"), ".001"); + is(remove_leading_zeros("000.001"), "0.001"); + is(remove_leading_zeros("001bar"), "001bar"); + is(remove_leading_zeros("fizzbuzz"), "fizzbuzz"); + done_testing(); +} diff --git a/challenge-002/lars-balker/perl5/ch-2.pl b/challenge-002/lars-balker/perl5/ch-2.pl new file mode 100644 index 0000000000..97846efdfb --- /dev/null +++ b/challenge-002/lars-balker/perl5/ch-2.pl @@ -0,0 +1,31 @@ +# Write a script that can convert integers to and from a base35 +# representation, using the characters 0-9 and A-Y. + +use v5.10; +use strict; +use warnings; + +sub to_base35 { + my $num = shift; + my $res = ""; + my @val = (0..9, 'A'..'Y'); + do { + $res .= $val[$num % 35]; + $num = int($num / 35); + } while $num; + $res = reverse $res; + $res; +} + +if (@ARGV) { + say to_base35 shift; +} +else { + eval "use Test::More"; + is(to_base35(0), "0"); + is(to_base35(10), "A"); + is(to_base35(35), "10"); + is(to_base35(1337), "137"); + is(to_base35(20190401), "DFVXL"); + done_testing(); +} diff --git a/challenge-002/lars-balker/perl6/ch-1.pl6 b/challenge-002/lars-balker/perl6/ch-1.pl6 new file mode 100644 index 0000000000..31f9548d14 --- /dev/null +++ b/challenge-002/lars-balker/perl6/ch-1.pl6 @@ -0,0 +1,37 @@ +# Write a script or one-liner to remove leading zeros from positive +# numbers. + +use v6; +use Test; + +sub remove_leading_zeros($num) { + $num.subst(/^( + 0* # match all leading 0s followed by + <?before 0\.> # 0. (not included in match) + || + 0* # or just all leading 0s + ) + /, + ''); +} + +# we let multi dispatch pick positive numbers +multi sub MAIN(Numeric $num where $num > 0) { + say remove_leading_zeros($num); +} + +# if not a positive number, just echo input +multi sub MAIN($other) { + say $other; +} + +# test if no input +multi sub MAIN() { + # we only test the legal number input, because other input is + # sorted out by multi methods + is(remove_leading_zeros("0001"), "1"); + is(remove_leading_zeros("0.001"), "0.001"); + is(remove_leading_zeros(".001"), ".001"); + is(remove_leading_zeros("000.001"), "0.001"); + done-testing(); +} diff --git a/challenge-002/lars-balker/perl6/ch-2.pl6 b/challenge-002/lars-balker/perl6/ch-2.pl6 new file mode 100644 index 0000000000..719b8cebc7 --- /dev/null +++ b/challenge-002/lars-balker/perl6/ch-2.pl6 @@ -0,0 +1,23 @@ +# Write a script that can convert integers to and from a base35 +# representation, using the characters 0-9 and A-Y. + +use v6; +use Test; + +sub to_base35(Int $num) { + return $num.base(35); +} |
