From 0e3775f53fc56ed1528f508e80450ef85884d6ff Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Wed, 3 Nov 2021 14:55:57 +0000 Subject: Add Python solution to challenge 115 --- challenge-115/paulo-custodio/Makefile | 2 ++ challenge-115/paulo-custodio/python/ch-1.py | 43 +++++++++++++++++++++++++++++ challenge-115/paulo-custodio/python/ch-2.py | 39 ++++++++++++++++++++++++++ challenge-115/paulo-custodio/test.pl | 4 --- 4 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 challenge-115/paulo-custodio/Makefile create mode 100644 challenge-115/paulo-custodio/python/ch-1.py create mode 100644 challenge-115/paulo-custodio/python/ch-2.py delete mode 100755 challenge-115/paulo-custodio/test.pl diff --git a/challenge-115/paulo-custodio/Makefile b/challenge-115/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-115/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-115/paulo-custodio/python/ch-1.py b/challenge-115/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..cd2ec0c787 --- /dev/null +++ b/challenge-115/paulo-custodio/python/ch-1.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + +# Challenge 115 +# +# TASK #1 - String Chain +# Submitted by: Mohammad S Anwar +# You are given an array of strings. +# +# Write a script to find out if the given strings can be chained to form a +# circle. Print 1 if found otherwise 0. +# +# A string $S can be put before another string $T in circle if the last +# character of $S is same as first character of $T. +# +# Examples: +# Input: @S = ("abc", "dea", "cd") +# Output: 1 as we can form circle e.g. "abc", "cd", "dea". +# +# Input: @S = ("ade", "cbd", "fgh") +# Output: 0 as we can't form circle. + +import sys + +def is_circle(words): + def worker(pending, path): + found_solution = False + if len(pending)==0: + if not found_solution: + found_solution = path[-1][-1] == path[0][0] + else: + for word in pending: + if len(path)==0 or path[-1][-1] == word[0]: + new_pending = list(filter(lambda x:x != word, pending)) + if not found_solution: + found_solution = worker(new_pending, [*path, word]) + return found_solution + + return worker(words, []) + +if is_circle(sys.argv[1:]): + print(1) +else: + print(0) diff --git a/challenge-115/paulo-custodio/python/ch-2.py b/challenge-115/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..1e080c88f2 --- /dev/null +++ b/challenge-115/paulo-custodio/python/ch-2.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +# Challenge 115 +# +# TASK #2 - Largest Multiple +# Submitted by: Mohammad S Anwar +# You are given a list of positive integers (0-9), single digit. +# +# Write a script to find the largest multiple of 2 that can be formed from the +# list. +# +# Examples +# Input: @N = (1, 0, 2, 6) +# Output: 6210 +# +# Input: @N = (1, 4, 2, 8) +# Output: 8412 +# +# Input: @N = (4, 1, 7, 6) +# Output: 7614 + +import sys + +def largest_mult2(nums): + # select smallest even number for last element + even = list(filter(lambda x: x%2 == 0, nums)) + if len(even)==0: + return 0 # no even numbers + even.sort() + last = even[0] + + # sort the other elements in descending order + nums = list(filter(lambda x: x!=last, nums)) + nums.sort() + nums = nums[::-1] + + return int("".join([str(x) for x in [*nums, last]])) + +print(largest_mult2([int(x) for x in sys.argv[1:]])) diff --git a/challenge-115/paulo-custodio/test.pl b/challenge-115/paulo-custodio/test.pl deleted file mode 100755 index ba6c37260b..0000000000 --- a/challenge-115/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'; -- cgit