From f35ebb13433887c02b92a5fd1b3169d46f8dc70c Mon Sep 17 00:00:00 2001 From: southpawgeek Date: Mon, 24 Feb 2020 08:15:07 -0500 Subject: solution 1, template for solution 2 --- challenge-049/southpawgeek/ch-1.pl | 13 +++++++++++++ challenge-049/southpawgeek/ch-2.pl | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 challenge-049/southpawgeek/ch-1.pl create mode 100644 challenge-049/southpawgeek/ch-2.pl diff --git a/challenge-049/southpawgeek/ch-1.pl b/challenge-049/southpawgeek/ch-1.pl new file mode 100644 index 0000000000..8f7c7dce76 --- /dev/null +++ b/challenge-049/southpawgeek/ch-1.pl @@ -0,0 +1,13 @@ +#!/usr/bin/perl +use strict; +use warnings; +use feature qw/say/; + +my ($int) = @ARGV; +die "$int isn't an int! \n" if $int =~ /\D/; +say "checking $int"; + +for (my $i = 1;;$i++){ + my $bin = sprintf("%b", $i); + say "$bin is the smallest 0/1 multiple of $int" and last unless $bin % $int; +} diff --git a/challenge-049/southpawgeek/ch-2.pl b/challenge-049/southpawgeek/ch-2.pl new file mode 100644 index 0000000000..d2488c6d53 --- /dev/null +++ b/challenge-049/southpawgeek/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl +use strict; +use warnings; +use feature qw/say/; + +my ($size) = @ARGV; +die "$size isn't an int! \n" if $size =~ /\D/; +$size = 3 unless $size; +say "set cache size to $size"; + +set(1, 3); +set(2, 5); +set(3, 7); +get(2); +get(1); +get(4); +set(4, 9); +get(3); + +my (%cache, @keys); + +sub set { + my ($key, $value) = @_; + push @keys, $key; + $cache{$key} = $value; + say "key order.. @keys"; +} + +sub get { + my $key = shift; + say $cache{$key}; + say "key order.. @keys"; +} -- cgit From 0f6260120be35fc120f9d711790e7094e99fb72e Mon Sep 17 00:00:00 2001 From: southpawgeek Date: Mon, 24 Feb 2020 23:07:12 -0500 Subject: fixed challenge to account for 1s *and* 0s --- challenge-049/southpawgeek/ch-1.pl | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/challenge-049/southpawgeek/ch-1.pl b/challenge-049/southpawgeek/ch-1.pl index 8f7c7dce76..4edfd257c9 100644 --- a/challenge-049/southpawgeek/ch-1.pl +++ b/challenge-049/southpawgeek/ch-1.pl @@ -5,9 +5,15 @@ use feature qw/say/; my ($int) = @ARGV; die "$int isn't an int! \n" if $int =~ /\D/; -say "checking $int"; -for (my $i = 1;;$i++){ +for (my $i = 2;;$i++){ my $bin = sprintf("%b", $i); - say "$bin is the smallest 0/1 multiple of $int" and last unless $bin % $int; + next if $bin == $int; + + # we know it starts with 1, but make sure there's at least one 0 + next unless $bin =~ /0+/; + + say "$bin is the smallest multiple of $int with 1s *and* 0s." + and last + unless $bin % $int; } -- cgit From a42746b136af9ddf5f0460f32a7fb49ba76783d4 Mon Sep 17 00:00:00 2001 From: southpawgeek Date: Sat, 29 Feb 2020 11:18:56 -0500 Subject: skipping challenge #2 --- challenge-049/southpawgeek/ch-2.pl | 33 --------------------------------- 1 file changed, 33 deletions(-) delete mode 100644 challenge-049/southpawgeek/ch-2.pl diff --git a/challenge-049/southpawgeek/ch-2.pl b/challenge-049/southpawgeek/ch-2.pl deleted file mode 100644 index d2488c6d53..0000000000 --- a/challenge-049/southpawgeek/ch-2.pl +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/perl -use strict; -use warnings; -use feature qw/say/; - -my ($size) = @ARGV; -die "$size isn't an int! \n" if $size =~ /\D/; -$size = 3 unless $size; -say "set cache size to $size"; - -set(1, 3); -set(2, 5); -set(3, 7); -get(2); -get(1); -get(4); -set(4, 9); -get(3); - -my (%cache, @keys); - -sub set { - my ($key, $value) = @_; - push @keys, $key; - $cache{$key} = $value; - say "key order.. @keys"; -} - -sub get { - my $key = shift; - say $cache{$key}; - say "key order.. @keys"; -} -- cgit