aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-09-01 12:26:29 +0100
committerGitHub <noreply@github.com>2024-09-01 12:26:29 +0100
commit145c91e15f024b6c9644cee77986dcaded36d2a6 (patch)
tree7e3e6fa76221e8ee3e64df4b21dd999a1eb69c08
parent649dd36f4f9bf0183e9a20b192c14df87a11dbfd (diff)
parenta1a0939e3a029b61fe8d26e63a2e866f1659bb47 (diff)
downloadperlweeklychallenge-club-145c91e15f024b6c9644cee77986dcaded36d2a6.tar.gz
perlweeklychallenge-club-145c91e15f024b6c9644cee77986dcaded36d2a6.tar.bz2
perlweeklychallenge-club-145c91e15f024b6c9644cee77986dcaded36d2a6.zip
Merge pull request #10742 from pauloscustodio/master
Add Python solutions
-rw-r--r--challenge-049/paulo-custodio/Makefile1
-rw-r--r--challenge-049/paulo-custodio/perl/ch-2.pl34
-rw-r--r--challenge-049/paulo-custodio/python/ch-1.py27
-rw-r--r--challenge-049/paulo-custodio/python/ch-2.py107
-rw-r--r--challenge-049/paulo-custodio/t/test-2.yaml23
5 files changed, 175 insertions, 17 deletions
diff --git a/challenge-049/paulo-custodio/Makefile b/challenge-049/paulo-custodio/Makefile
index 9494a9025e..c3c762d746 100644
--- a/challenge-049/paulo-custodio/Makefile
+++ b/challenge-049/paulo-custodio/Makefile
@@ -1,3 +1,2 @@
all:
perl ../../challenge-001/paulo-custodio/test.pl
- perl ./perl/ch-2.pl \ No newline at end of file
diff --git a/challenge-049/paulo-custodio/perl/ch-2.pl b/challenge-049/paulo-custodio/perl/ch-2.pl
index 7af6cb2c74..804e1ca925 100644
--- a/challenge-049/paulo-custodio/perl/ch-2.pl
+++ b/challenge-049/paulo-custodio/perl/ch-2.pl
@@ -43,7 +43,6 @@
# get(3) # returns -1
use Modern::Perl;
-use Test::More;
{
package Cache;
@@ -90,31 +89,34 @@ use Test::More;
}
}
+say "Create cache, capacity=>3";
my $cache = Cache->new(capacity=>3);
-is $cache->as_string, "";
+say "Cache=", $cache->as_string;
+say "Set cache:1,3";
$cache->set(1, 3);
-is $cache->as_string, "(1=>3)";
+say "Cache=", $cache->as_string;
+say "Set cache:2,5";
$cache->set(2, 5);
-is $cache->as_string, "(1=>3)(2=>5)";
+say "Cache=", $cache->as_string;
+say "Set cache:3,7";
$cache->set(3, 7);
-is $cache->as_string, "(1=>3)(2=>5)(3=>7)";
+say "Cache=", $cache->as_string;
-is $cache->get(2), 5;
-is $cache->as_string, "(1=>3)(3=>7)(2=>5)";
+say "Get cache 2=>",$cache->get(2);
+say "Cache=", $cache->as_string;
-is $cache->get(1), 3;
-is $cache->as_string, "(3=>7)(2=>5)(1=>3)";
+say "Get cache 1=>",$cache->get(1);
+say "Cache=", $cache->as_string;
-is $cache->get(4), -1;
-is $cache->as_string, "(3=>7)(2=>5)(1=>3)";
+say "Get cache 4=>",$cache->get(4);
+say "Cache=", $cache->as_string;
+say "Set cache:4,9";
$cache->set(4, 9);
-is $cache->as_string, "(2=>5)(1=>3)(4=>9)";
+say "Cache=", $cache->as_string;
-is $cache->get(3), -1;
-is $cache->as_string, "(2=>5)(1=>3)(4=>9)";
-
-done_testing;
+say "Get cache 3=>",$cache->get(3);
+say "Cache=", $cache->as_string;
diff --git a/challenge-049/paulo-custodio/python/ch-1.py b/challenge-049/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..edad0e1069
--- /dev/null
+++ b/challenge-049/paulo-custodio/python/ch-1.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+
+# 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.
+
+import re
+import sys
+
+def smallest_multiple(n):
+ p = 1
+ while not is_zeros_ones(n*p):
+ p += 1
+ return n*p
+
+def is_zeros_ones(n):
+ return re.search(r'^[01]+$', str(n))
+
+n = int(sys.argv[1])
+print(smallest_multiple(n))
diff --git a/challenge-049/paulo-custodio/python/ch-2.py b/challenge-049/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..c299fc5530
--- /dev/null
+++ b/challenge-049/paulo-custodio/python/ch-2.py
@@ -0,0 +1,107 @@
+#!/usr/bin/env python3
+
+# 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
+
+class Cache():
+ capacity = 0
+ cache = []
+
+ def __init__(self, capacity):
+ self.capacity = capacity
+ self.cache = []
+
+ def __str__(self):
+ out = ""
+ for x in self.cache:
+ out += "(" + str(x[0]) + "=>" + str(x[1]) + ")"
+ return out
+
+ def get(self, k):
+ for i in range(len(self.cache)):
+ if self.cache[i][0] == k:
+ v = self.cache[i][1]
+ self.cache = self.cache[0:i] + self.cache[i+1:]
+ self.cache.append([k,v])
+ return v
+ return -1
+
+ def set(self, k, v):
+ found = self.get(k)
+ if found == -1:
+ self.cache.append([k,v])
+ while len(self.cache) > self.capacity:
+ self.cache = self.cache[1:]
+ else:
+ self.cache[-1][1] = v
+
+print("Create cache, capacity=>3")
+cache = Cache(3)
+print("Cache="+str(cache))
+
+print("Set cache:1,3")
+cache.set(1, 3)
+print("Cache="+str(cache))
+
+print("Set cache:2,5")
+cache.set(2, 5)
+print("Cache="+str(cache))
+
+print("Set cache:3,7")
+cache.set(3, 7)
+print("Cache="+str(cache))
+
+print("Get cache 2=>"+str(cache.get(2)))
+print("Cache="+str(cache))
+
+print("Get cache 1=>"+str(cache.get(1)))
+print("Cache="+str(cache))
+
+print("Get cache 4=>"+str(cache.get(4)))
+print("Cache="+str(cache))
+
+print("Set cache:4,9")
+cache.set(4, 9)
+print("Cache="+str(cache))
+
+print("Get cache 3=>"+str(cache.get(3)))
+print("Cache="+str(cache))
diff --git a/challenge-049/paulo-custodio/t/test-2.yaml b/challenge-049/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..4f5caf3240
--- /dev/null
+++ b/challenge-049/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,23 @@
+- setup:
+ cleanup:
+ args:
+ input:
+ output: |
+ |Create cache, capacity=>3
+ |Cache=
+ |Set cache:1,3
+ |Cache=(1=>3)
+ |Set cache:2,5
+ |Cache=(1=>3)(2=>5)
+ |Set cache:3,7
+ |Cache=(1=>3)(2=>5)(3=>7)
+ |Get cache 2=>5
+ |Cache=(1=>3)(3=>7)(2=>5)
+ |Get cache 1=>3
+ |Cache=(3=>7)(2=>5)(1=>3)
+ |Get cache 4=>-1
+ |Cache=(3=>7)(2=>5)(1=>3)
+ |Set cache:4,9
+ |Cache=(2=>5)(1=>3)(4=>9)
+ |Get cache 3=>-1
+ |Cache=(2=>5)(1=>3)(4=>9)