aboutsummaryrefslogtreecommitdiff
path: root/challenge-051/paulo-custodio/python/ch-1.py
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2024-09-16 09:41:35 +0200
committerLubos Kolouch <lubos@kolouch.net>2024-09-16 09:41:35 +0200
commit65b9d6b25e0a823ca7ab6d15744ff98eb3697471 (patch)
treefcbcdebd50e3e146dfecf519701ec04b191053eb /challenge-051/paulo-custodio/python/ch-1.py
parentbd1fe7ae50ca42bda58c134b9edfdc287fb3f386 (diff)
parent68e321dd32a834f54b55d5e8924f04358e41cf1f (diff)
downloadperlweeklychallenge-club-65b9d6b25e0a823ca7ab6d15744ff98eb3697471.tar.gz
perlweeklychallenge-club-65b9d6b25e0a823ca7ab6d15744ff98eb3697471.tar.bz2
perlweeklychallenge-club-65b9d6b25e0a823ca7ab6d15744ff98eb3697471.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-051/paulo-custodio/python/ch-1.py')
-rw-r--r--challenge-051/paulo-custodio/python/ch-1.py33
1 files changed, 33 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:]]))