diff options
| author | Jaldhar H. Vyas <jaldhar@braincells.com> | 2020-03-15 02:05:05 -0400 |
|---|---|---|
| committer | Jaldhar H. Vyas <jaldhar@braincells.com> | 2020-03-15 02:05:05 -0400 |
| commit | e56292d1d84a7dfbbb40ec719c48eb2f96a5d76e (patch) | |
| tree | 5cced265d0cd3d0e35746de39e629a18b155b5b0 /challenge-049 | |
| parent | 78587f1717a301a85558e71470eb81c719dd7654 (diff) | |
| download | perlweeklychallenge-club-e56292d1d84a7dfbbb40ec719c48eb2f96a5d76e.tar.gz perlweeklychallenge-club-e56292d1d84a7dfbbb40ec719c48eb2f96a5d76e.tar.bz2 perlweeklychallenge-club-e56292d1d84a7dfbbb40ec719c48eb2f96a5d76e.zip | |
Challenge 49 by Jaldhar H. Vyas - task 2
Diffstat (limited to 'challenge-049')
| -rwxr-xr-x | challenge-049/jaldhar-h-vyas/perl/ch-2.pl | 80 | ||||
| -rwxr-xr-x | challenge-049/jaldhar-h-vyas/raku/ch-2.p6 | 53 |
2 files changed, 133 insertions, 0 deletions
diff --git a/challenge-049/jaldhar-h-vyas/perl/ch-2.pl b/challenge-049/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..ef0e57de9c --- /dev/null +++ b/challenge-049/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,80 @@ +#!/usr/bin/perl + +package Data::LRUCache; +use Moo; +use namespace::clean; + +has _cache => ( + is => 'rw', + default => sub { [] } +); + +has _index => ( + is => 'rw', + default => sub { {} } +); + +has _capacity => ( + is => 'ro', +); + +sub BUILDARGS { + my ($orig, $class, @args) = @_; + + return { _capacity => $args[0] }; +}; + +sub get { + my ($self, $key) = @_; + + if (!grep { $_ == $key } @{ $self->{_cache} }) { + return -1; + } + @{ $self->{_cache} } = grep { $_ != $key } @{ $self->{_cache} }; + unshift @{ $self->{_cache}}, $key; + return $self->{_index}->{$key}; +} + +sub set { + my ($self, $key, $value) = @_; + + if (scalar @{ $self->{_cache} } == $self->{_capacity}) { + my $last = pop @{ $self->{_cache} }; + delete $self->{_index}->{$last}; + } + unshift @{ $self->{_cache} }, $key; + $self->{_index}->{$key} = $value; +} + +sub print() { + my ($self) = @_; + + for my $e (@{ $self->{_cache} }) { + print "$e => ", $self->{_index}->{$e}, "\n"; + } +} + +1; + +package main; +use warnings; +use strict; +use 5.010; + +my $capacity = shift // 3; + +my $cache = Data::LRUCache->new(capacity => $capacity); + +$cache->set(1, 3); +$cache->set(2, 5); +$cache->set(3, 7); +$cache->print; +say $cache->get(2); +$cache->print; +say $cache->get(1); +$cache->print; +say $cache->get(4); +$cache->print; +$cache->set(4, 9); +$cache->print; +say $cache->get(3); diff --git a/challenge-049/jaldhar-h-vyas/raku/ch-2.p6 b/challenge-049/jaldhar-h-vyas/raku/ch-2.p6 new file mode 100755 index 0000000000..d722d913e9 --- /dev/null +++ b/challenge-049/jaldhar-h-vyas/raku/ch-2.p6 @@ -0,0 +1,53 @@ +#!/usr/bin/perl6 + +class Data::LRUCache { + + has Int @!cache = (); + has %!index = (); + has Int $!capacity is required; + + submethod BUILD( :$capacity ) { + $!capacity = $capacity; + } + + method get(Int $key where { $_ > 0 }) { + if ($key != any @!cache) { + return -1; + } + @!cache = @!cache.grep({ $_ != $key }); + @!cache.unshift($key); + return %!index{$key} + } + + method set(Int $key, Any $value) { + if @!cache.elems ~~ $!capacity { + my $last = @!cache.pop; + %!index{$last} :delete; + } + @!cache.unshift($key); + %!index{$key} = $value; + } + + method print() { + for @!cache -> $e { + say "$e => ", %!index{$e}; + } + } +} + +my $capacity = @*ARGS[0] // 3; + +my $cache = Data::LRUCache.new(capacity => $capacity); +$cache.set(1, 3); +$cache.set(2, 5); +$cache.set(3, 7); +$cache.print; +say $cache.get(2); +$cache.print; +say $cache.get(1); +$cache.print; +say $cache.get(4); +$cache.print; +$cache.set(4, 9); +$cache.print; +say $cache.get(3); |
