aboutsummaryrefslogtreecommitdiff
path: root/challenge-085/lubos-kolouch/python
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2020-11-08 19:40:05 +0100
committerAbigail <abigail@abigail.be>2020-11-08 19:40:05 +0100
commit4e34dd5d0e89307aca9f4cd3bb6072c07f942ff1 (patch)
tree52495c7853bf31c427ec26f500c66c5bed6753b4 /challenge-085/lubos-kolouch/python
parent520db2cfca438cdc9a78932e5f479e28abee150b (diff)
parentff7475c4559b1087f402725a7c526d3f780e3434 (diff)
downloadperlweeklychallenge-club-4e34dd5d0e89307aca9f4cd3bb6072c07f942ff1.tar.gz
perlweeklychallenge-club-4e34dd5d0e89307aca9f4cd3bb6072c07f942ff1.tar.bz2
perlweeklychallenge-club-4e34dd5d0e89307aca9f4cd3bb6072c07f942ff1.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-085/lubos-kolouch/python')
-rw-r--r--challenge-085/lubos-kolouch/python/ch-1.py42
-rw-r--r--challenge-085/lubos-kolouch/python/ch-2.py25
2 files changed, 67 insertions, 0 deletions
diff --git a/challenge-085/lubos-kolouch/python/ch-1.py b/challenge-085/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..43ae98ab31
--- /dev/null
+++ b/challenge-085/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,42 @@
+#!/bin/env python
+"""
+#===============================================================================
+#
+# FILE: ch-1.py
+#
+# USAGE: ./ch-1.py
+#
+# DESCRIPTION: The weekly challenge #085
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-085/
+# Task 1 - Triplet Sum
+#
+# AUTHOR: Lubos Kolouch
+#===============================================================================
+"""
+
+from itertools import combinations
+
+
+def is_triplet_existing(in_arr):
+ " Return if the triplet exists "
+
+ # we can exclude any numbers greater than 2, the upper limit
+ limited_arr = [elem for elem in in_arr if elem < 2]
+
+ # exit if we don't have at least a triplet left
+ if len(limited_arr) < 3:
+ return 0
+
+ # Loop throug the combinator, exit if found a valid combination
+ for comb in combinations(limited_arr, 3):
+ if 2 > sum(comb) > 1:
+ return 1
+
+ # Bad luck, return 0
+ return 0
+
+
+assert is_triplet_existing([1.2, 0.4, 0.1, 2.5]) == 1
+assert is_triplet_existing([0.2, 1.5, 0.9, 1.1]) == 0
+assert is_triplet_existing([0.5, 1.1, 0.3, 0.7]) == 1
+assert is_triplet_existing([1, 2, 4, 6]) == 0
diff --git a/challenge-085/lubos-kolouch/python/ch-2.py b/challenge-085/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..1f93585797
--- /dev/null
+++ b/challenge-085/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,25 @@
+#!/bin/env python
+" Task 2 Challenge 085 "
+
+from math import log, sqrt
+
+
+def is_power_two_integers(what):
+ " Find out if the number can be represented as x^y "
+
+ if what == 1:
+ return 1
+
+ for i in range(2, int(sqrt(what)) + 1):
+ diff = log(what) / log(i)
+
+ if diff - int(diff) < 0.00000001:
+ return 1
+
+ return 0
+
+
+assert is_power_two_integers(8) == 1
+assert is_power_two_integers(15) == 0
+assert is_power_two_integers(125) == 1
+assert is_power_two_integers(625) == 1