diff options
| -rw-r--r-- | challenge-049/andrezgz/perl/ch-1.pl | 29 | ||||
| -rw-r--r-- | challenge-049/andrezgz/perl/ch-2.pl | 160 |
2 files changed, 189 insertions, 0 deletions
diff --git a/challenge-049/andrezgz/perl/ch-1.pl b/challenge-049/andrezgz/perl/ch-1.pl new file mode 100644 index 0000000000..dc39bf4962 --- /dev/null +++ b/challenge-049/andrezgz/perl/ch-1.pl @@ -0,0 +1,29 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-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 strict; +use warnings; + +my $n = shift or die "USAGE: $0 <positive number>"; +die "The argument must be a positive number" unless ( $n =~ /\d+/ && $n > 0); + +my $m = $n; +$m += $n while ($m !~ /^[01]+$/); +print $m; + +__END__ + +./ch-1.pl 25 +100 + +./ch-1.pl 9 +111111111 diff --git a/challenge-049/andrezgz/perl/ch-2.pl b/challenge-049/andrezgz/perl/ch-2.pl new file mode 100644 index 0000000000..11cf27eefa --- /dev/null +++ b/challenge-049/andrezgz/perl/ch-2.pl @@ -0,0 +1,160 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-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 +# +# I found this amazing post talking about LRU Cache. +# https://www.interviewcake.com/concept/java/lru-cache + +use strict; +use warnings; + +my $c = shift or die "USAGE: $0 <capacity>"; +die "Capacity of the LRU Cache must be a positive number" unless ( $c =~ /\d+/ && $c > 0); + +print "Capacity = $c\n\n"; + +my @lru = (); +my %cache = (); + +set(1, 3); +set(2, 5); +set(3, 7); +get(2); +get(1); +get(4); +set(4, 9); +get(3); + + +sub set { + my ($k, $v) = @_; + + print "set($k, $v)\n\n"; + + push @lru, $k; + + if (@lru > $c) { + my $out = shift @lru; + delete $cache{$out}; + print "Cache is full: pushed out key = $out\n"; + } + else { + print "Cache updated:\n"; + } + + $cache{$k} = $v; + + show(@lru); +} + +sub get { + my ($k) = @_; + + print "get($k)\t-> returns "; + + if (exists $cache{$k}) { + print "$cache{$k}\n\nCache updated:\n"; + @lru = grep { $_ != $k } @lru; + push @lru,$k; + } + else { + print "-1\n\nCache unchanged:\n"; + } + + show(@lru); + +} + +sub show { + my @lru = @_; + print '[Least recently used] '; + print join ',',@lru; + print " [most recently used]\n\n"; +} + +__END__ + +./ch-2.pl 2 +Capacity = 2 + +set(1, 3) + +Cache updated: +[Least recently used] 1 [most recently used] + +set(2, 5) + +Cache updated: +[Least recently used] 1,2 [most recently used] + +set(3, 7) + +Cache is full: pushed out key = 1 +[Least recently used] 2,3 [most recently used] + +get(2) -> returns 5 + +Cache updated: +[Least recently used] 3,2 [most recently used] + +get(1) -> returns -1 + +Cache unchanged: +[Least recently used] 3,2 [most recently used] + +get(4) -> returns -1 + +Cache unchanged: +[Least recently used] 3,2 [most recently used] + +set(4, 9) + +Cache is full: pushed out key = 3 +[Least recently used] 2,4 [most recently used] + +get(3) -> returns -1 + +Cache unchanged: +[Least recently used] 2,4 [most recently used] |
