diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-13 15:45:00 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-13 15:45:00 +0100 |
| commit | 52723f74a6fc8680e19ea79ddd01bae1d9e47829 (patch) | |
| tree | 9bde73f6bd6e38f0e6bbfb0f6854e09076ef52aa | |
| parent | 1f611de5f4a63b384e9721f184fc770acf7797a8 (diff) | |
| parent | aad6c403c706dc507f36e573894874ffb0301503 (diff) | |
| download | perlweeklychallenge-club-52723f74a6fc8680e19ea79ddd01bae1d9e47829.tar.gz perlweeklychallenge-club-52723f74a6fc8680e19ea79ddd01bae1d9e47829.tar.bz2 perlweeklychallenge-club-52723f74a6fc8680e19ea79ddd01bae1d9e47829.zip | |
Merge branch 'manwar:master' into master
37 files changed, 2978 insertions, 2217 deletions
diff --git a/challenge-160/andinus/README b/challenge-160/andinus/README index bd1dd6e3ce..cb7306aa47 100644 --- a/challenge-160/andinus/README +++ b/challenge-160/andinus/README @@ -1,51 +1,113 @@ ━━━━━━━━━━━━━━━ - CHALLENGE 135 + CHALLENGE 160 Andinus ━━━━━━━━━━━━━━━ - 2021-10-22 + 2022-04-12 -Task 1 - Middle 3-digits -════════════════════════ +Task 1 - Four Is Magic +══════════════════════ - You are given an integer. + You are given a positive number, $n < 10. - Write a script find out the middle 3-digits of the given integer, if - possible otherwise throw sensible error. + Write a script to generate english text sequence starting with the + English cardinal representation of the given number, the word ‘is’ and + then the English cardinal representation of the count of characters + that made up the first word, followed by a comma. Continue until you + reach four. ┌──── - │ Input: $n = 1234567 - │ Output: 345 + │ Input: $n = 5 + │ Output: Five is four, four is magic. │ - │ Input: $n = -123 - │ Output: 123 + │ Input: $n = 7 + │ Output: Seven is five, five is four, four is magic. │ - │ Input: $n = 1 - │ Output: too short + │ Input: $n = 6 + │ Output: Six is three, three is five, five is four, four is magic. + └──── + + +Raku +──── + + Take a positive number, less than 10 as input from MAIN. Then we + define an array that holds the string representation of integers. The + `multi sub' `four-is-magic' is called on the input. It runs + recursively until `4' is called. + + `.tc' is called on the result to make the first character uppercase. + + ┌──── + │ unit sub MAIN( + │ UInt $n where * < 10, #= positive number, less than 10 + │ ); + │ + │ my @num-to-str = <zero one two three four five six seven eight nine>; + │ + │ multi sub four-is-magic(4 --> Str) { + │ return "four is magic."; + │ } + │ + │ multi sub four-is-magic(Int $n where * < 10 --> Str) { + │ my $n-str = @num-to-str[$n]; + │ return "$n-str is { @num-to-str[$n-str.chars] }, " ~ four-is-magic($n-str.chars); + │ } │ - │ Input: $n = 10 - │ Output: even number of digits + │ put four-is-magic($n).tc; + └──── + + +Task 2 - Equilibrium Index +══════════════════════════ + + You are give an array of integers, @n. + + Write a script to find out the Equilibrium Index of the given array, + if found. + + For an array A consisting n elements, index i is an + equilibrium index if the sum of elements of subarray + A[0…i-1] is equal to the sum of elements of subarray + A[i+1…n-1]. + + ┌──── + │ Input: @n = (1, 3, 5, 7, 9) + │ Output: 3 + │ + │ Input: @n = (1, 2, 3, 4, 5) + │ Output: -1 as no Equilibrium Index found. + │ + │ Input: @n = (2, 4, 2) + │ Output: 1 └──── Raku ──── - Input's absolute value is taken because the sign is meaningless here. - To get middle 3-digits we take 3 digits from `$n.chars div 2 - 1' - position, `-1' because Arrays are 0-indexed. It's guaranteed that we - have odd number of digits so `div 2' will land us on left of middle - digit, we just take 3 digits from there. + Takes an array of integers as input. Then it loops over the array by + index and does as the problem states, takes sum of all elements before + the index and compares it with the sum of all elements after the + index, if they're equal it prints the index and exits. If there is no + Equilibrium Index then it prints -1. ┌──── - │ $n = abs $n; - │ die "too short" if $n.chars < 3; - │ die "even number of digits" if $n.chars %% 2; - │ put $n.substr($n.chars div 2 - 1, 3); + │ unit sub MAIN( + │ *@n, #= array of integers + │ ); + │ + │ for 0 .. @n.end -> $i { + │ if @n[0 .. $i - 1].sum == @n[$i + 1 .. *].sum { + │ put $i; + │ exit; + │ } + │ } + │ put -1; └──── diff --git a/challenge-160/andinus/README.org b/challenge-160/andinus/README.org new file mode 100644 index 0000000000..d4ae1142fa --- /dev/null +++ b/challenge-160/andinus/README.org @@ -0,0 +1,101 @@ +#+title: Challenge 160 +#+date: 2022-04-12 +#+html_link_up: ../ +#+export_file_name: index +#+options: toc:nil +#+setupfile: ~/.emacs.d/org-templates/level-2.org + +* Task 1 - Four Is Magic + +You are given a positive number, $n < 10. + +Write a script to generate english text sequence starting with the +English cardinal representation of the given number, the word ‘is’ and +then the English cardinal representation of the count of characters that +made up the first word, followed by a comma. Continue until you reach +four. + +#+begin_src +Input: $n = 5 +Output: Five is four, four is magic. + +Input: $n = 7 +Output: Seven is five, five is four, four is magic. + +Input: $n = 6 +Output: Six is three, three is five, five is four, four is magic. +#+end_src + +** Raku + +Take a positive number, less than 10 as input from MAIN. Then we define +an array that holds the string representation of integers. The ~multi sub~ +~four-is-magic~ is called on the input. It runs recursively until ~4~ is +called. + +~.tc~ is called on the result to make the first character uppercase. + +#+begin_src raku +unit sub MAIN( + UInt $n where * < 10, #= positive number, less than 10 +); + +my @num-to-str = <zero one two three four five six seven eight nine>; + +multi sub four-is-magic(4 --> Str) { + return "four is magic."; +} + +multi sub four-is-magic(Int $n where * < 10 --> Str) { + my $n-str = @num-to-str[$n]; + return "$n-str is { @num-to-str[$n-str.chars] }, " ~ four-is-magic($n-str.chars); +} + +put four-is-magic($n).tc; +#+end_src + +* Task 2 - Equilibrium Index + +You are give an array of integers, @n. + +Write a script to find out the Equilibrium Index of the given array, if +found. + +#+begin_quote +For an array A consisting n elements, index i is an equilibrium index if +the sum of elements of subarray A[0…i-1] is equal to the sum of elements +of subarray A[i+1…n-1]. +#+end_quote + +#+begin_src +Input: @n = (1, 3, 5, 7, 9) +Output: 3 + +Input: @n = (1, 2, 3, 4, 5) +Output: -1 as no Equilibrium Index found. + +Input: @n = (2, 4, 2) +Output: 1 +#+end_src + +** Raku + +Takes an array of integers as input. Then it loops over the array by +index and does as the problem states, takes sum of all elements before +the index and compares it with the sum of all elements after the index, +if they're equal it prints the index and exits. If there is no +Equilibrium Index then it prints -1. + +#+begin_src raku +unit sub MAIN( + *@n, #= array of integers +); + +for 0 .. @n.end -> $i { + if @n[0 .. $i - 1].sum == @n[$i + 1 .. *].sum { + put $i; + exit; + } +} +put -1; +#+end_src diff --git a/challenge-160/andinus/blog-1.txt b/challenge-160/andinus/blog-1.txt new file mode 100644 index 0000000000..e2ce699697 --- /dev/null +++ b/challenge-160/andinus/blog-1.txt @@ -0,0 +1 @@ +https://andinus.unfla.me/pwc/challenge-160/ diff --git a/challenge-160/andinus/blog-2.txt b/challenge-160/andinus/blog-2.txt new file mode 100644 index 0000000000..e2ce699697 --- /dev/null +++ b/challenge-160/andinus/blog-2.txt @@ -0,0 +1 @@ +https://andinus.unfla.me/pwc/challenge-160/ diff --git a/challenge-160/andinus/raku/ch-1.raku b/challenge-160/andinus/raku/ch-1.raku new file mode 100644 index 0000000000..7ab3307848 --- /dev/null +++ b/challenge-160/andinus/raku/ch-1.raku @@ -0,0 +1,16 @@ +unit sub MAIN( + UInt $n where * < 10, #= positive number, less than 10 +); + +my @num-to-str = <zero one two three four five six seven eight nine>; + +multi sub four-is-magic(4 --> Str) { + return "four is magic."; +} + +multi sub four-is-magic(Int $n where * < 10 --> Str) { + my $n-str = @num-to-str[$n]; + return "$n-str is { @num-to-str[$n-str.chars] }, " ~ four-is-magic($n-str.chars); +} + +put four-is-magic($n).tc; diff --git a/challenge-160/andinus/raku/ch-2.raku b/challenge-160/andinus/raku/ch-2.raku new file mode 100644 index 0000000000..a9045a2712 --- /dev/null +++ b/challenge-160/andinus/raku/ch-2.raku @@ -0,0 +1,11 @@ +unit sub MAIN( + *@n, #= array of integers +); + +for 0 .. @n.end -> $i { + if @n[0 .. $i - 1].sum == @n[$i + 1 .. *].sum { + put $i; + exit; + } +} +put -1; diff --git a/challenge-160/deadmarshal/cpp/ch-1.cpp b/challenge-160/deadmarshal/cpp/ch-1.cpp new file mode 100644 index 0000000000..4db8e75aa7 --- /dev/null +++ b/challenge-160/deadmarshal/cpp/ch-1.cpp @@ -0,0 +1,32 @@ +#include<iostream> +#include<string> +#include<map> + +std::string four_is_magic(int n) +{ + std::map<int, std::string> numbers{}; + std::string ret{}; + std::string arr[] = {"one","two","three","four","five","six","seven", + "eight","nine"}; + for(int i = 0; i < 9; ++i){ + numbers[i+1] = arr[i]; + } + size_t len = numbers[n].size(); + do{ + ret += numbers[n] + " is " + numbers[len] + ", "; + n = len; + len = numbers[n].size(); + }while(n != 4); + ret[0] = std::toupper(ret[0]); + ret += "four is magic."; + return ret; +} + +int main() +{ + std::cout << four_is_magic(5) << '\n'; + std::cout << four_is_magic(7) << '\n'; + std::cout << four_is_magic(6) << '\n'; + + return 0; +} diff --git a/challenge-160/deadmarshal/cpp/ch-2.cpp b/challenge-160/deadmarshal/cpp/ch-2.cpp new file mode 100644 index 0000000000..79ab7736f6 --- /dev/null +++ b/challenge-160/deadmarshal/cpp/ch-2.cpp @@ -0,0 +1,34 @@ +#include<iostream> +#include<vector> +#include<numeric> + +int equilibrium_index(const std::vector<int>& vec) +{ + int left = 0, right = std::accumulate(vec.begin(), vec.end(), 0); + int ret = -1; + for(std::size_t i = 0; i < vec.size(); ++i) + { + right -= vec[i]; + if(left == right) + { + ret = i; + break; + } + left += vec[i]; + } + return ret; +} + +int main() +{ + std::vector<int> vec{1,3,5,7,9}; + std::cout << equilibrium_index(vec) << '\n'; + + vec = {1,2,3,4,5}; + std::cout << equilibrium_index(vec) << '\n'; + + vec = {2,4,2}; + std::cout << equilibrium_index(vec) << '\n'; + + return 0; +} diff --git a/challenge-160/deadmarshal/lua/ch-1.lua b/challenge-160/deadmarshal/lua/ch-1.lua new file mode 100644 index 0000000000..215934f630 --- /dev/null +++ b/challenge-160/deadmarshal/lua/ch-1.lua @@ -0,0 +1,29 @@ +if #arg == 0 then + io.stderr:write('No args provided!') + os.exit(1) +end + +if tonumber(arg[1]) > 9 then + io.stderr:write('Number must be less than 10!') + os.exit(1) +end + +function first_to_upper(str) + return (str:gsub("^%l", string.upper)) +end + +function four_is_magic(n) + assert(type(n) == "number", "n must be a number!") + local t = {'one', 'two', 'three', 'four', 'five', 'six', 'seven', + 'eight', 'nine'} + local tbl, len = {}, #t[n] + repeat + tbl[#tbl+1] = t[n] .. ' is ' .. t[len] + n = len + len = #t[n] + until n == 4 + tbl[#tbl+1] = 'four is magic.' + return first_to_upper(table.concat(tbl, ', ')) +end + +print(four_is_magic(tonumber(arg[1]))) diff --git a/challenge-160/deadmarshal/lua/ch-2.lua b/challenge-160/deadmarshal/lua/ch-2.lua new file mode 100644 index 0000000000..9682d9c617 --- /dev/null +++ b/challenge-160/deadmarshal/lua/ch-2.lua @@ -0,0 +1,24 @@ +function array_sum(t) + assert(type(t) == "table", "t must be a table!") + local sum = 0 + for i=1, #t do sum = sum + t[i] end + return sum +end + +function equilibrium_index(t) + assert(type(t) == "table", "t must be a table!") + local left, right, ret = 0, array_sum(t), -1 + for i,j in pairs(t) do + right = right - j + if left == right then + ret = i + break + end + left = left + j + end + return ret +end + +print(equilibrium_index({1,3,5,7,9})) +print(equilibrium_index({1,2,3,4,5})) +print(equilibrium_index({2,4,2})) diff --git a/challenge-160/deadmarshal/pascal/ch1.pas b/challenge-160/deadmarshal/pascal/ch1.pas new file mode 100644 index 0000000000..2d597b733a --- /dev/null +++ b/challenge-160/deadmarshal/pascal/ch1.pas @@ -0,0 +1,44 @@ +program Ch1; + +{$mode objfpc} + +uses + SysUtils,Types; +var + Input:Integer; + +function FourIsMagic(N:Integer):AnsiString; +var + Words:TStringDynArray = ('one','two','three','four','five', + 'six','seven','eight','nine'); + Len:Integer; +begin + Result := ''; + Len := Length(Words[N-1]); + repeat + Result := Result + Words[N-1] + ' is ' + Words[Len-1] + ', '; + N := Len; + Len := Length(Words[N-1]); + until(N = 4); + Result := Result + 'four is magic.'; + Result[1] := UpCase(Result[1]); +end; + +begin + if ParamCount < 1 then + begin + WriteLn(StdErr, 'No args provided!'); + Halt(1); + end; + if StrToInt(ParamStr(1)) > 9 then + begin + WriteLn(StdErr, 'Number must be less than 10!'); + Halt(1); + end; + try + Input := StrToInt(ParamStr(1)); + except on E:EConvertError do + Halt(1); + end; + WriteLn(FourIsMagic(Input)); +end. diff --git a/challenge-160/deadmarshal/pascal/ch2.pas b/challenge-160/deadmarshal/pascal/ch2.pas new file mode 100644 index 0000000000..c531d8a3a3 --- /dev/null +++ b/challenge-160/deadmarshal/pascal/ch2.pas @@ -0,0 +1,47 @@ +program Ch2; + +{$mode objfpc} + +uses + SysUtils,Types; +var + Arr:TIntegerDynArray; + +function ArraySum(Arr:TIntegerDynArray):Integer; +var + I:Integer; +begin + Result := 0; + for I := Low(Arr) to High(Arr) do Result := Result + Arr[I]; +end; + +function EquilibriumIndex(Arr:TIntegerDynArray):Integer; +var + I,Left,Right:Integer; +begin + Result := -1; + Left := 0; + Right := ArraySum(Arr); + I := 0; + while(I < High(Arr)) do + begin + Right := Right - Arr[I]; + if(Left = Right) then + begin + Result := I; + break; + end; + Left := Left + Arr[I]; + Inc(I); + end; +end; + +begin + Arr := TIntegerDynArray.Create(1,3,5,7,9); + WriteLn(EquilibriumIndex(Arr)); + Arr := TIntegerDynArray.Create(1,2,3,4,5); + WriteLn(EquilibriumIndex(Arr)); + Arr := TIntegerDynArray.Create(2,4,2); + WriteLn(EquilibriumIndex(Arr)); +end. + diff --git a/challenge-160/deadmarshal/perl/ch-1.pl b/challenge-160/deadmarshal/perl/ch-1.pl new file mode 100644 index 0000000000..955c4d47a6 --- /dev/null +++ b/challenge-160/deadmarshal/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Lingua::EN::Numbers qw(num2en); + +die "No args provided!" unless @ARGV == 1; +die "$ARGV[0] must be less than 10!" if $ARGV[0] > 10; + +sub four_is_magic{ + my ($n) = @_; + my @arr; + my $len = length(num2en($n)); + do{ + push @arr, num2en($n) . ' is ' . num2en($len); + $n = $len; + $len = length(num2en($n)); + }until($n == 4); + push @arr, "four is magic."; + return ucfirst join ', ', @arr; +} + +print four_is_magic($ARGV[0]); diff --git a/challenge-160/deadmarshal/perl/ch-2.pl b/challenge-160/deadmarshal/perl/ch-2.pl new file mode 100644 index 0000000000..af7b28dcdd --- /dev/null +++ b/challenge-160/deadmarshal/perl/ch-2.pl @@ -0,0 +1,20 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use List::Util qw(sum0); + +sub equilibrium_index{ + my ($arr) = @_; + my ($left, $right) = (0, sum0 @$arr); + my $ret; + while(my ($i, $j) = each @$arr){ + $right -= $j; + $ret = $i if $left == $right; + $left += $j; + } + return $ret ? $ret : -1; +} + +print equilibrium_index([1, 3, 5, 7, 9]), "\n"; +print equilibrium_index([1, 2, 3, 4, 5]), "\n"; +print equilibrium_index([2, 4, 2]), "\n"; diff --git a/challenge-160/deadmarshal/raku/ch-1.raku b/challenge-160/deadmarshal/raku/ch-1.raku new file mode 100644 index 0000000000..1ce4863220 --- /dev/null +++ b/challenge-160/deadmarshal/raku/ch-1.raku @@ -0,0 +1,23 @@ +#!/usr/bin/env raku +use Lingua::EN::Numbers; + +sub four-is-magic(Int $n is copy){ + my @arr; + my $len = cardinal($n).chars; + repeat { + push @arr, cardinal($n) ~ ' is ' ~ cardinal($len); + $n = $len; + $len = cardinal($n).chars; + }until ($n == 4); + push @arr, 'four is magic.'; + return tclc join ', ', @arr; +} + +sub MAIN(Int $num) +{ + if $num > 9 { + note "argument must be less than 10!"; + exit 1; + } + put four-is-magic($num.Int); +} diff --git a/challenge-160/robert-dicicco/julia/ch-1.jl b/challenge-160/robert-dicicco/julia/ch-1.jl new file mode 100644 index 0000000000..3764c453c8 --- /dev/null +++ b/challenge-160/robert-dicicco/julia/ch-1.jl @@ -0,0 +1,50 @@ +#!julia.exe + +# AUTHOR +# Robert DiCicco +# DATE: 12-APR-2022 +# Challenge 160 Four Is Magic ( Julia ) + +num_name = Dict( 1 => "One", + 2 => "Two", + 3 => "Three", + 4 => "Four", + 5 => "Five", + 6 => "Six", + 7 => "Seven", + 8 => "Eight", + 9 => "Nine", + ) + +function checkArgs(args) + global num + + try + num = parse(Int64, args[1]) + catch + println("Error: Argument must be an integer") + exit(0) + finally + if num <= 0 || num > 9 + println("Error: Argument must be 1 <= n <= 9") + exit(0) + else + main(num) + end + end +end + +function main(n) + println("Input: \$n = $n") + while n != 4 + n_name = get(num_name,n,1) + len = length(n_name) + l_name = get(num_name,len,1) + print("$n_name is $l_name, ") + n = len + end + + println("Four is magic.") +end + +checkArgs(ARGS) diff --git a/challenge-160/robert-dicicco/julia/ch-2.jl b/challenge-160/robert-dicicco/julia/ch-2.jl new file mode 100644 index 0000000000..017a63bbc4 --- /dev/null +++ b/challenge-160/robert-dicicco/julia/ch-2.jl @@ -0,0 +1,59 @@ +#!julia.exe + +# AUTHOR: Robert DiCicco +# DATE: 12-AP:R-2022 +# Challenge 160 Equilibrium Index ( Julia ) + +function leftsum(a, i ) + sum = 0 + + while i > 0 + sum += a[i] + i = i - 1 + end + + return sum +end + +function rightsum(a, i) + sum = 0 + + while i <= length(a) + sum += a[i] + i = i + 1 + end + + return sum +end + +function balance(a) + flag = 0 + len = length(a) + + for i in 2:len-1 + leftval = leftsum(a, i) + rightval = rightsum(a, i) + + if (leftval == rightval) + i -= 1 # compare sums, subtract 1 to compensate for julia array numbering that starts at 1.... + println("Output: $i\n") + flag = flag + 1 # increment flag if equal + break + end + end + + if ( flag == 0 ) + println("Output: -1 as no Equilibrium Index found.\n") + return + end +end + +function main(a) + println("Input: @n = $a") + balance(a) +end + +arr = [[1,3,5,7,9], [1,2,3,4,5], [2,4,2] ] + +x = collect(arr) +foreach(main,x) diff --git a/challenge-160/robert-dicicco/ruby/ch-1.rb b/challenge-160/robert-dicicco/ruby/ch-1.rb new file mode 100644 index 0000000000..b5399e934d --- /dev/null +++ b/challenge-160/robert-dicicco/ruby/ch-1.rb @@ -0,0 +1,18 @@ +#!ruby.exe + +# AUTHOR: Robert DiCicco +# DATE: 11-APR-2022 +# Challenge 160 Four Is Magic ( Ruby ) + +require 'humanize' + +n = ARGV[0].to_i +puts "Input \$n = #{n}" + +while n != 4 do + n_len = n.humanize.length + print "#{n.humanize.capitalize} is #{n_len.humanize.capitalize}, "; + n = n_len +end + +puts "Four is magic." diff --git a/challenge-160/robert-dicicco/ruby/ch-2.rb b/challenge-160/robert-dicicco/ruby/ch-2.rb new file mode 100644 index 0000000000..dd84de1c87 --- /dev/null +++ b/challenge-160/robert-dicicco/ruby/ch-2.rb @@ -0,0 +1,54 @@ +#!ruby.exe + +# AUTHOR: Robert DiCicco +# DATE: 12=APR-2022 +# Challenge 160 Equilibrium Index ( Ruby ) + +def leftsum ( a, i) + sum = 0 + + while ( i >= 0 ) + sum += a[i] + i = i - 1 + end + + return sum +end + +def rightsum ( a, i) + sum = 0 + + while ( i < a.length ) + sum += a[i] + i = i + 1 + end + + return sum +end + +def balance ( a ) + flag = 0 + len = a.length + + for i in 1..len-2 do + leftval = leftsum(a, i ) + rightval = rightsum(a, i ) + if (leftval == rightval) # compare sums + print "Output: #{i}\n\n" + flag = flag + 1 # increment flag if equal + break + end + end + + if ( flag == 0 ) + print "Output: -1 as no Equilibrium Index found.\n\n" + return + end +end + +arr = [ [1,3,5,7,9], [1,2,3,4,5], [2,4,2] ] + +arr.each do |subarr| + print "Input: \@n = #{subarr}\n" + balance(subarr) +end diff --git a/stats/pwc-challenge-049.json b/stats/pwc-challenge-049.json index e469b803f6..13a5b01667 100644 --- a/stats/pwc-challenge-049.json +++ b/stats/pwc-challenge-049.json @@ -1,53 +1,49 @@ { - "chart" : { - "type" : "column" - }, "subtitle" : { - "text" : "[Champions: 35] Last updated at 2021-01-01 19:38:18 GMT" + "text" : "[Champions: 36] Last updated at 2022-04-13 02:42:34 GMT" }, "tooltip" : { "head |
