From eaaf2ee7172c6b56a0d2cf86fc0d8ea538c6be75 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 24 Apr 2023 07:11:55 +0000 Subject: Challenge 214 Solutions (Raku) --- challenge-214/mark-anderson/raku/ch-1.raku | 16 ++++++++++++++++ challenge-214/mark-anderson/raku/ch-2.raku | 27 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 challenge-214/mark-anderson/raku/ch-1.raku create mode 100644 challenge-214/mark-anderson/raku/ch-2.raku (limited to 'challenge-214') diff --git a/challenge-214/mark-anderson/raku/ch-1.raku b/challenge-214/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..aeb0059f6e --- /dev/null +++ b/challenge-214/mark-anderson/raku/ch-1.raku @@ -0,0 +1,16 @@ +#!/usr/bin/env raku +use Test; + +is-deeply rank-score(<1 2 4 3 5>), <5 4 S B G>>>.Str; +is-deeply rank-score(<8 5 6 7 4>), >>.Str; +is-deeply rank-score(<3 5 4 2>), >>.Str; +is-deeply rank-score(<2 5 2 1 7 5 1>), >>.Str; +is-deeply rank-score(<3 5>), ; + +sub rank-score($a) +{ + my $u := $a.unique.sort(-*); + my $s := ...*; + + $a.trans($u => $s.head($u)).comb(//) +} diff --git a/challenge-214/mark-anderson/raku/ch-2.raku b/challenge-214/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..fcc690a760 --- /dev/null +++ b/challenge-214/mark-anderson/raku/ch-2.raku @@ -0,0 +1,27 @@ +#!/usr/bin/env raku +use Adverb::Eject; +use Test; + +is collect-points(2,4,3,3,3,4,5,4,2), 23; +is collect-points(1,2,2,2,2,1), 20; +is collect-points(1), 1; +is collect-points(2,2,2,1,1,2,2,2), 40; +is collect-points(9,1,1,1,35,35,44,1,5,5,2,5,2,5,44,85,7,7,7,85,1,1,1), 91; + +sub collect-points(*@a) +{ + sum gather while @a + { + my $c = @a.pairs.classify({ .value }, :as{ .key }); + my $p = $c.first({ consecutive(.value) }); + my $a = $p ?? $p.value !! $c{ @a.Bag.minpairs.head.key }; + + @a[ $a ]:eject; + take $a.elems ** 2 + } +} + +sub consecutive(@a) +{ + .tail - .head == .end and [<] $_ given @a +} -- cgit From c9649ccd3cfb48e26c19886bb399bd1820715f8c Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Mon, 24 Apr 2023 16:02:44 +0200 Subject: Challenge 214 LK Perl Python --- challenge-214/lubos-kolouch/perl/ch-1.pl | 44 ++++++++++++++++++++++++++++ challenge-214/lubos-kolouch/perl/ch-2.pl | 46 ++++++++++++++++++++++++++++++ challenge-214/lubos-kolouch/python/ch-1.py | 31 ++++++++++++++++++++ challenge-214/lubos-kolouch/python/ch-2.py | 43 ++++++++++++++++++++++++++++ 4 files changed, 164 insertions(+) create mode 100644 challenge-214/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-214/lubos-kolouch/perl/ch-2.pl create mode 100644 challenge-214/lubos-kolouch/python/ch-1.py create mode 100644 challenge-214/lubos-kolouch/python/ch-2.py (limited to 'challenge-214') diff --git a/challenge-214/lubos-kolouch/perl/ch-1.pl b/challenge-214/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..c0b67d75eb --- /dev/null +++ b/challenge-214/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,44 @@ +#!/usr/bin/perl +use strict; +use warnings; +use Test::More; + +sub rank_scores { + my @scores = @_; + my @sorted_scores = sort { $b <=> $a } @scores; + my %ranks; + my $rank = 1; + for my $i (0 .. $#sorted_scores) { + if ($i > 0 && $sorted_scores[$i] == $sorted_scores[$i - 1]) { + $ranks{$sorted_scores[$i]} = $ranks{$sorted_scores[$i - 1]}; + } else { + $ranks{$sorted_scores[$i]} = $rank; + } + $rank++; + } + my @output; + for my $score (@scores) { + if ($ranks{$score} == 1) { + push @output, 'G'; + } elsif ($ranks{$score} == 2) { + push @output, 'S'; + } elsif ($ranks{$score} == 3) { + push @output, 'B'; + } else { + push @output, $ranks{$score}; + } + } + return \@output; +} + +my @test_cases = ( + { input => [1,2,4,3,5], output => ['5','4','S','B','G'] }, + { input => [8,5,6,7,4], output => ['G','4','B','S','5'] }, + { input => [3,5,4,2], output => ['B','G','S','4'] }, + { input => [2,5,2,1,7,5,1], output => ['4','S','4','6','G','S','6'] }, +); + +for my $test_case (@test_cases) { + is_deeply(rank_scores(@{ $test_case->{input} }), $test_case->{output}); +} + diff --git a/challenge-214/lubos-kolouch/perl/ch-2.pl b/challenge-214/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..5314086d5e --- /dev/null +++ b/challenge-214/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,46 @@ +use strict; +use warnings; + +sub max_score { + my @numbers = @_; + return helper(\@numbers, {}); +} + +sub helper { + my ($numbers, $memo) = @_; + + return 0 if scalar(@$numbers) == 0; + + my $key = join(',', @$numbers); + return $memo->{$key} if exists $memo->{$key}; + + my $max_score = 0; + my $i = 0; + while ($i < scalar(@$numbers)) { + my $j = $i; + while ($j < scalar(@$numbers) && $numbers->[$j] == $numbers->[$i]) { + $j++; + } + + my $length = $j - $i; + my @next_numbers = (@$numbers[0 .. $i - 1], @$numbers[$j .. scalar(@$numbers) - 1]); + my $score = $length * $length + helper(\@next_numbers, $memo); + $max_score = $max_score > $score ? $max_score : $score; + $i = $j; + } + + $memo->{$key} = $max_score; + return $max_score; +} + +# Test cases +my @numbers1 = (2, 4, 3, 3, 3, 4, 5, 4, 2); +my @numbers2 = (1, 2, 2, 2, 2, 1); +my @numbers3 = (1); +my @numbers4 = (2, 2, 2, 1, 1, 2, 2, 2); + +print max_score(@numbers1), "\n"; # Output: 23 +print max_score(@numbers2), "\n"; # Output: 20 +print max_score(@numbers3), "\n"; # Output: 1 +print max_score(@numbers4), "\n"; # Output: 40 + diff --git a/challenge-214/lubos-kolouch/python/ch-1.py b/challenge-214/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..710c81ea42 --- /dev/null +++ b/challenge-214/lubos-kolouch/python/ch-1.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +from typing import List + + +def rank_scores(scores: List[int]) -> List[str]: + sorted_scores = sorted(scores, reverse=True) + ranks = {} + rank = 1 + for i in range(len(sorted_scores)): + if i > 0 and sorted_scores[i] == sorted_scores[i - 1]: + ranks[sorted_scores[i]] = ranks[sorted_scores[i - 1]] + else: + ranks[sorted_scores[i]] = rank + rank += 1 + output = [] + for score in scores: + if ranks[score] == 1: + output.append('G') + elif ranks[score] == 2: + output.append('S') + elif ranks[score] == 3: + output.append('B') + else: + output.append(str(ranks[score])) + return output + + +scores = [1, 2, 4, 3, 5] +result = rank_scores(scores) +print(result) diff --git a/challenge-214/lubos-kolouch/python/ch-2.py b/challenge-214/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..72d7207b50 --- /dev/null +++ b/challenge-214/lubos-kolouch/python/ch-2.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + +def max_score(numbers): + + def helper(numbers, memo): + if not numbers: + return 0 + + key = tuple(numbers) + if key in memo: + return memo[key] + + max_score = 0 + i = 0 + while i < len(numbers): + j = i + while j < len(numbers) and numbers[j] == numbers[i]: + j += 1 + + length = j - i + next_numbers = numbers[:i] + numbers[j:] + score = length * length + helper(next_numbers, memo) + max_score = max(max_score, score) + i = j + + memo[key] = max_score + return max_score + + return helper(numbers, {}) + + +# Test cases +numbers1 = [2, 4, 3, 3, 3, 4, 5, 4, 2] +numbers2 = [1, 2, 2, 2, 2, 1] +numbers3 = [1] +numbers4 = [2, 2, 2, 1, 1, 2, 2, 2] + +print(max_score(numbers1)) # Output: 23 +print(max_score(numbers2)) # Output: 20 +print(max_score(numbers3)) # Output: 1 +print(max_score(numbers4)) # Output: 40 -- cgit From b0b0afd59d09807515f79edd6ce3d3cb24d1140c Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 24 Apr 2023 14:28:32 -0400 Subject: Week 214 --- challenge-214/zapwai/perl/ch-1.pl | 64 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 challenge-214/zapwai/perl/ch-1.pl (limited to 'challenge-214') diff --git a/challenge-214/zapwai/perl/ch-1.pl b/challenge-214/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..974552f6e0 --- /dev/null +++ b/challenge-214/zapwai/perl/ch-1.pl @@ -0,0 +1,64 @@ +use v5.30.0; +my @scores = (1,2,4,3,5); + +my @copy = @scores; +my %vals; + +$vals{$_} = 1 foreach (@scores); + +my @ordered_vals = keys %vals; +@ordered_vals = reverse sort @ordered_vals; +my @ranks; +for my $k (0 .. $#scores) { + foreach my $j (0 .. $#ordered_vals) { + $ranks[$k] = $j + 1 if ($scores[$k] == $ordered_vals[$j]); + } +} + +my ($one, $two, $tre); +foreach (0 .. $#ranks) { + if ($ranks[$_] == 1) { + $one++; + } elsif ($ranks[$_] == 2) { + $two++; + } elsif ($ranks[$_] == 3) { + $tre++; + } +} + +if ($one > 3) { + # Assign only gold + foreach (0 .. $#ranks) { + if ($ranks[$_] == 1) { + $ranks[$_] = "G"; + } + } +} elsif ( $one + $two >= 3 ) { + # assign gold and silver + foreach (0 .. $#ranks) { + if ($ranks[$_] == 1) { + $ranks[$_] = "G"; + } elsif ($ranks[$_] == 2) { + $ranks[$_] = "S"; + } + } +} else { + foreach (0 .. $#ranks) { + if ($ranks[$_] == 1) { + $ranks[$_] = "G"; + } elsif ($ranks[$_] == 2) { + $ranks[$_] = "S"; + } elsif ($ranks[$_] == 3) { + $ranks[$_] = "B"; + } + } + +} + +print "\@scores: "; +print foreach (@scores); +say ""; + +print "\@ranks : "; +print foreach (@ranks); +say ""; -- cgit From 9d16fe75b0ec973971729f62695e3600a7e9502c Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 24 Apr 2023 22:37:55 +0000 Subject: ch-2.raku was incorrect --- challenge-214/mark-anderson/raku/ch-2.raku | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 challenge-214/mark-anderson/raku/ch-2.raku (limited to 'challenge-214') diff --git a/challenge-214/mark-anderson/raku/ch-2.raku b/challenge-214/mark-anderson/raku/ch-2.raku deleted file mode 100644 index fcc690a760..0000000000 --- a/challenge-214/mark-anderson/raku/ch-2.raku +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/env raku -use Adverb::Eject; -use Test; - -is collect-points(2,4,3,3,3,4,5,4,2), 23; -is collect-points(1,2,2,2,2,1), 20; -is collect-points(1), 1; -is collect-points(2,2,2,1,1,2,2,2), 40; -is collect-points(9,1,1,1,35,35,44,1,5,5,2,5,2,5,44,85,7,7,7,85,1,1,1), 91; - -sub collect-points(*@a) -{ - sum gather while @a - { - my $c = @a.pairs.classify({ .value }, :as{ .key }); - my $p = $c.first({ consecutive(.value) }); - my $a = $p ?? $p.value !! $c{ @a.Bag.minpairs.head.key }; - - @a[ $a ]:eject; - take $a.elems ** 2 - } -} - -sub consecutive(@a) -{ - .tail - .head == .end and [<] $_ given @a -} -- cgit From 0a056bf9f4653d39f17c4d308ae18e89d4f3a255 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 24 Apr 2023 22:26:27 -0600 Subject: Solve PWC214 --- challenge-214/wlmb/blog.txt | 2 ++ challenge-214/wlmb/perl/ch-1.pl | 17 +++++++++++++++++ challenge-214/wlmb/perl/ch-2.pl | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 challenge-214/wlmb/blog.txt create mode 100755 challenge-214/wlmb/perl/ch-1.pl create mode 100755 challenge-214/wlmb/perl/ch-2.pl (limited to 'challenge-214') diff --git a/challenge-214/wlmb/blog.txt b/challenge-214/wlmb/blog.txt new file mode 100644 index 0000000000..1bdbc7f091 --- /dev/null +++ b/challenge-214/wlmb/blog.txt @@ -0,0 +1,2 @@ +https://wlmb.github.io/2023/04/24/PWC214/ + diff --git a/challenge-214/wlmb/perl/ch-1.pl b/challenge-214/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..036f69a21c --- /dev/null +++ b/challenge-214/wlmb/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/env perl +# Perl weekly challenge 214 +# Task 1: Rank Score +# +# See https://wlmb.github.io/2023/04/24/PWC214/#task-1-rank-score +use v5.36; +die <<~"FIN" unless @ARGV; + Usage: $0 S1 [S2..] + to rank the scores S1 S2... + FIN +my @scores=@ARGV; +my @ranks=(qw(Gold Silver Bronce), 4..@scores); # G S B 4 5 6... +my $counter; +my %score_to_rank; +map {my $current=$ranks[$counter++]; $score_to_rank{$_}//=$current} + sort {$b<=>$a} @scores; +say join " ", @scores, "->", @score_to_rank{@scores} diff --git a/challenge-214/wlmb/perl/ch-2.pl b/challenge-214/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..e94355f79b --- /dev/null +++ b/challenge-214/wlmb/perl/ch-2.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl +# Perl weekly challenge 214 +# Task 2: Collect Points +# +# See https://wlmb.github.io/2023/04/24/PWC214/#task-2-collect-points +use v5.36; +die <<~"FIN" unless @ARGV; + Usage: $0 N1 [N2...] + to collect all points from the list N1 N2... + FIN +# Build list of nodes, one for each group +my @list=@ARGV; +my $first=shift @list; +my @current=($first, 1); # nodes are of the form [value, count] +my @nodes; +for(@list){ + push(@nodes, [@current]), @current=($_, 0) unless $current[0] eq $_; # New node if value changes + $current[1]++; +} +push @nodes, [@current]; # array of pairs [value, count] +# remove all groups with different starting group and choose largest score +my @points = sort {$b <=> $a} map {remove($_, \@nodes)} 0..@nodes-1; +my $points=$points[0]; # largest score +say "@ARGV -> $points"; + +# Get max points after removing all $remaining nodes starting from $which +sub remove($which, $remaining){ + my @copy=@$remaining; + my $points = $copy[$which][1]**2; + # join neighbor groups if possible + $copy[$which-1]=[$copy[$which-1][0],$copy[$which-1][1]+$copy[$which+1][1]], + splice @copy, $which+1,1 + if 0<$which<@copy-1 && $copy[$which-1][0] eq $copy[$which+1][0]; + splice @copy, $which, 1; + my @points= sort {$b <=> $a} map {remove($_, \@copy)} 0..@copy-1; + $points += $points[0] if @points; + return $points; +} -- cgit From a0d9ecf91bcc757cea259d0c869c50d8794d8edd Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Tue, 25 Apr 2023 10:36:40 +0000 Subject: ch-1.raku was incorrect too :-( --- challenge-214/mark-anderson/raku/ch-1.raku | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 challenge-214/mark-anderson/raku/ch-1.raku (limited to 'challenge-214') diff --git a/challenge-214/mark-anderson/raku/ch-1.raku b/challenge-214/mark-anderson/raku/ch-1.raku deleted file mode 100644 index aeb0059f6e..0000000000 --- a/challenge-214/mark-anderson/raku/ch-1.raku +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env raku -use Test; - -is-deeply rank-score(<1 2 4 3 5>), <5 4 S B G>>>.Str; -is-deeply rank-score(<8 5 6 7 4>), >>.Str; -is-deeply rank-score(<3 5 4 2>), >>.Str; -is-deeply rank-score(<2 5 2 1 7 5 1>), >>.Str; -is-deeply rank-score(<3 5>), ; - -sub rank-score($a) -{ - my $u := $a.unique.sort(-*); - my $s := ...*; - - $a.trans($u => $s.head($u)).comb(//) -} -- cgit From c2faea053751fb6a48e1ddaab1476ccac3336376 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 25 Apr 2023 12:01:15 +0100 Subject: - Added solutions by Mark Anderson. - Added solutions by Lubos Kolouch. - Added solutions by David Ferrone. --- challenge-214/eric-cheung/python/ch-1.py | 12 ++++++++++++ challenge-214/eric-cheung/python/ch-2.py | 11 +++++++++++ 2 files changed, 23 insertions(+) create mode 100755 challenge-214/eric-cheung/python/ch-1.py create mode 100755 challenge-214/eric-cheung/python/ch-2.py (limited to 'challenge-214') diff --git a/challenge-214/eric-cheung/python/ch-1.py b/challenge-214/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..ea68026c42 --- /dev/null +++ b/challenge-214/eric-cheung/python/ch-1.py @@ -0,0 +1,12 @@ + +arrInputScore = [1, 2, 4, 3, 5] ## Example 1 +## arrInputScore = [8, 5, 6, 7, 4] ## Example 2 +## arrInputScore = [3, 5, 4, 2] ## Example 3 +## arrInputScore = [2, 5, 2, 1, 7, 5, 1] ## Example 4 + +arrUniqSortScore = list(set(arrInputScore)) +arrUniqSortScore = arrUniqSortScore[::-1] +arrInterRank = [max(arrUniqSortScore.index(nElem), len([1 for nLoop in arrInputScore if nLoop > nElem])) for nElem in arrInputScore] +arrOutputRank = ["G" if nElem == 0 else "S" if nElem == 1 else "B" if nElem == 2 else nElem + 1 for nElem in arrInterRank] + +print (arrOutputRank) diff --git a/challenge-214/eric-cheung/python/ch-2.py b/challenge-214/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..323352eaf9 --- /dev/null +++ b/challenge-214/eric-cheung/python/ch-2.py @@ -0,0 +1,11 @@ + +## arrNum = [2, 4, 3, 3, 3, 4, 5, 4, 2] ## Example 1 +## arrNum = [1, 2, 2, 2, 2, 1] ## Example 2 +## arrNum = [1] ## Example 3 +arrNum = [2, 2, 2, 1, 1, 2, 2, 2] ## Example 4 + +arrUniq = list(set(arrNum)) +arrCount = [arrNum.count(nElem) for nElem in arrUniq] +arrSQ = [nElem * nElem for nElem in arrCount] + +print (sum(arrSQ)) -- cgit From 371a7c6db7e3b1a1d011185ad195f4950054a0a1 Mon Sep 17 00:00:00 2001 From: deoac Date: Tue, 25 Apr 2023 22:10:30 -0400 Subject: Initial skeleton files --- challenge-214/shimon-ben-avraham/raku/ch-1.raku | 48 ++++++++++++++++++++++ challenge-214/shimon-ben-avraham/raku/ch-2.raku | 53 +++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 challenge-214/shimon-ben-avraham/raku/ch-1.raku create mode 100644 challenge-214/shimon-ben-avraham/raku/ch-2.raku (limited to 'challenge-214') diff --git a/challenge-214/shimon-ben-avraham/raku/ch-1.raku b/challenge-214/shimon-ben-avraham/raku/ch-1.raku new file mode 100644 index 0000000000..ae79f4641c --- /dev/null +++ b/challenge-214/shimon-ben-avraham/raku/ch-1.raku @@ -0,0 +1,48 @@ +#! /usr/bin/env raku +use v6.d; + +#=============================================================================== +# +# FILE: <.raku> +# +# DESCRIPTION: +# +# AUTHOR: Shimon Bollinger (deoac.shimon@gmail.com) +# REVISION: Last modified: Tue 25 Apr 2023 10:08:36 PM EDT +#=============================================================================== + +=begin comment + Task 1: Rank Score + Submitted by: Mohammad S Anwar + You are given a list of scores (>=1). + + Write a script to rank each score in descending order. First three will get medals i.e. G (Gold), S (Silver) and B (Bronze). Rest will just get the ranking number. + + Using the standard model of giving equal scores equal rank, then advancing that number of ranks. + + + Example 1 + Input: @scores = (1,2,4,3,5) + Output: (5,4,S,B,G) + + Score 1 is the 5th rank. + Score 2 is the 4th rank. + Score 4 is the 2nd rank i.e. Silver (S). + Score 3 is the 3rd rank i.e. Bronze (B). + Score 5 is the 1st rank i.e. Gold (G). + Example 2 + Input: @scores = (8,5,6,7,4) + Output: (G,4,B,S,5) + + Score 8 is the 1st rank i.e. Gold (G). + Score 4 is the 4th rank. + Score 6 is the 3rd rank i.e. Bronze (B). + Score 7 is the 2nd rank i.e. Silver (S). + Score 4 is the 5th rank. + Example 3 + Input: @list = (3,5,4,2) + Output: (B,G,S,4) + Example 4 + Input: @scores = (2,5,2,1,7,5,1) + Output: (4,S,4,6,G,S,6) +=end comment diff --git a/challenge-214/shimon-ben-avraham/raku/ch-2.raku b/challenge-214/shimon-ben-avraham/raku/ch-2.raku new file mode 100644 index 0000000000..1f5b20126f --- /dev/null +++ b/challenge-214/shimon-ben-avraham/raku/ch-2.raku @@ -0,0 +1,53 @@ +#! /usr/bin/env raku +use v6.d; + +#=============================================================================== +# FiLE: +# +# Description: +# +# Author: () +# Last Modified: Tue 25 Apr 2023 10:08:20 PM EDT +#=============================================================================== + + +=begin comment + Task 2: Collect Points + Submitted by: Mohammad S Anwar + You are given a list of numbers. + + You will perform a series of removal operations. For each operation, you remove from the list N (one or more) equal and consecutive numbers, and add to your score N × N. + + Determine the maximum possible score. + + Example 1: + Input: @numbers = (2,4,3,3,3,4,5,4,2) + Output: 23 + + We see three 3's next to each other so let us remove that first and collect 3 x 3 points. + So now the list is (2,4,4,5,4,2). + Let us now remove 5 so that all 4's can be next to each other and collect 1 x 1 point. + So now the list is (2,4,4,4,2). + Time to remove three 4's and collect 3 x 3 points. + Now the list is (2,2). + Finally remove both 2's and collect 2 x 2 points. + So the total points collected is 9 + 1 + 9 + 4 => 23. + Example 2: + Input: @numbers = (1,2,2,2,2,1) + Output: 20 + + Remove four 2's first and collect 4 x 4 points. + Now the list is (1,1). + Finally remove the two 1's and collect 2 x 2 points. + So the total points collected is 16 + 4 => 20. + Example 3: + Input: @numbers = (1) + Output: 1 + Example 4: + Input: @numbers = (2,2,2,1,1,2,2,2) + Output: 40 + + Remove two 1's = 2 x 2 points. + Now the list is (2,2,2,2,2,2). + Then reomove six 2's = 6 x 6 points. +=end comment -- cgit From 05c74535ed438cd5875cff7acc9df7091c4df68a Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Wed, 26 Apr 2023 04:42:41 +0000 Subject: ch-1.raku do-over --- challenge-214/mark-anderson/raku/ch-1.raku | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 challenge-214/mark-anderson/raku/ch-1.raku (limited to 'challenge-214') diff --git a/challenge-214/mark-anderson/raku/ch-1.raku b/challenge-214/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..94051dcb01 --- /dev/null +++ b/challenge-214/mark-anderson/raku/ch-1.raku @@ -0,0 +1,19 @@ +#!/usr/bin/env raku +use Test; + +is rank-score(<1 2 4 3 5>), <5 4 S B G>; +is rank-score(<8 5 6 7 4>), ; +is rank-score(<3 5 4 2>), ; +is rank-score(<2 5 2 1 7 5 1>), <4 S 4 6 G S 6>; +is rank-score(<22 77 11 55 77 11 22 22 11 22 22>), <4 G 9 B G 9 4 4 9 4 4>; + +sub rank-score($list) +{ + my $b = $list.Bag; + my @a = [\+] flat 1, $b.sort(-*.key)>>.value; + my %h = <1 2 3> Z=> ; + + @a .= map({ %h{$_} ?? %h{$_} !! $_ }); + %h = $b.keys.sort(-*) Z=> @a; + $list.map({ %h{$_} }) +} -- cgit From 088c13928eb9de940b85234ed243f1e152df17c6 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Wed, 26 Apr 2023 10:44:02 +0000 Subject: ch-2.raku do-over --- challenge-214/mark-anderson/raku/ch-2.raku | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 challenge-214/mark-anderson/raku/ch-2.raku (limited to 'challenge-214') diff --git a/challenge-214/mark-anderson/raku/ch-2.raku b/challenge-214/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..0908191003 --- /dev/null +++ b/challenge-214/mark-anderson/raku/ch-2.raku @@ -0,0 +1,36 @@ +#!/usr/bin/env raku +use Adverb::Eject; +use Test; + +is collect-points(<2 4 3 3 3 4 5 4 2>), 23; +is collect-points(<1 2 2 2 2 1>), 20; +is collect-points(1), 1; +is collect-points(<2 2 2 1 1 2 2 2>), 40; +is collect-points(<1 1 1 25 3 25 3 25 44 9 25 44 25 5 25 5 25 1 1 1>), 92; + +sub collect-points(*@a) +{ + sum gather while @a + { + my $c = @a.pairs.classify({ .value }, :as{ .key }); + my $p = $c.first({ consecutive(.value) }); + + if $p + { + @a[ $c{$p.key} ]:eject + } + + else + { + $p = $c.classify({ .key }, :as{ .value.elems }).minpairs.head; + @a[ $c{$p.key}.head ]:eject + } + + take $p.value.elems ** 2 + } +} + +sub consecutive(@a) +{ + .tail - .head == .end and [<] $_ given @a +} -- cgit From e9d0323937129330c7cd9b49c827a96a05ff9892 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Wed, 26 Apr 2023 11:39:28 +0000 Subject: Challenge 214 do-over (Raku) --- challenge-214/mark-anderson/raku/ch-1.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-214') diff --git a/challenge-214/mark-anderson/raku/ch-1.raku b/challenge-214/mark-anderson/raku/ch-1.raku index 94051dcb01..a710eb92b6 100644 --- a/challenge-214/mark-anderson/raku/ch-1.raku +++ b/challenge-214/mark-anderson/raku/ch-1.raku @@ -5,7 +5,7 @@ is rank-score(<1 2 4 3 5>), <5 4 S B G>; is rank-score(<8 5 6 7 4>), ; is rank-score(<3 5 4 2>), ; is rank-score(<2 5 2 1 7 5 1>), <4 S 4 6 G S 6>; -is rank-score(<22 77 11 55 77 11 22 22 11 22 22>), <4 G 9 B G 9 4 4 9 4 4>; +is rank-score(<20 76 15 55 76 15 20 20 15 20 20>), <4 G 9 B G 9 4 4 9 4 4>; sub rank-score($list) { -- cgit From cbb264a59f463b98178ebefd4d04e89963098a19 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Wed, 26 Apr 2023 14:27:02 +0000 Subject: Challenge 214 (Raku) do-over --- challenge-214/mark-anderson/raku/ch-2.raku | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'challenge-214') diff --git a/challenge-214/mark-anderson/raku/ch-2.raku b/challenge-214/mark-anderson/raku/ch-2.raku index 0908191003..45ec626ff2 100644 --- a/challenge-214/mark-anderson/raku/ch-2.raku +++ b/challenge-214/mark-anderson/raku/ch-2.raku @@ -17,16 +17,16 @@ sub collect-points(*@a) if $p { - @a[ $c{$p.key} ]:eject + take $p.value.elems ** 2 } else { $p = $c.classify({ .key }, :as{ .value.elems }).minpairs.head; - @a[ $c{$p.key}.head ]:eject + take $p.value.Slip } - - take $p.value.elems ** 2 + + @a[ $c{$p.key} ]:eject } } -- cgit From b9faa383870f302998cb57d301d3a2b496d24549 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Wed, 26 Apr 2023 15:19:36 +0000 Subject: Challenge 214 (Raku) do-over --- challenge-214/mark-anderson/raku/ch-1.raku | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'challenge-214') diff --git a/challenge-214/mark-anderson/raku/ch-1.raku b/challenge-214/mark-anderson/raku/ch-1.raku index a710eb92b6..a889828098 100644 --- a/challenge-214/mark-anderson/raku/ch-1.raku +++ b/challenge-214/mark-anderson/raku/ch-1.raku @@ -11,9 +11,8 @@ sub rank-score($list) { my $b = $list.Bag; my @a = [\+] flat 1, $b.sort(-*.key)>>.value; - my %h = <1 2 3> Z=> ; + @a .= map({ [$_] || $_ }); - @a .= map({ %h{$_} ?? %h{$_} !! $_ }); - %h = $b.keys.sort(-*) Z=> @a; + my %h = $b.keys.sort(-*) Z=> @a; $list.map({ %h{$_} }) } -- cgit From dcdafad868a191af65f9f69060293de258b5420d Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Wed, 26 Apr 2023 15:30:31 +0000 Subject: Challenge 214 (Raku) do-over --- challenge-214/mark-anderson/raku/ch-1.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-214') diff --git a/challenge-214/mark-anderson/raku/ch-1.raku b/challenge-214/mark-anderson/raku/ch-1.raku index a889828098..fe4c92d8e4 100644 --- a/challenge-214/mark-anderson/raku/ch-1.raku +++ b/challenge-214/mark-anderson/raku/ch-1.raku @@ -11,7 +11,7 @@ sub rank-score($list) { my $b = $list.Bag; my @a = [\+] flat 1, $b.sort(-*.key)>>.value; - @a .= map({ [$_] || $_ }); + @a .= map({ [$_] or $_ }); my %h = $b.keys.sort(-*) Z=> @a; $list.map({ %h{$_} }) -- cgit From 7e024efdae17d8000dd9ab43be60f6e642f48227 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Wed, 26 Apr 2023 19:37:51 +0000 Subject: w214 - Task 1 --- challenge-214/perlboy1967/perl/ch1.pl | 56 +++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100755 challenge-214/perlboy1967/perl/ch1.pl (limited to 'challenge-214') diff --git a/challenge-214/perlboy1967/perl/ch1.pl b/challenge-214/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..0f6027a815 --- /dev/null +++ b/challenge-214/perlboy1967/perl/ch1.pl @@ -0,0 +1,56 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 214 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-214 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Rank Score +Submitted by: Mohammad S Anwar + +You are given a list of scores (>=1). + +Write a script to rank each score in descending order. First three will get medals +i.e. G (Gold), S (Silver) and B (Bronze). Rest will just get the ranking number. + +|| Using the standard model of giving equal scores equal rank, then advancing that +|| number of ranks. + +=cut + +use v5.16; + +use common::sense; + +use Test::More; +use Test::Deep; + +use List::MoreUtils qw(uniq indexes); + +sub rankScore { + state $medals = {1=>'G', 2=>'S', 3=>'B'}; + my @uniqScores = sort { $b <=> $a } uniq @_; + my ($rank,@ranks) = (1); + + for my $score (@uniqScores) { + my @indexes = indexes { $_ == $score } @_; + map { $ranks[$_] = $medals->{$rank} // $rank } @indexes; + $rank += scalar(@indexes); + } + + return @ranks; + +} + +cmp_deeply([rankScore(1,2,4,3,5)],[qw(5 4 S B G)]); +cmp_deeply([rankScore(8,5,6,7,4)],[qw(G 4 B S 5)]); +cmp_deeply([rankScore(3,5,4,2)],[qw(B G S 4)]); +cmp_deeply([rankScore(2,5,2,1,7,5,1)],[qw(4 S 4 6 G S 6)]); +cmp_deeply([rankScore(1,2,3,3,3,3)],[qw(6 5 G G G G)]); +cmp_deeply([rankScore(1,2,2,3,3)],[qw(5 B B G G)]); + +done_testing; + + -- cgit From 1294e29fc71a9b3887b38a3650f3f274ff2464f7 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Thu, 27 Apr 2023 02:19:18 +0000 Subject: Challenge 214 (Raku) do-over --- challenge-214/mark-anderson/raku/ch-2.raku | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'challenge-214') diff --git a/challenge-214/mark-anderson/raku/ch-2.raku b/challenge-214/mark-anderson/raku/ch-2.raku index 45ec626ff2..e24dd8ebf8 100644 --- a/challenge-214/mark-anderson/raku/ch-2.raku +++ b/challenge-214/mark-anderson/raku/ch-2.raku @@ -22,8 +22,8 @@ sub collect-points(*@a) else { - $p = $c.classify({ .key }, :as{ .value.elems }).minpairs.head; - take $p.value.Slip + $p = $c.Bag.minpairs.head; + take $p.value } @a[ $c{$p.key} ]:eject -- cgit From 8b713fc25fbce7655ee3d3ad15817b2f003425aa Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Thu, 27 Apr 2023 00:38:45 -0700 Subject: Robbie Hatley's Perl solution to The Weekly Challenge 214-1. (May add 214-2 and blog later.) --- challenge-214/robbie-hatley/perl/ch-1.pl | 136 +++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100755 challenge-214/robbie-hatley/perl/ch-1.pl (limited to 'challenge-214') diff --git a/challenge-214/robbie-hatley/perl/ch-1.pl b/challenge-214/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..f10c9cfae2 --- /dev/null +++ b/challenge-214/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,136 @@ +#! /bin/perl -CSDA + +=pod + +------------------------------------------------------------------------------------------------------------------------ +COLOPHON: +This is a 120-character-wide Unicode UTF-8 Perl-source-code text file with hard Unix line breaks ("\x0A"). +¡Hablo Español! Говорю Русский. Björt skjöldur. ॐ नमो भगवते वासुदेवाय. 看的星星,知道你是爱。麦藁雪、富士川町、山梨県。 + +------------------------------------------------------------------------------------------------------------------------ +TITLE BLOCK: +ch-1.pl +Robbie Hatley's Perl solutions for The Weekly Challenge 214-1. +Written by Robbie Hatley on Tue Apr 25, 2023. + +------------------------------------------------------------------------------------------------------------------------ +PROBLEM DESCRIPTION: +Task 1: Rank Score +Submitted by: Mohammad S Anwar +You are given a list of scores (>=1). Write a script to rank each score in descending order. First three will get medals +i.e. G (Gold), S (Silver) and B (Bronze). Rest will just get the ranking number. Use the standard model of giving equal +scores equal rank, then advancing that number of ranks. +Example 1: Input: (1,2,4,3,5) Output: (5,4,S,B,G) +Example 2: Input: (8,5,6,7,4) Output: (G,4,B,S,5) +Example 3: Input: (3,5,4,2) Output: (B,G,S,4) +Example 4: Input: (2,5,2,1,7,5,1) Output: (4,S,4,6,G,S,6) + +------------------------------------------------------------------------------------------------------------------------ +PROBLEM NOTES: +The phrase "descending order" is a bit confusing. I think what it means is that as the scores go UP, the rank numbers +go DOWN. I'll go by that assumption. + +The phrase "giving equal scores equal rank, then advancing that number of ranks" is also confusing, especially the +usage of the word "advancing" in that context. I'll assume that the "advancing" is done in the UPWARDS direction of +rank numbers, so that if two rank 1s are given, skip to rank 3; or if three rank 3s are given, skip to rank 6. + +It seems to me that since I'm going to be handling a sequence of raw scores in THREE different sortings (by index, +by score, and by rank), I should make each score not a single number but rather a 3-tuple: [index, score, rank]. +Put the 3-tuples into an array: +[ [0,8,x], [1,5,x], [2,6,x], [3,7,x], [4,4,x] ] +and sort that array in-situ (don't make multiple copies) as many times and ways as necessary. + +------------------------------------------------------------------------------------------------------------------------ +INPUT / OUTPUT NOTES: +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a single-quoted +array of arrays in proper Perl syntax, with each inner array being a sequence of non-negative integers, like so: +./ch-1.pl '([0,40,97,50,40],[3,1,38,47,38]),[17,42,47,42,42,50]' + +Output is to STDOUT and will be each list of scores in original order, followed by a positionally-corresponding list of +ranks. + +=cut + +# ---------------------------------------------------------------------------------------------------------------------- +# PRELIMINARIES: +use v5.32; +use strict; +use warnings; +no warnings 'numeric'; +use utf8; +use Sys::Binmode; +use Time::HiRes 'time'; +$"=', '; + +# ---------------------------------------------------------------------------------------------------------------------- +# DEFAULT INPUTS: +my @arrays = +( + [1,2,4,3,5], + [8,5,6,7,4], + [3,5,4,2], + [2,5,2,1,7,5,1], +); + +# ---------------------------------------------------------------------------------------------------------------------- +# NON-DEFAULT INPUTS: +if (@ARGV) {@arrays = eval($ARGV[0]);} + +# ---------------------------------------------------------------------------------------------------------------------- +# MAIN BODY OF PROGRAM: +{ # begin main + my $t0 = time; + foreach my $sref (@arrays) { + # How many scores in this score set? + my $size = scalar(@$sref); + + # Make an array of (index, score, rank) 3-tuples: + my @tuples; + for ( my $i = 0 ; $i < $size ; ++$i ) { + $tuples[$i] = [$i, $$sref[$i], 'rank']; + } + + # Sort our 3-tuples in order of descending score: + @tuples = sort {$$b[1] <=> $$a[1]} @tuples; + + # For each 3-tuple, assign current index (NOT saved index!!!) +1 to rank: + for ( my $i = 0 ; $i < $size ; ++$i ) { + $tuples[$i][2] = $i+1; + } + + # If any 3-tuple has same score as 3-tuple to its left, assign same rank: + for ( my $i = 1 ; $i < $size ; ++$i ) { + if ($tuples[$i][1] == $tuples[$i-1][1]) { + $tuples[$i][2] = $tuples[$i-1][2]; + } + } + + # Assign gold, silver, and bronze medals: + for ( my $i = 0 ; $i < $size ; ++$i ) { + if ($tuples[$i][2] == 1) {$tuples[$i][2] = 'G';} + if ($tuples[$i][2] == 2) {$tuples[$i][2] = 'S';} + if ($tuples[$i][2] == 3) {$tuples[$i][2] = 'B';} + } + + # Sort our 3-tuples back to original (saved) indexes: + @tuples = sort {$$a[0] <=> $$b[0]} @tuples; + + # Print results: + say ''; + print 'Scores: ('; + for ( my $i = 0 ; $i < $size ; ++$i ) { + print $tuples[$i][1]; + if ($i != $size-1) {print ", ";} + } + print ")\n"; + print 'Ranks: ('; + for ( my $i = 0 ; $i < $size ; ++$i ) { + print $tuples[$i][2]; + if ($i != $size-1) {print ", ";} + } + print ")\n"; + } + my $µs = 1000000 * (time - $t0); + printf("\nExecution time was %.3fµs.\n", $µs); + exit 0; +} # end main -- cgit From 55dea37ed6ad367eb42a3c22095b80977fe4a1c4 Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Thu, 27 Apr 2023 00:48:25 -0700 Subject: fixed error --- challenge-214/robbie-hatley/perl/ch-1.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-214') diff --git a/challenge-214/robbie-hatley/perl/ch-1.pl b/challenge-214/robbie-hatley/perl/ch-1.pl index f10c9cfae2..1e4bb086ee 100755 --- a/challenge-214/robbie-hatley/perl/ch-1.pl +++ b/challenge-214/robbie-hatley/perl/ch-1.pl @@ -44,7 +44,7 @@ and sort that array in-situ (don't make multiple copies) as many times and ways INPUT / OUTPUT NOTES: Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a single-quoted array of arrays in proper Perl syntax, with each inner array being a sequence of non-negative integers, like so: -./ch-1.pl '([0,40,97,50,40],[3,1,38,47,38]),[17,42,47,42,42,50]' +./ch-1.pl '([0,40,97,50,40],[3,1,38,47,38],[17,42,47,42,42,50])' Output is to STDOUT and will be each list of scores in original order, followed by a positionally-corresponding list of ranks. -- cgit From 12fa389d6e1dc5ab45926f20d9bc3eff560dbd52 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Thu, 27 Apr 2023 13:32:38 +0100 Subject: RogerBW solutions for challenge no. 214 --- challenge-214/roger-bell-west/javascript/ch-1.js | 88 ++++++++ challenge-214/roger-bell-west/javascript/ch-2.js | 70 +++++++ challenge-214/roger-bell-west/kotlin/ch-1.kt | 58 +++++ challenge-214/roger-bell-west/kotlin/ch-2.kt | 73 +++++++ challenge-214/roger-bell-west/lua/ch-1.lua | 102 +++++++++ challenge-214/roger-bell-west/lua/ch-2.lua | 86 ++++++++ challenge-214/roger-bell-west/perl/ch-1.pl | 36 ++++ challenge-214/roger-bell-west/perl/ch-2.pl | 57 +++++ challenge-214/roger-bell-west/postscript/ch-1.ps | 256 +++++++++++++++++++++++ challenge-214/roger-bell-west/postscript/ch-2.ps | 178 ++++++++++++++++ challenge-214/roger-bell-west/python/ch-1.py | 44 ++++ challenge-214/roger-bell-west/python/ch-2.py | 53 +++++ challenge-214/roger-bell-west/raku/ch-1.p6 | 34 +++ challenge-214/roger-bell-west/raku/ch-2.p6 | 52 +++++ challenge-214/roger-bell-west/ruby/ch-1.rb | 51 +++++ challenge-214/roger-bell-west/ruby/ch-2.rb | 63 ++++++ challenge-214/roger-bell-west/rust/ch-1.rs | 54 +++++ challenge-214/roger-bell-west/rust/ch-2.rs | 65 ++++++ challenge-214/roger-bell-west/tests.yaml | 87 ++++++++ 19 files changed, 1507 insertions(+) create mode 100755 challenge-214/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-214/roger-bell-west/javascript/ch-2.js create mode 100644 challenge-214/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-214/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-214/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-214/roger-bell-west/lua/ch-2.lua create mode 100755 challenge-214/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-214/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-214/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-214/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-214/roger-bell-west/python/ch-1.py create mode 100755 challenge-214/roger-bell-west/python/ch-2.py create mode 100755 challenge-214/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-214/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-214/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-214/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-214/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-214/roger-bell-west/rust/ch-2.rs create mode 100644 challenge-214/roger-bell-west/tests.yaml (limited to 'challenge-214') diff --git a/challenge-214/roger-bell-west/javascript/ch-1.js b/challenge-214/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..01464b4ec2 --- /dev/null +++ b/challenge-214/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,88 @@ +#! /usr/bin/node + +"use strict" + +// by Frank Tan +// https://stackoverflow.com/questions/38400594/javascript-deep-comparison +function deepEqual(a,b) +{ + if( (typeof a == 'object' && a != null) && + (typeof b == 'object' && b != null) ) + { + var count = [0,0]; + for( var key in a) count[0]++; + for( var key in b) count[1]++; + if( count[0]-count[1] != 0) {return false;} + for( var key in a) + { + if(!(key in b) || !deepEqual(a[key],b[key])) {return false;} + } + for( var key in b) + { + if(!(key in a) || !deepEqual(b[key],a[key])) {return false;} + } + return true; + } + else + { + return a === b; + } +} + +function rankscore(a) { + let av = new Map; + for (let s of a) { + if (av.has(s)) { + av.set(s, av.get(s) + 1); + } else { + av.set(s, 1); + } + } + let kk = Array.from(av.keys()); + kk.sort(function(a,b) { + return b-a; + }); + let rank = 1; + let tab = new Map; + for (let k of kk) { + const siz = av.get(k); + let r = ""; + if (rank <= 3) { + r = ["G", "S", "B"][rank - 1]; + } else { + r = rank.toString(); + } + if (siz > 1) { + r += "="; + } + tab.set(k, r); + rank += siz; + } + return a.map(i => tab.get(i)); +} + + +if (deepEqual(rankscore([1, 2, 4, 3, 5]), ['5', '4', 'S', 'B', 'G'])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(rankscore([8, 5, 6, 7, 4]), ['G', '4', 'B', 'S', '5'])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(rankscore([3, 5, 4, 2]), ['B', 'G', 'S', '4'])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(rankscore([2, 5, 2, 1, 7, 5, 1]), ['4=', 'S=', '4=', '6=', 'G', 'S=', '6='])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-214/roger-bell-west/javascript/ch-2.js b/challenge-214/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..aabc5e43dc --- /dev/null +++ b/challenge-214/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,70 @@ +#! /usr/bin/node + +"use strict" + +function collectpoints(a) { + let m = []; + let st = 0; + while (st < a.length) { + const k = a[st]; + let e = st; + while (e < a.length && a[e] == k) { + e += 1; + } + m.push([k, e - st]); + st = e; + } + let rv = 0; + let stack = [[m, 0]]; + while (stack.length > 0) { + const s = stack.pop(); + if (s[0].length == 0) { + rv = Math.max(rv, s[1]); + } else { + for (let i = 0; i < s[0].length; i++) { + let ss = JSON.parse(JSON.stringify(s)); + ss[1] += ss[0][i][1] * ss[0][i][1]; + ss[0].splice(i, 1); + let j = i; + while (true) { + if (j > 0 && + j < ss[0].length && + ss[0][j][0] == ss[0][j - 1][0]) { + ss[0][j][1] += ss[0][j - 1][1]; + ss[0].splice(j - 1, 1); + j -= 1; + } else { + break; + } + } + stack.push(ss); + } + } + } + return rv; +} + +if (collectpoints([2, 4, 3, 3, 3, 4, 5, 4, 2]) == 23) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (collectpoints([1, 2, 2, 2, 2, 1]) == 20) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (collectpoints([1]) == 1) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (collectpoints([2, 2, 2, 1, 1, 2, 2, 2]) == 40) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-214/roger-bell-west/kotlin/ch-1.kt b/challenge-214/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..657c05c684 --- /dev/null +++ b/challenge-214/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,58 @@ +fun rankscore(a: List): List { + var av = mutableMapOf() + for (s in a) { + if (av.contains(s)) { + av.put(s, av.get(s)!! + 1) + } else { + av.put(s, 1) + } + } + var kk = ArrayList(av.keys) + kk.sortWith(compareByDescending { it }) + var rank = 1 + var tab = mutableMapOf() + for (k in kk) { + val siz = av.get(k)!! + var r: String + if (rank <= 3) { + r = listOf("G", "S", "B")[rank - 1] + } else { + r = rank.toString() + } + if (siz > 1) { + r += "=" + } + tab.put(k, r) + rank += siz + } + return a.map {tab.get(it)!!}.toList() +} + +fun main() { + + if (rankscore(listOf(1, 2, 4, 3, 5)) == listOf("5", "4", "S", "B", "G")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (rankscore(listOf(8, 5, 6, 7, 4)) == listOf("G", "4", "B", "S", "5")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (rankscore(listOf(3, 5, 4, 2)) == listOf("B", "G", "S", "4")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (rankscore(listOf(2, 5, 2, 1, 7, 5, 1)) == listOf("4=", "S=", "4=", "6=", "G", "S=", "6=")) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-214/roger-bell-west/kotlin/ch-2.kt b/challenge-214/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..b5941135f5 --- /dev/null +++ b/challenge-214/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,73 @@ +import kotlin.math.max + +fun collectpoints(a: List): Int { + var m = ArrayList>() + var st = 0 + while (st < a.size) { + val k = a[st] + var e = st + while (e < a.size && a[e] == k) { + e += 1 + } + m.add(Pair(k, e - st)) + st = e + } + var rv = 0 + var stack = ArrayList>, Int>>() + stack.add(Pair(m, 0)) + while (stack.size > 0) { + val s = stack.removeLast() + if (s.first.size == 0) { + rv = max(rv, s.second) + } else { + for (i in 0 .. s.first.size - 1) { + var ss = Pair(ArrayList(s.first), s.second) + ss = Pair(ss.first, ss.second + ss.first[i].second * ss.first[i].second) + ss.first.removeAt(i) + var j = i + while (true) { + if (j >0 && + j < ss.first.size && + ss.first[j].first == ss.first[j - 1].first) { + ss.first[j] = Pair(ss.first[j].first, ss.first[j].second + ss.first[j - 1].second) + ss.first.removeAt(j - 1) + j -= 1 + } else { + break + } + } + stack.add(ss) + } + } + } + return rv +} + +fun main() { + + if (collectpoints(listOf(2, 4, 3, 3, 3, 4, 5, 4, 2)) == 23) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (collectpoints(listOf(1, 2, 2, 2, 2, 1)) == 20) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (collectpoints(listOf(1)) == 1) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (collectpoints(listOf(2, 2, 2, 1, 1, 2, 2, 2)) == 40) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-214/roger-bell-west/lua/ch-1.lua b/challenge-214/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..d656246e7d --- /dev/null +++ b/challenge-214/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,102 @@ +#! /usr/bin/lua + +-- by Michael Anderson at +-- https://stackoverflow.com/questions/8722620/comparing-two-index-tables-by-index-value-in-lua +-- modified by Roger +function recursive_compare(t1,t2) + -- Use usual comparison first. + if t1==t2 then return true end + -- We only support non-default behavior for tables + if (type(t1)~="table") then return false end + -- They better have the same metatables + local mt1 = getmetatable(t1) + local mt2 = getmetatable(t2) + if( not recursive_compare(mt1,mt2) ) then return false end + -- Build list of all keys + local kk = {} + for k1, _ in pairs(t1) do + kk[k1] = true + end + for k2, _ in pairs(t2) do + kk[k2] = true + end + -- Check each key that exists in at least one table + for _, k in ipairs(kk) do + if (not recursive_compare(t1[k], t2[k])) then + return false + end + end + return true +end + +-- Roger's lualib +function keys(t) + local a = {} + for k, v in pairs(t) do + table.insert(a, k) + end + return a +end + +function rankscore(a) + local av = {} + for i, s in ipairs(a) do + if av[s] == nil then + av[s] = 1 + else + av[s] = av[s] + 1 + end + end + local kk = keys(av) + table.sort(kk, function (aa, bb) return bb < aa end) + local rank = 1 + local tab = {} + for i, k in ipairs(kk) do + local siz = av[k] + local r = "" + if rank <= 3 then + r = string.sub("GSB", rank, rank) + else + r = tostring(rank) + end + if siz > 1 then + r = r .. "=" + end + tab[k] = r + rank = rank + siz + end + local out = {} + for i, s in ipairs(a) do + table.insert(out, tab[s]) + end + return out +end + +if recursive_compare(rankscore({1, 2, 4, 3, 5}), {"5", "4", "S", "B", "G"}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(rankscore({8, 5, 6, 7, 4}), {"G", "4", "B", "S", "5"}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(rankscore({3, 5, 4, 2}), {"B", "G", "S", "4"}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(rankscore({2, 5, 2, 1, 7, 5, 1}), {"4=", "S=", "4=", "6=", "G", "S=", "6="}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-214/roger-bell-west/lua/ch-2.lua b/challenge-214/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..04a9cf7f6f --- /dev/null +++ b/challenge-214/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,86 @@ +#! /usr/bin/lua + +-- from Roger's lualib +function deepcopy(src) + local dst = {} + for k, v in pairs(src) do + if type(v) == "table" then + v = deepcopy(v) + end + dst[k] = v + end + return dst +end + +function collectpoints(a) + local m = {} + local s = 1 + while s <= #a do + local k = a[s] + local e = s + while e <= #a and a[e] == k do + e = e + 1 + end + table.insert(m, {k, e - s}) + s = e + end + local rv = 0 + local stack = { { m, 0 } } + while #stack > 0 do + local s = table.remove(stack) + if #(s[1]) == 0 then + if rv < s[2] then + rv = s[2] + end + else + for i = 1, #(s[1]) do + local ss = deepcopy(s) + ss[2] = ss[2] + ss[1][i][2] * ss[1][i][2] + table.remove(ss[1], i) + j = i + while true do + if j > 1 and + j <= #(ss[1]) and + ss[1][j][1] == ss[1][j - 1][1] then + ss[1][j][2] = ss[1][j][2] + ss[1][j - 1][2] + table.remove(ss[1], j - 1) + j = j - 1 + else + break + end + end + table.insert(stack, ss) + end + end + end + return rv +end + +if collectpoints({2, 4, 3, 3, 3, 4, 5, 4, 2}) == 23 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if collectpoints({1, 2, 2, 2, 2, 1}) == 20 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if collectpoints({1}) == 1 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if collectpoints({2, 2, 2, 1, 1, 2, 2, 2}) == 40 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-214/roger-bell-west/perl/ch-1.pl b/challenge-214/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..1c27c6a80b --- /dev/null +++ b/challenge-214/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,36 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 4; + +is_deeply(rankscore([1, 2, 4, 3, 5]), ['5', '4', 'S', 'B', 'G'], 'example 1'); +is_deeply(rankscore([8, 5, 6, 7, 4]), ['G', '4', 'B', 'S', '5'], 'example 2'); +is_deeply(rankscore([3, 5, 4, 2]), ['B', 'G', 'S', '4'], 'example 3'); +is_deeply(rankscore([2, 5, 2, 1, 7, 5, 1]), ['4=', 'S=', '4=', '6=', 'G', 'S=', '6='], 'example 4'); + +sub rankscore($a) { + my %av; + foreach my $s (@{$a}) { + $av{$s}++; + } + my $rank = 1; + my %tab; + foreach my $k (sort {$::b <=> $::a} keys %av) { + my $siz = $av{$k}; + my $r; + if ($rank <= 3) { + $r = [qw(G S B)]->[$rank - 1]; + } else { + $r = $rank; + } + if ($siz > 1) { + $r .= "="; + } + $tab{$k} = $r; + $rank += $siz; + } + return [map {$tab{$_}} @{$a}]; +} diff --git a/challenge-214/roger-bell-west/perl/ch-2.pl b/challenge-214/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..741c69ff65 --- /dev/null +++ b/challenge-214/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,57 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 4; + +is(collectpoints([2, 4, 3, 3, 3, 4, 5, 4, 2]), 23, 'example 1'); +is(collectpoints([1, 2, 2, 2, 2, 1]), 20, 'example 2'); +is(collectpoints([1]), 1, 'example 3'); +is(collectpoints([2, 2, 2, 1, 1, 2, 2, 2]), 40, 'example 4'); + +use Storable qw(dclone); +use List::Util qw(max); + +sub collectpoints($a) { + my @m; + my $s = 0; + while ($s <= $#{$a}) { + my $k = $a->[$s]; + my $e = $s; + while ($e <= $#{$a} && $a->[$e] == $k) { + $e++; + } + push @m,[$k, $e - $s]; + $s = $e; + } + my $rv = 0; + my @stack = ([\@m, 0]); + while (scalar @stack > 0) { + my $s = pop @stack; + if (scalar @{$s->[0]} == 0) { + $rv = max($rv, $s->[1]); + } else { + foreach my $i (0..$#{$s->[0]}) { + my $ss = dclone($s); + $ss->[1] += $ss->[0][$i][1] * $ss->[0][$i][1]; + splice @{$ss->[0]}, $i, 1; + my $j = $i; + while (1) { + if ($j > 0 && + $j < scalar @{$ss->[0]} && + $ss->[0][$j][0] == $ss->[0][$j-1][0]) { + $ss->[0][$j][1] += $ss->[0][$j-1][1]; + splice @{$ss->[0]}, $j - 1, 1; + $j--; + } else { + last; + } + } + push @stack, $ss; + } + } + } + return $rv; +} diff --git a/challenge-214/roger-bell-west/postscript/ch-1.ps b/challenge-214/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..3e2caf889f --- /dev/null +++ b/challenge-214/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,256 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + +/quicksort.main { % lo hi -> (null) + 3 dict begin + /hi exch def + /lo exch def + /xit false def + lo 0 lt { + /xit true def + } if + hi 0 lt { + /xit true def + } if + lo hi ge { + /xit true def + } if + xit not { + /p quicksort.partition def + lo p quicksort.main + p 1 add hi quicksort.main + } if + end +} bind def + +/quicksort.partition { + 3 dict begin + /pivot arr hi lo add 2 idiv get def + /i lo 1 sub def + /j hi 1 add def + { + { + /i i 1 add def + arr i get pivot ge { + exit + } if + } loop + { + /j j 1 sub def + arr j get pivot le { + exit + } if + } loop + i j ge { + j + exit + } if + i j quicksort.swap + } loop + end +} bind def + +/quicksort { % [ a c b ] -> [ a b c ] + 1 dict begin + /arr exch def + arr length 0 gt { + 0 arr length 1 sub quicksort.main + } if + arr + end +} bind def + +/reverse { + 1 dict begin + dup length /l exch def + [ exch + aload pop + 2 1 l { + -1 roll + } for + ] + end +} bind def + +/quicksort.swap { + 2 dict begin + /bi exch def + /ai exch def + arr ai get + arr bi get + arr exch ai exch put + arr exch bi exch put + end +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/safestring { + dup length string cvs +} bind def + +/deepeq { + 2 dict begin + /a exch def + /b exch def + a type b type eq { + a type /dicttype eq { + a length b length eq { + << + a { + pop + true + } forall + b { + pop + true + } forall + >> + true exch + { + pop + dup a exch known { + dup b exch known { + dup a exch get exch b exch get deepeq not { + pop false + } if + } { + false + } ifelse + } { + false + } ifelse + } forall + } { + false + } ifelse + } { + a type dup /arraytype eq exch /stringtype eq or { + a length b length eq { + true + 0 1 a length 1 sub { + dup a exch get exch b exch get deepeq not { + pop false + exit + } if + } for + } { + false + } ifelse + } { + a b eq + } ifelse + } ifelse + } { + false + } ifelse + end +} bind def + +/keys { % dict -> array of dict keys + [ exch + { + pop + } forall + ] +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/alloccvs { + 2 dict begin + /n exch def + /a 1 def + n + dup 0 lt { + /a a 1 add def + neg + } if + { + dup 10 lt { + exit + } if + /a a 1 add def + 10 idiv + } loop + pop + n a string cvs + end +} bind def + + +% end included library code + +/rankscore { + 8 dict begin + /a exch def + /av 0 dict def + a { + /s exch def + av s known { + av s av s get 1 add put + } { + av s 1 put + } ifelse + } forall + /rank 1 def + /tab 0 dict def + av keys quicksort reverse { + /k exch def + /siz av k get def + rank 3 le { + /r [ (G) (S) (B) ] rank 1 sub get def + } { + /r rank alloccvs def + } ifelse + siz 1 gt { + /s r length 1 add string def + r s copy + s r length (=) putinterval + /r s def + } if + tab k r safestring put + /rank rank siz add def + } forall + [ + a { + tab exch get + } forall + ] + end +} bind def + +(rankscore) test.start +[1 2 4 3 5] rankscore [(5) (4) (S) (B) (G)] deepeq test +[8 5 6 7 4] rankscore [(G) (4) (B) (S) (5)] deepeq test +[3 5 4 2] rankscore [(B) (G) (S) (4)] deepeq test +[2 5 2 1 7 5 1] rankscore [(4=) (S=) (4=) (6=) (G) (S=) (6=)] deepeq test +test.end diff --git a/challenge-214/roger-bell-west/postscript/ch-2.ps b/challenge-214/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..8cafbdd0e3 --- /dev/null +++ b/challenge-214/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,178 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/deepcopy { + 2 dict begin + /a exch def + a type /dicttype eq { + << + a keys { + /k exch get + k + a k get deepcopy + } forall + >> + } { + a type /arraytype eq { + [ + a { + deepcopy + } forall + ] + } { + a type /stringtype eq { + a dup length string cvs + } { + a + } ifelse + } ifelse + } ifelse + end +} bind def + +/apush.right { % [a b] c -> [a b c] + exch + [ exch aload length 2 add -1 roll ] +} bind def + +/apop.right { % [a b c] -> [a b] c + [ exch aload length 1 add 1 roll ] exch +} bind def + +/keys { % dict -> array of dict keys + [ exch + { + pop + } forall + ] +} bind def + +/remove { + 2 dict begin + /i exch def + aload length /l exch def + l i sub -1 roll pop + l 1 sub array astore + end +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/test.end { +