diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-11-15 01:19:11 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-11-15 01:19:11 +0000 |
| commit | ebc08a3eeb4b90e10c5c6e013e476c34e07b01d1 (patch) | |
| tree | f518160b6f2aa0baa5fa47398f0230b6dc469215 /challenge-086 | |
| parent | 056d904037e4c3a32913158368fde214b0dde3f0 (diff) | |
| parent | 7a4afe3f5543d127d5ad2d3127dfc6e0272d0fc5 (diff) | |
| download | perlweeklychallenge-club-ebc08a3eeb4b90e10c5c6e013e476c34e07b01d1.tar.gz perlweeklychallenge-club-ebc08a3eeb4b90e10c5c6e013e476c34e07b01d1.tar.bz2 perlweeklychallenge-club-ebc08a3eeb4b90e10c5c6e013e476c34e07b01d1.zip | |
Merge pull request #2761 from LubosKolouch/master
Solutions task 1 Perl Python LK
Diffstat (limited to 'challenge-086')
| -rw-r--r-- | challenge-086/lubos-kolouch/perl/ch-1.pl | 48 | ||||
| -rw-r--r-- | challenge-086/lubos-kolouch/python/ch-1.py | 39 |
2 files changed, 87 insertions, 0 deletions
diff --git a/challenge-086/lubos-kolouch/perl/ch-1.pl b/challenge-086/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..0603c9b929 --- /dev/null +++ b/challenge-086/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,48 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-1.pl +# +# USAGE: ./ch-1.pl +# +# DESCRIPTION: Perl Weekly Challenge 086 +# Task 1 +# Pair Difference +# +# AUTHOR: Lubos Kolouch +# CREATED: 11/14/2020 01:27:06 PM +#=============================================================================== + +use strict; +use warnings; +use Data::Dumper; + +sub is_pair_difference { + my $data = shift; + + # to avoid sorting and/or scanning through all pairs in the array + # or double nested loop, + # I will convert the array to a hash, then check if the + # corresponding key exists + + my $arr = $$data{n}; + my %h; + for (@$arr) { + $h{$_} = 1; + } + + for my $x (keys %h) { + return 1 if $h{$x + $$data{a}}; + } + + return 0; +} + +use Test::More; + +is(is_pair_difference( { 'n' => [10, 8, 12, 15, 5], 'a' => 7 }), 1); +is(is_pair_difference( { 'n' => [1, 5, 2, 9, 7], 'a' => 6 }), 1); +is(is_pair_difference( { 'n' => [10, 30, 20, 50, 40], 'a' => 15 }), 0); + +done_testing; + diff --git a/challenge-086/lubos-kolouch/python/ch-1.py b/challenge-086/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..0f284bf4c6 --- /dev/null +++ b/challenge-086/lubos-kolouch/python/ch-1.py @@ -0,0 +1,39 @@ +#!env python +""" +#=============================================================================== +# +# FILE: ch-1.py +# +# USAGE: ./ch-1.py +# +# DESCRIPTION: Perl Weekly Challenge 086 +# Task 1 +# Pair Difference +# +# AUTHOR: Lubos Kolouch +# CREATED: 11/14/2020 01:27:06 PM +#=============================================================================== +""" + + +def is_pair_difference(n: list, a: int): + """ + # to avoid sorting and/or scanning through all pairs in the array + # or double nested loop, + # I will convert the array to a hash, then check if the + # corresponding key exists + """ + + hash_keys = {} + for number in n: + hash_keys[number] = 1 + + for x in hash_keys: + if hash_keys.get(x+a, 0): + return 1 + return 0 + + +assert is_pair_difference(n=[10, 8, 12, 15, 5], a=7) == 1 +assert is_pair_difference(n=[1, 5, 2, 9, 7], a=6) == 1 +assert is_pair_difference(n=[10, 30, 20, 50, 40], a=15) == 0 |
