aboutsummaryrefslogtreecommitdiff
path: root/challenge-107
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-11-05 15:58:08 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-11-05 15:58:08 +0000
commitc332a12738c06cd4dcf92702e86ce69c2e826bd7 (patch)
tree74f81957c0455b8fd218454d9131d4fcf5430064 /challenge-107
parent99eada75aaf9004ad00c84c089c4a98d1c72bf14 (diff)
downloadperlweeklychallenge-club-c332a12738c06cd4dcf92702e86ce69c2e826bd7.tar.gz
perlweeklychallenge-club-c332a12738c06cd4dcf92702e86ce69c2e826bd7.tar.bz2
perlweeklychallenge-club-c332a12738c06cd4dcf92702e86ce69c2e826bd7.zip
Add Python solution to challenge 107
Diffstat (limited to 'challenge-107')
-rw-r--r--challenge-107/paulo-custodio/Makefile2
-rw-r--r--challenge-107/paulo-custodio/python/ch-1.py59
-rw-r--r--challenge-107/paulo-custodio/python/ch-2.py43
-rwxr-xr-xchallenge-107/paulo-custodio/test.pl4
4 files changed, 104 insertions, 4 deletions
diff --git a/challenge-107/paulo-custodio/Makefile b/challenge-107/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-107/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-107/paulo-custodio/python/ch-1.py b/challenge-107/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..db2c65edd5
--- /dev/null
+++ b/challenge-107/paulo-custodio/python/ch-1.py
@@ -0,0 +1,59 @@
+#!/usr/bin/env python3
+
+# Challenge 107
+#
+# TASK #1 - Self-descriptive Numbers
+# Submitted by: Mohammad S Anwar
+# Write a script to display the first three self-descriptive numbers. As per
+# wikipedia, the definition of Self-descriptive Number is
+#
+# In mathematics, a self-descriptive number is an integer m that in a given
+# base b is b digits long in which each digit d at position n (the most
+# significant digit being at position 0 and the least significant at position
+# b-1) counts how many instances of digit n are in m.
+#
+# For example:
+#
+# 1210 is a four-digit self-descriptive number:
+#
+# position 0 has value 1 i.e. there is only one 0 in the number
+# position 1 has value 2 i.e. there are two 1 in the number
+# position 2 has value 1 i.e. there is only one 2 in the number
+# position 3 has value 0 i.e. there is no 3 in the number
+# Output
+# 1210, 2020, 21200
+
+def is_self_descriptive(n):
+ for i in range(len(n)):
+ count = len(list(filter(lambda x: x==i, n)))
+ if n[i]!=count:
+ return False
+ return True
+
+def increment(n, base):
+ i = len(n)-1
+ while i >= 0:
+ n[i] += 1
+ if n[i] < base:
+ return
+ else:
+ n[i] = 0
+ i -= 1
+ n.insert(0, 1)
+
+def self_descriptive(num):
+ found = []
+ base = 4
+ while True:
+ n = [0]*base
+ n[0] = 1
+ while len(n)==base:
+ if is_self_descriptive(n):
+ found.append("".join([str(x) for x in n]))
+ if len(found) >= num:
+ return found
+ increment(n, base)
+ base += 1
+
+self_descr = self_descriptive(3)
+print(", ".join(self_descr))
diff --git a/challenge-107/paulo-custodio/python/ch-2.py b/challenge-107/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..5874da0378
--- /dev/null
+++ b/challenge-107/paulo-custodio/python/ch-2.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python3
+
+# Challenge 107
+#
+# TASK #2 - List Methods
+# Submitted by: Mohammad S Anwar
+# Write a script to list methods of a package/class.
+#
+# Example
+# package Calc;
+#
+# use strict;
+# use warnings;
+#
+# sub new { bless {}, shift; }
+# sub add { }
+# sub mul { }
+# sub div { }
+#
+# 1;
+# Output
+# BEGIN
+# mul
+# div
+# new
+# add
+
+class Calc():
+ def __init__(self):
+ pass
+ def new(self):
+ pass
+ def add(self):
+ pass
+ def mul(self):
+ pass
+ def div(self):
+ pass
+
+method_list = sorted([func for func in dir(Calc) \
+ if callable(getattr(Calc, func)) and not func.startswith("__")])
+for func in method_list:
+ print(func)
diff --git a/challenge-107/paulo-custodio/test.pl b/challenge-107/paulo-custodio/test.pl
deleted file mode 100755
index ba6c37260b..0000000000
--- a/challenge-107/paulo-custodio/test.pl
+++ /dev/null
@@ -1,4 +0,0 @@
-#!/usr/bin/env perl
-use Modern::Perl;
-use Test::More;
-require '../../challenge-001/paulo-custodio/test.pl';