diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-05-16 14:19:05 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-05-16 14:19:05 +0200 |
| commit | b7b029f85595734938066ed2efefded43aacf260 (patch) | |
| tree | a93898fbd047c8554f837b1ddf22b1582613e4a1 | |
| parent | dcef53a5d2200928fdcd05504867da75d03c2f36 (diff) | |
| download | perlweeklychallenge-club-b7b029f85595734938066ed2efefded43aacf260.tar.gz perlweeklychallenge-club-b7b029f85595734938066ed2efefded43aacf260.tar.bz2 perlweeklychallenge-club-b7b029f85595734938066ed2efefded43aacf260.zip | |
feat(challenge-217/lubos-kolouch/[perl,python]/ch-[12].p[ly]): Challenge 217 LK Perl Python
| -rw-r--r-- | challenge-217/lubos-kolouch/perl/ch-1.pl | 18 | ||||
| -rw-r--r-- | challenge-217/lubos-kolouch/perl/ch-2.pl | 15 | ||||
| -rw-r--r-- | challenge-217/lubos-kolouch/python/ch-1.py | 16 | ||||
| -rw-r--r-- | challenge-217/lubos-kolouch/python/ch-2.py | 15 |
4 files changed, 64 insertions, 0 deletions
diff --git a/challenge-217/lubos-kolouch/perl/ch-1.pl b/challenge-217/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..0fae6d6897 --- /dev/null +++ b/challenge-217/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,18 @@ +use strict; +use warnings; + +sub third_smallest { + my @matrix = @_; + my @flattened = map { @$_ } @matrix; + @flattened = sort { $a <=> $b } @flattened; + return $flattened[2]; +} + +my @matrix1 = ([3, 1, 2], [5, 2, 4], [0, 1, 3]); +my @matrix2 = ([2, 1], [4, 5]); +my @matrix3 = ([1, 0, 3], [0, 0, 0], [1, 2, 1]); + +print third_smallest(@matrix1), "\n"; +print third_smallest(@matrix2), "\n"; +print third_smallest(@matrix3), "\n"; + diff --git a/challenge-217/lubos-kolouch/perl/ch-2.pl b/challenge-217/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..63441a1796 --- /dev/null +++ b/challenge-217/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,15 @@ +use strict; +use warnings; + +sub highest_value { + my @nums = @_; + @nums = sort { $b . $a cmp $a . $b } @nums; + return join '', @nums; +} + +print highest_value(1, 23), "\n"; +print highest_value(10, 3, 2), "\n"; +print highest_value(31, 2, 4, 10), "\n"; +print highest_value(5, 11, 4, 1, 2), "\n"; +print highest_value(1, 10), "\n"; + diff --git a/challenge-217/lubos-kolouch/python/ch-1.py b/challenge-217/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..3dc8b355e2 --- /dev/null +++ b/challenge-217/lubos-kolouch/python/ch-1.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +def third_smallest(matrix): + flattened = [elem for sublist in matrix for elem in sublist] + flattened.sort() + return flattened[2] + + +matrix1 = [[3, 1, 2], [5, 2, 4], [0, 1, 3]] +matrix2 = [[2, 1], [4, 5]] +matrix3 = [[1, 0, 3], [0, 0, 0], [1, 2, 1]] + +print(third_smallest(matrix1)) +print(third_smallest(matrix2)) +print(third_smallest(matrix3)) diff --git a/challenge-217/lubos-kolouch/python/ch-2.py b/challenge-217/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..d1ae89e84d --- /dev/null +++ b/challenge-217/lubos-kolouch/python/ch-2.py @@ -0,0 +1,15 @@ +from functools import cmp_to_key + + +def highest_value(nums): + nums = map(str, nums) # convert numbers to strings + result = ''.join(sorted(nums, key=cmp_to_key( + lambda x, y: 1 if y+x > x+y else -1))) + return int(result) + + +print(highest_value([1, 23])) +print(highest_value([10, 3, 2])) +print(highest_value([31, 2, 4, 10])) +print(highest_value([5, 11, 4, 1, 2])) +print(highest_value([1, 10])) |
