aboutsummaryrefslogtreecommitdiff
path: root/challenge-002
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2023-03-10 20:16:08 +0100
committerLubos Kolouch <lubos@kolouch.net>2023-03-10 20:16:08 +0100
commit5d6aa430b0aef716b216cc1e8de2297b576c8d72 (patch)
treeedc9685bd9ddfe18b5691ca3e7f479a134139995 /challenge-002
parentf2e33c0038917ad43651d0c4e8b0bb310eaed541 (diff)
downloadperlweeklychallenge-club-5d6aa430b0aef716b216cc1e8de2297b576c8d72.tar.gz
perlweeklychallenge-club-5d6aa430b0aef716b216cc1e8de2297b576c8d72.tar.bz2
perlweeklychallenge-club-5d6aa430b0aef716b216cc1e8de2297b576c8d72.zip
Challenges 2 3 4 LK Perl Python
Diffstat (limited to 'challenge-002')
-rw-r--r--challenge-002/lubos-kolouch/perl/ch-1.pl44
-rw-r--r--challenge-002/lubos-kolouch/perl/ch-2.pl44
-rw-r--r--challenge-002/lubos-kolouch/python/ch-1.py32
-rw-r--r--challenge-002/lubos-kolouch/python/ch-2.py59
4 files changed, 179 insertions, 0 deletions
diff --git a/challenge-002/lubos-kolouch/perl/ch-1.pl b/challenge-002/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..51d07dda4d
--- /dev/null
+++ b/challenge-002/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use Test::More tests => 2;
+
+=head1 NAME
+
+remove_leading_zeros - removes leading zeros from positive numbers
+
+=head1 SYNOPSIS
+
+ use remove_leading_zeros;
+ my $num = "0001234";
+ my $result = remove_leading_zeros($num);
+ print "$result\n"; # "1234"
+
+=head1 DESCRIPTION
+
+This module exports one function, remove_leading_zeros, which takes a string
+containing a positive number with leading zeros and returns the number with
+leading zeros removed.
+
+=head1 FUNCTIONS
+
+=head2 remove_leading_zeros
+
+ my $result = remove_leading_zeros($num);
+
+Removes leading zeros from the positive number in $num and returns the result.
+
+=cut
+
+sub remove_leading_zeros {
+ my $num = shift;
+ $num =~ s/^0+//;
+ return $num;
+}
+
+# Test remove_leading_zeros function
+is(remove_leading_zeros("0001234"), "1234", "Leading zeros removed from number");
+is(remove_leading_zeros("000"), "", "Single zero ");
+
+done_testing();
+
diff --git a/challenge-002/lubos-kolouch/perl/ch-2.pl b/challenge-002/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..73f8c52931
--- /dev/null
+++ b/challenge-002/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More tests => 3;
+
+my @chars = ('0' .. '9', 'A' .. 'Y');
+
+# Convert from base35 to integer
+sub base35_to_int {
+ my $str = shift;
+ my $int = 0;
+ my $len = length($str);
+ for (my $i = 0; $i < $len; $i++) {
+ my $char = substr($str, $i, 1);
+ my $val = 0;
+ for (my $j = 0; $j < @chars; $j++) {
+ if ($chars[$j] eq $char) {
+ $val = $j;
+ last;
+ }
+ }
+ $int += $val * (35 ** ($len - $i - 1));
+ }
+ return $int;
+}
+
+# Convert from integer to base35
+sub int_to_base35 {
+ my $int = shift;
+ my $str = '';
+ while ($int > 0) {
+ my $mod = $int % 35;
+ $str = $chars[$mod] . $str;
+ $int = int($int / 35);
+ }
+ return $str;
+}
+
+# Test cases
+is(base35_to_int('A'), 10);
+is(base35_to_int('1A'), 45);
+is(int_to_base35(45), '1A');
+is(int_to_base35(12345), 'A2P');
diff --git a/challenge-002/lubos-kolouch/python/ch-1.py b/challenge-002/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..3e431db4ad
--- /dev/null
+++ b/challenge-002/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3
+"""
+remove_leading_zeros - removes leading zeros from positive numbers
+"""
+
+import re
+import unittest
+
+
+def remove_leading_zeros(num: str) -> str:
+ """
+ Removes leading zeros from the positive number in `num` and returns the result.
+
+ Args:
+ num: A string containing a positive number with leading zeros.
+
+ Returns:
+ A string with the number in `num` with leading zeros removed.
+ """
+ return re.sub(r'^0+(?!$)', '', num)
+
+
+class TestRemoveLeadingZeros(unittest.TestCase):
+
+ def test_remove_leading_zeros(self):
+ self.assertEqual(remove_leading_zeros("0001234"), "1234")
+ self.assertEqual(remove_leading_zeros("000"), "0")
+ self.assertEqual(remove_leading_zeros("0"), "0")
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/challenge-002/lubos-kolouch/python/ch-2.py b/challenge-002/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..52da486e95
--- /dev/null
+++ b/challenge-002/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,59 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# Importing the necessary libraries
+import unittest
+
+
+# Define the base35 class
+class Base35:
+
+ def __init__(self):
+ self.digits = [
+ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C",
+ "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",
+ "Q", "R", "S", "T", "U", "V", "W", "X", "Y"
+ ]
+
+ # Function to convert int to base35
+ def int2base35(self, n):
+ if n < 0:
+ raise ValueError("Negative numbers can't be converted to base35")
+ elif n == 0:
+ return 0
+ else:
+ result = ""
+ while n > 0:
+ digit = self.digits[n % 35]
+ result = digit + result
+ n = n // 35
+ return result
+
+ # Function to convert base35 to int
+ def base352int(self, b):
+ result = 0
+ for i, c in enumerate(b[::-1]):
+ result += self.digits.index(c) * (35**i)
+ return result
+
+
+# Define the test class
+class TestBase35(unittest.TestCase):
+ # Set up the test class
+ def setUp(self):
+ self.base35 = Base35()
+
+ # Test to convert int to base35
+ def test_int2base35(self):
+ self.assertEqual(self.base35.int2base35(100), "2U")
+ self.assertEqual(self.base35.int2base35(200), "5P")
+
+ # Test to convert base35 to int
+ def test_base352int(self):
+ self.assertEqual(self.base35.base352int("2U"), 100)
+ self.assertEqual(self.base35.base352int("5P"), 200)
+
+
+# Run the tests
+if __name__ == '__main__':
+ unittest.main()