aboutsummaryrefslogtreecommitdiff
path: root/challenge-006
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2023-03-11 14:01:51 +0100
committerLubos Kolouch <lubos@kolouch.net>2023-03-11 14:01:51 +0100
commite1775c1d9e4d74de0a21e4e971a0eb701f21c8a5 (patch)
treed4844ae921ba5b4a3ce336efe063eec70293119c /challenge-006
parent5d6aa430b0aef716b216cc1e8de2297b576c8d72 (diff)
downloadperlweeklychallenge-club-e1775c1d9e4d74de0a21e4e971a0eb701f21c8a5.tar.gz
perlweeklychallenge-club-e1775c1d9e4d74de0a21e4e971a0eb701f21c8a5.tar.bz2
perlweeklychallenge-club-e1775c1d9e4d74de0a21e4e971a0eb701f21c8a5.zip
Blog 207
Diffstat (limited to 'challenge-006')
-rw-r--r--challenge-006/lubos-kolouch/perl/ch-1.pl72
-rw-r--r--challenge-006/lubos-kolouch/python/ch-1.py50
2 files changed, 122 insertions, 0 deletions
diff --git a/challenge-006/lubos-kolouch/perl/ch-1.pl b/challenge-006/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..03722cf548
--- /dev/null
+++ b/challenge-006/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,72 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+=head1 DESCRIPTION
+
+This script takes a list of numbers from command line and print the same in the compact form.
+
+For example, if you pass “1,2,3,4,9,10,14,15,16” then it should print the compact form like “1-4,9,10,14-16”.
+
+=cut
+
+# Get the list of numbers from command line
+my @numbers = @ARGV;
+
+# Sort the list of numbers in ascending order
+@numbers = sort { $a <=> $b } @numbers;
+
+# Initializing variables
+my @compact_list;
+my $first_num = $numbers[0];
+my $last_num = $numbers[0];
+
+# Generate the compact list
+foreach my $num (@numbers) {
+ if ( $num == $last_num + 1 ) {
+
+ # If the current number is 1 more than the last number,
+ # update the last number
+ $last_num = $num;
+ }
+ else {
+ # If the current number is not 1 more than the last number,
+ # add the range of numbers to the compact list
+ if ( $first_num == $last_num ) {
+ push @compact_list, $first_num;
+ }
+ else {
+ push @compact_list, "$first_num-$last_num";
+ }
+
+ # Reset the variables
+ $first_num = $num;
+ $last_num = $num;
+ }
+}
+
+# Add the last range of numbers to the compact list
+if ( $first_num == $last_num ) {
+ push @compact_list, $first_num;
+}
+else {
+ push @compact_list, "$first_num-$last_num";
+}
+
+# Print the compact list
+print join( ',', @compact_list );
+
+=head1 TESTING
+
+=over 4
+
+=item *
+
+Input: 1,2,3,4,9,10,14,15,16
+
+Expected Output: 1-4,9,10,14-16
+
+=back
+
+=cut
diff --git a/challenge-006/lubos-kolouch/python/ch-1.py b/challenge-006/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..24f6e3d0e0
--- /dev/null
+++ b/challenge-006/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,50 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+"""
+This script takes a list of numbers from command line and prints them in the compact form.
+
+Example:
+ Input: 1,2,3,4,9,10,14,15,16
+ Output: 1-4,9,10,14-16
+"""
+
+import sys
+
+
+def CompactList(numbers):
+ """This function takes a list of numbers as input and returns the list in compact form.
+
+ Args:
+ numbers (list): A list of numbers
+
+ Returns:
+ str: The list in compact form
+
+ Examples:
+ >>> CompactList([1,2,3,4,9,10,14,15,16])
+ '1-4,9,10,14-16'
+ """
+ compact_list = []
+ start = numbers[0]
+ end = numbers[0]
+ for i in range(1, len(numbers)):
+ if numbers[i] - numbers[i - 1] == 1:
+ end = numbers[i]
+ else:
+ if start == end:
+ compact_list.append(str(start))
+ else:
+ compact_list.append(str(start) + "-" + str(end))
+ start = numbers[i]
+ end = numbers[i]
+ if start == end:
+ compact_list.append(str(start))
+ else:
+ compact_list.append(str(start) + "-" + str(end))
+ return ",".join(compact_list)
+
+
+if __name__ == "__main__":
+ numbers = [int(i) for i in sys.argv[1].split(",")]
+ print(CompactList(numbers))