added typehints

This commit is contained in:
2026-04-16 14:16:23 -04:00
parent 864d07348d
commit 34e88112c6
7 changed files with 21 additions and 11 deletions

2
.idea/misc.xml generated
View File

@@ -3,5 +3,5 @@
<component name="Black">
<option name="sdkName" value="Python 3.14 (test)" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.14 (test)" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.14" project-jdk-type="Python SDK" />
</project>

2
.idea/test.iml generated
View File

@@ -4,7 +4,7 @@
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.14 (test)" jdkType="Python SDK" />
<orderEntry type="jdk" jdkName="Python 3.14" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@@ -31,6 +31,8 @@ class Card:
def __add__(self, other) -> int:
#Adding 2 cards together
return "Cat"
if isinstance(other, Card):
return self._value + other._value
elif isinstance(other, int):

View File

@@ -39,7 +39,8 @@ def test_sum_cards():
Card("spade", 12),
Card("clubs", 1)
]
assert sum_cards_iter(cards) == 21
assert sum_cards_recursive(cards) == 21
print(sum_cards_iter(cards))
# assert sum_cards_iter(cards) == 21
# assert sum_cards_recursive(cards) == 21
test_sum_cards()

Binary file not shown.

View File

@@ -9,6 +9,7 @@
import random #to generate the 100 random values
import time #for part 4, to calculae total time
from collections.abc import Sequence
#I decided to put all 4 functions into one class to keep it organized
class Search:
@@ -16,8 +17,8 @@ class Search:
"""
This function generates the list of random integers.
"""
def generate_random_list(self, n):
array = [] #array starts as blank
def generate_random_list(self, n: int) -> list[int]:
array: list[int] = [] #array starts as blank
for element in range(n): #will run n times
array.append(random.randint(0, 1000)) #random integer between 0-1000
@@ -30,7 +31,7 @@ class Search:
"""
This funciton preforms a search on the list for the target value, and prints the number of checks it took to find the target.
"""
def linear_search_print(self, arr, target):
def linear_search_print(self, arr: Sequence[int], target: int) -> int:
checks = 0 #check count starts at 0
for value in arr:
if value == target:
@@ -44,7 +45,7 @@ class Search:
"""
This function is the same as the previous one, but instead of printing the number of checks, it returns the number of checks.
"""
def linear_search_count(self, arr, target):
def linear_search_count(self, arr: Sequence[int], target: int) -> int:
checks = 0
for value in arr:
checks += 1
@@ -55,7 +56,13 @@ class Search:
"""
This function uses recursive searching.
"""
def binary_search_recursive(self, arr, target, low, high):
def binary_search_recursive(
self,
arr: Sequence[int],
target: int,
low: int,
high: int,
) -> int:
if low > high:
return -1 #base case: if low is greater than high, the target is not found
@@ -68,7 +75,7 @@ class Search:
else:
return self.binary_search_recursive(arr, target, low, mid - 1)
def main():
def main() -> None:
search = Search() #search object to call Search() class
"""