diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-04-24 16:02:44 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-04-24 16:02:44 +0200 |
| commit | c9649ccd3cfb48e26c19886bb399bd1820715f8c (patch) | |
| tree | bc6610da37b7e8d06a5fc0e3366ae0947720cdd0 | |
| parent | 9df2d961ae00534346eaaceffaf8cfee4ecc88bb (diff) | |
| download | perlweeklychallenge-club-c9649ccd3cfb48e26c19886bb399bd1820715f8c.tar.gz perlweeklychallenge-club-c9649ccd3cfb48e26c19886bb399bd1820715f8c.tar.bz2 perlweeklychallenge-club-c9649ccd3cfb48e26c19886bb399bd1820715f8c.zip | |
Challenge 214 LK Perl Python
| -rw-r--r-- | challenge-214/lubos-kolouch/perl/ch-1.pl | 44 | ||||
| -rw-r--r-- | challenge-214/lubos-kolouch/perl/ch-2.pl | 46 | ||||
| -rw-r--r-- | challenge-214/lubos-kolouch/python/ch-1.py | 31 | ||||
| -rw-r--r-- | challenge-214/lubos-kolouch/python/ch-2.py | 43 |
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 |
