aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-03-07 07:52:40 +0000
committerGitHub <noreply@github.com>2021-03-07 07:52:40 +0000
commit9f289cc3388976fe7e4c23b92c2f3d6400d92fc8 (patch)
tree056f2c61e9c3751db11ec7353298aaa576a836c7
parent25c6859b46fe93da29f9b33f68e85bda60bb7ba9 (diff)
parent2fe873a82153977fc43eebe84424fa4349389f3d (diff)
downloadperlweeklychallenge-club-9f289cc3388976fe7e4c23b92c2f3d6400d92fc8.tar.gz
perlweeklychallenge-club-9f289cc3388976fe7e4c23b92c2f3d6400d92fc8.tar.bz2
perlweeklychallenge-club-9f289cc3388976fe7e4c23b92c2f3d6400d92fc8.zip
Merge pull request #3665 from LubosKolouch/master
Challenge 102 LK
-rw-r--r--challenge-102/lubos-kolouch/perl/ch-1.pl45
-rw-r--r--challenge-102/lubos-kolouch/perl/ch-2.pl50
-rw-r--r--challenge-102/lubos-kolouch/python/ch-1.py51
-rw-r--r--challenge-102/lubos-kolouch/python/ch-2.py42
4 files changed, 188 insertions, 0 deletions
diff --git a/challenge-102/lubos-kolouch/perl/ch-1.pl b/challenge-102/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..066d25b295
--- /dev/null
+++ b/challenge-102/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/perl
+#===============================================================================
+#
+# FILE: ch-1.pl
+#
+# USAGE: ./ch-1.pl
+#
+# DESCRIPTION: Perl Weekly Challenge #102
+# Task 1 - Rare Numbers
+#
+# AUTHOR: Lubos Kolouch
+# CREATED: 03/06/2021 11:18:30 AM
+#===============================================================================
+
+use strict;
+use warnings;
+
+sub get_rare_numbers {
+ my $what = shift;
+
+ my $pos = 1 . '0'x($what-1);
+
+ my @output;
+ while (1) {
+ $pos++;
+ last unless (length($pos) == $what);
+ my $rev_num = reverse $pos;
+
+ next unless $pos - $rev_num > 0;
+ next unless sqrt($pos - $rev_num) == int(sqrt($pos - $rev_num));
+ next unless sqrt($pos + $rev_num) == int(sqrt($pos + $rev_num));
+ print "$pos \n";
+
+ push @output, $pos;
+ }
+
+ return \@output;
+}
+
+use Test::More;
+
+is_deeply(get_rare_numbers(2), [65]);
+is_deeply(get_rare_numbers(6), [621770]);
+is_deeply(get_rare_numbers(9), [281089082]);
+
diff --git a/challenge-102/lubos-kolouch/perl/ch-2.pl b/challenge-102/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..600a29572a
--- /dev/null
+++ b/challenge-102/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,50 @@
+#!/usr/bin/perl
+#===============================================================================
+#
+# FILE: ch-2.pl
+#
+# USAGE: ./ch-2.pl
+#
+# DESCRIPTION: Perl Weekly Challenge #102
+# Task 2 - Hash-counting String
+#
+# AUTHOR: Lubos Kolouch
+# CREATED: 03/06/2021 11:18:30 AM
+#===============================================================================
+
+use strict;
+use warnings;
+
+sub get_hash {
+ my $what = shift;
+
+ # we know the last position has length
+ # of the input. So we can just backtrack
+
+ my $output = '';
+
+ my $pos = $what;
+ while ($pos > 0) {
+ my $append_str = $pos.'#';
+
+ if ($pos > 1) {
+ $output = $pos.'#'.$output;
+ } else {
+ $output = '#'.$output;
+ }
+
+ $pos -= length($append_str);
+ }
+
+ return $output;
+}
+
+use Test::More;
+
+is_deeply(get_hash(1), '#');
+is_deeply(get_hash(2), '2#');
+is_deeply(get_hash(3), '#3#');
+is_deeply(get_hash(10), '#3#5#7#10#');
+is_deeply(get_hash(14), '2#4#6#8#11#14#');
+
+done_testing;
diff --git a/challenge-102/lubos-kolouch/python/ch-1.py b/challenge-102/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..27093ae3fe
--- /dev/null
+++ b/challenge-102/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,51 @@
+#!/bin/env python
+"""
+#===============================================================================
+#
+# FILE: ch-1.py
+#
+# USAGE: ./ch-1.py
+#
+# DESCRIPTION: Perl Weekly Challenge #102
+# Task 1 - Rare Numbers
+#
+# AUTHOR: Lubos Kolouch
+# CREATED: 03/06/2021 11:18:30 AM
+#===============================================================================
+"""
+from math import sqrt
+
+
+def get_rare_numbers(what: int):
+ """ Get all rare number in the give range """
+
+ pos = '1' + '0'*(what-1)
+
+ output = []
+ while 1:
+ pos = str(int(pos)+1)
+
+ if len(pos) > what:
+ break
+
+ rev_num = pos[::-1]
+
+ if int(pos) - int(rev_num) <= 0:
+ continue
+
+ if sqrt(int(pos) - int(rev_num)) != int(sqrt(int(pos) - int(rev_num))):
+ continue
+
+ if sqrt(int(pos) + int(rev_num)) != int(sqrt(int(pos) + int(rev_num))):
+ continue
+
+ print(pos)
+
+ output.append(int(pos))
+
+ return output
+
+
+assert get_rare_numbers(2) == [65]
+assert get_rare_numbers(6) == [621770]
+assert get_rare_numbers(9) == [281089082]
diff --git a/challenge-102/lubos-kolouch/python/ch-2.py b/challenge-102/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..16d7705793
--- /dev/null
+++ b/challenge-102/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,42 @@
+#!/bin/env python
+"""
+#===============================================================================
+#
+# FILE: ch-2.py
+#
+# USAGE: ./ch-2.p
+#
+# DESCRIPTION: Perl Weekly Challenge #102
+# Task 2 - Hash-counting String
+#
+# AUTHOR: Lubos Kolouch
+# CREATED: 03/06/2021 11:18:30 AM
+#===============================================================================
+"""
+
+
+def get_hash(what: int):
+ """ get the hash """
+ # we know the last position has length
+ # of the input. So we can just backtrack
+ output = ''
+
+ pos = what
+ while pos > 0:
+ append_str = str(pos)+'#'
+
+ if pos > 1:
+ output = str(pos)+'#'+output
+ else:
+ output = '#'+output
+
+ pos = int(pos) - len(append_str)
+
+ return output
+
+
+assert get_hash(1) == '#'
+assert get_hash(2) == '2#'
+assert get_hash(3) == '#3#'
+assert get_hash(10) == '#3#5#7#10#'
+assert get_hash(14) == '2#4#6#8#11#14#'