Skip to content

Commit

Permalink
Annotate variables
Browse files Browse the repository at this point in the history
  • Loading branch information
donjuardo committed Jul 10, 2024
1 parent c4f3d46 commit 587fb1f
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 8 deletions.
4 changes: 3 additions & 1 deletion python3/1/a.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

class Solution:
def twoSum(self, nums: list[int], target: int) -> list[int]:
n = len(nums)
n: int = len(nums)

i: int
j: int
# enumerate does not allow the last item to be skipped without slicing,
# which would require linear auxiliary space.
for i in range(n - 1):
Expand Down
8 changes: 5 additions & 3 deletions python3/1/b.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

class Solution:
def twoSum(self, nums: list[int], target: int) -> list[int]:
i = 0
j = len(nums) - 1
snums = sorted(nums)
snums: list[int] = sorted(nums)

i: int = 0
j: int = len(nums) - 1
total: int
res: list[int]
while i < j:
total = snums[i] + snums[j]
if total == target:
Expand Down
5 changes: 4 additions & 1 deletion python3/1/c.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@

class Solution:
def twoSum(self, nums: list[int], target: int) -> list[int]:
visited = {}
visited: dict[int, int] = {}

i: int
num: int
diff: int
for i, num in enumerate(nums):
diff = target - num
if diff in visited:
Expand Down
4 changes: 3 additions & 1 deletion python3/217/a.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

class Solution:
def containsDuplicate(self, nums: list[int]) -> bool:
n = len(nums)
n: int = len(nums)
if n < 2:
return False

i: int
j: int
# enumerate does not allow the last item to be skipped without slicing,
# which would require linear auxiliary space.
for i in range(n - 1):
Expand Down
2 changes: 2 additions & 0 deletions python3/217/b.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def containsDuplicate(self, nums: list[int]) -> bool:

nums.sort()

i: int
num: int
for i, num in enumerate(nums):
# nums[0] is needlessly compared to nums[-1] so as to avoid
# checking if i > 0 at every iteration.
Expand Down
3 changes: 2 additions & 1 deletion python3/217/c.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ def containsDuplicate(self, nums: list[int]) -> bool:
if len(nums) < 2:
return False

visited = set()
visited: set[int] = set()

num: int
for num in nums:
if num in visited:
return True
Expand Down
4 changes: 3 additions & 1 deletion python3/242/b.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ def isAnagram(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False

count = {}
count: dict[str, int] = {}

i: int
c: str
for i, c in enumerate(s):
count[c] = count.get(c, 0) + 1
count[t[i]] = count.get(t[i], 0) - 1
Expand Down

0 comments on commit 587fb1f

Please sign in to comment.