diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-11-10 00:28:54 +0000 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-11-10 00:28:54 +0000 |
| commit | bc98f8691d0f728de2d509fdc558890d28c658c7 (patch) | |
| tree | f7a20a67c439307650df4a5abc1c999a401339bd | |
| parent | 11dac4b07a99858054ea87306d68fd4cedcbcb0f (diff) | |
| parent | 25f153c6fff481b33a8dae950984e819c24846a9 (diff) | |
| download | perlweeklychallenge-club-bc98f8691d0f728de2d509fdc558890d28c658c7.tar.gz perlweeklychallenge-club-bc98f8691d0f728de2d509fdc558890d28c658c7.tar.bz2 perlweeklychallenge-club-bc98f8691d0f728de2d509fdc558890d28c658c7.zip | |
Merge remote-tracking branch 'upstream/master'
58 files changed, 2881 insertions, 1718 deletions
diff --git a/challenge-104/paulo-custodio/Makefile b/challenge-104/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-104/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-104/paulo-custodio/perl/ch-1.pl b/challenge-104/paulo-custodio/perl/ch-1.pl index 4736e2a0a3..43524f8121 100644 --- a/challenge-104/paulo-custodio/perl/ch-1.pl +++ b/challenge-104/paulo-custodio/perl/ch-1.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 104 # diff --git a/challenge-104/paulo-custodio/perl/ch-2.pl b/challenge-104/paulo-custodio/perl/ch-2.pl index 7106fb6ea0..1296d650ac 100644 --- a/challenge-104/paulo-custodio/perl/ch-2.pl +++ b/challenge-104/paulo-custodio/perl/ch-2.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 104 # diff --git a/challenge-104/paulo-custodio/python/ch-1.py b/challenge-104/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..ae10182903 --- /dev/null +++ b/challenge-104/paulo-custodio/python/ch-1.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +# Challenge 104 +# +# TASK #1 > FUSC Sequence +# Submitted by: Mohammad S Anwar +# Write a script to generate first 50 members of FUSC Sequence. Please refer to +# OEIS for more information. +# +# The sequence defined as below: +# +# fusc(0) = 0 +# fusc(1) = 1 +# for n > 1: +# when n is even: fusc(n) = fusc(n / 2), +# when n is odd: fusc(n) = fusc((n-1)/2) + fusc((n+1)/2) + +import sys + +def fusc(num): + data = [0, 1] + for n in range(2, num): + if n%2==0: + data.append(data[int(n/2)]) + else: + data.append(data[int((n-1)/2)] + data[int((n+1)/2)]) + + return data + +print(" ".join([str(x) for x in fusc(int(sys.argv[1]))])) diff --git a/challenge-104/paulo-custodio/python/ch-2.py b/challenge-104/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..6a71800c90 --- /dev/null +++ b/challenge-104/paulo-custodio/python/ch-2.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 + +# Challenge 104 +# +# TASK #2 > NIM Game +# Submitted by: Mohammad S Anwar +# Write a script to simulate the NIM Game. +# +# It is played between 2 players. For the purpose of this task, let assume you +# play against the machine. +# +# There are 3 simple rules to follow: +# +# a) You have 12 tokens +# b) Each player can pick 1, 2 or 3 tokens at a time +# c) The player who picks the last token wins the game + +# A plays to win, B plays randomly, A wins 91% of the time +# If both play to win, the second player to play always wins + +import sys +import random + +matches = 100000 +random_play = True +wins = {'A':0, 'B':0} + +def draw_to_win(t): + if t <= 3: + return t # win the game + elif (t % 4)==0: + return 1 # loose the game + elif (t % 5)==0: + return 1 # win the game + elif (t % 6)==0: + return 2 # win the game + elif (t % 7)==0: + return 3 # win the game + elif (t % 9)==0: + return 2 # win the game + elif (t % 11)==0: + return 2 # win the game + else: + return None # not reached + +def draw_random(t): + return random.randint(1, 3) + +def draw(t): + global random_play + + if random_play: + return draw_random(t) + else: + return draw_to_win(t) + +def play_match(): + global wins + t = 12 + while t > 0: + for player in ['A', 'B']: + if player=='A': + move = draw_to_win(t) + else: + move = draw(t) + t -= move + if t <= 0: + wins[player] += 1 + break + +def play_matches(): + for i in range(0, matches): + play_match() + +if sys.argv[1]=="random": + random_play = True +else: + random_play = False +play_matches() +a_wins = int(100*wins['A']/(wins['A']+wins['B'])) +print(f"A wins {a_wins}% of the matches.") diff --git a/challenge-104/paulo-custodio/test.pl b/challenge-104/paulo-custodio/test.pl deleted file mode 100644 index ba6c37260b..0000000000 --- a/challenge-104/paulo-custodio/test.pl +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env perl -use Modern::Perl; -use Test::More; -require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-105/paulo-custodio/Makefile b/challenge-105/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-105/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-105/paulo-custodio/perl/ch-1.pl b/challenge-105/paulo-custodio/perl/ch-1.pl index c6ef0fbb89..09238281d9 100644 --- a/challenge-105/paulo-custodio/perl/ch-1.pl +++ b/challenge-105/paulo-custodio/perl/ch-1.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 105 # diff --git a/challenge-105/paulo-custodio/perl/ch-2.pl b/challenge-105/paulo-custodio/perl/ch-2.pl index 683584cdbd..be883d4e89 100644 --- a/challenge-105/paulo-custodio/perl/ch-2.pl +++ b/challenge-105/paulo-custodio/perl/ch-2.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 105 # diff --git a/challenge-105/paulo-custodio/python/ch-1.py b/challenge-105/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..fe52a1081e --- /dev/null +++ b/challenge-105/paulo-custodio/python/ch-1.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +# Challenge 105 +# +# TASK #1 > Nth root +# Submitted by: Mohammad S Anwar +# You are given positive numbers $N and $k. +# +# Write a script to find out the $Nth root of $k. For more information, please +# take a look at the wiki page. +# +# Example +# Input: $N = 5, $k = 248832 +# Output: 12 +# +# Input: $N = 5, $k = 34 +# Output: 2.02 + +import sys + +def round(n): + ROUND_FACTOR = 10000 + result = int(n*ROUND_FACTOR+0.5)/ROUND_FACTOR + if int(result)==result: + result = int(result) + return result + +n, k = int(sys.argv[1]), int(sys.argv[2]) +print(round(k ** (1/n))) diff --git a/challenge-105/paulo-custodio/python/ch-2.py b/challenge-105/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..e074ad3185 --- /dev/null +++ b/challenge-105/paulo-custodio/python/ch-2.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 + +# Challenge 105 +# +# TASK #2 > The Name Game +# Submitted by: Mohammad S Anwar +# You are given a $name. +# +# Write a script to display the lyrics to the Shirley Ellis song The Name Game. +# Please checkout the wiki page for more information. +# +# Example +# Input: $name = "Katie" +# Output: +# +# Katie, Katie, bo-batie, +# Bonana-fanna fo-fatie +# Fee fi mo-matie +# Katie! + +import sys +import re + +name = sys.argv[1] +end = re.sub(r"^[bcdfghjklmnpqrstvwxyz]", "", name, flags=re.IGNORECASE).lower() + +if re.search(r"^b", name, flags=re.IGNORECASE): + b = "" +else: + b = "b" + +if re.search(r"^f", name, flags=re.IGNORECASE): + f = "" +else: + f = "f" + +if re.search(r"^m", name, flags=re.IGNORECASE): + m = "" +else: + m = "m" + +print(f"{name}, {name}, bo-{b}{end},") +print(f"Bonana-fanna fo-{f}{end}") +print(f"Fee fi mo-{m}{end}") +print(f"{name}!") diff --git a/challenge-105/paulo-custodio/test.pl b/challenge-105/paulo-custodio/test.pl deleted file mode 100644 index ba6c37260b..0000000000 --- a/challenge-105/paulo-custodio/test.pl +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env perl -use Modern::Perl; -use Test::More; -require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-106/paulo-custodio/perl/ch-2.pl b/challenge-106/paulo-custodio/perl/ch-2.pl index 390fa68cb1..aa2f756590 100644 --- a/challenge-106/paulo-custodio/perl/ch-2.pl +++ b/challenge-106/paulo-custodio/perl/ch-2.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # TASK #2 › Decimal String # Submitted by: Mohammad S Anwar diff --git a/challenge-106/paulo-custodio/python/ch-2.py b/challenge-106/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..6a019e26d6 --- /dev/null +++ b/challenge-106/paulo-custodio/python/ch-2.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +# TASK #2 > Decimal String +# Submitted by: Mohammad S Anwar +# You are given numerator and denominator i.e. $N and $D. +# +# Write a script to convert the fraction into decimal string. If the fractional +# part is recurring then put it in parenthesis. +# +# Example +# Input: $N = 1, $D = 3 +# Output: "0.(3)" +# +# Input: $N = 1, $D = 2 +# Output: "0.5" +# +# Input: $N = 5, $D = 66 +# Output: "0.0(75)" + +import sys +import re +from decimal import * + +def frac2str(n, d): + getcontext().prec = 1000 + getcontext().rounding = ROUND_DOWN + + n = Decimal(n) + d = Decimal(d) + q = str(n/d) + + for rept in range(1,101): + match = re.search(r"((\d{"+str(rept)+r"})\2+\d*)", q) + if match: + q = re.sub(match.group(1), "("+match.group(2)+")", q) + return q + return q + +print(frac2str(int(sys.argv[1]), int(sys.argv[2]))) diff --git a/challenge-137/dave-jacoby/blog.txt b/challenge-137/dave-jacoby/blog.txt new file mode 100644 index 0000000000..ab97ea3038 --- /dev/null +++ b/challenge-137/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2021/11/01/2020-was-a-long-year-the-weekly-challenge-137.html diff --git a/challenge-137/dave-jacoby/perl/ch-1.pl b/challenge-137/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..2a93667f0d --- /dev/null +++ b/challenge-137/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,56 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say state postderef signatures }; +no warnings qw{ experimental }; + +use DateTime; + +# This table shows how this will work for any given year +# +# dow => day of week (numerical) +# leap => is a leap year (bool) +# woy => week of year (1, 52, 53) +# +# dow leap woy day +# --------------------------------------- +# 1 0 1 Monday +# 1 1 1 Monday +# 2 0 1 Tuesday +# 2 1 1 Tuesday +# 3 0 1 Wednesday +# 3 1 1 Wednesday +# 4 0 53 Thursday +# 4 1 53 Thursday +# 5 0 52 Friday +# 5 1 53 Friday +# 6 0 52 Saturday +# 6 1 52 Saturday +# 7 0 52 Sunday +# 7 1 52 Sunday + +my @years; +for my $year ( 1900 .. 2100 ) { + my $dt = DateTime->new( + month => 12, + day => 31, + year => $year, + ); + my ( undef, $week_of_year ) = $dt->week; + my $dow = $dt->day_of_week; + my $nam = $dt->day_name; + my $is_leap = $dt->is_leap_year; + push @years, $year if $week_of_year == 53; +} + +my @x; +while (@years) { + push @x, shift @years; + if ( scalar @x == 5 ) { + say join ', ', @x, ''; + @x = (); + } +} + +say join ', ', @x; diff --git a/challenge-137/dave-jacoby/perl/ch-2.pl b/challenge-137/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..a6ad0430c2 --- /dev/null +++ b/challenge-137/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,60 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say postderef signatures state }; +no warnings qw{ experimental }; + +use Math::BigInt; +use Getopt::Long; +use List::Util qw{uniq}; + +my @examples; +my @numbers; +my $lychrel = 0; +GetOptions( + 'number=i' => \@numbers, + 'lychrel' => \$lychrel, +); + +if ( scalar @numbers ) { + @examples = uniq sort { $a <=> $b } @numbers; +} +else { @examples = ( 10 .. 1000 ); } + +for my $e (@examples) { + my $l = is_lychrel($e); + next if !$l && $lychrel; + say <<"END"; + Input: \$n = $e + Output: $l +END +} + +exit; + +sub is_lychrel($e) { + my $n = $e; + my $c = 0; + while ( !is_palindrome($n) ) { + $n = lychrel($n); + $c++; + return 1 if $c >= 500; + } + return 0; +} + +sub lychrel( $n ) { + my $bign = Math::BigInt->new($n); + my $u = join '', reverse split //, $n; + my $bigu = Math::BigInt->new($u); + my $new = $bign->badd($bigu); + return $new; +} + +sub is_palindrome ($n) { + my $u = join '', reverse split //, $n; + $u =~ s/^0+//mix; + return $u eq $n ? 1 : 0; +} + diff --git a/challenge-138/dave-jacoby/blog.txt b/challenge-138/dave-jacoby/blog.txt new file mode 100644 index 0000000000..13e0a70a85 --- /dev/null +++ b/challenge-138/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2021/11/08/i-thank-you-for-the-days-the-weekly-challenge-138.html diff --git a/challenge-138/dave-jacoby/perl/ch-1.pl b/challenge-138/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..16bd98bbd4 --- /dev/null +++ b/challenge-138/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,63 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say state postderef signatures }; +no warnings qw{ experimental }; + +use DateTime; + +for my $y ( 2000 .. 2040 ) { + say join "\t", $y, workdays2($y), workdays1($y); +} + +# The brute force solution, where I go through each year, +# checking if each day is a work day, and keeping count. + +sub workdays1 ( $year ) { + my $day = DateTime->new( + day => 1, + month => 1, + year => $year, + time_zone => 'floating' + ); + my $c = 0; + while ( $year == $day->year ) { + $c++ if $day->day_of_week <= 5; + $day->add( days => 1 ); + } + return $c; +} + +# But there are ONLY 14 years. Leap year or not = 2. Days of week = 7. +# 2 * 7 == 14. So it's perfectly reasonable to know that, if the year +# is a leapyar and starts on a Saturday, or starts on a Sunday, leap year +# or no, that's going to be a 260-workday year, and if it's a leap year +# and starts on Monday, Tuesday, Wednesday or Thurday, there will be +# 262, and otherwise, there will be 261. + +sub workdays2( $year ) { + my $table = {}; + $table->{0}{1} = 261; + $table->{0}{2} = 261; + $table->{0}{3} = 261; + $table->{0}{4} = 261; + $table->{0}{5} = 261; + $table->{0}{6} = 260; + $table->{0}{7} = 260; + $table->{1}{1} = 262; + $table->{1}{2} = 262; + $table->{1}{3} = 262; + $table->{1}{4} = 262; + $table->{1}{5} = 261; + $table->{1}{6} = 260; + $table->{1}{7} = 261; + my $day = DateTime->new( + day => 1, + month => 1, + year => $year, + time_zone => 'floating' + ); + return $table->{ $day->is_leap_year }{ $day->dow }; +} + diff --git a/challenge-138/dave-jacoby/perl/ch-2.pl b/challenge-138/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..7aa380dbe5 --- /dev/null +++ b/challenge-138/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,60 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say postderef signatures state }; +no warnings qw{ experimental }; + +use List::Util qw{ sum0 uniq }; + +my @squares = map { $_**2 } 1 .. 100; + +for my $n (@squares) { + my $split = split_number($n); + say join "\t", '', $split, $n,; +} + +sub split_number($n) { + my $sqrt = sqrt($n); + my @split = split //, $n; + if ( scalar @split == 1 ) { + my $s = shift @split; + return $s == $sqrt ? 1 : 0; + } + else { + my @numbers = break_up( 1, @split ); + for my $num (@numbers) { + my $sum = sum0 split /\D/, $num; + return 1 if $sqrt == $sum; + } + } + return 0; +} + +sub break_up ( $position, @array ) { + my @output; + my $len = scalar @array; + my @dup = @array; + if ( $len <= $position ) { + return join '+', @array; + } + + my @copy; + my $i = 0; + while (@dup) { + if ( $i eq $position ) { + my $x = shift @dup; + $copy[-1] .= $x; + } + else { + push @copy, shift @dup; + } + $i++; + } + + push @output, break_up( $position, @copy ); + push @output, break_up( $position + 1, @array ); + + @output = uniq sort grep { defined } @output; + return @output; +} diff --git a/challenge-138/eric-cheung/excel-vba/Challenge_138.xls |
