From ecd7c6320e34455f5f57d51b3ce20fb9b1237763 Mon Sep 17 00:00:00 2001 From: tiaz0128 Date: Sun, 4 Feb 2024 17:07:33 +0900 Subject: [PATCH 1/2] init --- src/tiaz0128/ch_08/solution_019.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tiaz0128/ch_08/solution_019.py b/src/tiaz0128/ch_08/solution_019.py index 62aa608..d8fdfeb 100644 --- a/src/tiaz0128/ch_08/solution_019.py +++ b/src/tiaz0128/ch_08/solution_019.py @@ -1,2 +1,2 @@ def solution(string_list, query_list): - return + return [query in string_list for query in query_list] From 6452c1ef7155f24b2ed47500832a2bf5bfde6c8f Mon Sep 17 00:00:00 2001 From: tiaz0128 Date: Sun, 4 Feb 2024 17:18:28 +0900 Subject: [PATCH 2/2] try-1 --- src/tiaz0128/ch_08/solution_019.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/tiaz0128/ch_08/solution_019.py b/src/tiaz0128/ch_08/solution_019.py index d8fdfeb..e301b86 100644 --- a/src/tiaz0128/ch_08/solution_019.py +++ b/src/tiaz0128/ch_08/solution_019.py @@ -1,2 +1,20 @@ +def polynomial_hash(str): + p = 31 # 소수 + m = 1_000_000_007 # 버킷 크기 + hash_value = 0 + for char in str: + hash_value = (hash_value * p + ord(char)) % m + return hash_value + + def solution(string_list, query_list): - return [query in string_list for query in query_list] + hash_list = [polynomial_hash(str) for str in string_list] + + result = [] + for query in query_list: + query_hash = polynomial_hash(query) + if query_hash in hash_list: + result.append(True) + else: + result.append(False) + return result