diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-07 16:26:29 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-07 16:26:29 +0100 |
| commit | 7af11923bf07ca9f2d20243ee3649651c06378e6 (patch) | |
| tree | f9639cd8992c00e2bad3f1d91bfab622eda91d02 /challenge-049 | |
| parent | 20ffdf0f472b18b14672347152a7611942687f76 (diff) | |
| download | perlweeklychallenge-club-7af11923bf07ca9f2d20243ee3649651c06378e6.tar.gz perlweeklychallenge-club-7af11923bf07ca9f2d20243ee3649651c06378e6.tar.bz2 perlweeklychallenge-club-7af11923bf07ca9f2d20243ee3649651c06378e6.zip | |
Add Perl solution to challenge 049
Diffstat (limited to 'challenge-049')
| -rw-r--r-- | challenge-049/paulo-custodio/Makefile | 3 | ||||
| -rw-r--r-- | challenge-049/paulo-custodio/README | 1 | ||||
| -rw-r--r-- | challenge-049/paulo-custodio/perl/ch-1.pl | 25 | ||||
| -rw-r--r-- | challenge-049/paulo-custodio/perl/ch-2.pl | 120 | ||||
| -rw-r--r-- | challenge-049/paulo-custodio/t/test-1.yaml | 10 |
5 files changed, 159 insertions, 0 deletions
diff --git a/challenge-049/paulo-custodio/Makefile b/challenge-049/paulo-custodio/Makefile new file mode 100644 index 0000000000..9494a9025e --- /dev/null +++ b/challenge-049/paulo-custodio/Makefile @@ -0,0 +1,3 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl + perl ./perl/ch-2.pl
\ No newline at end of file diff --git a/challenge-049/paulo-custodio/README b/challenge-049/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-049/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-049/paulo-custodio/perl/ch-1.pl b/challenge-049/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..d26c6c6bee --- /dev/null +++ b/challenge-049/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl + +# Challenge 049 +# +# TASK #1 +# Smallest Multiple +# Write a script to accept a positive number as command line argument and print +# the smallest multiple of the given number consists of digits 0 and 1. +# +# For example: +# +# For given number 55, the smallest multiple is 110 consisting of digits 0 and 1. + +use Modern::Perl; + +say smallest_multiple(shift||1); + +sub smallest_multiple { + my($n) = @_; + my $v = $n; + while ($v !~ /^[01]+$/) { + $v += $n; + } + return $v; +} diff --git a/challenge-049/paulo-custodio/perl/ch-2.pl b/challenge-049/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..7af6cb2c74 --- /dev/null +++ b/challenge-049/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,120 @@ +#!/usr/bin/env perl + +# Challenge 049 +# +# TASK #2 +# LRU Cache +# Write a script to demonstrate LRU Cache feature. It should support operations +# get and set. Accept the capacity of the LRU Cache as command line argument. +# +# Definition of LRU: An access to an item is defined as a get or a set operation +# of the item. “Least recently used” item is the one with the oldest access time. +# +# For example: +# +# capacity = 3 +# set(1, 3) +# set(2, 5) +# set(3, 7) +# +# Cache at this point: +# [Least recently used] 1,2,3 [most recently used] +# +# get(2) # returns 5 +# +# Cache looks like now: +# [Least recently used] 1,3,2 [most recently used] +# +# get(1) # returns 3 +# +# Cache looks like now: +# [Least recently used] 3,2,1 [most recently used] +# +# get(4) # returns -1 +# +# Cache unchanged: +# [Least recently used] 3,2,1 [most recently used] +# +# set(4, 9) +# +# Cache is full, so pushes out key = 3: +# [Least recently used] 2,1,4 [most recently used] +# +# get(3) # returns -1 + +use Modern::Perl; +use Test::More; + +{ + package Cache; + use Object::Tiny::RW qw( capacity cache ); + + sub new { + my($class, %args) = @_; + return bless {cache=>[], %args}, $class; + } + + sub as_string { + my($self) = @_; + my $out = ""; + for (@{$self->cache}) { + $out .= "(".$_->[0]."=>".$_->[1].")"; + } + return $out; + } + + sub get { + my($self, $k) = @_; + for my $i (0 .. @{$self->cache}-1) { + if ($self->cache->[$i][0] == $k) { + my $v = $self->cache->[$i][1]; + splice(@{$self->cache}, $i, 1); + push @{$self->cache}, [$k=>$v]; + shift @{$self->cache} while @{$self->cache} > $self->capacity; + return $v; + } + } + return -1; + } + + sub set { + my($self, $k, $v) = @_; + my $found = $self->get($k); + if ($found == -1) { + push @{$self->cache}, [$k=>$v]; + shift @{$self->cache} while @{$self->cache} > $self->capacity; + } + else { + $self->cache->[-1][1] = $v; + } + } +} + +my $cache = Cache->new(capacity=>3); +is $cache->as_string, ""; + +$cache->set(1, 3); +is $cache->as_string, "(1=>3)"; + +$cache->set(2, 5); +is $cache->as_string, "(1=>3)(2=>5)"; + +$cache->set(3, 7); +is $cache->as_string, "(1=>3)(2=>5)(3=>7)"; + +is $cache->get(2), 5; +is $cache->as_string, "(1=>3)(3=>7)(2=>5)"; + +is $cache->get(1), 3; +is $cache->as_string, "(3=>7)(2=>5)(1=>3)"; + +is $cache->get(4), -1; +is $cache->as_string, "(3=>7)(2=>5)(1=>3)"; + +$cache->set(4, 9); +is $cache->as_string, "(2=>5)(1=>3)(4=>9)"; + +is $cache->get(3), -1; +is $cache->as_string, "(2=>5)(1=>3)(4=>9)"; + +done_testing; diff --git a/challenge-049/paulo-custodio/t/test-1.yaml b/challenge-049/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..6e8693d81e --- /dev/null +++ b/challenge-049/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 5 + input: + output: 10 +- setup: + cleanup: + args: 55 + input: + output: 110 |
