diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-03-01 08:17:09 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-01 08:17:09 +0000 |
| commit | 48fda921985994bb7740815224e74cccf187131e (patch) | |
| tree | 4345a7fddcb21d954baf763894d62e66d9935012 | |
| parent | 384402c93fb76735a364bffc755dc3190af7fcbf (diff) | |
| parent | b8dfa91eecff5076df66ada9256cf199abdee831 (diff) | |
| download | perlweeklychallenge-club-48fda921985994bb7740815224e74cccf187131e.tar.gz perlweeklychallenge-club-48fda921985994bb7740815224e74cccf187131e.tar.bz2 perlweeklychallenge-club-48fda921985994bb7740815224e74cccf187131e.zip | |
Merge pull request #1330 from user-person/master
User Person's solutions for challenge 49.
| -rwxr-xr-x | challenge-049/user-person/perl/ch-1.sh | 18 | ||||
| -rwxr-xr-x | challenge-049/user-person/perl/ch-2.pl | 131 | ||||
| -rwxr-xr-x | challenge-049/user-person/python/ch-1.py | 24 | ||||
| -rwxr-xr-x | challenge-049/user-person/python/ch-2.py | 102 |
4 files changed, 275 insertions, 0 deletions
diff --git a/challenge-049/user-person/perl/ch-1.sh b/challenge-049/user-person/perl/ch-1.sh new file mode 100755 index 0000000000..c8082a90c0 --- /dev/null +++ b/challenge-049/user-person/perl/ch-1.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env sh -- + +########################################################################### +# script name: ch-1.sh # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-049/ # +# # +# 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. # +# # +########################################################################### + +# This short one works, but . . . +#perl -e'$a='$1';$a+='$1'while$a=~/[^01]/;print$a,$/' + +perl -e 'my $a = $ARGV[0]; my $b = $a; $a += $b while $a =~ /[^01]/; print"$a\n"' $1 diff --git a/challenge-049/user-person/perl/ch-2.pl b/challenge-049/user-person/perl/ch-2.pl new file mode 100755 index 0000000000..82417f07df --- /dev/null +++ b/challenge-049/user-person/perl/ch-2.pl @@ -0,0 +1,131 @@ +#!/usr/bin/env perl + +########################################################################### +# script name: ch-2.pl # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-049/ # +# # +# 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 Least recently item is the one with the oldest access # +# time. # +# # +########################################################################### + +use strict; +use warnings; +use FindBin; +use Term::ReadLine; + +my $term = Term::ReadLine->new('input'); +$term->ornaments(00,00,00,00); + +if ( scalar @ARGV < 1 ) { + print STDERR "$FindBin::Script requires one argument to assign LRU's capacity, but none was given.\n"; + exit(1); +} + +my $CAPACITY = $ARGV[0]; + +if ($CAPACITY !~ m{\A\d+\Z}) { + print STDERR "Command line argument assigns capacity. It should be an integer.\n"; + exit(1); +} else { + print "capacity = $CAPACITY\n"; +} + +my @cIndex = (); +my %cache = (); + +sub showCache { + print "[Least recently used] "; + for (my $i=0;$i <= $#cIndex; ++$i) { + print $cIndex[$i]; + $i == $#cIndex + ? print " " + : print ", "; + } + print "[most recently used]\n\n"; +} + +sub lruSet { + my $lruPos = $_[0]; + my $lruVal = $_[1]; + + if (exists($cache{$lruPos})) { + print "position $lruPos currently occupied in cache\n\n"; + } else { + push @cIndex, $lruPos; + $cache{$lruPos} = $lruVal; + + if ( scalar @cIndex == $CAPACITY ) { + print "\nCache at this point:\n"; + + showCache; + } elsif ( scalar @cIndex > $CAPACITY ) { + my $shifted = shift @cIndex; + delete $cache{$shifted}; + print "Cache is full, so pushes out key = $shifted:\n"; + showCache; + } + } +} + +sub lruGet { + my $lruInd = $_[0]; + + my $lruGetRet = -1; + + if (exists($cache{$lruInd})) { + + my $moveElement; + FIND: + for (my $j = 0;$j <= $#cIndex; ++$j) { + if ( $cIndex[$j] == $lruInd ) { + $moveElement = $j; + last FIND; + } + } + if ($moveElement != $#cIndex) { + my $toPush = splice @cIndex,$moveElement, 1; + push @cIndex, $toPush; + } + + $lruGetRet = $cache{$lruInd}; + } + + return $lruGetRet; +} + +ILOOP: +while (1) { + my $cmd = $term->readline(""); + + if ($cmd =~ m{x|q|exit|quit}i) { + + last ILOOP; + + } elsif ( $cmd =~ m{s(?:et)?[( ]?(\d+)\)?(?:\s+|\,)[( ]?(\d+)\)?}i ) { + + lruSet($1,$2); + + } elsif ( $cmd =~ m{g(?:et)?[( ]?(\d+)\)?}i ) { + + my $lruRet = lruGet($1); + + if ($lruRet == -1) { + print " # returns -1\n\nCache unchanged:\n"; + } else { + print " # returns $lruRet\n\nCache looks like now:\n"; + } + + showCache; + + } else { + print STDERR "\nunrecognized input: $cmd\nset(#,#) get(#), exit, quit, x, q are valid inputs\n\n"; + } +} diff --git a/challenge-049/user-person/python/ch-1.py b/challenge-049/user-person/python/ch-1.py new file mode 100755 index 0000000000..c9196dbfb4 --- /dev/null +++ b/challenge-049/user-person/python/ch-1.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 + +########################################################################### +# script name: ch-1.py # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-049/ # +# # +# 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. # +# # +########################################################################### + +import sys +import re + +num = int(sys.argv[1]) +add = num + +while re.search(r'[^01]',str(num)): + num += add + +print(num) diff --git a/challenge-049/user-person/python/ch-2.py b/challenge-049/user-person/python/ch-2.py new file mode 100755 index 0000000000..fbb20ece57 --- /dev/null +++ b/challenge-049/user-person/python/ch-2.py @@ -0,0 +1,102 @@ +#!/usr/bin/env python3 + +########################################################################### +# script name: ch-2.py # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-049/ # +# # +# 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 Least recently item is the one with the oldest access # +# time. # +# # +########################################################################### + +import os +import re +import readline +import sys + +if (len(sys.argv) < 2): + badArgString = os.path.basename(__file__) + " requires one argument to assign LRU's capacity, but none was given.\n" + sys.stderr.write(badArgString) + sys.exit(1) + +CAPACITY = sys.argv[1] + +capacityMatch = re.search("\A\d+\Z", CAPACITY) +if not capacityMatch: + sys.stderr.write('Command line argument assigns capacity. It should be an integer.\n') + sys.exit(1) +else: + print('capacity = ', CAPACITY) + +cIndex = [] +cache = dict() + +def showCache(): + print('[Least recently used] ',end='') + for i in range(len(cIndex)): + print(cIndex[i],end='') + print(' ',end='') if i == len(cIndex)-1 else print(', ',end='') + print('[most recently used]\n') + +def lruSet(lruPos,lruVal): + if lruPos in cache.keys(): + print('position ', lruPos, ' currently occupied in cache\n') + else: + cIndex.append(lruPos) + cache[lruPos] = lruVal + + if len(cIndex) == int(CAPACITY): + print('\nCache at this point:') + showCache() + + elif len(cIndex) > int(CAPACITY): + popped = cIndex.pop(0) + del cache[popped] + print('\nCache is full, so pushes out key = ' + popped + ':') + showCache() + +def lruGet(lruInd): + lruGetRet = -1 + + if lruInd in cache.keys(): + moveInd = cIndex.index(lruInd) + if moveInd != len(cIndex)-1: + cIndex.append(cIndex.pop(moveInd)) + + lruGetRet = cache[lruInd] + return lruGetRet + +while True: + cmd = input() + + setPat = re.compile(r's(?:et)?[( ]?(\d+)\)?(?:\s+|\,)[( ]?(\d+)\)?',re.I) + setMatch = re.match(setPat, cmd) + getPat = re.compile(r'g(?:et)?[( ]?(\d+)\)?', re.I) + getMatch = re.match(getPat, cmd) + + if re.search(r'x|q|exit|quit', cmd, re.I): + break + + elif setMatch: + lruSet(setMatch.group(1),setMatch.group(2)) + + elif getMatch: + lruRet = lruGet(getMatch.group(1)) + + if lruRet == -1: + print(' # returns -1\n\nCache unchanged:') + else: + print(' # returns ', lruRet, '\n\nCache looks like now:') + + showCache() + + else: + elseError = '\nunrecognized input: $cmd\nset(#,#) get(#), exit, quit, x, q are valid inputs\n' + sys.stderr.write(elseError)
\ No newline at end of file |
