diff options
Diffstat (limited to 'challenge-085/lubos-kolouch/python')
| -rw-r--r-- | challenge-085/lubos-kolouch/python/ch-1.py | 42 | ||||
| -rw-r--r-- | challenge-085/lubos-kolouch/python/ch-2.py | 25 |
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 |
