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 / plot_map.R
Created February 27, 2018 12:15
Map of women's suffrage in Europe #DataIsBeautiful #1
library(ggplot2)
library(grid)
library(rworldmap)
library(grid)
# Data sources:
# https://en.wikipedia.org/wiki/Timeline_of_women%27s_suffrage
# http://womensuffrage.org/?page_id=97
@oskar-j
oskar-j / MonteCarloToolbox.java
Created October 10, 2016 08:46
Monte carlo simulation of the PI value
package model;
public class MonteCarloToolbox {
public static boolean[] dartThrow(int r, int d) {
boolean[] booleanArray = new boolean[d];
for (int i = 0; i < d; i++) {
double xCoord = Math.random() * r;
double yCoord = Math.random() * r;
// condition check
@oskar-j
oskar-j / dataset2.py
Created September 16, 2015 09:52
Downloading comments from a particular youtube video
#!/usr/bin/python
# Usage example:
# python comments.py --videoid='<video_id>' --text='<text>'
import httplib2
import os
import sys
from apiclient.discovery import build_from_document
@oskar-j
oskar-j / swaps_needed.py
Created July 31, 2015 11:22
Tells whether max 1 swap is needed to sort an array in Python
from itertools import tee, izip
from copy import copy
def pairwise(seq):
a, b = tee(seq)
b.next()
return izip(a, b)
@oskar-j
oskar-j / perfect_squares.py
Created July 31, 2015 11:19
Find perfect squares in a range of (A,B) in Python
import math
def solution(A, B):
bottom = int(math.ceil(math.sqrt(A)))
upper = int(math.floor(math.sqrt(B)))
# int should be enough
return upper - bottom + 1
@oskar-j
oskar-j / dominant.py
Created July 29, 2015 20:53
Finds a dominant element of array in Python
# Majority Element of an Array (Moore’s Algorithm)
# If n is odd then it should appear at least (n+1)/2 times
# If n is even then it should appear at least (n/2) + 1 times
def dominant(A):
x = None
count = 0
for i in A:
@oskar-j
oskar-j / Asymetry.java
Last active August 29, 2015 14:26
Find a point in an array of integers in Java, which divides an array into 2 halfs making best asymetry
public class Asymetry {
public static int solution(int X, int[] A) {
int n = -1;
int right = A.length - 1;
int left = 0;
int rightCounter = 0;
int leftCounter = 0;
@oskar-j
oskar-j / MaxSliceSum.java
Created July 28, 2015 16:00
Find maximum sum in slices (subsequences) of an array in Java
public class MaxSliceSum {
public int solution(int[] A) {
int ans = A[0], sum = 0;
for (int i = 0; i < A.length; i++) {
if (sum > 0) {
sum += A[i];
} else {
sum = A[i];
}
ans = Math.max(ans, sum);
@oskar-j
oskar-j / palindrome.py
Created July 24, 2015 16:23
Different ways to check if string is a palindrome in Python
from timeit import timeit
def is_palindrome(input):
return input == reduce(lambda x, y: y + x, input)
def is_palindrome2(input):
start = stop = None
step = -1
@oskar-j
oskar-j / samepairs.py
Last active August 29, 2015 14:25
Find max distance (highest difference between indexes) in pairs from a Python list
from collections import defaultdict
def get_distance(l):
assert len(l) > 0
return l[-1] - l[0] # takes O(1) time
def solution(A):
distances = defaultdict(list)