aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-03-01 13:56:48 +0000
committerGitHub <noreply@github.com>2020-03-01 13:56:48 +0000
commit4e29dfdaa14d9a3a22a2ab6706cbf0355a5c24e9 (patch)
tree707fc7b441f70f59a5184055ea8a4b465a4e710a
parent32960796717448bbd0b5509d94f5310b34858c8c (diff)
parentfabec5ef15134eefc9840b3c6d5e3b187341cb4b (diff)
downloadperlweeklychallenge-club-4e29dfdaa14d9a3a22a2ab6706cbf0355a5c24e9.tar.gz
perlweeklychallenge-club-4e29dfdaa14d9a3a22a2ab6706cbf0355a5c24e9.tar.bz2
perlweeklychallenge-club-4e29dfdaa14d9a3a22a2ab6706cbf0355a5c24e9.zip
Merge pull request #1332 from drclaw1394/master
Ruben's solutions to w49 ch-1 and ch-2. perl and raku
-rw-r--r--challenge-049/ruben-westerberg/README8
-rwxr-xr-xchallenge-049/ruben-westerberg/perl/ch-1.pl7
-rwxr-xr-xchallenge-049/ruben-westerberg/perl/ch-2.pl112
-rwxr-xr-xchallenge-049/ruben-westerberg/raku/ch-1.raku5
-rwxr-xr-xchallenge-049/ruben-westerberg/raku/ch-2.raku108
5 files changed, 238 insertions, 2 deletions
diff --git a/challenge-049/ruben-westerberg/README b/challenge-049/ruben-westerberg/README
index 9f659c966a..1e26e2861c 100644
--- a/challenge-049/ruben-westerberg/README
+++ b/challenge-049/ruben-westerberg/README
@@ -2,8 +2,12 @@ Solution by Ruben Westerberg
ch-1.pl and ch-1.raku
===================
-Survivor program. Run to find the survivor
+Run program with a single commandline argument with the number to test. Prints out a multiple of the number only containing 1s and 0s. 55 is used as the default if not value is provided
ch-2.pl and ch-2.raku
===================
-Palindrone dates between 2000 and 2999
+Demonstration of LRU cache.
+
+A 'slow large' backing store is simulated with a delay in accessing the elements of the store.The cache loads values from the store when a cache 'miss' occours in reading. In writing, the cache only writes to the backing store when the cached element is to be deleted from the LRU list.
+
+The demonstration program randomly uses read and write calls to the cache. There are more acces operations than the original size of the backing store. It will grow and show undefined elements as 'N/A'
diff --git a/challenge-049/ruben-westerberg/perl/ch-1.pl b/challenge-049/ruben-westerberg/perl/ch-1.pl
new file mode 100755
index 0000000000..b2df8150ad
--- /dev/null
+++ b/challenge-049/ruben-westerberg/perl/ch-1.pl
@@ -0,0 +1,7 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+my ($num, $factor)=($ARGV[0]//55,1);
+$factor++ until ($num*$factor) =~ /^[01]+$/;
+printf "Smallest multiple: %d\n",$num*$factor;
diff --git a/challenge-049/ruben-westerberg/perl/ch-2.pl b/challenge-049/ruben-westerberg/perl/ch-2.pl
new file mode 100755
index 0000000000..148ec30c52
--- /dev/null
+++ b/challenge-049/ruben-westerberg/perl/ch-2.pl
@@ -0,0 +1,112 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+no warnings 'experimental';
+use Time::HiRes;
+use v5.24;
+my ($GET_CMD,$SET_CMD,$SYNC_CMD,$UPDATE_CMD,$DUMP_CMD)=0..4;
+
+
+print "Inital backing store contents:\n";
+largeSlowStore($DUMP_CMD);
+
+my $counter=0;
+
+for (0..19) {
+ print "\nOperation: $counter\n";
+ fastSmallCache(int(rand($SET_CMD+1)),int(rand(20)), int(rand(100)));
+ fastSmallCache($DUMP_CMD);
+ print '=' x 20,"\n";
+ $counter++;
+}
+print "Resulting backing store before flush/sync of lru cache\n";
+largeSlowStore($DUMP_CMD);
+
+fastSmallCache($SYNC_CMD);
+print "Resulting backing store after sync \n";
+largeSlowStore($DUMP_CMD);
+
+sub fastSmallCache {
+ my ($cmd,$index,$value)=@_;
+ state $capacity=5;
+ state %index;
+ state @lru;
+ do {
+ given ($cmd) {
+ when ($GET_CMD) {
+ print "\nRead Cache: ";
+ do {given ($index{$index}) {
+ when (undef) {
+ print "-Miss\n";
+ #cache miss
+ my $val=largeSlowStore($GET_CMD,$index);
+ fastSmallCache($UPDATE_CMD,$index,$val) if defined $val;
+ }
+
+ default {
+ #cache hit
+ print "-Hit\n";
+ fastSmallCache($UPDATE_CMD,$index,$_);
+ }
+ }
+ }
+
+ }
+ when ($SET_CMD) {
+ print "\nWrite cache: \n";
+ fastSmallCache($UPDATE_CMD,$index,$value);
+ }
+ when ($SYNC_CMD) {
+ print "Syncing cache\n";
+ largeSlowStore($SET_CMD,$_, $index{$_}) for (@lru);
+ %index=();
+ @lru=();
+ }
+ when ($UPDATE_CMD) {
+ print "Updating cache: $index => $value\n";
+ my ($k)=grep {$lru[$_] == $index} 0..@lru-1;
+ my $tmp;
+ $tmp=splice @lru,$k,1 if defined $k;
+ #print "cached pre key: $tmp\n";
+ my $del=shift @lru unless @lru < $capacity;
+ if (defined $del) {
+ print "Cache overflow\n";
+ largeSlowStore($SET_CMD,$del,$index{$del});
+ delete $index{$del};
+ }
+ push @lru,$index;
+ $index{$index}=$value;
+ }
+ when ($DUMP_CMD) {
+ print "LRU keys: ", join ", ", @lru;
+ print "\n\n";
+ }
+
+ }
+ }
+}
+
+
+
+sub largeSlowStore {
+ my ($cmd,$index,$value)=@_;
+ state @data= map {int rand 10} 1..10; #initialize store
+ do {
+ given ($cmd) {
+ Time::HiRes::usleep 1000000 * rand 2;
+ when ($GET_CMD) {
+ print "Read backing store\n";
+ $data[$index];
+ };
+ when ($SET_CMD) {
+ print "Write backing store\n";
+ $data[$index]=$value;
+ }
+ when ($DUMP_CMD) {
+ print join ", ", map { defined $_? $_:"N/A"} @data;
+ print "\n";
+ }
+ }
+ }
+}
diff --git a/challenge-049/ruben-westerberg/raku/ch-1.raku b/challenge-049/ruben-westerberg/raku/ch-1.raku
new file mode 100755
index 0000000000..46a1be2e4a
--- /dev/null
+++ b/challenge-049/ruben-westerberg/raku/ch-1.raku
@@ -0,0 +1,5 @@
+#!/usr/bin/env raku
+
+my ($num, $factor)=(@*ARGS[0]//55,1);
+$factor++ until ($num*$factor) ~~ /^<[01]>+$/;
+printf "Smallest multiple: %d\n",$num*$factor;
diff --git a/challenge-049/ruben-westerberg/raku/ch-2.raku b/challenge-049/ruben-westerberg/raku/ch-2.raku
new file mode 100755
index 0000000000..9f48e9db3f
--- /dev/null
+++ b/challenge-049/ruben-westerberg/raku/ch-2.raku
@@ -0,0 +1,108 @@
+#!/usr/bin/env raku
+
+my ($GET_CMD,$SET_CMD,$SYNC_CMD,$UPDATE_CMD,$DUMP_CMD)=0..4;
+
+
+put "Inital backing store contents:";
+largeSlowStore($DUMP_CMD);
+
+my $counter=0;
+
+for ^20 {
+ put "\nOperation: $counter";
+ fastSmallCache(($SET_CMD+1).rand.Int,index=>20.rand.Int, value=>100.rand.Int);
+ fastSmallCache($DUMP_CMD);
+ put '=' x 20;
+ $counter++;
+}
+
+put "Resulting backing store before flush/sync of lru cache";
+largeSlowStore($DUMP_CMD);
+
+fastSmallCache($SYNC_CMD);
+put "Resulting backing store after sync ";
+largeSlowStore($DUMP_CMD);
+
+sub fastSmallCache($cmd,:$index,:$value){
+ state $capacity=5;
+ state %index;
+ state @lru;
+ do {
+ given $cmd {
+ when $GET_CMD {
+ put "\nRead Cache: ";
+ do { given %index{$index} {
+ when Any {
+ put "-Miss";
+ #cache miss
+ my $val=largeSlowStore($GET_CMD,:$index);
+ fastSmallCache($UPDATE_CMD,:$index,value=>$val) if defined $val;
+ }
+
+ default {
+ #cache hit
+ put "-Hit";
+ fastSmallCache($UPDATE_CMD,:$index,value=>$_);
+ }
+ }
+ }
+
+ }
+
+ when ($SET_CMD) {
+ put "\nWrite cache: ";
+ fastSmallCache($UPDATE_CMD,:$index,:$value);
+ }
+
+ when ($SYNC_CMD) {
+ put "Syncing cache";
+ largeSlowStore($SET_CMD,index=>$_, value=>%index{$_}) for (@lru);
+ %index=();
+ @lru=();
+ }
+
+ when ($UPDATE_CMD) {
+ put "Updating cache: $index => $value";
+ my ($k)=(^@lru).grep({@lru[$_] == $index});
+ my $tmp;
+ $tmp=@lru.splice($k,1) when $k.defined;
+ my $del=@lru.shift unless @lru < $capacity;
+ if (defined $del) {
+ put "Cache overflow";
+ largeSlowStore($SET_CMD,index=>$del,value=>%index{$del});
+ %index{$del}:delete;
+ }
+ @lru.push: $index;
+ %index{$index}=$value;
+ }
+ when ($DUMP_CMD) {
+ put "LRU keys: ", @lru.join: ", ";
+ put "\n";
+ }
+
+ }
+ }
+}
+
+
+
+sub largeSlowStore($cmd,:$index,:$value) {
+ state @data= 10.rand.Int xx 10;
+ do {
+ given ($cmd) {
+ sleep(2.rand);
+ when ($GET_CMD) {
+ put "Read backing store";
+ @data[$index];
+ };
+ when ($SET_CMD) {
+ put "Write backing store";
+ @data[$index]=$value;
+ }
+ when ($DUMP_CMD) {
+ put @data.map({ defined($_)??$_!!"N/A"}).join: ", ";
+ put "";
+ }
+ }
+ }
+}