diff options
| author | Simon Proctor <simon.proctor@zpg.co.uk> | 2020-02-24 09:44:09 +0000 |
|---|---|---|
| committer | Simon Proctor <simon.proctor@zpg.co.uk> | 2020-02-24 09:44:09 +0000 |
| commit | 3dbb9a3609c00c76109fda5f9c02188b5f31f767 (patch) | |
| tree | 1ac7b0bc714020aa3a27de976b6a67e74042f109 | |
| parent | 5734596d7d7d0620a856b03ddd375aca18df1ab2 (diff) | |
| download | perlweeklychallenge-club-3dbb9a3609c00c76109fda5f9c02188b5f31f767.tar.gz perlweeklychallenge-club-3dbb9a3609c00c76109fda5f9c02188b5f31f767.tar.bz2 perlweeklychallenge-club-3dbb9a3609c00c76109fda5f9c02188b5f31f767.zip | |
Challenge 49
| -rw-r--r-- | challenge-049/simon-proctor/raku/ch-1.p6 | 10 | ||||
| -rw-r--r-- | challenge-049/simon-proctor/raku/ch-2.p6 | 73 |
2 files changed, 83 insertions, 0 deletions
diff --git a/challenge-049/simon-proctor/raku/ch-1.p6 b/challenge-049/simon-proctor/raku/ch-1.p6 new file mode 100644 index 0000000000..82a55473f7 --- /dev/null +++ b/challenge-049/simon-proctor/raku/ch-1.p6 @@ -0,0 +1,10 @@ +#!/usr/bin/env perl6 + +use v6.d; + +#| find the first multiple of x made of only 1's and 0's +sub MAIN( + UInt $x #= Number to look for multiple of +) { + ( $x, * + $x...* ).hyper.first( { $_ ~~ m!^ <[10]>+ $! } ).say; +} diff --git a/challenge-049/simon-proctor/raku/ch-2.p6 b/challenge-049/simon-proctor/raku/ch-2.p6 new file mode 100644 index 0000000000..2653b0d430 --- /dev/null +++ b/challenge-049/simon-proctor/raku/ch-2.p6 @@ -0,0 +1,73 @@ +#!/usr/bin/env perl6 + +use v6.d; + +class Cache::LRU { + + has Int $!capacity where * > 0; + has %!cache = {}; + has @!key-list = []; + + submethod BUILD ( :$capacity ) { $!capacity = $capacity } + + method !freshen ( $key ) { + @!key-list.unshift($key).=unique; + if @!key-list.elems > $!capacity { + %!cache{@!key-list.pop}:delete; + } + } + + method current () { + return @!key-list; + } + + method get( $key ) { + if %!cache{$key}:exists { + self!freshen( $key ); + return %!cache{$key}; + } + return Any; + } + + method set( $key, $value ) { + %!cache{$key} = $value; + self!freshen( $key ); + return $value; + } +} + +#| Interactive LRU Cache example. Call with cache capacity +sub MAIN ( + Int $capacity where * > 0 #= Cache capacity (must be greater than zero) +) { + my $cache = Cache::LRU.new( :$capacity ); + + my $done = False; + + my multi sub action( "get", $key ) { + say $cache.get($key) // "Not found"; + }; + my multi sub action( "set", $key, $value ) { + $cache.set( $key, $value ); + say "Set $key to $value"; + }; + my multi sub action( "keys" ) { + say "Current Keys : {$cache.current().join(",")}"; + }; + my multi sub action( "quit" ) { + say "Bye"; + $done = True; + }; + my multi sub action( *@ ) { + say "I'm sorry Dave I don't know how to do that."; + say "Valid options are :\n\tget \{key\}\n\tset \{key\} \{value\}\n\tkeys\n\tquit"; + }; + + say "Welcome to the cache demo\nValid options are :\n\tget \{key\}\n\tset \{key\} \{value\}\n\tkeys\n\tquit"; + + while ! $done { + my @input = ( prompt "What would you like to do? " ).words; + action( |@input ); + } +} + |
