diff options
| -rw-r--r-- | challenge-219/lubos-kolouch/perl/ch-1.pl | 12 | ||||
| -rw-r--r-- | challenge-219/lubos-kolouch/perl/ch-2.pl | 30 | ||||
| -rw-r--r-- | challenge-219/lubos-kolouch/python/ch-1.py | 10 | ||||
| -rw-r--r-- | challenge-219/lubos-kolouch/python/ch-2.py | 23 |
4 files changed, 75 insertions, 0 deletions
diff --git a/challenge-219/lubos-kolouch/perl/ch-1.pl b/challenge-219/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..525e403445 --- /dev/null +++ b/challenge-219/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,12 @@ +use strict; +use warnings; + +sub sorted_squares { + my @nums = @_; + return sort {$a <=> $b} map { $_**2 } @nums; +} + +# Test +print join(", ", sorted_squares(-2, -1, 0, 3, 4)), "\n"; # Output: 0, 1, 4, 9, 16 +print join(", ", sorted_squares(5, -4, -1, 3, 6)), "\n"; # Output: 1, 9, 16, 25, 36 + diff --git a/challenge-219/lubos-kolouch/perl/ch-2.pl b/challenge-219/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..debb48e86c --- /dev/null +++ b/challenge-219/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,30 @@ +use strict; +use warnings; + +sub min_cost { + my ($costs, $days) = @_; + my $N = $days->[-1] + 1; + my @dp = (0) x $N; + my %days = map { $_ => 1 } @$days; + for my $i (1..$N-1) { + if (!exists $days{$i}) { + $dp[$i] = $dp[$i-1]; + } + else { + $dp[$i] = min( + $dp[$i-1] + $costs->[0], + $dp[max(0, $i-7)] + $costs->[1], + $dp[max(0, $i-30)] + $costs->[2] + ); + } + } + return $dp[-1]; +} + +sub min { my $min = shift; for (@_) { $min = $_ if $_ < $min } return $min } +sub max { my $max = shift; for (@_) { $max = $_ if $_ > $max } return $max } + +# Tests +print min_cost([2, 7, 25], [1, 5, 6, 7, 9, 15]), "\n"; # Output: 11 +print min_cost([2, 7, 25], [1, 2, 3, 5, 7, 10, 11, 12, 14, 20, 30, 31]), "\n"; # Output: 20 + diff --git a/challenge-219/lubos-kolouch/python/ch-1.py b/challenge-219/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..7e90d839ef --- /dev/null +++ b/challenge-219/lubos-kolouch/python/ch-1.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +def sorted_squares(nums): + return sorted(x**2 for x in nums) + + +# Test +print(sorted_squares([-2, -1, 0, 3, 4])) # Output: [0, 1, 4, 9, 16] +print(sorted_squares([5, -4, -1, 3, 6])) # Output: [1, 9, 16, 25, 36] diff --git a/challenge-219/lubos-kolouch/python/ch-2.py b/challenge-219/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..9743ea475b --- /dev/null +++ b/challenge-219/lubos-kolouch/python/ch-2.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +def min_cost(costs, days): + N = max(days) + 1 + dp = [0]*N + days = set(days) + for i in range(1, N): + if i not in days: + dp[i] = dp[i-1] + else: + dp[i] = min( + dp[i-1] + costs[0], + dp[max(0, i-7)] + costs[1], + dp[max(0, i-30)] + costs[2] + ) + return dp[-1] + + +# Tests +print(min_cost([2, 7, 25], [1, 5, 6, 7, 9, 15])) # Output: 11 +# Output: 20 +print(min_cost([2, 7, 25], [1, 2, 3, 5, 7, 10, 11, 12, 14, 20, 30, 31])) |
