Skip to content

Instantly share code, notes, and snippets.

View ShiangYong's full-sized avatar

Shiang Yong Looi ShiangYong

View GitHub Profile
@ShiangYong
ShiangYong / main.py
Created April 15, 2017 22:08
Simulation code to solve the Riddler Classic puzzle (Supreme Court Judges),https://fivethirtyeight.com/features/how-many-bingo-cards-are-there-in-the-world/
import numpy as np
def runSimulation(N):
judge_num = np.zeros(N, dtype=np.int)
for i in range(9):
judge_num[:(np.random.randint(0, 41))] += 1
for i in range(4, N, 4):
if np.random.random() > 0.5:
@ShiangYong
ShiangYong / four_square_ranches.py
Created March 19, 2017 18:16
Monte Carlo code to solve the Four Square Ranches (Riddler Classic), https://fivethirtyeight.com/features/can-you-find-the-honest-prince/
import numpy as np
import time
import matplotlib.pyplot as plt
def isConvex(dx, dy):
for j in range(4):
if dx[j]*dy[(j+1) % 4] < dy[j]*dx[(j+1) % 4]:
return False
return True
@ShiangYong
ShiangYong / strategy.txt
Created March 16, 2017 05:21
Optimal Strategy and proof for the Space Race (Riddler Classic), https://fivethirtyeight.com/features/who-will-win-the-space-race/
The player that places first is guaranteed to win, as long as he/she uses the optimal strategy.
Winning Strategy
----------------
1. First player begins by placing the first coin right right at the center of the table
2. Second player places a coin anywhere on the table
3. First player tries to mimic second player's coin in the following way:
Imagine rotating the table about the center by 180 degrees, then place the coin where the second player's coin was before the rotation.
Done correctly, this newly placed coin makes the table look identical under a 180 degrees rotation about the center.
4. If there is no more space on the table, first player wins. Otherwise go back to Step 2.
@ShiangYong
ShiangYong / mnist.py
Created January 13, 2017 07:12
Deep convolutional neural network to solve MNIST classification problem. Adapted code from Anuj Shah's tutorial https://www.youtube.com/watch?v=yDVap0lpYKg.
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten, normalization, Convolution2D, MaxPooling2D
from keras.utils import np_utils
import matplotlib.pyplot as plt
from sklearn import metrics
import numpy as np
# input image dimensions
@ShiangYong
ShiangYong / stormtroopers.nb
Created December 30, 2016 02:14
Mathematica module to solve the Stormtroopers puzzle for general N>9, http://fivethirtyeight.com/features/build-your-own-death-star-and-defeat-the-stormtroopers/
calcLimitingProb[K_] :=
Module[{p, n, plose, pdrop, pwin, tmp, solution},
p = {(999 K)/(1000 + 999 K)};
solution = {N[1000/999]};
For[n = 2, n <= 20, n++,
pdrop = (K*Sqrt[n]/1000)*(999/1000)^n;
plose = 1 - (999/1000)^n + (1 - p[[n - 1]])*pdrop;
pwin = p[[n - 1]]*pdrop;
tmp = Simplify[pwin/(plose + pwin)];
import math
import matplotlib.pyplot as plot
def generate_path(speed, num_steps):
scaled_step = speed/num_steps
step = 1.0/num_steps
ram_x = [0] * (num_steps + 1)
ram_y = [0] * (num_steps + 1)
@ShiangYong
ShiangYong / main.py
Created December 6, 2016 03:04
Python code to solve the Secret Santa puzzle (Riddler Classic) from http://fivethirtyeight.com/features/can-you-unmask-the-secret-santas/
import numpy as np
import math
import matplotlib.pyplot as plot
import time
def gen_rand_derang(n):
comparison = np.arange(n)
sigma = np.random.permutation(n)
@ShiangYong
ShiangYong / main.py
Last active December 4, 2016 17:56
Python code to solve the Lonesome King problem, http://fivethirtyeight.com/features/the-puzzle-of-the-lonesome-king/
import statistics
import random
import numpy as np
import time
def repeated_cull_sim(initial_size, runs):
sum = 0
init_time = time.time()