Skip to content

Instantly share code, notes, and snippets.

@hunj
hunj / path_stripper.rb
Created June 25, 2015 00:59
Strip path from domain, off from sitemap xml file.
def path_strip(input_file, domain, output_file)
raise "domain must be string form" unless domain.is_a? String
raise "invalid input file name" unless input_file.is_a? String
raise "invalid output file name" unless output_file.is_a? String
file = File.open(input_file, "r")
data = file.read
file.close
data_lines = data.lines
cleared_arr = []
@hunj
hunj / path_strip.rb
Created July 7, 2015 04:28
Strip path from domain
# path_strip(input_file, domain, output_file)
# imports an xml file containing paths of the website's pages,
# strips all unnecessary strings except for the path of the pages.
## Parameters:
# +input_file+ name of the input xml file
# +domain+ the domain of the URL to exclude in the result
# +output_file+ name of the output file, ending in .csv (preferred)
def path_strip(input_file, domain, output_file)
raise "domain must be string form" unless domain.is_a? String
raise "invalid input file name" unless input_file.is_a? String
@hunj
hunj / The Technical Interview Cheat Sheet.md
Last active August 29, 2015 14:28 — forked from tsiege/The Technical Interview Cheat Sheet.md
This is my technical interview cheat sheet. Feel free to fork it or do whatever you want with it. PLEASE let me know if there are any errors or if anything crucial is missing. I will add more links soon.

Studying for a Tech Interview Sucks, so Here's a Cheat Sheet to Help

This list is meant to be a both a quick guide and reference for further research into these topics. It's basically a summary of that comp sci course you never took or forgot about, so there's no way it can cover everything in depth. It also will be available as a gist on Github for everyone to edit and add to.

Data Structure Basics

###Array ####Definition:

  • Stores data elements based on an sequential, most commonly 0 based, index.
  • Based on tuples from set theory.
@hunj
hunj / dummy_generate.rb
Created September 24, 2015 01:12
Generates a dummy file full of 0's and 1's up to desired size
DESIRED_SIZE = 1073741824 # 1gb
FILE_NAME = "dummy" # name of the dummy file
dummy_file = File.new(FILE_NAME, "w")
while dummy_file.size < DESIRED_SIZE
dummy_file.write([0,1].sample)
end
dummy_file.close
@hunj
hunj / perfect_number.rb
Created February 7, 2016 20:04
perfect number!
def perfect_number? number
return number == divisors_of(number)
end
def divisors_of number
arr = []
(1..number).each do |iter|
if number % iter == 0
arr << iter
end
@hunj
hunj / gen.rb
Created March 6, 2016 13:39
Generate random numbers
require 'csv'
# Data generation
@data = {}
(0..100).each do |i|
@data[i] = {
:age => Random.rand(18...80),
:gender => Random.rand(0..1),
:prescription_history => []
@hunj
hunj / euler_003.rb
Last active June 15, 2016 04:17
Project Euler #003 solution, comparing with square root
require 'benchmark'
the_number = 600_851_475_143
# monkeypatching
class Fixnum
def prime? # O(n)
(2..(self - 1)).each do |divisor|
return false if self % divisor == 0
end
@hunj
hunj / compute_pi.py
Created November 8, 2016 21:31
Compute π using Nilakantha's series
from decimal import *
def compute_pi():
pi = Decimal(3.0)
curr_multiplier = Decimal(2)
iteration = Decimal(0)
load = Decimal(1)
getcontext().prec = 6
while 1:
@hunj
hunj / gist:8549840acc266c1e19761959bd028300
Created December 1, 2016 15:06
result of running `python pi_monte-carlo.py 1`
Calculated pi: 3.1411426428303537942242780347543442930366295786973371671458932366545818227278409801225153144143017877234654331791473934241780222527815976997124640580072509063632954119264908113514189273659207400925115639454931866483310413801725215651956494561820227528441055131891486435804475559444930616327040880110013751718964870608826103262907863482935366920865108138517314664333041630203775471933991748968621077634704338042255281910238779847480935116889611201400175021877734716839604950618827353419177397174646830853856732091511438929866233279159894986873359169896237029628703587948493561695211901487685960745093136642080260032504063007875984498062257782222777847230903862982872859107388423552944118014751843980497562195274409301162645330666333291661457682210276284535566945868233529191148893611701462682835354419302412801600200025003125390673834229278659832479059882485310663832979122390298787348418552319039879984998124765595699462432804100512564070508813601700212526565820727590948868608576072009001125140642580322540
@hunj
hunj / params.py
Created July 11, 2018 15:46
[Python] Inspect params inside a function
import inspect
frame = inspect.currentframe()
args, _, _, values = inspect.getargvalues(frame)
print('function name "%s"' % inspect.getframeinfo(frame)[2])
for i in args:
print(" %s = %s" % (i, values[i]))