From a1302d32a8d739f3d4cf8d2cf7b5e1691fb027ab Mon Sep 17 00:00:00 2001 From: katarzynakaz Date: Fri, 20 Feb 2026 20:37:37 +0000 Subject: [PATCH 1/3] lru cache task completed --- Sprint-2/implement_lru_cache/lru_cache.py | 102 ++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index e69de29..b59ba4e 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -0,0 +1,102 @@ +import time + +our_list = [] +lookup_map = {} +limit = 0 + +# `LruCache(limit)` should construct +# an LRU cache which never stores more than `limit` entries. +def LruCache(user_limit): + global limit, our_list, lookup_map + limit = user_limit + our_list = [] + lookup_map = {} + +# * `set(key, value)` should associate `value` with the passed `key`. +def set(key, value): + global our_list, lookup_map + + if key in lookup_map: + old_item = lookup_map[key] + our_list.remove(old_item) + + wrapped_item = { + "key": key, + "value": value, + "tracker": time.time() + } + +#add to list and map + our_list.insert(0, wrapped_item) + lookup_map[key] = wrapped_item + +#if full remove oldest timestamp so last + if len(our_list) > limit: + oldest_item = our_list.pop() + + del lookup_map[oldest_item["key"]] + + +# * `get(key)` should look-up the value previously associated with `key`. +def get(key): + global our_list, lookup_map +#check map instead of for loop + if key in lookup_map: + # find by key + item = lookup_map[key] + + # // tracker to now timestamp updaed + item["tracker"] = time.time() + + #to front + our_list.remove(item) + our_list.insert(0, item) + + return item["value"] + + return None + + +#before i did it this was but was looping over each item and did not +#fit the required complexity +# def get_old(key): +# for item in our_list: +# if item["key"] == key: +# item["tracker"] = time.time() +# our_list.remove(item) +# our_list.insert(0, item) +# return item["value"] +# return None + +#and before tried with an id not timestamp +# tracker_number = 0 + +# def set(key, value): +# global tracker_number, our_list, lookup_map + + +# wrapped_item = { +# "key": key, +# "value": value, +# "tracker": tracker_number +# } + + +# tracker_number += 1 + +# our_list.insert(0, wrapped_item) +# lookup_map[key] = wrapped_item + +# and the loop +# def get_old(key): +# global tracker_number, our_list +# for item in our_list: +# if item["key"] == key: +# item["tracker"] = tracker_number +# tracker_number += 1 + +# our_list.remove(item) +# our_list.insert(0, item) + +# return item["value"] +# return None \ No newline at end of file From 104a1ded304e14ac6316b2bcfa6d14772545b5b4 Mon Sep 17 00:00:00 2001 From: katarzynakaz Date: Sat, 18 Apr 2026 03:14:56 +0100 Subject: [PATCH 2/3] sort strs upfront --- .../common_prefix/common_prefix.py | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py b/Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py index f4839e7..f58f135 100644 --- a/Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py +++ b/Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py @@ -7,12 +7,20 @@ def find_longest_common_prefix(strings: List[str]): In the event that an empty list, a list containing one string, or a list of strings with no common prefixes is passed, the empty string will be returned. """ + if len(strings) < 2: + return "" longest = "" - for string_index, string in enumerate(strings): - for other_string in strings[string_index+1:]: - common = find_common_prefix(string, other_string) - if len(common) > len(longest): - longest = common + + sorted_strings = sorted(strings) + + for i in range(len(sorted_strings) - 1): + common = find_common_prefix( + sorted_strings[i], + sorted_strings[i+1] + ) + if len(common) > len(longest): + longest = common + return longest @@ -22,3 +30,5 @@ def find_common_prefix(left: str, right: str) -> str: if left[i] != right[i]: return left[:i] return left[:min_length] + + From 869a6ae505039ac3e08ed108e9d4985210147aa1 Mon Sep 17 00:00:00 2001 From: katarzynakaz Date: Sat, 18 Apr 2026 03:33:51 +0100 Subject: [PATCH 3/3] add set to count_letters --- .../count_letters/count_letters.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Sprint-2/improve_with_precomputing/count_letters/count_letters.py b/Sprint-2/improve_with_precomputing/count_letters/count_letters.py index 62c3ec0..3329b32 100644 --- a/Sprint-2/improve_with_precomputing/count_letters/count_letters.py +++ b/Sprint-2/improve_with_precomputing/count_letters/count_letters.py @@ -2,13 +2,15 @@ def count_letters(s: str) -> int: """ count_letters returns the number of letters which only occur in upper case in the passed string. """ - only_upper = set() - for letter in s: - if is_upper_case(letter): - if letter.lower() not in s: - only_upper.add(letter) - return len(only_upper) + present_chars = set(s) + only_upper_count = 0 + for char in present_chars: + if char.isupper(): + if char.lower() not in present_chars: + only_upper_count += 1 + + return only_upper_count def is_upper_case(letter: str) -> bool: return letter == letter.upper()