aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2021-03-06 12:39:27 +0100
committerLubos Kolouch <lubos@kolouch.net>2021-03-06 12:39:27 +0100
commit2fe873a82153977fc43eebe84424fa4349389f3d (patch)
treef60807a75305d9bc177ac89d8f1d3798afc7ff95
parentb1706186629563669fdfc3ef8916f89197165568 (diff)
downloadperlweeklychallenge-club-2fe873a82153977fc43eebe84424fa4349389f3d.tar.gz
perlweeklychallenge-club-2fe873a82153977fc43eebe84424fa4349389f3d.tar.bz2
perlweeklychallenge-club-2fe873a82153977fc43eebe84424fa4349389f3d.zip
Challenge 102 LK Python
-rw-r--r--challenge-102/lubos-kolouch/python/ch-1.py51
-rw-r--r--challenge-102/lubos-kolouch/python/ch-2.py42
2 files changed, 93 insertions, 0 deletions
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#'