diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-09-02 21:51:10 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-09-02 21:51:10 +0100 |
| commit | ba7bd85b4ae5c65a9d91f94e650f211753363694 (patch) | |
| tree | fa1a18f1a129dfc3644798036e00f039fc79a02e | |
| parent | 22dc7f476dac1732c9303ab7bf3cc9b361899053 (diff) | |
| download | perlweeklychallenge-club-ba7bd85b4ae5c65a9d91f94e650f211753363694.tar.gz perlweeklychallenge-club-ba7bd85b4ae5c65a9d91f94e650f211753363694.tar.bz2 perlweeklychallenge-club-ba7bd85b4ae5c65a9d91f94e650f211753363694.zip | |
Add Python solution to challenge 051
| -rw-r--r-- | challenge-051/paulo-custodio/python/ch-1.py | 33 | ||||
| -rw-r--r-- | challenge-051/paulo-custodio/python/ch-2.py | 28 |
2 files changed, 61 insertions, 0 deletions
diff --git a/challenge-051/paulo-custodio/python/ch-1.py b/challenge-051/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..41d49dfb04 --- /dev/null +++ b/challenge-051/paulo-custodio/python/ch-1.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 + +# Challenge 051 +# +# TASK #1 +# 3 Sum +# Given an array @Lof integers. Write a script to find all unique triplets such +# that a + b + c is same as the given target T. Also make sure a <= b <= c. +# +# Here is wiki page for more information. +# +# Example: +# +# @L = (-25, -10, -7, -3, 2, 4, 8, 10); +# +# One such triplet for target 0 i.e. -10 + 2 + 8 = 0. + +import sys + +TARGET = 0 + +def _3sum(nums): + for i in range(len(nums)-2): + outi = "+"+str(nums[i]) if nums[i] >= 0 else str(nums[i]) + for j in range(i+1, len(nums)-1): + outj = "+"+str(nums[j]) if nums[j] >= 0 else str(nums[j]) + for k in range(j+1, len(nums)): + outk = "+"+str(nums[k]) if nums[k] >= 0 else str(nums[k]) + if sum([nums[i], nums[j], nums[k]]) == TARGET: + return outi+outj+outk+"="+str(TARGET) + return "" + +print(_3sum([int(x) for x in sys.argv[1:]])) diff --git a/challenge-051/paulo-custodio/python/ch-2.py b/challenge-051/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..5a120fe9de --- /dev/null +++ b/challenge-051/paulo-custodio/python/ch-2.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + +# Challenge 051 +# +# TASK #2 +# Colourful Number +# Write a script to display all Colorful Number with 3 digits. +# +# A number can be declare Colorful Number where all the products of consecutive +# subsets of digit are different. +# +# For example, 263 is a Colorful Number since 2, 6, 3, 2x6, 6x3, 2x6x3 are +# unique. + +def uniq(mylist): + return list(dict.fromkeys(mylist)) + +def colorful(): + out = [] + for a in range (1, 10): + for b in range(10): + for c in range(10): + prods = [a, b, c, a*b, b*c, a*b*c] + if len(uniq(prods)) == 6: + out.append(a*100+b*10+c) + return out + +print(", ".join([str(x) for x in colorful()])) |
