From 3165b84ede465772a96ed02f9d8e607140dc5760 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sun, 1 Mar 2020 13:39:37 +0100 Subject: CH049 LK --- challenge-049/lubos-kolouch/perl/ch-1.pl | 56 ++++++++++++++++++++++++++++++++ challenge-049/lubos-kolouch/perl/ch-2.pl | 44 +++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 challenge-049/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-049/lubos-kolouch/perl/ch-2.pl diff --git a/challenge-049/lubos-kolouch/perl/ch-1.pl b/challenge-049/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..c500098aac --- /dev/null +++ b/challenge-049/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,56 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-1.pl +# +# USAGE: ./ch-1.pl +# +# DESCRIPTION: 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. +# +# OPTIONS: --- +# REQUIREMENTS: --- +# BUGS: --- +# NOTES: --- +# AUTHOR: Lubos Kolouch +# ORGANIZATION: +# VERSION: 1.0 +# CREATED: 02/29/2020 08:38:26 PM +# REVISION: --- +#=============================================================================== + +use strict; +use warnings; +use feature qw/say/; + + +sub find_multiple_0_1 { + my $input = shift; + + my $count = 0; + + while ($input !~ /^[01]+$/) { + $input += $input; + # for some numbers there is probably no result, so let's just return -1 + return -1 if $count == 1000; + $count++; + } + + return $input; +} + +my $what = $ARGV[0]; + +say find_multiple_0_1($what); + +use Test::More; + +is(find_multiple_0_1(55),110); +done_testing; diff --git a/challenge-049/lubos-kolouch/perl/ch-2.pl b/challenge-049/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..3f154f73b4 --- /dev/null +++ b/challenge-049/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-2.pl +# +# USAGE: ./ch-2.pl +# +# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-049/ +# +# 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. +# +# OPTIONS: --- +# REQUIREMENTS: --- +# BUGS: --- +# NOTES: --- +# AUTHOR: Lubos Kolouch +# ORGANIZATION: +# VERSION: 1.0 +# CREATED: 02/29/2020 08:53:24 PM +# REVISION: --- +#=============================================================================== + +use strict; +use warnings; +use Cache::LRU; + +my $size = $ARGV[0] or die 'Usage: script size'; + +my $cache = Cache::LRU->new( + size => $size +); + + +use Test::More; + +$cache->set(1=>3); +$cache->set(2=>5); +$cache->set(3=>7); + +is($cache->get(2),5); +is($cache->get(1),3); +is($cache->get(4),undef); +$cache->set(4=>9); +is($cache->get(3),undef); -- cgit