diff --git a/easy/1 Two Sum/solution.py b/easy/1 Two Sum/solution.py new file mode 100644 index 0000000..336bd91 --- /dev/null +++ b/easy/1 Two Sum/solution.py @@ -0,0 +1,19 @@ +from typing import List + +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + for i in range(len(nums)): + for j in range(i + 1, len(nums)): + if nums[i] + nums[j] == target: + return [i, j] + + def twoSum_better(self, nums: List[int], target: int) -> List[int]: + num_dict = {} + + for i in range(len(nums)): + complement = target - nums[i] + if complement in num_dict: + return [num_dict[complement], i] + else: + num_dict[nums[i]] = i + return [] \ No newline at end of file diff --git a/easy/2877 Create a Dataframe from List/Solution.py b/easy/2877 Create a Dataframe from List/Solution.py new file mode 100644 index 0000000..74ede0b --- /dev/null +++ b/easy/2877 Create a Dataframe from List/Solution.py @@ -0,0 +1,4 @@ +import pandas as pd + +def createDataframe(student_data: list[list[int]]) -> pd.DataFrame: + return pd.DataFrame(student_data, columns=["student_id", "age"]) \ No newline at end of file diff --git a/easy/2877 Create a Dataframe from List/testing.py b/easy/2877 Create a Dataframe from List/testing.py new file mode 100644 index 0000000..ded8618 --- /dev/null +++ b/easy/2877 Create a Dataframe from List/testing.py @@ -0,0 +1,8 @@ +from Solution import createDataframe + +print(createDataframe([ + [1, 15], + [2, 11], + [3, 11], + [4, 20] +])) \ No newline at end of file diff --git a/easy/67 Add Binary/Solution.py b/easy/67 Add Binary/Solution.py new file mode 100644 index 0000000..45831db --- /dev/null +++ b/easy/67 Add Binary/Solution.py @@ -0,0 +1,25 @@ +class Solution: + def addBinary(self, a: str, b: str) -> str: + a = a[::-1] + b = b[::-1] + carry = 0 + c = "" + for i in range(max(len(a), len(b))): + d = carry + int(a[i]) + int(b[i]) + if d == 3: + c = c + "1" + carry = 1 + if d == 2: + c = c + "0" + carry = 1 + if d == 1: + c = c + "1" + carry = 0 + if d == 0: + c = c + "0" + carry = 0 + else: + print(d) + raise Exception + + return(c[::-1]) \ No newline at end of file diff --git a/easy/67 Add Binary/testing.py b/easy/67 Add Binary/testing.py new file mode 100644 index 0000000..ae39877 --- /dev/null +++ b/easy/67 Add Binary/testing.py @@ -0,0 +1,5 @@ +import Solution + +s = Solution.Solution() + +print(s.addBinary("11", "1")) \ No newline at end of file diff --git a/hard/1307 Verbal Arithmetic Puzzle/Solution.py b/hard/1307 Verbal Arithmetic Puzzle/Solution.py new file mode 100644 index 0000000..492508e --- /dev/null +++ b/hard/1307 Verbal Arithmetic Puzzle/Solution.py @@ -0,0 +1,35 @@ +class Solution(object): + def isSolvable(self, words, result): + """ + :type words: List[str] + :type result: str + :rtype: bool + """ + self.encode = {} + self.avail_keys = [1,2,3,4,5,6,7,8,9,10] + + for c in reversed(result): + for i in range(10): + self.encode[c]=i + + for word in words: + for l in word: + # print(f"{i} and {l}") + return True + + def is_letter_solvable(self, letters, result: int): + # can the letters add up to the result? + if len(letters) == 1: + if result in self.avail_keys: + self.encode[letters[0]] = result + self.avail_keys.remove(result) + return True + else: return False + + for c in letters: + if c in self.encode: + i = self.encode[c] + return self.is_letter_solvable(letters[1:], result - i) + else: + for i in self.avail_keys: + pass \ No newline at end of file diff --git a/hard/1307 Verbal Arithmetic Puzzle/testing.py b/hard/1307 Verbal Arithmetic Puzzle/testing.py new file mode 100644 index 0000000..a3d40a5 --- /dev/null +++ b/hard/1307 Verbal Arithmetic Puzzle/testing.py @@ -0,0 +1,9 @@ +import Solution + +s = Solution.Solution() + +print(s.isSolvable(["SEND","MORE"], "MONEY")) + +print(s.isSolvable(["SIX","SEVEN","SEVEN"], "TWENTY")) + +print(s.isSolvable(["LEET","CODE"], "POINT")) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..1411a4a --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pandas \ No newline at end of file