Skip to content

Instantly share code, notes, and snippets.

View ta1hia's full-sized avatar
🐖

tahia ta1hia

🐖
View GitHub Profile
#include <stdio.h>
#include <stdlib.h>
int main() {
int nbytes, ncases, i, j, k;
char c, res;
char ** msgs;
scanf("%d", &ncases);
msgs = malloc(ncases * sizeof(char *));
@ta1hia
ta1hia / quickest_route.c
Created November 10, 2015 23:00
given a time-table, a starting city, a starting time and a destination city, how would you compute the soonest you could get to the destination city?
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define N_CITIES 4
typedef enum{A, B, C, D, None=-1}city;
typedef struct {
int start;
@ta1hia
ta1hia / maze.c
Created November 10, 2015 19:20
given a 2d array of black and white entries representing a maze with designated entrance and exit points, find a path from the entrance to the exist, if one exists.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define N 5
typedef enum{BLACK, WHITE, START, END} tile;
typedef enum{UNDISCOVERED, DISCOVERED, PROCESSED}graph_state;
typedef struct {
@ta1hia
ta1hia / qsort_3parts.c
Created November 10, 2015 03:52
qsort with 3 way partitions (<T, ==T, >T)
#include <stdio.h>
void qsort3(int *X, int L, int U) {
int T, M, E, i, tmp;
if (L < U) {
T = X[L];
M = L;
E = U;
for (i = L+1; i < E; i++) {
if (X[i] < T) {
@ta1hia
ta1hia / detect_cycles.py
Last active November 9, 2015 01:13
given adjacency matrix, detect # of cycles in directed graph
def find_cycles(g, x, path, num_cycles):
if x in path:
num_cycles+=1
return num_cycles
path.append(x)
N = len(g[x])
for i in range(N):
if g[x][i]:
@ta1hia
ta1hia / graph.py
Last active November 9, 2015 00:18
# graph.py
class Vertex:
def __init__(self, x):
self.x = x
self.edges = {}
def __str__(self):
return "Vertex %d: %s" % (self.x, self.edges)
@ta1hia
ta1hia / graph.c
Created November 8, 2015 18:39
adjacency list implementation in c
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#define N 10
typedef struct {
int y;
int weight;
struct Edge *next; /* next in adj list */
@ta1hia
ta1hia / bst.py
Last active November 8, 2015 04:45
class Node:
def __init__(self, data):
self.data = data
self.lchild = None
self.rchild = None
def insert(self, key):
if key < self.data:
if self.lchild:
self.lchild.insert(key)
@ta1hia
ta1hia / ll.c
Created November 7, 2015 20:08
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
struct Node *prev;
struct Node *next;
int data;
} Node;
#include <stdio.h>
void qsort(int *X, int L, int U) {
int T, M, i, tmp;
if (L < U) {
T = X[L];
M = L;
for (i = L+1; i < U; i++) {
if (X[i] < T) {
M += 1;