Skip to content

Instantly share code, notes, and snippets.

View h2rashee's full-sized avatar

Harris Rasheed h2rashee

View GitHub Profile
@h2rashee
h2rashee / UTFstream.java
Created August 26, 2015 03:46
Given a stream of UTF8 bytes, verify if it's valid
class UTFstream
{
// Given a stream of UTF8 bytes, verify if it's valid
public static boolean validateUtf8(byte[] stream) {
boolean expectedBytes = 0;
for(int i = 0; i < stream.length; i++) {
if(expectedBytes > 0) {
// 10xx xxxx
if(stream[i] >= 0x80 && stream[i] <= 0xBF) {
@h2rashee
h2rashee / tictactoe.py
Last active August 29, 2015 14:02
2-player Tic-Tac-Toe
#!/usr/bin/env/python
grid = [' '] * 9
curTurn = 'O'
#Print the X and O grid representing current game state
def displayBoard(board):
print "%s | %s | %s" % (board[0], board[1], board[2])
print "---------"
print "%s | %s | %s" % (board[3], board[4], board[5])
@h2rashee
h2rashee / emailProcess.cc
Created June 6, 2014 19:24
Processes e-mail lists and extracts e-mails from: "Name" (Email); ...
#include <iostream>
#include <vector>
using namespace std;
vector<string> emails;
void printVector()
{
for(vector<string>::iterator it = emails.begin(); it != emails.end(); it++)
@h2rashee
h2rashee / fib.py
Last active August 29, 2015 14:02
Fibonacci Numbers
#!/usr/bin/env/python
n = input('Please specify n for the nth fibonacci number: ')
#Base Case
fiblist = [0,1]
#Build list up until n
#+1 for off-by-one adjustment
while len(fiblist) < n+1:
@h2rashee
h2rashee / Problem.java
Last active August 29, 2015 14:07
Re-arrange Number Bit Positions
/************************************************************************
Problem:
Given a number of an unspecified type, consider it's binary representation.
Given a configuration, make the necessary changes and find the resulting number.
Example:
typeX num = 0x55
@h2rashee
h2rashee / rearrange
Last active August 29, 2015 14:07
Elegant Re-arrangement of a Number's Bit Positions
// Pseudo code
// See original problem at https://gist.github.com/h2rashee/3b93f99ee78d16804740
// Proposed by Mike Li
int order[] = [7,6,5,4,3,2,1,0];
long x = 85;
int main() {
long y = 0;
for (int i = 0; i < order.length(); i++) {
@h2rashee
h2rashee / bit.cc
Last active August 29, 2015 14:08
Modifying bit positions of an integer
#include <stdio.h>
#define BIT(x) (0x1 << x)
int set(int val, int pos)
{
return (val |= BIT(pos));
}
int unset(int val, int pos)
@h2rashee
h2rashee / Coolness.java
Last active August 29, 2015 14:08
Find the coolness of a number; coolness is number of zeros contained in the decimal representation of the number
import java.io.*;
import java.util.*;
class Coolness
{
public static void main(String[] args)
{
long num;
long num1 = Long.parseLong(args[0]);
/*
Problem 1: Given n, which is the length of a set starting from 'a', ex n = 3, set = { 'a', 'b', 'c' },
return all possible permutations from the full set in dictionary order.
Ex input: n = 3
Ex output: abc acb bac bca cab cba
1 2 3 4 5 6
*/
@h2rashee
h2rashee / tauqirify.py
Last active August 29, 2015 14:08
Given a stream of text, append " ..." to the end of each word and separate words with a newline
import fileinput
for line in fileinput.input():
for word in line.split():
print word + " ..."