diff options
| author | 冯昶 <fengchang@novel-supertv.com> | 2024-09-02 15:40:05 +0800 |
|---|---|---|
| committer | 冯昶 <fengchang@novel-supertv.com> | 2024-09-02 15:40:05 +0800 |
| commit | 0c04a0825fd1dc67c5295fffdfbf3cb90ba075b8 (patch) | |
| tree | d963a7fe57782d1d25580ea05c96f969768e8067 /challenge-049/paulo-custodio/python | |
| parent | 30a42138e1d8bdd2f174e06fb18515ced0b2b241 (diff) | |
| parent | 0512711fccf91c731495f1dd901349cc900ec299 (diff) | |
| download | perlweeklychallenge-club-0c04a0825fd1dc67c5295fffdfbf3cb90ba075b8.tar.gz perlweeklychallenge-club-0c04a0825fd1dc67c5295fffdfbf3cb90ba075b8.tar.bz2 perlweeklychallenge-club-0c04a0825fd1dc67c5295fffdfbf3cb90ba075b8.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-049/paulo-custodio/python')
| -rw-r--r-- | challenge-049/paulo-custodio/python/ch-1.py | 27 | ||||
| -rw-r--r-- | challenge-049/paulo-custodio/python/ch-2.py | 107 |
2 files changed, 134 insertions, 0 deletions
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)) |
