diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-11-03 11:07:35 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-11-03 11:07:35 +0000 |
| commit | da070373c4ff340135306b593a16d601daabc6ea (patch) | |
| tree | 4d94f9603d940a344300cdeb5682a684fae49c95 | |
| parent | 87883da68b695f54a3bd4f108fc5c31784879dac (diff) | |
| download | perlweeklychallenge-club-da070373c4ff340135306b593a16d601daabc6ea.tar.gz perlweeklychallenge-club-da070373c4ff340135306b593a16d601daabc6ea.tar.bz2 perlweeklychallenge-club-da070373c4ff340135306b593a16d601daabc6ea.zip | |
Add Python solution to challenge 116
| -rw-r--r-- | challenge-001/paulo-custodio/test.pl | 2 | ||||
| -rw-r--r-- | challenge-116/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-116/paulo-custodio/python/ch-1.py | 52 | ||||
| -rw-r--r-- | challenge-116/paulo-custodio/python/ch-2.py | 37 | ||||
| -rwxr-xr-x | challenge-116/paulo-custodio/test.pl | 4 |
5 files changed, 92 insertions, 5 deletions
diff --git a/challenge-001/paulo-custodio/test.pl b/challenge-001/paulo-custodio/test.pl index c8dc2b9f75..7efe038555 100644 --- a/challenge-001/paulo-custodio/test.pl +++ b/challenge-001/paulo-custodio/test.pl @@ -148,7 +148,7 @@ sub build { return "bf $prog_wo_ext.bf"; } if (/^c$/) { - run("gcc $prog -o $prog_wo_ext -lmpfr -lgmp") if (!-f $exe || -M $exe > -M $prog); + run("gcc $prog -o $prog_wo_ext -lm -lmpfr -lgmp") if (!-f $exe || -M $exe > -M $prog); return $exe; } if (/^cpp$/) { diff --git a/challenge-116/paulo-custodio/Makefile b/challenge-116/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-116/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-116/paulo-custodio/python/ch-1.py b/challenge-116/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..4dd2b06e2c --- /dev/null +++ b/challenge-116/paulo-custodio/python/ch-1.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 + +# Challenge 116 +# +# TASK #1 - Number Sequence +# Submitted by: Mohammad S Anwar +# You are given a number $N >= 10. +# +# Write a script to split the given number such that the difference between two +# consecutive numbers is always 1 and it shouldn't have leading 0. +# +# Print the given number if it impossible to split the number. +# +# Example +# Input: $N = 1234 +# Output: 1,2,3,4 +# +# Input: $N = 91011 +# Output: 9,10,11 +# +# Input: $N = 10203 +# Output: 10203 as it is impossible to split satisfying the conditions. + +import sys +import re + +def print_sequences(rest): + def worker(rest, prev): + found_solution = False + if rest=='': + if not found_solution: + print(",".join(prev)) + found_solution = True + else: + for i in range(1, len(rest)+1): + pref = rest[:i] + suff = rest[i:] + if not re.match(r"^0", suff): + if len(prev) > 0: + if int(prev[-1])+1 == int(pref): + if not found_solution: + if worker(suff, [*prev, pref]): + found_solution = True + else: + if not found_solution: + if worker(suff, [*prev, pref]): + found_solution = True + return found_solution + + return worker(rest, []) + +print_sequences(sys.argv[1]) diff --git a/challenge-116/paulo-custodio/python/ch-2.py b/challenge-116/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..5d538ff59e --- /dev/null +++ b/challenge-116/paulo-custodio/python/ch-2.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + +# Challenge 116 +# +# TASK #2 - Sum of Squares +# Submitted by: Mohammad Meraj Zia +# You are given a number $N >= 10. +# +# Write a script to find out if the given number $N is such that sum of squares +# of all digits is a perfect square. Print 1 if it is otherwise 0. +# +# Example +# Input: $N = 34 +# Ouput: 1 as 3^2 + 4^2 => 9 + 16 => 25 => 5^2 +# +# Input: $N = 50 +# Output: 1 as 5^2 + 0^2 => 25 + 0 => 25 => 5^2 +# +# Input: $N = 52 +# Output: 0 as 5^2 + 2^2 => 25 + 4 => 29 + +import sys +import math + +def sum_of_squares_is_perfect_square(num): + if num < 10: + return False + sum = 0 + for digit in [int(x) for x in str(num)]: + sum += digit**2 + sqint = int(math.sqrt(sum)) + return sqint**2 == sum + +if sum_of_squares_is_perfect_square(int(sys.argv[1])): + print(1) +else: + print(0) diff --git a/challenge-116/paulo-custodio/test.pl b/challenge-116/paulo-custodio/test.pl deleted file mode 100755 index ba6c37260b..0000000000 --- a/challenge-116/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'; |
