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 /challenge-003 | |
| 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
Diffstat (limited to 'challenge-003')
| -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 |
4 files changed, 126 insertions, 0 deletions
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]))) |
