diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-07-25 01:43:36 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-25 01:43:36 +0100 |
| commit | 7500c73907a1d84df0ad12f2952df5d7c7bd5529 (patch) | |
| tree | 27f22a096301d3511af37562eb76e6928de9195a | |
| parent | adea0b7bee4436512173307633b2e42019d83e4e (diff) | |
| parent | 29f18fad687e91423be3b61541a2255a48e5fc0a (diff) | |
| download | perlweeklychallenge-club-7500c73907a1d84df0ad12f2952df5d7c7bd5529.tar.gz perlweeklychallenge-club-7500c73907a1d84df0ad12f2952df5d7c7bd5529.tar.bz2 perlweeklychallenge-club-7500c73907a1d84df0ad12f2952df5d7c7bd5529.zip | |
Merge pull request #4586 from LubosKolouch/master
Challenge 122 LK Perl Python
| -rw-r--r-- | challenge-122/lubos-kolouch/perl/ch-1.pl | 47 | ||||
| -rw-r--r-- | challenge-122/lubos-kolouch/perl/ch-2.pl | 63 | ||||
| -rw-r--r-- | challenge-122/lubos-kolouch/python/ch-1.py | 24 | ||||
| -rw-r--r-- | challenge-122/lubos-kolouch/python/ch-2.py | 48 |
4 files changed, 182 insertions, 0 deletions
diff --git a/challenge-122/lubos-kolouch/perl/ch-1.pl b/challenge-122/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..05b9a9668e --- /dev/null +++ b/challenge-122/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-1.pl +# +# USAGE: ./ch-1.pl +# +# DESCRIPTION: The Weekly Challenge 122 +# Task 1 - Average of Stream +# +# AUTHOR: Lubos Kolouch +# CREATED: 07/24/2021 10:27:06 AM +#=============================================================================== + +use strict; +use warnings; + +sub print_moving_average { + my @what = @_; + + return unless @what; + + my $total = $what[0]; + my $pos = 1; + my $arr_str = $what[0]; + + # print the first MA to avoid unnecessary IF in the loop for each number + print "Average of the first number is $total \n"; + + shift(@what); + # now if Perl only had enumerate... + for my $i (@what) { + $pos++; + $total += $i; + $arr_str .= "+".$i; + print "Average of the first $pos numbers (".$arr_str.")/$pos = ".$total/$pos."\n"; + } + + return $total/$pos; + +} + +use Test::More; + + +is(print_moving_average(10, 20, 30, 40, 50, 60, 70, 80, 90), 50); +done_testing; diff --git a/challenge-122/lubos-kolouch/perl/ch-2.pl b/challenge-122/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..2c26231861 --- /dev/null +++ b/challenge-122/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,63 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use List::Util qw/sum/; +use feature qw/say/; +# Perl weekly challenge 122 Task 2 - Basketball Points + +my $iter_count = 0; + +sub iterate { + + my ($arr, $target_sum) = @_; + my $arr_sum = sum(@$arr) || 0; + + if ($arr_sum == $target_sum) { + print "*********** SCORE: "; + say join " ", @$arr; + $iter_count++; + } + + return if $arr_sum >= $target_sum; + + # expand the array and try again + my @new_arr = @$arr; + push @new_arr, 1; + print join " ", @$arr; + print " > "; + say join " ", @new_arr; + iterate((\@new_arr, $target_sum)); + + # cannot expand, try increasing the last poing + my @new_arr = @$arr; + if ( (@new_arr) and ($new_arr[-1] < $target_sum) and ($new_arr[-1] < 3) ) { + $new_arr[-1]++; + print join " ", @$arr; + print " ^ "; + say join " ", @new_arr; + iterate((\@new_arr, $target_sum)); + } + +} + + +sub print_points { + my $target_sum = shift; + iterate(([], $target_sum)); + + + + +} + +use Test::More; + +$iter_count = 0; +print_points(4); +is($iter_count, 7); + +$iter_count = 0; +print_points(5); +is($iter_count, 13); + +done_testing; diff --git a/challenge-122/lubos-kolouch/python/ch-1.py b/challenge-122/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..1c0cd20444 --- /dev/null +++ b/challenge-122/lubos-kolouch/python/ch-1.py @@ -0,0 +1,24 @@ +# DESCRIPTION: The Weekly Challenge 122 +# Task 1 - Average of Stream +# +# AUTHOR: Lubos Kolouch +# CREATED: 07/24/2021 10:27:06 AM +# =============================================================================== + +def print_moving_average(what): + + total = what[0] + arr_str = str(what[0]) + + # print the first MA to avoid unnecessary IF in the loop for each number + print(f"Average of the first number is {total}") + + for pos, i in enumerate(what[1:]): + total += i + arr_str += f"+{str(i)}" + print(f"Average of the first {pos+1} numbers ({arr_str})/{pos+2} = {total/(pos+2)}") + + return total/len(what) + + +assert print_moving_average([10, 20, 30, 40, 50, 60, 70, 80, 90]) == 50 diff --git a/challenge-122/lubos-kolouch/python/ch-2.py b/challenge-122/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..8ebe8d589a --- /dev/null +++ b/challenge-122/lubos-kolouch/python/ch-2.py @@ -0,0 +1,48 @@ +# Perl weekly challenge 122 Task 2 - Basketball Points + +iter_count = 0 + + +def iterate(arr, target_sum): + """ Iteratively print the possibilities """ + global iter_count + + arr_sum = sum(arr) + + if arr_sum == target_sum: + print("*********** SCORE: ", end="") + print(" ".join(list(map(str, arr)))) + iter_count += 1 + + if arr_sum >= target_sum: + return + + # expand the array and try again + new_arr = arr.copy() + new_arr.append(1) + print(" ".join(list(map(str, arr))), end="") + print(" > ", end="") + print(" ".join(list(map(str, new_arr)))) + iterate(new_arr, target_sum) + + # cannot expand, try increasing the last poing + new_arr = arr.copy() + if ((new_arr) and (new_arr[-1] < target_sum) and (new_arr[-1] < 3)): + new_arr[-1] += 1 + print(" ".join(list(map(str, arr))), end="") + print(" ^ ", end="") + print(" ".join(list(map(str, new_arr)))) + iterate(new_arr, target_sum) + + +def print_points(target_sum: int): + iterate([], target_sum) + + +iter_count = 0 +print_points(4) +assert iter_count == 7 + +iter_count = 0 +print_points(5) +assert iter_count == 13 |
