Skip to content

Wesmoody #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# CS Build Week 2: Interview Prep

This is a general repo that you can fork for holding any artifacts you
might need to submit during the week. Feel free to make subdirectories
here as necessary.
=======
# CS Build Week 2

For all details about the week, [check out the page in
TrainingKit](https://learn.lambdaschool.com/cs/sprint/reco0t22NdXmr8VyL).

## Taking the opportunity to save my code challenge practice.

Some of the problems are done in both python and Javascript. Some were solved more than one way.
>>>>>>> 6c54ba4bca544d7403a83a6c6d5edf803644a1b2
39 changes: 39 additions & 0 deletions add-two-numbers/add-two-numbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var addTwoNumbers = function (l1, l2) {
let sum = 0;
let curr = new ListNode(0);
let res = curr;

while (l1 || l2) {
if (l1) {
sum += l1.val;
l1 = l1.next;
}
if (l2) {
sum += l2.val;
l2 = l2.next;
}

curr.next = new ListNode(sum % 10);
curr = curr.next;

sum = sum > 9 ? 1 : 0;
}

if (sum) {
curr.next = new ListNode(sum);
}

return res.next;
};
33 changes: 33 additions & 0 deletions add-two-numbers/add-two-numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
# convert the linked lists to integers
def conv_int(l: ListNode):
s = ""
while l != None:
s += str(l.val)
l = l.next
return int(s[::-1])

# convert sum of integers to a linked list
def to_list(n: int):
s = str(n)[::-1]
head = prev = None
for ch in s:
node = ListNode(int(ch))
if prev is not None:
prev.next = node
prev = node
if head is None:
head = prev
return head

# return the sum of the two linked lists
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
a = Solution.conv_int(l1)
b = Solution.conv_int(l2)
return Solution.to_list(a + b)

23 changes: 23 additions & 0 deletions contains-dups/contains-dups.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @param {number[]} x
* @return {boolean}
*/

var containsDuplicate = function (x) {
// create a set b/c sets cannot contain dups
let numbers = new Set();
// iterate over the array
for (let num of x) {
// get to a number, check if it's already in the set
if (!numbers.has(num)) {
// if it's not in the set add the number to the set
numbers.add(num);
// but if you see it was already added to the set
} else {
// signal the array contains duplicates
return true;
}
}
// if code runs and no duplicates are found, return false
return false;
};
20 changes: 20 additions & 0 deletions contains-dups/contains-dups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* @param {number[]} nums
* @return {boolean}
*/
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:

# First pass solution not very efficient
checked = set()

for number in nums:
if number in checked:
return True
checked.add(number)
return False



# Single line solution, slightly more efficient
return len(set(nums)) < len(nums)
18 changes: 18 additions & 0 deletions find-duplicate-num.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution:
def findDuplicate(self, nums: List[int]) -> int:
# create a set b/c sets can't have dups
num_set = set()
no_dups = -1

# loop through the list
for i in range(len(nums)):
# check for number that is repeated
if nums[i] in num_set:
# return that number
return nums[i]
#. if the number is not repeating
else:
# add it to the set
num_set.add(nums[i])

return no_dups
18 changes: 18 additions & 0 deletions first_not_repeating_character.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
s = "abacabad"

def first_not_repeating_character(s):

nothing = s
# Check that the string exists
while len(nothing) > 0:
# Evaluate the character at index 0
curr = nothing[0]
# Iterate through entire string, looking for char[0] again
if curr in nothing[1:]:
# If it repeats, remove all instances from the string
nothing = nothing.replace(curr, '')
# If the char doesn't repeat, return the char[0]
else:
return curr
# If there are only repeating characters, per instructions return '_'
return '_'
22 changes: 22 additions & 0 deletions longest-substring-wout-dups/longest-substring-wout-dups.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function(s) {
let start = 0, res = 0;
let charsInWindow = new Set();

for (let end = 0; end < s.length; end++) {
let entering = s[end];
console.log('test', entering)

while (charsInWindow.has(entering)) {
charsInWindow.delete(s[start++]);
}
charsInWindow.add(s[end]);
console.log('look', s[end])
res = Math.max(res, end - start + 1);
console.log(res, end - start + 1)
}
return res;
}
14 changes: 14 additions & 0 deletions longest-substring-wout-dups/longest-substring-wout-dups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def lengthOfLongestSubstring(self, s):
hash_table = {}
maxLength = 0
start = 0

for i in range(len(s)):

if (s[i] in hash_table):
start = max(start, hash_table[s[i]]+1)

maxLength = max(maxLength,i-start+1)
hash_table[s[i]]=i

return maxLength
23 changes: 23 additions & 0 deletions lowest-common-ancestor/lowest-common-ancestor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
if root==None: #Base Case
return None

if root==p or root==q:
return root
left = self.lowestCommonAncestor(root.left,p,q) #Check Left Nodes
right = self.lowestCommonAncestor(root.right,p,q) #Check Right Nodes

if left!=None and right!=None: #Pass Node to root
return root
if left==None and right==None: #Pass None to root
return None

return left if left else right
33 changes: 33 additions & 0 deletions maximum-depth-binary-tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def height(self, root) -> int: # indicates return will be an integer
# Check for a value in root node
if root is None:
# if root node is empty return 0
return 0
else:
# if root node is not empty, find max height on either left or right side of tree and add one
# return the value
return 1 + max(self.height(root.left), self.height(root.right))


def isBalanced(self, root: TreeNode) -> bool: # indicates return will be a boolean
# check for a value in root node
if root == None:
# empty root node == balanced => return true
return True
# if it's not empty, find the height of the left and right sides then compare them to find if they're balanced and return the boolean
else: return abs(self.height(root.left) - self.height(root.right)) <= 1 and (self.isBalanced(root.left) and self.isBalanced(root.right))








26 changes: 26 additions & 0 deletions merge-2-sorted-lists/merg-2-sorted-lists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var mergeTwoLists = function(l1, l2) {
function merge(left, right) {
if(!left) return right;
if(!right) return left;

if(left.val < right.val) {
left.next = merge(left.next, right);
return left;
}
right.next = merge(left, right.next);
return right;
}
return merge(l1, l2);
};
45 changes: 45 additions & 0 deletions merge-2-sorted-lists/merge-2-sorted-lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
# initialize head to list node 0
head = ListNode(0)
ptr = head

while True:
# check for nodes in both lists
if l1 is None and l2 is None:
# if both lists are empty, loop is done
break
# if the first list is empty but second is not
elif l1 is None:
# return all of the second list
ptr.next = l2
break
# if the second list is empty but first is not
elif l2 is None:
# return all of first list
ptr.next = l1
break
else:
# set temporary head of 0
smallerVal = 0
# check if value of next node in l1 is less than in l2
if l1.val < l2.val:
smallerVal = l1.val
# if that's the case, insert into new list and move pointer to next
l1 = l1.next
else:
# if value in l2 is greater than in l1, insert into new list
smallerVal = l2.val
# then move the pointer to the next node in l2
l2 = l2.next

newNode = ListNode(smallerVal)
ptr.next = newNode
ptr = ptr.next

return head.next
27 changes: 27 additions & 0 deletions palindrome-number/palindrome-number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function(x) {
// quickly check if negative
if (x < 0) {
// if yes, return false
return false
}
// check if x is the same as it's reversed form
return x === reversedInteger(x);

};
//
var reversedInteger = function(x) {
// initialize variable starting as 0
let reversed = 0;
// checking if you have positive integer
while (x > 0) {
// variable started as 0, * 10 is 0 then add next int of x % 10
reversed = (reversed * 10) + (x % 10);
// remove last number of given input
x = Math.floor(x / 10);
}
return reversed;
};
Loading