Skip to content

Instantly share code, notes, and snippets.

View oskar-j's full-sized avatar
🖖
Live long and prosper.

Oskar Jarczyk oskar-j

🖖
Live long and prosper.
View GitHub Profile
@oskar-j
oskar-j / titanic.R
Last active August 29, 2015 14:22
Make classifier for Titanic passengers and try to guess who survives the catastrophe
setwd("C:/big data/kaggle/titanic/")
library(ggplot2)
library(caret)
library(doParallel)
library(randomForest)
library(stringr)
registerDoParallel(detectCores())
@oskar-j
oskar-j / mysolv.m
Created June 5, 2015 21:09
LinkedIn riddle
function f = eq(z)
x=z(1);
y=z(2);
s=z(3);
f(1)=3*x - 24;
f(2)=x + y - 25;
f(3)=y - s - 8;
end
guess=[2 3 4]
@oskar-j
oskar-j / subset_sum.py
Last active August 29, 2015 14:23
Find smallest set of i*2 or i+1 subitems which sum makes N
# you can use print for debugging purposes, e.g.
# print "this is a debug message"
def solution(N):
q = [(1,)]
visited = set()
for seq in q:
s = sum(seq)
if s == N:
@oskar-j
oskar-j / heatmaps.R
Created July 6, 2015 23:11
Heatmaps for 3x3 game matrices in R language
library(ggplot2)
library(reshape2)
# Multiple plot function
#
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols: Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
@oskar-j
oskar-j / amplitudes.py
Created July 15, 2015 11:34
Check amplitude of a tree in Python language (recursive)
def solution(T):
amplitudes = []
if (T[1] is None and T[2] is None):
return 0
if (T[1] is not None):
walk(T[1], T[0], T[0], amplitudes)
if (T[2] is not None):
walk(T[2], T[0], T[0], amplitudes)
return max(amplitudes)
@oskar-j
oskar-j / count_inversions_io.py
Created July 19, 2015 22:01
Count Inversions in an array with Python 2.7
import timeit
def get_table():
table = []
with open("IntegerArray.txt", 'r') as f:
for line in f:
table.append(int(line))
return table
@oskar-j
oskar-j / alive.py
Last active August 29, 2015 14:25
Find the year with the most number of people alive in Python
from collections import Counter
from itertools import chain
import timeit
from bisect import insort
import numpy as np
from collections import namedtuple
def get_data():
return [(1920, 1939), (1911, 1944), (1920, 1955), (1938, 1939), (1937, 1940),
@oskar-j
oskar-j / quicksort.py
Created July 21, 2015 17:44
Quicksort a list in Python language
def quicksort(arr):
""" Quicksort a list
:type arr: list
:param arr: List to sort
:returns: list -- Sorted list
"""
if not arr:
return []
@oskar-j
oskar-j / distance_pairs.py
Last active August 29, 2015 14:25
Find the monotonic pair whose indices are the furthest apart in Python
# method helps with fact that enumerate from reversed
# won't yield reversed indexes
def reverse_enum(L):
# guaranteed to NOT make any extra copy or search operations, O(1)
for index in reversed(xrange(len(L))):
yield index, L[index]
def solution(A):
top = list()
@oskar-j
oskar-j / min_avg_slice.py
Created July 21, 2015 22:21
Find lowest index of array's slices having lowest average of elements (in Python)
def solution(A):
min_avg_value = (A[0] + A[1])/2.0 # The mininal average
min_avg_pos = 0 # The begin position of the first
# slice with mininal average
for index in xrange(0, len(A)-2):
# Try the next 2-element slice
if (A[index] + A[index+1]) / 2.0 < min_avg_value:
min_avg_value = (A[index] + A[index+1]) / 2.0
min_avg_pos = index