diff options
| author | Ian Rifkin <ian.p.rifkin@gmail.com> | 2020-03-01 18:45:47 -0500 |
|---|---|---|
| committer | Ian Rifkin <ian.p.rifkin@gmail.com> | 2020-03-01 18:45:47 -0500 |
| commit | 894b9834539fae814410536b4b734ea4764c7fb9 (patch) | |
| tree | c82c4bf90cd5546b0f69e9e062d5dbed3f475d01 | |
| parent | ed38af056a3b29ce9d3811605b07c23e16d0d5f6 (diff) | |
| download | perlweeklychallenge-club-894b9834539fae814410536b4b734ea4764c7fb9.tar.gz perlweeklychallenge-club-894b9834539fae814410536b4b734ea4764c7fb9.tar.bz2 perlweeklychallenge-club-894b9834539fae814410536b4b734ea4764c7fb9.zip | |
Submitting my solutions for the two challenges.
| -rw-r--r-- | challenge-049/ianrifkin/perl/ch-1.pl | 7 | ||||
| -rw-r--r-- | challenge-049/ianrifkin/perl/ch-2.pl | 85 |
2 files changed, 89 insertions, 3 deletions
diff --git a/challenge-049/ianrifkin/perl/ch-1.pl b/challenge-049/ianrifkin/perl/ch-1.pl index 99e2b30dcc..67d764a4a5 100644 --- a/challenge-049/ianrifkin/perl/ch-1.pl +++ b/challenge-049/ianrifkin/perl/ch-1.pl @@ -16,9 +16,6 @@ my $input = $ARGV[0]; die("Input must be a positive number") unless $input > 0; -my $i = 2; -my $output = undef; - # You could loop through every number and check if it's correct # For smaller answers like when $input = 2, or 5, or 100 it's quick @@ -39,6 +36,8 @@ sub dec2bin { # it would take 1011111110 loops to get to 1011111111 # But in this method it only takes 766 loops. +my $i = 2; +my $output = undef; while ($output == undef) { my $bin_i = dec2bin($i); $i++ and next unless $bin_i =~ /^1[1]*[0]+[0-1]*$/; @@ -46,6 +45,8 @@ while ($output == undef) { $test->bdiv($input); $output = $bin_i if $test == $test->as_int() and $input != $bin_i; $i++; + #Consider if there needs to be an emergency exit after too many iterations + #without finding an answer } print "\n\nThe smallest multiple of $input with only digits 0 and 1 is: $output\n\n"; diff --git a/challenge-049/ianrifkin/perl/ch-2.pl b/challenge-049/ianrifkin/perl/ch-2.pl new file mode 100644 index 0000000000..1f0333688f --- /dev/null +++ b/challenge-049/ianrifkin/perl/ch-2.pl @@ -0,0 +1,85 @@ +#!/usr/bin/perl +use strict; +use Term::Prompt; +use feature qw( say ); +use Data::Dumper; + +#### 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. +#### Solution by ianrifkin + +#### Notes: At first I was trying to have a %lru_map +#### not contain contents by refs to content, but I'm not sure why +#### And the 'next' and 'prev' were orginally hash refs but I got confused +#### how to delete from the cache + +#### Originally was going to organize code better using subs but alas +#### here we are! + +### Globals +my $max_cap = $ARGV[0]; +die("Input must be a positive number") unless $max_cap > 0; +my @lru_cache = []; +my %lru_map = {}; +my $head = undef; +my $tail = undef; + +say "Welcome to the LRU Cache."; + +while(1) { + my $action = &prompt('x', "Choose: [get,set,quit]",''); + + if ($action eq 'get') { + my $item_key = &prompt('x', "Input item key to get", ''); + if (defined $lru_map{$item_key}) { + say "Data from cache for key $item_key:"; + say $lru_map{$item_key}{'data'}; + $lru_map{$head}{'prev'} = $item_key; #set outgoing first item's prev to new first item + $lru_map{$item_key}{'next'} = $head; #set new item's 'next' to outgoing head + $head = $item_key; #Update head to current item key + $lru_map{$item_key}{'prev'} = undef; #no prev since first in cache + + if ($tail eq $head) { #if the new head was the old tail + $tail = $lru_map{$tail}{'next'}; #set tail to new last item + } + } + else { + say "Item $item_key not currently in cache. Maybe you want to set it?"; + } + + } + elsif ($action eq 'set') { + my $item_key = &prompt('x', "Input key of new item to add to the cache", ''); + my $item_data = &prompt('x', "Input item to add to cache", ''); + + $lru_map{$item_key} = {}; + if ($head) { + $lru_map{$head}{'prev'} = $item_key; #set outgoing head's prev to current item + $lru_map{$item_key}{'next'} = $head; #set new item's 'next' to outgoing head + } + + $head = $item_key; #set new item as head + $tail = $item_key unless $tail; #set tail if no tail yet AKA 1st in cache + + my $size = keys %lru_map; + $size--; #don't count item being currently added + if ($size > $max_cap) { #if this new item can't fit in cache + $tail = $lru_map{$tail}{'prev'}; #set new tail + delete $lru_map{$lru_map{$tail}{'next'}}; #delete last item in cache + delete $lru_map{$tail}{'next'}; #delete new last item's next since it's now last + } + + $lru_map{$item_key}{'data'} = $item_data; #load actual cache + $lru_map{$item_key}{'prev'} = undef; #no prev since first in cache + + next; + } + elsif ($action eq 'quit') { + say "Action is $action"; + die("Thank you for trying the LRU Cache!"); + } + else { + say "Invalid option selected. Please try again."; + next; + } +} |
