From 0cbb2f6950487fbb5c37180c54c9a3df66d85f7f Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Tue, 16 May 2023 15:34:49 +0200 Subject: feat(challenge-069/lubos-kolouch/p[erl,ython]/ch-[1,2].[ly]): Challenge 069 LK Python Perl --- challenge-069/lubos-kolouch/perl/ch-1.pl | 23 +++++++++++++++++++++++ challenge-069/lubos-kolouch/perl/ch-2.pl | 15 +++++++++++++++ challenge-069/lubos-kolouch/python/ch-1.py | 21 +++++++++++++++++++++ challenge-069/lubos-kolouch/python/ch-2.py | 13 +++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 challenge-069/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-069/lubos-kolouch/perl/ch-2.pl create mode 100644 challenge-069/lubos-kolouch/python/ch-1.py create mode 100644 challenge-069/lubos-kolouch/python/ch-2.py diff --git a/challenge-069/lubos-kolouch/perl/ch-1.pl b/challenge-069/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..1fe61abdb1 --- /dev/null +++ b/challenge-069/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,23 @@ +use strict; +use warnings; +use feature 'say'; +use List::Util 'all'; + +sub is_strobogrammatic { + my $n = shift; + my %strobo_pairs = ('0' => '0', '1' => '1', '6' => '9', '8' => '8', '9' => '6'); + my @str_n = split //, $n; + return all { $strobo_pairs{$str_n[$_]} eq $str_n[-$_-1] } 0..@str_n/2; +} + +sub find_strobogrammatic { + my ($A, $B) = @_; + return grep {is_strobogrammatic($_)} $A..$B; +} + +# test cases to validate the solution + +use Test::More; +is_deeply([find_strobogrammatic(50, 100)], [69, 88, 96], 'Test Case 1'); +done_testing(); + diff --git a/challenge-069/lubos-kolouch/perl/ch-2.pl b/challenge-069/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..54529d0196 --- /dev/null +++ b/challenge-069/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,15 @@ +use strict; +use warnings; +use feature 'say'; + +sub generate_string { + my $n = shift; + return "" if $n == 0; + return "0" if $n == 1; + my $s_n_1 = generate_string($n - 1); + my $reversed_switched = join "", map { $_ eq '0' ? '1' : '0' } reverse split //, $s_n_1; + return $s_n_1 . "0" . $reversed_switched; +} + +say generate_string(30); + diff --git a/challenge-069/lubos-kolouch/python/ch-1.py b/challenge-069/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..b5e736fa47 --- /dev/null +++ b/challenge-069/lubos-kolouch/python/ch-1.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def is_strobogrammatic(n: int) -> bool: + """Check if a number is strobogrammatic.""" + strobo_pairs = {'0': '0', '1': '1', '6': '9', '8': '8', '9': '6'} + str_n = str(n) + return all(strobo_pairs.get(i) == j for i, j in zip(str_n, str_n[::-1])) + + +def find_strobogrammatic(A: int, B: int) -> List[int]: + """Find all strobogrammatic numbers in a given range.""" + return [n for n in range(A, B+1) if is_strobogrammatic(n)] + + +# test cases to validate the solution + +assert (find_strobogrammatic(50, 100) == [69, 88, 96]) diff --git a/challenge-069/lubos-kolouch/python/ch-2.py b/challenge-069/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..5efef48bb8 --- /dev/null +++ b/challenge-069/lubos-kolouch/python/ch-2.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +def generate_string(n: int) -> str: + if n == 0: + return "" + if n == 1: + return "0" + s_n_1 = generate_string(n - 1) + return s_n_1 + "0" + "".join('0' if c == '1' else '1' for c in s_n_1[::-1]) + + +print(generate_string(30)) -- cgit