diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-03-10 20:16:08 +0100 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-03-10 20:16:08 +0100 |
| commit | 5d6aa430b0aef716b216cc1e8de2297b576c8d72 (patch) | |
| tree | edc9685bd9ddfe18b5691ca3e7f479a134139995 | |
| parent | f2e33c0038917ad43651d0c4e8b0bb310eaed541 (diff) | |
| download | perlweeklychallenge-club-5d6aa430b0aef716b216cc1e8de2297b576c8d72.tar.gz perlweeklychallenge-club-5d6aa430b0aef716b216cc1e8de2297b576c8d72.tar.bz2 perlweeklychallenge-club-5d6aa430b0aef716b216cc1e8de2297b576c8d72.zip | |
Challenges 2 3 4 LK Perl Python
| -rw-r--r-- | challenge-002/lubos-kolouch/perl/ch-1.pl | 44 | ||||
| -rw-r--r-- | challenge-002/lubos-kolouch/perl/ch-2.pl | 44 | ||||
| -rw-r--r-- | challenge-002/lubos-kolouch/python/ch-1.py | 32 | ||||
| -rw-r--r-- | challenge-002/lubos-kolouch/python/ch-2.py | 59 | ||||
| -rw-r--r-- | challenge-003/lubos-kolouch/perl/ch-1.pl | 36 | ||||
| -rw-r--r-- | challenge-003/lubos-kolouch/perl/ch-2.pl | 30 | ||||
| -rw-r--r-- | challenge-003/lubos-kolouch/python/ch-1.py | 37 | ||||
| -rw-r--r-- | challenge-003/lubos-kolouch/python/ch-2.py | 23 | ||||
| -rw-r--r-- | challenge-004/lubos-kolouch/perl/ch-1.pl | 4 | ||||
| -rw-r--r-- | challenge-004/lubos-kolouch/perl/ch-2.pl | 29 | ||||
| -rw-r--r-- | challenge-004/lubos-kolouch/python/ch-1.py | 6 |
11 files changed, 344 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() diff --git a/challenge-003/lubos-kolouch/perl/ch-1.pl b/challenge-003/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..b565324530 --- /dev/null +++ b/challenge-003/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,36 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +sub is_smooth { + my $n = shift; + while ( $n % 2 == 0 ) { $n /= 2; } + while ( $n % 3 == 0 ) { $n /= 3; } + while ( $n % 5 == 0 ) { $n /= 5; } + return $n == 1; +} + +sub generate_smooth_numbers { + my $n = shift; + my @smooth_numbers = (1); + my $candidate = 1; + while ( @smooth_numbers < $n ) { + $candidate++; + push @smooth_numbers, $candidate if is_smooth($candidate); + } + return @smooth_numbers; +} + +my $n = shift || 10; +my @smooth_numbers = generate_smooth_numbers($n); + +print "First $n 5-smooth numbers:\n"; +print join( ", ", @smooth_numbers ), "\n"; + +use Test::More; + +my @expected = ( 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 ); +is_deeply( [ generate_smooth_numbers(10) ], \@expected, "generate_smooth_numbers(10)" ); + +done_testing; diff --git a/challenge-003/lubos-kolouch/perl/ch-2.pl b/challenge-003/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..3d51098713 --- /dev/null +++ b/challenge-003/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,30 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +sub generate_pascal_triangle { + my $rows = shift; + my @triangle = (); + for my $i ( 0 .. $rows - 1 ) { + my @row = (1); + for my $j ( 1 .. $i ) { + push @row, $triangle[ $i - 1 ][ $j - 1 ] + ( $triangle[ $i - 1 ][$j] // 0 ); + } + push @row, 1; + push @triangle, \@row; + } + return @triangle; +} + +my $rows = shift || 3; +if ( $rows < 3 ) { + die "Error: Pascal's Triangle must have at least 3 rows\n"; +} +my @triangle = generate_pascal_triangle($rows); + +print "Pascal's Triangle with $rows rows:\n"; +for my $i ( 0 .. $rows - 1 ) { + my $padding = " " x ( $rows - $i - 1 ); + print "$padding", join( " ", @{ $triangle[$i] } ), "\n"; +} diff --git a/challenge-003/lubos-kolouch/python/ch-1.py b/challenge-003/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..b4b34a55a4 --- /dev/null +++ b/challenge-003/lubos-kolouch/python/ch-1.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +def is_smooth(n): + while n % 2 == 0: + n //= 2 + while n % 3 == 0: + n //= 3 + while n % 5 == 0: + n //= 5 + return n == 1 + +def generate_smooth_numbers(n): + smooth_numbers = [1] + candidate = 1 + while len(smooth_numbers) < n: + candidate += 1 + if is_smooth(candidate): + smooth_numbers.append(candidate) + return smooth_numbers + +n = int(input("Enter the number of 5-smooth numbers to generate: ")) +smooth_numbers = generate_smooth_numbers(n) + +print(f"First {n} 5-smooth numbers:") +print(", ".join(map(str, smooth_numbers))) + +import unittest + + +class TestSmoothNumbers(unittest.TestCase): + def test_generate_smooth_numbers(self): + expected = [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] + self.assertEqual(generate_smooth_numbers(10), expected) + +if __name__ == '__main__': + unittest.main() diff --git a/challenge-003/lubos-kolouch/python/ch-2.py b/challenge-003/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..bca2eb9b03 --- /dev/null +++ b/challenge-003/lubos-kolouch/python/ch-2.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +def generate_pascal_triangle(rows): + triangle = [] + for i in range(rows): + row = [1] + for j in range(1, i): + row.append(triangle[i-1][j-1] + triangle[i-1][j]) + if i > 0: + row.append(1) + triangle.append(row) + return triangle + +rows = int(input("Enter the number of rows to generate: ")) +if rows < 3: + print("Error: Pascal's Triangle must have at least 3 rows") +else: + triangle = generate_pascal_triangle(rows) + print(f"Pascal's Triangle with {rows} rows:") + for i in range(rows): + padding = " " * (rows-i-1) + print(padding + " ".join(map(str, triangle[i]))) diff --git a/challenge-004/lubos-kolouch/perl/ch-1.pl b/challenge-004/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..57f17649ca --- /dev/null +++ b/challenge-004/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,4 @@ +my $pi = + "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"; + +print substr( $pi, 0, -1 * ( -s $0 ) ); diff --git a/challenge-004/lubos-kolouch/perl/ch-2.pl b/challenge-004/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..adad77eb43 --- /dev/null +++ b/challenge-004/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,29 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Test::More; + +# Read in the list of letters +my $letters = shift; +my %letter_counts; +for my $char ( split '', lc $letters ) { + $letter_counts{$char}++; +} + +# Read in the file of words and print out the matching ones +while ( my $word = <> ) { + chomp $word; + my %word_counts; + for my $char ( split '', lc $word ) { + $word_counts{$char}++; + } + my $matches = 1; + for my $char ( keys %word_counts ) { + unless ( $word_counts{$char} <= $letter_counts{$char} ) { + $matches = 0; + last; + } + } + print "$word\n" if $matches; +} diff --git a/challenge-004/lubos-kolouch/python/ch-1.py b/challenge-004/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..207ba38b08 --- /dev/null +++ b/challenge-004/lubos-kolouch/python/ch-1.py @@ -0,0 +1,6 @@ +import os + +script_size = os.path.getsize(__file__) +pi = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679" +print(pi[:script_size]) + |
