This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Minimal character-level Vanilla RNN model. Written by Andrej Karpathy (@karpathy) | |
BSD License | |
""" | |
import numpy as np | |
# data I/O | |
data = open('input.txt', 'r').read() # should be simple plain text file | |
chars = list(set(data)) | |
data_size, vocab_size = len(data), len(chars) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" Trains an agent with (stochastic) Policy Gradients on Pong. Uses OpenAI Gym. """ | |
import numpy as np | |
import cPickle as pickle | |
import gym | |
# hyperparameters | |
H = 200 # number of hidden layer neurons | |
batch_size = 10 # every how many episodes to do a param update? | |
learning_rate = 1e-4 | |
gamma = 0.99 # discount factor for reward |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from subprocess import Popen, PIPE | |
from time import sleep | |
# run the shell as a subprocess: | |
p = Popen(['python', 'shell.py'], | |
stdin = PIPE, stdout = PIPE, stderr = PIPE, shell = False) | |
# issue command: | |
p.stdin.write('command\n') | |
# let the shell output the result: | |
sleep(0.1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int find_min_diff(vector<int>& A, vector<int>& B) { | |
if(A.empty() || B.empty()) return -1; | |
int m = A.size(), n = B.size(); | |
int i = 0, j = 0; | |
int min_diff = INT_MAX; | |
while(i < m && j < n) { | |
int diff = abs(A[i] - B[j]); | |
min_diff = min(min_diff, diff); | |
if(min_diff == 0) return 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://stackoverflow.com/questions/5212037/find-the-pair-across-2-arrays-with-kth-largest-sum | |
// result may not be correct!!!!!!!!!!!!!!!!! | |
// 6/6/2015. attempted to calculate time complexity seems to be: | |
// log1 + log2 + ... + logk = logk! = O(klogk) | |
// here is how to calculate logk! | |
// http://stackoverflow.com/questions/2095395/is-logn-%CE%98n-logn | |
// http://ballinger.disted.camosun.bc.ca/math126/lognfactorial.pdf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void dfs(int start, int idx, int a[], int N, bool *visited, int& len) { | |
if(idx >= N) { | |
len = 1; | |
return; | |
} | |
visited[idx] = true; | |
len++; | |
//if(start == a[idx]) return; | |
if(visited[a[idx]] == false) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given an array of integers where each element points to the index of the next element | |
how would you detect if there is a cycle in this array | |
Note: if the element of array is in range[0, n-1], where n is the length of array, then there must be at least one cycle. | |
Thus, the element may be negative or out of range[0,n-1] | |
/* | |
bool has_cycle_in_array(int a[], int N) { | |
if(N == 1) return false; | |
int slow = 0, fast = 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// tournament tree |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// threaded binary tree | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define MAXV 1000 | |
bool processed[MAXV+1]; | |
bool discovered[MAXV+1]; | |
int parent[MAXV+1]; | |
struct edgenode { | |
int y; | |
int weight; | |
edgenode *next; |
NewerOlder