From 61b70a91f36392829303ca1c279e2972b1fcb71e Mon Sep 17 00:00:00 2001 From: ilotoki0804 Date: Mon, 22 Jan 2024 19:32:47 +0900 Subject: [PATCH 1/5] refactoring 11-1 --- 3-linear-data-structures/ch07/11-1.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/3-linear-data-structures/ch07/11-1.py b/3-linear-data-structures/ch07/11-1.py index 7a756ce..973e35b 100644 --- a/3-linear-data-structures/ch07/11-1.py +++ b/3-linear-data-structures/ch07/11-1.py @@ -3,15 +3,16 @@ class Solution: def productExceptSelf(self, nums: List[int]) -> List[int]: - out = [] - p = 1 - # 왼쪽 곱셈 - for i in range(0, len(nums)): - out.append(p) - p = p * nums[i] - p = 1 - # 왼쪽 곱셈 결과에 오른쪽 값을 차례대로 곱셈 - for i in range(len(nums) - 1, 0 - 1, -1): - out[i] = out[i] * p - p = p * nums[i] - return out + result = [] + + left_accumulated = 1 + for number in nums: + result.append(left_accumulated) + left_accumulated *= number + + right_accumulated = 1 + for i in range(0, len(nums))[::-1]: + result[i] *= right_accumulated + right_accumulated *= nums[i] + + return result From b933b868d185ed471590d6ce155b3aff3fea1d32 Mon Sep 17 00:00:00 2001 From: ilotoki0804 Date: Mon, 22 Jan 2024 19:36:03 +0900 Subject: [PATCH 2/5] Use comprehension --- 3-linear-data-structures/ch07/10-2.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/3-linear-data-structures/ch07/10-2.py b/3-linear-data-structures/ch07/10-2.py index 7e8492f..15d9cd7 100644 --- a/3-linear-data-structures/ch07/10-2.py +++ b/3-linear-data-structures/ch07/10-2.py @@ -3,12 +3,10 @@ class Solution: def arrayPairSum(self, nums: List[int]) -> int: - sum = 0 nums.sort() - for i, n in enumerate(nums): - # 짝수 번째 값의 합 계산 - if i % 2 == 0: - sum += n - - return sum + return sum( + number + for i, number in enumerate(nums) + if i % 2 == 0 + ) From 8c9850bfabb2e957d7b23a63d025559b1ac129e0 Mon Sep 17 00:00:00 2001 From: ilotoki0804 Date: Mon, 22 Jan 2024 20:09:37 +0900 Subject: [PATCH 3/5] Add new alternative --- 3-linear-data-structures/ch07/12-2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3-linear-data-structures/ch07/12-2.py b/3-linear-data-structures/ch07/12-2.py index ebd2109..355f01b 100644 --- a/3-linear-data-structures/ch07/12-2.py +++ b/3-linear-data-structures/ch07/12-2.py @@ -5,7 +5,7 @@ class Solution: def maxProfit(self, prices: List[int]) -> int: profit = 0 - min_price = sys.maxsize + min_price = sys.maxsize # or prices[0] # 최소값과 최대값 계속 갱신 for price in prices: From b078475305fda409fb38302095323c5214913875 Mon Sep 17 00:00:00 2001 From: ilotoki0804 Date: Wed, 24 Jan 2024 23:17:27 +0900 Subject: [PATCH 4/5] =?UTF-8?q?=EC=9E=84=EC=8B=9C=20=EB=B3=80=EC=88=98=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 기존 방식은 간결한 대신 논리가 너무 압축적이라 쉽게 알아보기 힘들었다면 이 방식은 조금 더 장황하지만 알아보기는 쉽다. rev, slow, fast는 관행 같아서 따로 고치지는 않았지만 rev는 reversed와 같이 이름을 고치는 것도 가독성에 도움이 될 듯 하다. 또한 조금 더 비효율적이긴 하지만 rev.next에 재할당하는 대신 rev.prev에 새로 할당해 혼란을 줄이는 것도 고려할 만 하다. --- 3-linear-data-structures/ch08/13-4.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/3-linear-data-structures/ch08/13-4.py b/3-linear-data-structures/ch08/13-4.py index 18f9617..7173926 100644 --- a/3-linear-data-structures/ch08/13-4.py +++ b/3-linear-data-structures/ch08/13-4.py @@ -12,7 +12,13 @@ def isPalindrome(self, head: ListNode) -> bool: # 런너를 이용해 역순 연결 리스트 구성 while fast and fast.next: fast = fast.next.next - rev, rev.next, slow = slow, rev, slow.next + + next_node = slow.next + previous_node = rev + + rev = slow + rev.next = previous_node + slow = next_node if fast: slow = slow.next From 5ecd73db5e91ab029818438138a4d2bb0412bec4 Mon Sep 17 00:00:00 2001 From: ilotoki0804 Date: Sun, 24 Mar 2024 22:12:38 +0900 Subject: [PATCH 5/5] Rename and use comprehension --- 2-python/ch06/1-1.py | 11 ++++------- 2-python/ch06/1-2.py | 16 +++++----------- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/2-python/ch06/1-1.py b/2-python/ch06/1-1.py index 257d2d6..e3b25b3 100644 --- a/2-python/ch06/1-1.py +++ b/2-python/ch06/1-1.py @@ -1,13 +1,10 @@ class Solution: - def isPalindrome(self, s: str) -> bool: - strs = [] - for char in s: - if char.isalnum(): - strs.append(char.lower()) + def isPalindrome(self, string: str) -> bool: + chars = [char.lower() for char in string if char.isalnum()] # 팰린드롬 여부 판별 - while len(strs) > 1: - if strs.pop(0) != strs.pop(): + while len(chars) > 1: + if chars.pop(0) != chars.pop(): return False return True diff --git a/2-python/ch06/1-2.py b/2-python/ch06/1-2.py index 1d9caa7..3f7d032 100644 --- a/2-python/ch06/1-2.py +++ b/2-python/ch06/1-2.py @@ -1,18 +1,12 @@ -import collections -from typing import Deque +from collections import deque class Solution: - def isPalindrome(self, s: str) -> bool: - # 자료형 데크로 선언 - strs: Deque = collections.deque() + def isPalindrome(self, string: str) -> bool: + chars = deque(char.lower() for char in string if char.isalnum()) - for char in s: - if char.isalnum(): - strs.append(char.lower()) - - while len(strs) > 1: - if strs.popleft() != strs.pop(): + while len(chars) > 1: + if chars.popleft() != chars.pop(): return False return True