diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-02-29 22:52:46 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-29 22:52:46 +0000 |
| commit | 9bcb76ac89d3895dcc651f2cde2e0559698b360f (patch) | |
| tree | 1273ce8a4f7967e106c8aeb911785730ec2eb6a8 | |
| parent | c79d9bee0c9d0df3117ee9b1ad20daeeaf36eff0 (diff) | |
| parent | 9436a5c9a6b63619bf7b232d5a7bfd161aaafcf0 (diff) | |
| download | perlweeklychallenge-club-9bcb76ac89d3895dcc651f2cde2e0559698b360f.tar.gz perlweeklychallenge-club-9bcb76ac89d3895dcc651f2cde2e0559698b360f.tar.bz2 perlweeklychallenge-club-9bcb76ac89d3895dcc651f2cde2e0559698b360f.zip | |
Merge pull request #1328 from kevincolyer/branch-049
PWC 49 complete
| -rw-r--r-- | challenge-049/kevin-colyer/raku/ch-1.p6 | 22 | ||||
| -rw-r--r-- | challenge-049/kevin-colyer/raku/ch-2.p6 | 105 |
2 files changed, 127 insertions, 0 deletions
diff --git a/challenge-049/kevin-colyer/raku/ch-1.p6 b/challenge-049/kevin-colyer/raku/ch-1.p6 new file mode 100644 index 0000000000..8acb119abb --- /dev/null +++ b/challenge-049/kevin-colyer/raku/ch-1.p6 @@ -0,0 +1,22 @@ +#!perl6 +# Task 1 Challenge 049 Solution by kevincolyer +# 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. + + +sub MAIN(Int $number where * > 1) { + for 1..Inf -> $zero-ones { + # calculate all numbers with 0 or 1's in them (i.e. binary!) + my $z10=$zero-ones.base(2); + # use the binary string as a decimal value in trial division + if $z10 %% $number { + say "Smallest multiple of $number that consists of 0's and 1's is {$z10 / $number} gives {$z10} (found in $zero-ones iterations)"; + last; + } + } +} diff --git a/challenge-049/kevin-colyer/raku/ch-2.p6 b/challenge-049/kevin-colyer/raku/ch-2.p6 new file mode 100644 index 0000000000..a113eb3a8c --- /dev/null +++ b/challenge-049/kevin-colyer/raku/ch-2.p6 @@ -0,0 +1,105 @@ +#!perl6 + +use Test; + +# Task 2 Challenge 049 Solution by kevincolyer +# 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 + +class MRUCache { + has $.capacity = 3; + has @!MRUcache; + has %!store; + + method set(Int $key,$value) { + # TODO if key exists do we make Most Recently useed on update? + if %!store{$key}:exists { + # swap value - key already exisits + %!store{$key}=$value; + return; + } + # if cache full swap out oldest + if @!MRUcache.elems == $.capacity { + %!store{@!MRUcache[0]}:delete; + @!MRUcache.shift; + } + + # add new key to store + @!MRUcache.push($key); + %!store{$key}=$value; + return; + } + + method get(Int $key) { + # check key is in store + return -1 unless %!store{$key}:exists; + + # update MRU list + # search from morst recent to least recent + for (^$.capacity).reverse -> $i { + next unless @!MRUcache[$i]==$key; + # update cache position by cutting out old key + @!MRUcache.splice($i,1); + # and pushing in new key in more recent position (end) + @!MRUcache.push($key); + } + + # return the value of the key + return %!store{$key}; + } + + # just for debugging + method viewcache() { + return @!MRUcache; + } +} + + +multi MAIN(Int $capacity where * > 0) { + my $c= MRUCache.new(capacity=>$capacity); + say "created MRUcache of capacity {$c.capacity}"; +} + +multi MAIN ('test') { + my $c= MRUCache.new(capacity=>3); + + $c.set(1,3); + $c.set(2, 5); + is $c.viewcache,(1,2),"cache grows correctly"; + $c.set(3, 7); + + is $c.get(2),5,"get item 2"; + is $c.viewcache,(1,3,2),"peep at cache"; + is $c.get(1),3,"get first item"; + is $c.viewcache,(3,2,1),"peep at cache 2"; + is $c.get(4),-1,"try to get cache item > capacity"; + $c.set(4, 9); + is $c.viewcache,(2,1,4),"peep at cache"; + is $c.get(3),-1,"get item 3 - but no longer in in cache"; + done-testing; +} |
