aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-05-16 11:57:54 +0100
committerGitHub <noreply@github.com>2023-05-16 11:57:54 +0100
commit9244d228f73b1f3b27c7788df8ed96876e1b8ae7 (patch)
tree378edba71cad951c692d32dfb31cd93a4b65719e
parent61877159158923aacc0f5424d1b8f049bd4d2418 (diff)
parent351ab7549b122a1c6eb162fd15248f2d2696aa39 (diff)
downloadperlweeklychallenge-club-9244d228f73b1f3b27c7788df8ed96876e1b8ae7.tar.gz
perlweeklychallenge-club-9244d228f73b1f3b27c7788df8ed96876e1b8ae7.tar.bz2
perlweeklychallenge-club-9244d228f73b1f3b27c7788df8ed96876e1b8ae7.zip
Merge pull request #8082 from manfredi/challenge-217
Perl Solution for Challenge-217 Task #2 by Leo Manfredi
-rwxr-xr-xchallenge-217/manfredi/perl/ch-2.pl43
-rwxr-xr-xchallenge-217/manfredi/python/ch-2.py37
2 files changed, 80 insertions, 0 deletions
diff --git a/challenge-217/manfredi/perl/ch-2.pl b/challenge-217/manfredi/perl/ch-2.pl
new file mode 100755
index 0000000000..c0255b11c3
--- /dev/null
+++ b/challenge-217/manfredi/perl/ch-2.pl
@@ -0,0 +1,43 @@
+#!/usr/bin/env perl
+
+use v5.36;
+
+say "challenge-217-task2";
+
+# Task 2: Max Number
+# You are given a list of positive integers.
+# Write a script to concatenate the integers to form the highest possible value.
+
+# Example 1
+# Input: @list = (1, 23)
+# Output: 231
+
+# Example 2
+# Input: @list = (10, 3, 2)
+# Output: 3210
+
+# Example 3
+# Input: @list = (31, 2, 4, 10)
+# Output: 431210
+
+sub max_number {
+ my @list = @{+shift};
+ $" = ', ';
+ say "Input: \@list = (@list)";
+ my $out = join "", sort { $b cmp $a } @list;
+ say "Output: $out\n";
+}
+
+while (<DATA>) {
+ chomp;
+ my @list = split /\s*,\s*/;
+ max_number(\@list);
+}
+
+
+__DATA__
+1, 23
+10, 3 ,2
+31, 2 , 4, 10
+5, 11, 4, 1, 2
+1, 10
diff --git a/challenge-217/manfredi/python/ch-2.py b/challenge-217/manfredi/python/ch-2.py
new file mode 100755
index 0000000000..5742710086
--- /dev/null
+++ b/challenge-217/manfredi/python/ch-2.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python3
+# Python 3.9.2 on Debian GNU/Linux 11 (bullseye)
+
+print('challenge-217-task2')
+
+# Task 2: Max Number
+# You are given a list of positive integers.
+# Write a script to concatenate the integers to form the highest possible value.
+
+# Example 1
+# Input: @list = (1, 23)
+# Output: 231
+
+# Example 2
+# Input: @list = (10, 3, 2)
+# Output: 3210
+
+# Example 3
+# Input: @list = (31, 2, 4, 10)
+# Output: 431210
+
+def max_number(nums: list[int]) -> int:
+ print(f"Input: list = {nums}")
+ out = ''.join(sorted([ str(i) for i in nums], reverse = True))
+ return int(out)
+
+
+def main():
+ print("Output: ", max_number([1, 23]), "\n")
+ print("Output: ", max_number([10, 3 ,2]), "\n")
+ print("Output: ", max_number([31, 2 , 4, 10]), "\n")
+ print("Output: ", max_number([5, 11, 4, 1, 2]), "\n")
+ print("Output: ", max_number([1, 10]), "\n")
+
+
+if __name__ == '__main__':
+ main()