Skip to content

Instantly share code, notes, and snippets.

View woodRock's full-sized avatar
🫖
Activate zen mode (CTRL + K, Z)

Jesse Wood woodRock

🫖
Activate zen mode (CTRL + K, Z)
View GitHub Profile
@woodRock
woodRock / 184.md
Created February 5, 2019 10:08
Describe what happens when you type a URL into your browser and press Enter.
  1. You enter a URL into a web browser
  2. The browser looks up the IP address for the domain name via DNS
  3. The browser sends a HTTP request to the server
  4. The server sends back a HTTP response
  5. The browser begins rendering the HTML
  6. The browser sends requests for additional objects embedded in HTML (images, css, JavaScript) and repeats steps 3-5.
  7. Once the page is loaded, the browser sends further async requests as needed.
@woodRock
woodRock / 184.rb
Last active March 7, 2020 10:49
This problem was asked by Amazon. Given n numbers, find the greatest common denominator between them. For example, given the numbers [42, 56, 14], return 14.
def gcd a, b
return a if b == 0
return gcd(b, a % b)
end
def gcd_in_list list
list = list.sort
grt = 0
list.each do |i|
grt = gcd(i, grt)
# Sudoku is a puzzle where you're given a partially-filled 9 by 9 grid with digits. The objective is to fill the grid with the constraint that every row, column, and box (3 by 3 subgrid) must contain all of the digits from 1 to 9.
# A Backtracking program in Pyhton to solve Sudoku problem
def line():
return '*' * 13
# A Utility Function to print the Grid
def print_grid(arr):
@woodRock
woodRock / daily-coding-problem-53.rb
Created September 27, 2018 14:30
Implement a queue using two stacks. Recall that a queue is a FIFO (first-in, first-out) data structure with the following methods: enqueue, which inserts an element into the queue, and dequeue, which removes it.
#!/usr/bin/env ruby
class Queue
def initialize
@s1,@s2 = [],[]
end
def enqueue e
while not @s1.empty?
@s2 << @s1.last()
@woodRock
woodRock / daily-coding-problem-49.rb
Created September 24, 2018 07:00
Given an array of numbers, find the maximum sum of any contiguous subarray of the array. For example, given the array [34, -50, 42, 14, -5, 86], the maximum sum would be 137, since we would take elements 42, 14, -5, and 86. Given the array [-5, -1, -8, -9], the maximum sum would be 0, since we would not take any elements. Do this in O(N) time.
#!/usr/bin/env ruby
def max_sum(array)
max_so_far = 0
max_ending_here = 0
array.each do |i|
max_ending_here += i
max_so_far = max_ending_here if max_so_far < max_ending_here
max_ending_here = 0 if max_ending_here < 0
end
@woodRock
woodRock / daily-coding-problem-47.rb
Last active September 24, 2018 07:00
Given a array of numbers representing the stock prices of a company in chronological order, write a function that calculates the maximum profit you could have made from buying and selling that stock once. You must buy before you can sell it. For example, given [9, 11, 8, 5, 7, 10], you should return 5, since you could buy the stock at 5 dollars …
def maximum_profit stock
max = 0
stock.each_with_index do |buy, i|
(i+1...stock.size).each do |sell|
profit = stock[sell] - buy
max = profit if profit > max
end
end
return max
end
@woodRock
woodRock / daily-coding-problem-51.rb
Last active March 11, 2021 00:16
Given a function that generates perfectly random numbers between 1 and k (inclusive), where k is an input, write a function that shuffles a deck of cards represented as an array using only swaps. It should run in O(N) time. Hint: Make sure each one of the 52! permutations of the deck is equally likely.
#!/usr/bin/env ruby
def deck
ranks = ['A',*(2..10),'J','Q','K']
suits = ['♠','♦','♣','♥']
deck = []
suits.product(ranks).map { |rank, suit| deck << "#{rank}#{suit}" }
deck
end
@woodRock
woodRock / daily-coding-problem-50.rb
Created September 23, 2018 04:17
Suppose an arithmetic expression is given as a binary tree. Each leaf is an integer and each internal node is one of '+', '−', '∗', or '/'. Given the root to such a tree, write a function to evaluate it. For example, given the following tree: * / \ + + / \ / \ 3 2 4 5 You should return 45, as it is (3 + 2) * (4 + 5).
#!/usr/bin/env ruby
class Node
attr_accessor :val, :left, :right
def initialize val, left=nil, right=nil
@val = val
@left = left
@right = right
end
end
@woodRock
woodRock / daily-coding-problem-48.rb
Created September 22, 2018 05:04
Given pre-order and in-order traversals of a binary tree, write a function to reconstruct the tree. # For example, given the following preorder traversal: # [a, b, d, e, c, f, g] # And the following inorder traversal: # [d, b, e, a, f, c, g] # You should return the following tree: # a # / \ # b c # / \ / \ # d e f g
#!/usr/bin/env python
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def __str__(self, level=0):
ret = "\t"*level+repr(self.data)+"\n"
def all_palindromes(word)
q = "@#"
q += word.chars.join("#")
q += "\#$"
puts q
pld = [0] * q.size()
c,r = 0,0
search = (1...q.size())
search.each do |i|
mirror = c - (i - c)