aboutsummaryrefslogtreecommitdiff
path: root/challenge-214
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-04-25 11:34:18 +0100
committerGitHub <noreply@github.com>2023-04-25 11:34:18 +0100
commit0b58d3d4acc046547b3b8c0cce2f5ba9a0e7df2c (patch)
tree5f94a5354fc0ccfd6bc2c1bde6a36423379d106b /challenge-214
parent8f85486dbac39eedd05e465f9a9fa66c079d45c9 (diff)
parentf90349fbd02a4b243f73bcdfc245abfa36df497f (diff)
downloadperlweeklychallenge-club-0b58d3d4acc046547b3b8c0cce2f5ba9a0e7df2c.tar.gz
perlweeklychallenge-club-0b58d3d4acc046547b3b8c0cce2f5ba9a0e7df2c.tar.bz2
perlweeklychallenge-club-0b58d3d4acc046547b3b8c0cce2f5ba9a0e7df2c.zip
Merge pull request #7966 from LubosKolouch/master
Challenge 214 LK Perl Python
Diffstat (limited to 'challenge-214')
-rw-r--r--challenge-214/lubos-kolouch/perl/ch-1.pl44
-rw-r--r--challenge-214/lubos-kolouch/perl/ch-2.pl46
-rw-r--r--challenge-214/lubos-kolouch/python/ch-1.py31
-rw-r--r--challenge-214/lubos-kolouch/python/ch-2.py43
4 files changed, 164 insertions, 0 deletions
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