From e721971cb16b00e3877f484017d8b8001f53edaf Mon Sep 17 00:00:00 2001 From: 冯昶 Date: Mon, 11 Mar 2024 11:48:41 +0800 Subject: challenge 260, raku solutions --- challenge-260/feng-chang/raku/ch-1.raku | 6 ++++++ challenge-260/feng-chang/raku/ch-2.raku | 5 +++++ challenge-260/feng-chang/raku/test.raku | 24 ++++++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100755 challenge-260/feng-chang/raku/ch-1.raku create mode 100755 challenge-260/feng-chang/raku/ch-2.raku create mode 100755 challenge-260/feng-chang/raku/test.raku diff --git a/challenge-260/feng-chang/raku/ch-1.raku b/challenge-260/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..5a4a5a375b --- /dev/null +++ b/challenge-260/feng-chang/raku/ch-1.raku @@ -0,0 +1,6 @@ +#!/bin/env raku + +unit sub MAIN(*@ints); + +my @a = @ints.Bag.values; +put +(+@a.unique == +@a); diff --git a/challenge-260/feng-chang/raku/ch-2.raku b/challenge-260/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..12823d8afe --- /dev/null +++ b/challenge-260/feng-chang/raku/ch-2.raku @@ -0,0 +1,5 @@ +#!/bin/env raku + +unit sub MAIN(Str:D $w); + +put $w.comb.permutations».join.unique.sort.first($w, :k) + 1; diff --git a/challenge-260/feng-chang/raku/test.raku b/challenge-260/feng-chang/raku/test.raku new file mode 100755 index 0000000000..49b576a879 --- /dev/null +++ b/challenge-260/feng-chang/raku/test.raku @@ -0,0 +1,24 @@ +#!/bin/env raku + +# The Weekly Challenge 260 +use Test; + +sub pwc-test(Str:D $script, Bool :$deeply? = False, *@input) { + my ($expect, $assertion) = @input.splice(*-2, 2); + my $p = run $script, |@input, :out; + if $deeply { + is-deeply $p.out.slurp(:close).chomp.words.Bag, $expect, $assertion; + } else { + is $p.out.slurp(:close).chomp, $expect, $assertion; + } +} + +# Task 1, Unique Occurrences +pwc-test './ch-1.raku', <1 2 2 1 1 3>, 1, 'Unique Occurrences: (1,2,2,1,1,3) => 1'; +pwc-test './ch-1.raku', <1 2 3>, 0, 'Unique Occurrences: (1,2,3) => 0'; +pwc-test './ch-1.raku', <-- -2 0 1 -2 1 1 0 1 -2 9>, 1, 'Unique Occurrences: (-2,0,1,-2,1,1,0,1,-2,9) => 1'; + +# Task 2, Dictionary Rank +pwc-test './ch-2.raku', 'CAT', 3, 'Dictionary Rank: CAT => 3'; +pwc-test './ch-2.raku', 'GOOGLE', 88, 'Dictionary Rank: GOOGLE => 88'; +pwc-test './ch-2.raku', 'SECRET', 255, 'Dictionary Rank: SECRET => 255'; -- cgit From 203cde4aa1b9923988b6576eff6efd4adc5f3a70 Mon Sep 17 00:00:00 2001 From: Andrew Shitov Date: Mon, 18 Mar 2024 08:01:07 +0100 Subject: Raku solutions by ash --- challenge-261/ash/raku/ch-1.raku | 19 +++++++++++++++++++ challenge-261/ash/raku/ch-2.raku | 20 ++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 challenge-261/ash/raku/ch-1.raku create mode 100644 challenge-261/ash/raku/ch-2.raku diff --git a/challenge-261/ash/raku/ch-1.raku b/challenge-261/ash/raku/ch-1.raku new file mode 100644 index 0000000000..1c6a73c9ac --- /dev/null +++ b/challenge-261/ash/raku/ch-1.raku @@ -0,0 +1,19 @@ +# Solution to the Task 1 of The Weekly Challenge 261 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-261/#TASK1 + +# Test run: +# $ raku ch-1.raku +# 36 +# 9 +# 0 + +my @tests = + (1, 2, 3, 45), + (1, 12, 3), + (1, 2, 3, 4); + +for @tests -> @test { + my $sum = [+] @test; + my $dig = [+]((@test.map: *.comb).flat); + say ($dig - $sum).abs; +} diff --git a/challenge-261/ash/raku/ch-2.raku b/challenge-261/ash/raku/ch-2.raku new file mode 100644 index 0000000000..6864850b42 --- /dev/null +++ b/challenge-261/ash/raku/ch-2.raku @@ -0,0 +1,20 @@ +# Solution to the Task 2 of The Weekly Challenge 261 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-261/#TASK2 + +# Test run: +# $ raku ch-2.raku +# 24 +# 8 +# 2 + +my @tests = + ((5,3,6,1,12), 3), + ((1,2,4,3), 1), + ((5,6,7), 2); + +for @tests -> (@ints, $start is copy) { + my $ints = @ints.Bag; + + $start *= 2 while $ints{$start}; + say $start; +} -- cgit From 6828eb73e0cc1583b5e03dfb28af5afee549c695 Mon Sep 17 00:00:00 2001 From: pme Date: Mon, 18 Mar 2024 08:43:19 +0100 Subject: challenge-261 --- challenge-261/peter-meszaros/perl/ch-1.pl | 74 +++++++++++++++++++++++++++++++ challenge-261/peter-meszaros/perl/ch-2.pl | 72 ++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100755 challenge-261/peter-meszaros/perl/ch-1.pl create mode 100755 challenge-261/peter-meszaros/perl/ch-2.pl diff --git a/challenge-261/peter-meszaros/perl/ch-1.pl b/challenge-261/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..976027a7f6 --- /dev/null +++ b/challenge-261/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,74 @@ +#!/usr/bin/env perl +# +# +# You are given an array of integers, @ints. +# +# Write a script to evaluate the absolute difference between element and digit +# sum of the given array. +# Example 1 +# +# Input: @ints = (1,2,3,45) +# Output: 36 +# +# Element Sum: 1 + 2 + 3 + 45 = 51 +# Digit Sum: 1 + 2 + 3 + 4 + 5 = 15 +# Absolute Difference: | 51 - 15 | = 36 +# +# Example 2 +# +# Input: @ints = (1,12,3) +# Output: 9 +# +# Element Sum: 1 + 12 + 3 = 16 +# Digit Sum: 1 + 1 + 2 + 3 = 7 +# Absolute Difference: | 16 - 7 | = 9 +# +# Example 3 +# +# Input: @ints = (1,2,3,4) +# Output: 0 +# +# Element Sum: 1 + 2 + 3 + 4 = 10 +# Digit Sum: 1 + 2 + 3 + 4 = 10 +# Absolute Difference: | 10 - 10 | = 0 +# +# Example 4 +# +# Input: @ints = (236, 416, 336, 350) +# Output: 1296 +# + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [1, 2, 3, 45], + [1, 12, 3], + [1, 2, 3, 4], + [236, 416, 336, 350], +]; + +sub element_digit_sum +{ + my $l = shift; + + my $elem_sum = 0; + my $digi_sum = 0; + + for my $e (@$l) { + $elem_sum += $e; + $digi_sum += $_ for split('', $e); + } + + return abs($elem_sum - $digi_sum); +} + +is(element_digit_sum($cases->[0]), 36, 'Example 1'); +is(element_digit_sum($cases->[1]), 9, 'Example 2'); +is(element_digit_sum($cases->[2]), 0, 'Example 3'); +is(element_digit_sum($cases->[3]), 1296, 'Example 4'); +done_testing(); + +exit 0; diff --git a/challenge-261/peter-meszaros/perl/ch-2.pl b/challenge-261/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..1cc344c748 --- /dev/null +++ b/challenge-261/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,72 @@ +#!/usr/bin/env perl +# +# You are given an array of integers, @ints and an integer $start.. +# +# Write a script to do the followings: +# +# a) Look for $start in the array @ints, if found multiply the number by 2 +# b) If not found stop the process otherwise repeat +# +# In the end return the final value. +# Example 1 +# +# Input: @ints = (5,3,6,1,12) and $start = 3 +# Output: 24 +# +# Step 1: 3 is in the array so 3 x 2 = 6 +# Step 2: 6 is in the array so 6 x 2 = 12 +# Step 3: 12 is in the array so 12 x 2 = 24 +# +# 24 is not found in the array so return 24. +# +# Example 2 +# +# Input: @ints = (1,2,4,3) and $start = 1 +# Output: 8 +# +# Step 1: 1 is in the array so 1 x 2 = 2 +# Step 2: 2 is in the array so 2 x 2 = 4 +# Step 3: 4 is in the array so 4 x 2 = 8 +# +# 8 is not found in the array so return 8. +# +# Example 3 +# +# Input: @ints = (5,6,7) and $start = 2 +# Output: 2 +# +# 2 is not found in the array so return 2. +# + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[5, 3, 6, 1, 12], 3], + [[1, 2, 4, 3], 1], + [[5, 6, 7], 2], +]; + +sub multiply_by_two +{ + my ($l, $s) = $_[0]->@*; + + START: + for my $i (@$l) { + if ($i == $s) { + $s *= 2; + goto START; + } + } + return $s; +} + +is(multiply_by_two($cases->[0]), 24, 'Example 1'); +is(multiply_by_two($cases->[1]), 8, 'Example 2'); +is(multiply_by_two($cases->[2]), 2, 'Example 3'); +done_testing(); + +exit 0; + -- cgit From 4a75f843201aae3aab3d097ece0436c6a537304b Mon Sep 17 00:00:00 2001 From: 冯昶 Date: Mon, 18 Mar 2024 15:55:29 +0800 Subject: challenge 261, raku solutions --- challenge-261/feng-chang/raku/ch-1.raku | 5 +++++ challenge-261/feng-chang/raku/ch-2.raku | 7 +++++++ challenge-261/feng-chang/raku/test.raku | 25 +++++++++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100755 challenge-261/feng-chang/raku/ch-1.raku create mode 100755 challenge-261/feng-chang/raku/ch-2.raku create mode 100755 challenge-261/feng-chang/raku/test.raku diff --git a/challenge-261/feng-chang/raku/ch-1.raku b/challenge-261/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..b913f2f35c --- /dev/null +++ b/challenge-261/feng-chang/raku/ch-1.raku @@ -0,0 +1,5 @@ +#!/bin/env raku + +unit sub MAIN(*@ints); + +put @ints.sum - @ints».comb».sum.sum; diff --git a/challenge-261/feng-chang/raku/ch-2.raku b/challenge-261/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..fbb2ac591f --- /dev/null +++ b/challenge-261/feng-chang/raku/ch-2.raku @@ -0,0 +1,7 @@ +#!/bin/env raku + +unit sub MAIN(*@ints); + +my $start = @ints.pop; +$start *= 2 while @ints.grep($start); +put $start; diff --git a/challenge-261/feng-chang/raku/test.raku b/challenge-261/feng-chang/raku/test.raku new file mode 100755 index 0000000000..d78a96d7db --- /dev/null +++ b/challenge-261/feng-chang/raku/test.raku @@ -0,0 +1,25 @@ +#!/bin/env raku + +# The Weekly Challenge 261 +use Test; + +sub pwc-test(Str:D $script, Bool :$deeply? = False, *@input) { + my ($expect, $assertion) = @input.splice(*-2, 2); + my $p = run $script, |@input, :out; + if $deeply { + is-deeply $p.out.slurp(:close).chomp.words.Bag, $expect, $assertion; + } else { + is $p.out.slurp(:close).chomp, $expect, $assertion; + } +} + +# Task 1, Element Digit Sum +pwc-test './ch-1.raku', <1 2 3 45>, 36, 'Element Digit Sum: (1,2,3,45) => 36'; +pwc-test './ch-1.raku', <1 12 3>, 9, 'Element Digit Sum: (1,12,3) => 9'; +pwc-test './ch-1.raku', <1 2 3 4>, 0, 'Element Digit Sum: (1,2,3,4) => 0'; +pwc-test './ch-1.raku', <236 416 336 350>, 1296, 'Element Digit Sum: (236, 416, 336, 350) => 1296'; + +# Task 2, Multiply by Two +pwc-test './ch-2.raku', <5 3 6 1 12>, 3, 24, 'Multiply by Two: @ints=(5,3,6,1,12), $start=3 => 24'; +pwc-test './ch-2.raku', <1 2 4 3>, 1, 8, 'Multiply by Two: @ints=(1,2,4,3), $start=1 => 8'; +pwc-test './ch-2.raku', <5 6 7>, 2, 2, 'Multiply by Two: @ints=(5,6,7), $start=2 => 2'; -- cgit From 1284596f93434a7747874691e0734fccc968e425 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 18 Mar 2024 08:58:33 +0000 Subject: Challenge 261 Solutions (Raku) --- challenge-261/mark-anderson/blog-2.md | 132 ----------------------------- challenge-261/mark-anderson/raku/ch-1.raku | 12 +++ challenge-261/mark-anderson/raku/ch-2.raku | 9 ++ 3 files changed, 21 insertions(+), 132 deletions(-) delete mode 100644 challenge-261/mark-anderson/blog-2.md create mode 100644 challenge-261/mark-anderson/raku/ch-1.raku create mode 100644 challenge-261/mark-anderson/raku/ch-2.raku diff --git a/challenge-261/mark-anderson/blog-2.md b/challenge-261/mark-anderson/blog-2.md deleted file mode 100644 index 55b5699cf5..0000000000 --- a/challenge-261/mark-anderson/blog-2.md +++ /dev/null @@ -1,132 +0,0 @@ -# Weekly Challenge #260 - -### Task 2: Dictionary Rank -**Submitted by: Mark Anderson** - -You are given a word, ```$word```. - -Write a script to compute the dictionary rank of the given word. - -#### Example 1 -``` -Input: $word = 'CAT' -Output: 3 - -All possible combinations of the letters: -CAT, CTA, ATC, TCA, ACT, TAC - -Arrange them in alphabetical order: -ACT, ATC, CAT, CTA, TAC, TCA - -CAT is the 3rd in the list. -Therefore the dictionary rank of CAT is 3. -``` - -#### Example 2 -``` -Input: $word = 'GOOGLE' -Output: 88 -``` - -#### Example 3 -``` -Input: $word = 'SECRET' -Output: 255 -``` - ---- - -### Solution - -One approach is to create all permutations, sort them, and find the index of ```$word```. - -This is fine for short words but there's a better solution for long words. - -There are numerous videos on youtube explaining the algorithm - I think this a good one [https://www.youtube.com/watch?v=-MpL0X3AHAs](https://www.youtube.com/watch?v=-MpL0X3AHAs) - -Here's the gist of the algorithm: - -1. Find the rank of each letter. - - ``` - G O O G L E - 1 3 3 1 2 0 - ``` - -2. For each letter, find the number of letters to its right that have a lower rank. - - ``` - G O O G L E - 1 3 3 1 1 0 - ``` - -3. For each letter, take the number of repeating letters from that letter to the end of the string. - Take the factorial of each result and multiply them together. - - ``` - G O O G L E - 2!*2! 2! 1! 1! 1! 1! - ``` - - Starting with the first letter, there are 2 Gs and 2 Os so we end up with 2!*2!. - The next letter is O. From that letter to the end of the string there is just the repeating O so we end up with 2! - and so on. - -4. Take the terms from step 2 and divide them by the terms from step 3. - - ``` - G O O G L E - 1/4 3/2 3/1 1/1 1/1 0/1 - ``` - -5. Create the sequence ```$word```.end...0 and take the factorial of each term. - - ``` - G O O G L E - 5! 4! 3! 2! 1! 0! - ``` - -6. Multipy the terms from step 4 with the terms from step 5. - - ``` - G O O G L E - 120/4 (3*24)/2 3*6 1*2 1*1 0*1 - ``` - -7. Sum the terms from step 6 and add 1 for a result of 88. - ``` - G O O G L E - 30 + 36 + 18 + 2 + 1 + 0 + 1 = 88 - ``` -### My translation to Raku: - -``` -#!/usr/bin/env raku -use experimental :cached; - -say rank('google'); - -sub postfix:($n) is cached { [*] 1..$n } - -sub rank($word) -{ - my @w = $word.comb; - my @ranks = @w.sort.squish.antipairs.Map{@w}; - my $bag = @ranks.BagHash; - - my @n = gather for @ranks -> $r - { - my @less-than = $bag.keys.grep(* < $r); - take ([+] $bag{@less-than}) / ([*] $bag.values>>!); - $bag{$r}-- - } - - 1 + [+] @n Z* (@ranks.end...0)>>! -} -``` - -[The full program.](https://github.com/manwar/perlweeklychallenge-club/blob/master/challenge-260/mark-anderson/raku/ch-2.raku) - -Thank you for reading my solution to the [Weekly Challenge #260 Task #2.](https://theweeklychallenge.org/blog/perl-weekly-challenge-260/) - -*-Mark Anderson* diff --git a/challenge-261/mark-anderson/raku/ch-1.raku b/challenge-261/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..c1144ce45b --- /dev/null +++ b/challenge-261/mark-anderson/raku/ch-1.raku @@ -0,0 +1,12 @@ +#!/usr/bin/env raku +use Test; + +is element-digit-sum(1,2,3,45), 36; +is element-digit-sum(1,12,3), 9; +is element-digit-sum(1,2,3,4), 0; +is element-digit-sum(236,416,336,350), 1296; + +sub element-digit-sum(+@ints) +{ + abs ([+] @ints) - [+] @ints.comb(//) +} diff --git a/challenge-261/mark-anderson/raku/ch-2.raku b/challenge-261/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..4f453b02d7 --- /dev/null +++ b/challenge-261/mark-anderson/raku/ch-2.raku @@ -0,0 +1,9 @@ +#!/usr/bin/env raku +use Test; + +is multiply-by-two([5,3,6,1,12], 3), 24; +is multiply-by-two([1,2,4,3], 1), 8; +is multiply-by-two([5,6,7], 2), 2; + +multi multiply-by-two(@i, $s) { return $s } +multi multiply-by-two(@i, $s where $s (elem) @i) { samewith(@i, $s * 2) } -- cgit From ee68d75999a2bca24bf0d900513936d9170f6f7c Mon Sep 17 00:00:00 2001 From: Peter Campbell Smith Date: Mon, 18 Mar 2024 11:50:16 +0000 Subject: Week 261 challenge --- challenge-261/peter-campbell-smith/blog.txt | 1 + challenge-261/peter-campbell-smith/perl/ch-1.pl | 37 +++++++++++++++++++++++++ challenge-261/peter-campbell-smith/perl/ch-2.pl | 32 +++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 challenge-261/peter-campbell-smith/blog.txt create mode 100755 challenge-261/peter-campbell-smith/perl/ch-1.pl create mode 100755 challenge-261/peter-campbell-smith/perl/ch-2.pl diff --git a/challenge-261/peter-campbell-smith/blog.txt b/challenge-261/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..292afaaa8c --- /dev/null +++ b/challenge-261/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/261 diff --git a/challenge-261/peter-campbell-smith/perl/ch-1.pl b/challenge-261/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..d7d2b79ee1 --- /dev/null +++ b/challenge-261/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2024-03-18 +use utf8; # Week 261 - task 1 - Element digit sum +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; + +my (@ints, $j); + +element_digit_sum(1, 2, 3, 45); +element_digit_sum(1, 12, 3); +element_digit_sum(1, 2, 3, 4); +element_digit_sum(236, 416, 336, 350); + +# longer random example +for $j (0 .. 49) { + $ints[$j] = int(rand(1000)); +} +element_digit_sum(@ints); + +sub element_digit_sum { + + my (@ints, $digit_sum, $element_sum, $difference); + + # calculate sums + @ints = @_; + $element_sum += $_ for @ints; + $digit_sum += $_ for split('', join('', @ints)); + + # calculate difference + $difference = abs($element_sum - $digit_sum); + + say qq[\nInput: \@ints = (] . join(', ', @ints) . qq[)\n] . + qq[Output: $difference (element sum = $element_sum, digit sum = $digit_sum)]; +} diff --git a/challenge-261/peter-campbell-smith/perl/ch-2.pl b/challenge-261/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..0995b67779 --- /dev/null +++ b/challenge-261/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2024-03-18 +use utf8; # Week 261 - task 2 - Multiply by 2 +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; + +multiply_by_2([5, 3, 6, 1, 12], 3); +multiply_by_2([1, 2, 4, 3], 1); +multiply_by_2([5, 6, 7], 2); +multiply_by_2([64, 256, 4, 64, 128, 2, 16, 32, 8], 2); + +sub multiply_by_2 { + + my (@ints, $start, $string); + + # initialise + @ints = @{$_[0]}; + $start = $_[1]; + + # join @ints as string + $string = ',' . join(',', @ints) . ','; + + # search string as per task + $start *= 2 while ($string =~ m|,$start,|); + + say qq[\nInput: \@ints = (] . join(', ', @ints) . + qq[}, \$start = $_[1]]; + say qq[Output: $start]; +} -- cgit From b63339d7d4f13572bf0087b32a07f5760aa132af Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 18 Mar 2024 13:31:17 +0100 Subject: Solve 261: Element Digit Sum & Multiple by Two by E. Choroba --- challenge-261/e-choroba/perl/ch-1.pl | 17 +++++++++++++++++ challenge-261/e-choroba/perl/ch-2.pl | 15 +++++++++++++++ 2 files changed, 32 insertions(+) create mode 100755 challenge-261/e-choroba/perl/ch-1.pl create mode 100755 challenge-261/e-choroba/perl/ch-2.pl diff --git a/challenge-261/e-choroba/perl/ch-1.pl b/challenge-261/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..7d99202d5e --- /dev/null +++ b/challenge-261/e-choroba/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use List::Util qw{ sum }; + +sub element_digit_sum(@ints) { + abs(sum(@ints) - sum(map { split // } @ints)) +} + +use Test::More tests => 4; + +is element_digit_sum(1, 2, 3, 45), 36, 'Example 1'; +is element_digit_sum(1, 12, 3), 9, 'Example 2'; +is element_digit_sum(1, 2, 3, 4), 0, 'Example 3'; +is element_digit_sum(236, 416, 336, 350), 1296, 'Example 4'; diff --git a/challenge-261/e-choroba/perl/ch-2.pl b/challenge-261/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..2cca2ac3f6 --- /dev/null +++ b/challenge-261/e-choroba/perl/ch-2.pl @@ -0,0 +1,15 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub multiple_by_two($start, @ints) { + $start *= 2 while grep $_ == $start, @ints; + return $start +} + +use Test::More tests => 3; + +is multiple_by_two(3, 5, 3, 6, 1, 12), 24, 'Example 1'; +is multiple_by_two(1, 1, 2, 4, 3), 8, 'Example 2'; +is multiple_by_two(2, 5, 6, 7), 2, 'Example 3'; -- cgit From 1e9f31b6fcbe385e10c3cde2d47d41d453ec5c68 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 18 Mar 2024 08:20:37 -0600 Subject: Solve PWC261 --- challenge-261/wlmb/blog.txt | 1 + challenge-261/wlmb/perl/ch-1.pl | 13 +++++++++++++ challenge-261/wlmb/perl/ch-2.pl | 15 +++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 challenge-261/wlmb/blog.txt create mode 100755 challenge-261/wlmb/perl/ch-1.pl create mode 100755 challenge-261/wlmb/perl/ch-2.pl diff --git a/challenge-261/wlmb/blog.txt b/challenge-261/wlmb/blog.txt new file mode 100644 index 0000000000..caa8f9c76e --- /dev/null +++ b/challenge-261/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2024/03/18/PWC261/ diff --git a/challenge-261/wlmb/perl/ch-1.pl b/challenge-261/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..be54582864 --- /dev/null +++ b/challenge-261/wlmb/perl/ch-1.pl @@ -0,0 +1,13 @@ +#!/usr/bin/env perl +# Perl weekly challenge 261 +# Task 1: Element Digit Sum +# +# See https://wlmb.github.io/2024/03/18/PWC261/#task-1-element-digit-sum +use v5.36; +use List::Util qw(sum0); +die <<~"FIN" unless @ARGV; + usage: $0 N1 [N2...] + to find the diference between the sum of the numbers N1+N2+... + and the sum of their digits + FIN +say "@ARGV -> ", sum0 map {$_-sum0 split""} @ARGV; diff --git a/challenge-261/wlmb/perl/ch-2.pl b/challenge-261/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..52d50e3b1e --- /dev/null +++ b/challenge-261/wlmb/perl/ch-2.pl @@ -0,0 +1,15 @@ +#!/usr/bin/env perl +# Perl weekly challenge 261 +# Task 2: Multiply by Two +# +# See https://wlmb.github.io/2024/03/18/PWC261/#task-2-multiply-by-two +use v5.36; +use List::Util qw(first); +die <<~"FIN" unless @ARGV >= 2; + Usage: $0 S N1 [N2...] + to find the smallest number S*2^n that is not in the list N1 N2... + FIN +my ($start, @ints)=@ARGV; +print "Start=$start, ints=@ints -> "; +$start *= 2 while defined first {$_==$start} @ints; +say $start; -- cgit From 212e2e6526d40477e2c89106aa3e25da4a51ad46 Mon Sep 17 00:00:00 2001 From: pme Date: Mon, 18 Mar 2024 15:44:30 +0100 Subject: challenge-236 --- challenge-236/peter-meszaros/perl/ch-1.pl | 88 +++++++++++++++++++++++++++++++ challenge-236/peter-meszaros/perl/ch-2.pl | 85 +++++++++++++++++++++++++++++ 2 files changed, 173 insertions(+) create mode 100755 challenge-236/peter-meszaros/perl/ch-1.pl create mode 100755 challenge-236/peter-meszaros/perl/ch-2.pl diff --git a/challenge-236/peter-meszaros/perl/ch-1.pl b/challenge-236/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..345d5eeb55 --- /dev/null +++ b/challenge-236/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,88 @@ +#!/usr/bin/env perl +# +# You are asked to sell juice each costs $5. You are given an array of bills. +# You can only sell ONE juice to each customer but make sure you return exact +# change back. You only have $5, $10 and $20 notes. You do not have any change in +# hand at first. +# +# Write a script to find out if it is possible to sell to each customers with +# correct change. +# Example 1 +# +# Input: @bills = (5, 5, 5, 10, 20) +# Output: true +# +# From the first 3 customers, we collect three $5 bills in order. +# From the fourth customer, we collect a $10 bill and give back a $5. +# From the fifth customer, we give a $10 bill and a $5 bill. +# Since all customers got correct change, we output true. +# +# Example 2 +# +# Input: @bills = (5, 5, 10, 10, 20) +# Output: false +# +# From the first two customers in order, we collect two $5 bills. +# For the next two customers in order, we collect a $10 bill and give back a $5 bill. +# For the last customer, we can not give the change of $15 back because we only +# have two $10 bills. +# Since not every customer received the correct change, the answer is false. +# +# Example 3 +# +# Input: @bills = (5, 5, 5, 20) +# Output: true +# + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [5, 5, 5, 10, 20], + [5, 5, 10, 10, 20], + [5, 5, 5, 20], +]; + +sub exact_change +{ + my $bill = shift; + + my %change = ( + 5 => 0, + 10 => 0, + 20 => 0, + ); + my $ret = 1; + for my $bill (@$bill) { + ++$change{$bill}; + if ($bill == 10) { + if ($change{5}) { + --$change{5}; + } else { + $ret = 0; + last; + } + } elsif ($bill == 20) { + if ($change{5} && $change{10}) { + --$change{5}; + --$change{10}; + } elsif ($change{5} >= 3) { + $change{5} -= 3; + } else { + $ret = 0; + last; + } + } + } + + return $ret; +} + +is(exact_change($cases->[0]), 1, 'Example 1'); +is(exact_change($cases->[1]), 0, 'Example 2'); +is(exact_change($cases->[2]), 1, 'Example 3'); +done_testing(); + +exit 0; diff --git a/challenge-236/peter-meszaros/perl/ch-2.pl b/challenge-236/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..d258a37fbc --- /dev/null +++ b/challenge-236/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,85 @@ +#!/usr/bin/env perl +# +# You are given an array of unique integers. +# +# Write a script to determine how many loops are in the given array. +# +# To determine a loop: Start at an index and take the number at # +# array[index] and then proceed to that index and continue this until you end up +# at the starting index. +# +# +# Example 1 +# +# Input: @ints = (4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10) +# Output: 3 +# +# To determine the 1st loop, start at index 0, the number at that index is 4, +# proceed to index 4, the number at that index is 15, proceed to index 15 and so +# on until you're back at index 0. +# +# Loops are as below: +# [4 15 1 6 13 5 0] +# [3 8 7 18 9 16 12 17 2] +# [14 11 19 10] +# +# Example 2 +# +# Input: @ints = (0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19) +# Output: 6 +# +# Loops are as below: +# [0] +# [1] +# [13 9 14 17 18 15 5 8 2] +# [7 11 4 6 10 16 3] +# [12] +# [19] +# +# Example 3 +# +# Input: @ints = (9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17) +# Output: 1 +# +# Loop is as below: +# [9 4 5 7 19 17 15 1 8 12 18 6 13 2 3 11 10 14 16 0] +# + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + #0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + [4, 6, 3, 8, 15, 0, 13, 18, 7, 16, 14, 19, 17, 5, 11, 1, 12, 2, 9, 10], + [0, 1, 13, 7, 6, 8, 10, 11, 2, 14, 16, 4, 12, 9, 17, 5, 3, 18, 15, 19], + [9, 8, 3, 11, 5, 7, 13, 19, 12, 4, 14, 10, 18, 2, 16, 1, 0, 15, 6, 17], +]; + +sub array_loops +{ + my $l = shift; + + my $nloop = 0; + my %visited; + for my $start (0..$#$l) { + next if defined $visited{$start}; + my $next = $l->[$start]; + while ($next != $start || not defined $visited{$next}) { + ++$visited{$next}; + $next = $l->[$next]; + } + ++$nloop if $next == $start; + } + + return $nloop; +} + +is(array_loops($cases->[0]), 3, 'Example 1'); +is(array_loops($cases->[1]), 6, 'Example 2'); +is(array_loops($cases->[2]), 1, 'Example 3'); +done_testing(); + +exit 0; + -- cgit From 1eb85f19ecea3b92d910fde90d3256597949703e Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 18 Mar 2024 12:01:17 -0400 Subject: Week 261 --- challenge-261/zapwai/c/ch-1.c | 29 +++++++++++++++++++++++++++++ challenge-261/zapwai/c/ch-2.c | 26 ++++++++++++++++++++++++++ challenge-261/zapwai/javascript/ch-1.js | 25 +++++++++++++++++++++++++ challenge-261/zapwai/javascript/ch-2.js | 22 ++++++++++++++++++++++ challenge-261/zapwai/perl/ch-1.pl | 15 +++++++++++++++ challenge-261/zapwai/perl/ch-2.pl | 19 +++++++++++++++++++ challenge-261/zapwai/python/ch-1.py | 12 ++++++++++++ challenge-261/zapwai/python/ch-2.py | 14 ++++++++++++++ challenge-261/zapwai/rust/ch-1.rs | 26 ++++++++++++++++++++++++++ challenge-261/zapwai/rust/ch-2.rs | 21 +++++++++++++++++++++ 10 files changed, 209 insertions(+) create mode 100644 challenge-261/zapwai/c/ch-1.c create mode 100644 challenge-261/zapwai/c/ch-2.c create mode 100644 challenge-261/zapwai/javascript/ch-1.js create mode 100644 challenge-261/zapwai/javascript/ch-2.js create mode 100644 challenge-261/zapwai/perl/ch-1.pl create mode 100644 challenge-261/zapwai/perl/ch-2.pl create mode 100644 challenge-261/zapwai/python/ch-1.py create mode 100644 challenge-261/zapwai/python/ch-2.py create mode 100644 challenge-261/zapwai/rust/ch-1.rs create mode 100644 challenge-261/zapwai/rust/ch-2.rs diff --git a/challenge-261/zapwai/c/ch-1.c b/challenge-261/zapwai/c/ch-1.c new file mode 100644 index 0000000000..2cc1f36c05 --- /dev/null +++ b/challenge-261/zapwai/c/ch-1.c @@ -0,0 +1,29 @@ +#include +#include + +int digits_sum(int* ints, int ints_len) { + int sum = 0; + for (int i = 0; i < ints_len; i++) { + do { + sum += ints[i] % 10; + ints[i] /= 10; + } while (ints[i] != 0); + } + return sum; +} + +int sum(int* ints, int ints_len) { + int sum = 0; + for (int i = 0; i < ints_len; i++) + sum += ints[i]; + return sum; +} + +int main() { + int ints[] = {1,2,3,45}; + int ints_len = sizeof(ints) / sizeof(int); + printf("Input: ints = { "); + for (int i = 0; i < ints_len; i++) + printf("%d ", ints[i]); + printf("}\nOutput: %d\n", abs(sum(ints, ints_len) - digits_sum(ints, ints_len))); +} diff --git a/challenge-261/zapwai/c/ch-2.c b/challenge-261/zapwai/c/ch-2.c new file mode 100644 index 0000000000..efdb33fe13 --- /dev/null +++ b/challenge-261/zapwai/c/ch-2.c @@ -0,0 +1,26 @@ +#include +#include + +int double_me (int* ints, int ints_len, int start) { + bool flag = true; + while (flag) { + flag = false; + for (int i = 0; i < ints_len; i++) { + if (ints[i] == start) { + flag = true; + start *= 2; + } + } + } + return start; +} + +int main() { + int ints[] = {5, 3, 6, 1, 12}; + int start = 3; + printf("Input: ints = { "); + int ints_len = sizeof(ints) / sizeof(int); + for (int i = 0; i < ints_len; i++) + printf("%d ", ints[i]); + printf("}, start = %d\nOutput: %d\n", start, double_me(ints, ints_len, start)); +} diff --git a/challenge-261/zapwai/javascript/ch-1.js b/challenge-261/zapwai/javascript/ch-1.js new file mode 100644 index 0000000000..5b2c54cd9f --- /dev/null +++ b/challenge-261/zapwai/javascript/ch-1.js @@ -0,0 +1,25 @@ +function digits_sum(ints) { + let sum = 0; + for (let i = 0; i < ints.length; i++) { + do { + sum += ints[i] % 10; + ints[i] = Math.floor(ints[i] / 10); + } while (ints[i] != 0); + } + return sum; +} + +function sum(ints) { + let sum = 0; + for (let i = 0; i < ints.length; i++) + sum += ints[i]; + return sum; +} + +function main() { + let ints = [1,2,3,45]; + console.log("Input:", ints); + console.log("Output:", Math.abs(sum(ints) - digits_sum(ints))); +} + +main(); diff --git a/challenge-261/zapwai/javascript/ch-2.js b/challenge-261/zapwai/javascript/ch-2.js new file mode 100644 index 0000000000..e0dd478f78 --- /dev/null +++ b/challenge-261/zapwai/javascript/ch-2.js @@ -0,0 +1,22 @@ +function double_me (ints, start) { + flag = true; + while (flag) { + flag = false; + for (let i = 0; i < ints.length; i++) { + if (ints[i] == start) { + flag = true; + start *= 2; + } + } + } + return start; +} + +function main() { + let ints = [5, 3, 6, 1, 12]; + let start = 3; + console.log("Input: ints:", ints, "start:", start); + console.log("Output:", double_me(ints, start)); +} + +main(); diff --git a/challenge-261/zapwai/perl/ch-1.pl b/challenge-261/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..f0320d1b6d --- /dev/null +++ b/challenge-261/zapwai/perl/ch-1.pl @@ -0,0 +1,15 @@ +use v5.38; +use List::Util "sum"; + +sub digit_sum(@ints) { + my $sum = 0; + foreach (@ints) { + my @p = split "", $_; + $sum += $_ foreach (@p); + } + return $sum; +} + +my @ints = (1,2,3,45); +say "Input: @ints"; +say "Output: ", abs(digit_sum(@ints) - sum @ints); diff --git a/challenge-261/zapwai/perl/ch-2.pl b/challenge-261/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..3ba1a8bb01 --- /dev/null +++ b/challenge-261/zapwai/perl/ch-2.pl @@ -0,0 +1,19 @@ +use v5.38; +my @ints = (5,3,6,1,12); +my $start = 3; +doubles_scan(@ints); + +sub doubles_scan(@ints) { + say "Input: \@ints = @ints, \$start = $start"; + my $flag = 1; + do { + $start *= 2; + if (grep { $_ == $start } @ints) { + $flag = 1; + } else { + $flag = 0; + } + } while ($flag); + say "Output: $start"; +} + diff --git a/challenge-261/zapwai/python/ch-1.py b/challenge-261/zapwai/python/ch-1.py new file mode 100644 index 0000000000..fd3877e363 --- /dev/null +++ b/challenge-261/zapwai/python/ch-1.py @@ -0,0 +1,12 @@ +def digit_sum(ints): + sum = 0 + for integer in ints: + while integer != 0: + sum += integer % 10; + integer = int(integer / 10); + return sum + +ints = [1,2,3,45]; +print("Input:", ints); +print("Output:", abs(digit_sum(ints) - sum(ints))); + diff --git a/challenge-261/zapwai/python/ch-2.py b/challenge-261/zapwai/python/ch-2.py new file mode 100644 index 0000000000..1048ae11cd --- /dev/null +++ b/challenge-261/zapwai/python/ch-2.py @@ -0,0 +1,14 @@ +def double_me(ints,start): + flag = True + while flag: + flag = False + for i in ints: + if i == start: + flag = True + start *= 2 + return start + +ints = [5, 3, 6, 1, 12] +start = 3; +print("Input:", ints, "start:", start) +print("Output:", double_me(ints, start)) diff --git a/challenge-261/zapwai/rust/ch-1.rs b/challenge-261/zapwai/rust/ch-1.rs new file mode 100644 index 0000000000..7cc0b3da3d --- /dev/null +++ b/challenge-261/zapwai/rust/ch-1.rs @@ -0,0 +1,26 @@ +fn digits_sum(ints :&Vec) -> i32 { + let mut sum = 0; + for i in 0 .. ints.len() { + let mut val = ints[i]; + while val != 0 { + sum += val % 10; + val /= 10; + } + } + return sum; +} + +fn sum(ints :&Vec) -> i32 { + let mut sum = 0; + for i in 0 .. ints.len() { + sum += ints[i]; + } + return sum; +} + +fn main() { + let ints = vec![1,2,3,45]; + println!("Input: ints = {:?}", ints); + let value = sum(&ints) - digits_sum(&ints); + println!("Output: {}", value.abs()); +} diff --git a/challenge-261/zapwai/rust/ch-2.rs b/challenge-261/zapwai/rust/ch-2.rs new file mode 100644 index 0000000000..72158822de --- /dev/null +++ b/challenge-261/zapwai/rust/ch-2.rs @@ -0,0 +1,21 @@ +fn double_me (ints :&Vec, start :&mut i32) -> i32 { + let mut flag = true; + while flag { + flag = false; + for i in ints { + if i == start { + flag = true; + *start *= 2; + } + } + } + return *start; +} + +fn main() { + let ints = vec![5, 3, 6, 1, 12]; + let mut start = 3; + println!("Input: ints = {:?}, start = {start}", ints); + println!("Output: {}", double_me(&ints, &mut start)); +} + -- cgit From 3f1e8ce748cd9e440d7741ee9c032c55a74467fc Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 18 Mar 2024 16:38:08 +0000 Subject: Challenge 261 Solutions (Raku) --- challenge-261/mark-anderson/raku/ch-2.raku | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/challenge-261/mark-anderson/raku/ch-2.raku b/challenge-261/mark-anderson/raku/ch-2.raku index 4f453b02d7..8254214c62 100644 --- a/challenge-261/mark-anderson/raku/ch-2.raku +++ b/challenge-261/mark-anderson/raku/ch-2.raku @@ -5,5 +5,6 @@ is multiply-by-two([5,3,6,1,12], 3), 24; is multiply-by-two([1,2,4,3], 1), 8; is multiply-by-two([5,6,7], 2), 2; -multi multiply-by-two(@i, $s) { return $s } -multi multiply-by-two(@i, $s where $s (elem) @i) { samewith(@i, $s * 2) } +multi multiply-by-two(@i, $s) { samewith(@i.BagHash, $s) } +multi multiply-by-two($b, $s) { return $s } +multi multiply-by-two($b, $s where ?$b{$s}) { $b{(0..$s)}:delete; samewith($b, $s*2) } -- cgit From b91a4623cd4c2b51f779f59b53310a0bd745f03f Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Mon, 18 Mar 2024 17:15:14 +0000 Subject: - Added solutions by Eric Cheung. - Added solutions by Ulrich Rieke. - Added solutions by Andrew Shitov. - Added solutions by Peter Meszaros. - Added solutions by Feng Chang. - Added solutions by Mark Anderson. - Added solutions by Peter Campbell Smith. - Added solutions by E. Choroba. - Added solutions by W. Luis Mochan. - Added solutions by David Ferrone. --- challenge-261/eric-cheung/python/ch-1.py | 13 + challenge-261/eric-cheung/python/ch-2.py | 18 + challenge-261/ulrich-rieke/cpp/ch-1.cpp | 30 + challenge-261/ulrich-rieke/cpp/ch-2.cpp | 35 + challenge-261/ulrich-rieke/haskell/ch-1.hs | 17 + challenge-261/ulrich-rieke/haskell/ch-2.hs | 13 + challenge-261/ulrich-rieke/perl/ch-1.pl | 13 + challenge-261/ulrich-rieke/perl/ch-2.pl | 17 + challenge-261/ulrich-rieke/raku/ch-1.raku | 8 + challenge-261/ulrich-rieke/raku/ch-2.raku | 13 + challenge-261/ulrich-rieke/rust/ch-1.rs | 27 + challenge-261/ulrich-rieke/rust/ch-2.rs | 22 + stats/pwc-challenge-236.json | 531 ++++---- stats/pwc-challenge-260.json | 635 ++++++++++ stats/pwc-current.json | 603 ++------- stats/pwc-language-breakdown-summary.json | 60 +- stats/pwc-language-breakdown.json | 1837 ++++++++++++++-------------- stats/pwc-leaders.json | 456 +++---- stats/pwc-summary-1-30.json | 106 +- stats/pwc-summary-121-150.json | 120 +- stats/pwc-summary-151-180.json | 110 +- stats/pwc-summary-181-210.json | 26 +- stats/pwc-summary-211-240.json | 114 +- stats/pwc-summary-241-270.json | 104 +- stats/pwc-summary-271-300.json | 114 +- stats/pwc-summary-301-330.json | 36 +- stats/pwc-summary-31-60.json | 112 +- stats/pwc-summary-61-90.json | 98 +- stats/pwc-summary-91-120.json | 102 +- stats/pwc-summary.json | 60 +- 30 files changed, 2962 insertions(+), 2488 deletions(-) create mode 100755 challenge-261/eric-cheung/python/ch-1.py create mode 100755 challenge-261/eric-cheung/python/ch-2.py create mode 100755 challenge-261/ulrich-rieke/cpp/ch-1.cpp create mode 100755 challenge-261/ulrich-rieke/cpp/ch-2.cpp create mode 100755 challenge-261/ulrich-rieke/haskell/ch-1.hs create mode 100755 challenge-261/ulrich-rieke/haskell/ch-2.hs create mode 100755 challenge-261/ulrich-rieke/perl/ch-1.pl create mode 100755 challenge-261/ulrich-rieke/perl/ch-2.pl create mode 100755 challenge-261/ulrich-rieke/raku/ch-1.raku create mode 100755 challenge-261/ulrich-rieke/raku/ch-2.raku create mode 100755 challenge-261/ulrich-rieke/rust/ch-1.rs create mode 100755 challenge-261/ulrich-rieke/rust/ch-2.rs create mode 100644 stats/pwc-challenge-260.json diff --git a/challenge-261/eric-cheung/python/ch-1.py b/challenge-261/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..348279526f --- /dev/null +++ b/challenge-261/eric-cheung/python/ch-1.py @@ -0,0 +1,13 @@ + +## arrInt = [1, 2, 3, 45] ## Example 1 +## arrInt = [1, 12, 3] ## Example 2 +## arrInt = [1, 2, 3, 4] ## Example 3 +arrInt = [236, 416, 336, 350] ## Example 4 + +arrSplit = [] +for nLoop in arrInt: + arrSplit = arrSplit + [int(elem) for elem in str(nLoop)] + +## print (sum(arrInt)) +## print (sum(arrSplit)) +print (abs(sum(arrInt) - sum(arrSplit))) diff --git a/challenge-261/eric-cheung/python/ch-2.py b/challenge-261/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..a275000b69 --- /dev/null +++ b/challenge-261/eric-cheung/python/ch-2.py @@ -0,0 +1,18 @@ + +## Example 1 +## arrInt = [5, 3, 6, 1, 12] +## nStart = 3 + +## Example 2 +## arrInt = [1, 2, 4, 3] +## nStart = 1 + +## Example 3 +arrInt = [5, 6, 7] +nStart = 2 + +nFinal = nStart +while nFinal in arrInt: + nFinal = nFinal * 2 + +print (nFinal) diff --git a/challenge-261/ulrich-rieke/cpp/ch-1.cpp b/challenge-261/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..a088bfc147 --- /dev/null +++ b/challenge-261/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include + +int digitsum( int n ) { + int sum = 0 ; + while ( n != 0 ) { + sum += n % 10 ; + n /= 10 ; + } + return sum ; +} + +int main( ) { + std::cout << "Enter some integers, separated by blanks!\n" ; + std::cout << "Enter e to end!\n" ; + std::vector numbers {std::istream_iterator{std::cin} , + std::istream_iterator{}} ; + int element_sum = std::accumulate( numbers.begin( ) , numbers.end( ) , + 0 ) ; + std::vector digitsums ; + for ( int i : numbers ) + digitsums.push_back( digitsum( i ) ) ; + int total_digitsums = std::accumulate( digitsums.begin( ) , + digitsums.end( ) , 0 ) ; + std::cout << std::abs( total_digitsums - element_sum) << '\n' ; + return 0 ; +} diff --git a/challenge-261/ulrich-rieke/cpp/ch-2.cpp b/challenge-261/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..e5a956a286 --- /dev/null +++ b/challenge-261/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include + +std::vector split( const std::string & startline , + const std::string & sep ) { + std::vector separated ; + std::string::size_type start { 0 } ; + std::string::size_type pos ; + do { + pos = startline.find_first_of( sep , start ) ; + separated.push_back( startline.substr(start , pos - start )) ; + start = pos + 1 ; + } while ( pos != std::string::npos ) ; + return separated ; +} + +int main( ) { + std::cout << "Enter some integers, separated by blanks!\n" ; + std::string numberline ; + std::getline( std::cin , numberline ) ; + std::vector innumbers { split( numberline , " " ) } ; + std::vector numbers ; + for ( auto w : innumbers ) + numbers.push_back( std::stoi( w ) ) ; + std::cout << "Enter a start number!\n" ; + int start ; + std::cin >> start ; + while ( std::find( numbers.begin( ) , numbers.end( ) , start ) != + numbers.end( ) ) + start *= 2 ; + std::cout << start << '\n' ; + return 0 ; +} diff --git a/challenge-261/ulrich-rieke/haskell/ch-1.hs b/challenge-261/ulrich-rieke/haskell/ch-1.hs new file mode 100755 index 0000000000..515d9bb0d0 --- /dev/null +++ b/challenge-261/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,17 @@ +module Challenge261 + where +import Data.Char( digitToInt ) + +solution :: [Int] -> Int +solution list = abs ( elementsums - digitsums ) + where + elementsums :: Int + elementsums = sum list + digitsums :: Int + digitsums = sum $ map ( sum . map digitToInt . show ) list + +main :: IO ( ) +main = do + putStrLn "Enter some integers, separated by blanks!" + numbers <- getLine + print $ solution ( map read $ words numbers ) diff --git a/challenge-261/ulrich-rieke/haskell/ch-2.hs b/challenge-261/ulrich-rieke/haskell/ch-2.hs new file mode 100755 index 0000000000..13f141fd25 --- /dev/null +++ b/challenge-261/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,13 @@ +module Challenge261_2 + where + +solution :: [Int] -> Int -> Int +solution list start = until (\n -> not $ elem n list ) (* 2 ) start + +main :: IO ( ) +main = do + putStrLn "Enter some numbers, separated by blanks!" + numberstrings <- getLine + putStrLn "Enter a start number!" + startline <- getLine + print $ solution ( map read $ words numberstrings ) ( read startline ) diff --git a/challenge-261/ulrich-rieke/perl/ch-1.pl b/challenge-261/ulrich-rieke/perl/ch-1.pl new file mode 100755 index 0000000000..2239702a1c --- /dev/null +++ b/challenge-261/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,13 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; +use List::Util qw ( sum ) ; + +say "Enter some integers, separated by blanks!" ; +my $line = ; +chomp $line ; +my @numbers = split( /\s+/ , $line ) ; +my $element_sum = sum( @numbers) ; +my $digit_sum = sum( map { sum( split( // , $_ )) } @numbers ) ; +say abs( $element_sum - $digit_sum ) ; diff --git a/challenge-261/ulrich-rieke/perl/ch-2.pl b/challenge-261/ulrich-rieke/perl/ch-2.pl new file mode 100755 index 0000000000..40cbf5801d --- /dev/null +++ b/challenge-261/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; +use List::Util qw ( any ) ; + +say "Enter some integers, separated by blanks!" ; +my $line = ; +chomp $line ; +my @numbers = split( /\s+/ , $line ) ; +say "Enter a start number!" ; +my $start = ; +chomp $start ; +while ( any { $_ == $start } @numbers ) { + $start *= 2 ; +} +say $start ; diff --git a/challenge-261/ulrich-rieke/raku/ch-1.raku b/challenge-261/ulrich-rieke/raku/ch-1.raku new file mode 100755 index 0000000000..9ee268c6a8 --- /dev/null +++ b/challenge-261/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,8 @@ +use v6 ; + +say "Enter some integers, separated by blanks!" ; +my $line = $*IN.get ; +my @numbers = $line.words.map( {.Int} ) ; +my $element_sum = [+] @numbers ; +my $digit_sum = [+] @numbers.map( {[+] $_.Str.comb.map( {.Int} )} ) ; +say abs( $element_sum - $digit_sum ) ; diff --git a/challenge-261/ulrich-rieke/raku/ch-2.raku b/challenge-261/ulrich-rieke/raku/ch-2.raku new file mode 100755 index 0000000000..fda637bc4d --- /dev/null +++ b/challenge-261/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,13 @@ +use v6 ; + +say "Enter some integers, separated by blanks!" ; +my $line = $*IN.get ; +my @numbers = $line.words.map( {.Int} ) ; +say "Enter a start number!" ; +my $startn = $*IN.get ; +my $start = +$startn ; +my $found = $start ; +while ( so $found == @numbers.any ) { + $found *= 2 ; +} +say $found ; diff --git a/challenge-261/ulrich-rieke/rust/ch-1.rs b/challenge-261/ulrich-rieke/rust/ch-1.rs new file mode 100755 index 0000000000..aca36f9ab7 --- /dev/null +++ b/challenge-261/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,27 @@ +use std::io ; + +fn digitsum( num : i32 ) -> i32 { + let mut sum : i32 = 0 ; + let mut n : i32 = num ; + while n != 0 { + sum = sum + n % 10 ; + n = n / 10 ; + } + sum +} + +fn main() { + println!("Enter some integers, separated by blanks!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let entered_line : &str = &*inline ; + let numbers : Vec = entered_line.split_whitespace( ).map( | s | + s.trim( ).parse::( ).unwrap( )).collect( ) ; + let element_sum : i32 = numbers.iter( ).sum( ) ; + let mut digit_sum : i32 = 0 ; + numbers.iter( ).map( | d | { + let ds : i32 = digitsum( *d ) ; + ds + }).for_each( | n | digit_sum += n ) ; + println!("{}" , (element_sum - digit_sum).abs( ) ) ; +} diff --git a/challenge-261/ulrich-rieke/rust/ch-2.rs b/challenge-261/ulrich-rieke/rust/ch-2.rs new file mode 100755 index 0000000000..f5edd77a6d --- /dev/null +++ b/challenge-261/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,22 @@ +use std::io ; + +fn main() { + println!("Enter some integers, separated by blanks!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let entered_line : &str = &*inline ; + let numbers : Vec = entered_line.split_whitespace( ).map( | s | + s.trim( ).parse::( ).unwrap( )).collect( ) ; + println!("Enter a start number!") ; + let mut numline : String = String::new( ) ; + io::stdin( ).read_line( &mut numline ).unwrap( ) ; + let nline : &str = &*numline ; + let start : i32 = nline.trim( ).parse::( ).unwrap( ) ; + let mut found : i32 = start ; + let mut f = numbers.iter( ).find( | &d | *d == found ) ; + while f.is_some( ) { + found = found * 2 ; + f = numbers.iter( ).find( | &d | *d == found ) ; + } + println!("{}" , found ) ; +} diff --git a/stats/pwc-challenge-236.json b/stats/pwc-challenge-236.json index 29545b1188..296c35b4e3 100644 --- a/stats/pwc-challenge-236.json +++ b/stats/pwc-challenge-236.json @@ -1,17 +1,226 @@ { + "xAxis" : { + "type" : "category" + }, + "subtitle" : { + "text" : "[Champions: 36] Last updated at 2024-03-18 17:09:01 GMT" + }, + "title" : { + "text" : "The Weekly Challenge - 236" + }, "plotOptions" : { "series" : { + "borderWidth" : 0, "dataLabels" : { "format" : "{point.y}", "enabled" : 1 - }, - "borderWidth" : 0 + } } }, + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } + }, + "tooltip" : { + "followPointer" : 1, + "pointFormat" : "{point.name}: {point.y:f}
", + "headerFormat" : "{series.name}
" + }, + "chart" : { + "type" : "column" + }, + "series" : [ + { + "data" : [ + { + "drilldown" : "Adam Russell", + "name" : "Adam Russell", + "y" : 3 + }, + { + "name" : "Ali Moradi", + "drilldown" : "Ali Moradi", + "y" : 5 + }, + { + "drilldown" : "Arne Sommer", + "name" : "Arne Sommer", + "y" : 3 + }, + { + "name" : "Athanasius", + "drilldown" : "Athanasius", + "y" : 4 + }, + { + "y" : 3, + "drilldown" : "Avery Adams", + "name" : "Avery Adams" + }, + { + "y" : 4, + "drilldown" : "Bob Lied", + "name" : "Bob Lied" + }, + { + "y" : 2, + "name" : "Bruce Gray", + "drilldown" : "Bruce Gray" + }, + { + "y" : 2, + "name" : "Cheok-Yin Fung", + "drilldown" : "Cheok-Yin Fung" + }, + { + "y" : 2, + "name" : "Dave Jacoby", + "drilldown" : "Dave Jacoby" + }, + { + "y" : 2, + "drilldown" : "David Ferrone", + "name" : "David Ferrone" + }, + { + "y" : 2, + "drilldown" : "E. Choroba", + "name" : "E. Choroba" + }, + { + "name" : "Feng Chang", + "drilldown" : "Feng Chang", + "y" : 2 + }, + { + "name" : "Flavio Poletti", + "drilldown" : "Flavio Poletti", + "y" : 6 + }, + { + "drilldown" : "Jaldhar H. Vyas", + "name" : "Jaldhar H. Vyas", + "y" : 5 + }, + { + "y" : 2, + "name" : "Jan Krnavek", + "drilldown" : "Jan Krnavek" + }, + { + "name" : "Jorg Sommrey", + "drilldown" : "Jorg Sommrey", + "y" : 2 + }, + { + "drilldown" : "Laurent Rosenfeld", + "name" : "Laurent Rosenfeld", + "y" : 6 + }, + { + "y" : 5, + "name" : "Lubos Kolouch", + "drilldown" : "Lubos Kolouch" + }, + { + "y" : 8, + "name" : "Luca Ferrari", + "drilldown" : "Luca Ferrari" + }, + { + "name" : "Mark Anderson", + "drilldown" : "Mark Anderson", + "y" : 2 + }, + { + "name" : "Matthew Neleigh", + "drilldown" : "Matthew Neleigh", + "y" : 2 + }, + { + "drilldown" : "Matthias Muth", + "name" : "Matthias Muth", + "y" : 3 + }, + { + "y" : 2, + "drilldown" : "Niels van Dijke", + "name" : "Niels van Dijke" + }, + { + "name" : "Packy Anderson", + "drilldown" : "Packy Anderson", + "y" : 5 + }, + { + "drilldown" : "Peter Campbell Smith", + "name" : "Peter Campbell Smith", + "y" : 3 + }, + { + "y" : 2, + "name" : "Peter Meszaros", + "drilldown" : "Peter Meszaros" + }, + { + "drilldown" : "rcmlz", + "name" : "rcmlz", + "y" : 2 + }, + { + "drilldown" : "Robert DiCicco", + "name" : "Robert DiCicco", + "y" : 4 + }, + { + "y" : 2, + "drilldown" : "Robert Ransbottom", + "name" : "Robert Ransbottom" + }, + { + "name" : "Roger Bell_West", + "drilldown" : "Roger Bell_West", + "y" : 5 + }, + { + "y" : 4, + "name" : "Shimon Bollinger", + "drilldown" : "Shimon Bollinger" + }, + { + "y" : 3, + "name" : "Simon Green", + "drilldown" : "Simon Green" + }, + { + "y" : 4, + "drilldown" : "Thomas Kohler", + "name" : "Thomas Kohler" + }, + { + "y" : 3, + "name" : "W. Luis Mochan", + "drilldown" : "W. Luis Mochan" + }, + { + "y" : 2, + "drilldown" : "Wanderdoc", + "name" : "Wanderdoc" + }, + { + "drilldown" : "Yves Orton", + "name" : "Yves Orton", + "y" : 2 + } + ], + "colorByPoint" : 1, + "name" : "The Weekly Challenge - 236" + } + ], "drilldown" : { "series" : [ { - "name" : "Adam Russell", "data" : [ [ "Perl", @@ -22,10 +231,11 @@ 1 ] ], - "id" : "Adam Russell" + "id" : "Adam Russell", + "name" : "Adam Russell" }, { - "name" : "Ali Moradi", + "id" : "Ali Moradi", "data" : [ [ "Perl", @@ -40,9 +250,10 @@ 1 ] ], - "id" : "Ali Moradi" + "name" : "Ali Moradi" }, { + "id" : "Arne Sommer", "data" : [ [ "Raku", @@ -53,11 +264,11 @@ 1 ] ], - "id" : "Arne Sommer", "name" : "Arne Sommer" }, { "name" : "Athanasius", + "id" : "Athanasius", "data" : [ [ "Perl", @@ -67,10 +278,10 @@ "Raku", 2 ] - ], - "id" : "Athanasius" + ] }, { + "name" : "Avery Adams", "id" : "Avery Adams", "data" : [ [ @@ -81,11 +292,9 @@ "Blog", 2 ] - ], - "name" : "Avery Adams" + ] }, { - "name" : "Bob Lied", "id" : "Bob Lied", "data" : [ [ @@ -96,27 +305,28 @@ "Blog", 2 ] - ] + ], + "name" : "Bob Lied" }, { - "name" : "Bruce Gray", "id" : "Bruce Gray", "data" : [ [ "Raku", 2 ] - ] + ], + "name" : "Bruce Gray" }, { - "name" : "Cheok-Yin Fung", "data" : [ [ "Perl", 2 ] ], - "id" : "Cheok-Yin Fung" + "id" : "Cheok-Yin Fung", + "name" : "Cheok-Yin Fung" }, { "name" : "Dave Jacoby", @@ -129,34 +339,34 @@ ] }, { - "id" : "David Ferrone", + "name" : "David Ferrone", "data" : [ [ "Perl", 2 ] ], - "name" : "David Ferrone" + "id" : "David Ferrone" }, { - "name" : "E. Choroba", - "id" : "E. Choroba", "data" : [ [ "Perl", 2 ] - ] + ], + "id" : "E. Choroba", + "name" : "E. Choroba" }, { "name" : "Feng Chang", + "id" : "Feng Chang", "data" : [ [ "Raku", 2 ] - ], - "id" : "Feng Chang" + ] }, { "name" : "Flavio Poletti", @@ -177,6 +387,7 @@ "id" : "Flavio Poletti" }, { + "name" : "Jaldhar H. Vyas", "id" : "Jaldhar H. Vyas", "data" : [ [ @@ -191,18 +402,17 @@ "Blog", 1 ] - ], - "name" : "Jaldhar H. Vyas" + ] }, { - "name" : "Jan Krnavek", - "id" : "Jan Krnavek", "data" : [ [ "Raku", 2 ] - ] + ], + "id" : "Jan Krnavek", + "name" : "Jan Krnavek" }, { "id" : "Jorg Sommrey", @@ -215,7 +425,7 @@ "name" : "Jorg Sommrey" }, { - "name" : "Laurent Rosenfeld", + "id" : "Laurent Rosenfeld", "data" : [ [ "Perl", @@ -230,10 +440,9 @@ 2 ] ], - "id" : "Laurent Rosenfeld" + "name" : "Laurent Rosenfeld" }, { - "name" : "Lubos Kolouch", "id" : "Lubos Kolouch", "data" : [ [ @@ -248,9 +457,11 @@ "Blog", 1 ] - ] + ], + "name" : "Lubos Kolouch" }, { + "id" : "Luca Ferrari", "data" : [ [ "Raku", @@ -261,7 +472,6 @@ 6 ] ], - "id" : "Luca Ferrari", "name" : "Luca Ferrari" }, { @@ -275,18 +485,17 @@ ] }, { + "name" : "Matthew Neleigh", + "id" : "Matthew Neleigh", "data" : [ [ "Perl", 2 ] - ], - "id" : "Matthew Neleigh", - "name" : "Matthew Neleigh" + ] }, { "name" : "Matthias Muth", - "id" : "Matthias Muth", "data" : [ [ "Perl", @@ -296,7 +505,8 @@ "Blog", 1 ] - ] + ], + "id" : "Matthias Muth" }, { "id" : "Niels van Dijke", @@ -309,7 +519,6 @@ "name" : "Niels van Dijke" }, { - "name" : "Packy Anderson", "data" : [ [ "Perl", @@ -324,7 +533,8 @@ 1 ] ], - "id" : "Packy Anderson" + "id" : "Packy Anderson", + "name" : "Packy Anderson" }, { "name" : "Peter Campbell Smith", @@ -341,17 +551,26 @@ "id" : "Peter Campbell Smith" }, { - "name" : "rcmlz", + "name" : "Peter Meszaros", + "id" : "Peter Meszaros", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { "data" : [ [ "Raku", 2 ] ], - "id" : "rcmlz" + "id" : "rcmlz", + "name" : "rcmlz" }, { - "name" : "Robert DiCicco", "id" : "Robert DiCicco", "data" : [ [ @@ -362,19 +581,21 @@ "Raku", 2 ] - ] + ], + "name" : "Robert DiCicco" }, { + "id" : "Robert Ransbottom", "data" : [ [ "Raku", 2 ] ], - "id" : "Robert Ransbottom", "name" : "Robert Ransbottom" }, { + "id" : "Roger Bell_West", "data" : [ [ "Perl", @@ -389,7 +610,6 @@ 1 ] ], - "id" : "Roger Bell_West", "name" : "Roger Bell_West" }, { @@ -407,6 +627,8 @@ "name" : "Shimon Bollinger" }, { + "name" : "Simon Green", + "id" : "Simon Green", "data" : [ [ "Perl", @@ -416,11 +638,10 @@ "Blog", 1 ] - ], - "id" : "Simon Green", - "name" : "Simon Green" + ] }, { + "id" : "Thomas Kohler", "data" : [ [ "Perl", @@ -431,7 +652,6 @@ 2 ] ], - "id" : "Thomas Kohler", "name" : "Thomas Kohler" }, { @@ -450,13 +670,13 @@ }, { "name" : "Wanderdoc", + "id" : "Wanderdoc", "data" : [ [ "Perl", 2 ] - ], - "id" : "Wanderdoc" + ] }, { "name" : "Yves Orton", @@ -470,212 +690,7 @@ } ] }, - "title" : { - "text" : "The Weekly Challenge - 236" - }, - "subtitle" : { - "text" : "[Champions: 35] Last updated at 2024-03-04 19:00:00 GMT" - }, - "chart" : { - "type" : "column" - }, "legend" : { "enabled" : 0 - }, - "series" : [ - { - "data" : [ - { - "drilldown" : "Adam Russell", - "y" : 3, - "name" : "Adam Russell" - }, - { - "y" : 5, - "drilldown" : "Ali Moradi", - "name" : "Ali Moradi" - }, - { - "y" : 3, - "drilldown" : "Arne Sommer", -