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
#include <stdio.h> | |
#define N 20 | |
#define K 5 | |
// reverses letters in indices s[from:to] | |
void reverse_string(char * s, int from, int to) | |
{ | |
char * p1 = s + from; | |
char * p2 = s + to; |
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
import random | |
class Trie: | |
def __init__(self): | |
self.children = [None]*10 | |
def insert(self, i, n): | |
if n == 1: | |
self.children[i] = True | |
else: | |
d = i / 10**(n-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
#include <stdlib.h> | |
void shuffle(int *array, size_t n) | |
{ | |
if (n > 1) { | |
size_t i; | |
for (i = 0; i < n - 1; i++) { | |
size_t j = i + rand() / (RAND_MAX / (n - i) + 1); | |
int t = array[j]; | |
array[j] = array[i]; |
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
#include <stdio.h> | |
#include <string.h> | |
int main() { | |
char s[] = "hello world!"; | |
int n = strlen(s); | |
// reverse string in-place. | |
int i, j, t; |
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 sample(int * samples, size_t n, size_t N) { | |
size_t t = 0, m = 0; | |
while(m < n) | |
{ | |
double u = rand() / (double)RAND_MAX; | |
if((N-t)*u >= n-m) t++; | |
else samples[m++] = t++; | |
} | |
} |
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
import random | |
num_sticks = 22 | |
num_states = num_sticks + 6 | |
num_actions = 3 | |
action_list = range(num_actions) | |
num_iterations = 10000 | |
gamma = 0.1 | |
alpha = 0.5 |
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
#include <stdio.h> | |
typedef unsigned int UINT; | |
// does not handle overflow or negative numbers | |
UINT divide_naive(UINT a, UINT b) | |
{ | |
UINT result; | |
while (b < a) { | |
UINT k=0, b2k = b; | |
while(b2k << 1 < a) |
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
#include <iostream> | |
#include <string> | |
#include <map> | |
#include <algorithm> | |
#include <climits> | |
// using STL MAP | |
bool duplicate_check_maphist(std::string s) | |
{ | |
std::map<char, int> h; |
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
#include <iostream> | |
void build_histogram(std::string s, int * m) { | |
size_t len = s.length(); | |
for(int i = 0; i < len; ++i) | |
m[s[i]] +=1; | |
} | |
bool check_permutation(std::string s1, std::string s2) | |
{ |
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
#include <iostream> | |
#include <cstdio> | |
#define DIM 5 | |
void swap(int s[DIM][DIM], int i, int j, int k, int l) { | |
char c = s[i][j]; | |
s[i][j] = s[k][l]; | |
s[k][l] = c; | |
} |
OlderNewer