first problems
This commit is contained in:
@@ -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 []
|
||||
@@ -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"])
|
||||
@@ -0,0 +1,8 @@
|
||||
from Solution import createDataframe
|
||||
|
||||
print(createDataframe([
|
||||
[1, 15],
|
||||
[2, 11],
|
||||
[3, 11],
|
||||
[4, 20]
|
||||
]))
|
||||
@@ -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])
|
||||
@@ -0,0 +1,5 @@
|
||||
import Solution
|
||||
|
||||
s = Solution.Solution()
|
||||
|
||||
print(s.addBinary("11", "1"))
|
||||
Reference in New Issue
Block a user