Skip to content

Instantly share code, notes, and snippets.

View rfaisal's full-sized avatar

Faisal Rahman rfaisal

  • Facebook
  • Vancouver, BC
View GitHub Profile
@rfaisal
rfaisal / LittleElephantLemonade.java
Created September 8, 2013 23:31
Little Elephant likes lemonade. When Little Elephant visits any room, he finds the bottle of the lemonade in that room that contains the greatest number of litres of lemonade and drinks it all. There are n rooms (numbered from 0 to n-1), each contains Ci bottles. Each bottle has a volume (in litres). The first room visited by Little Elephant was…
import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;
public class LittleElephantLemonade {
private int drunkVolume;
private ArrayList<PriorityQueue<Integer>> rooms;
public LittleElephantLemonade() {
drunkVolume=0;
rooms=new ArrayList<PriorityQueue<Integer>>();
@rfaisal
rfaisal / math.thrift
Created October 9, 2013 15:23
Thrift file for a simple math library.
namespace java org.rfaisal.math
namespace csharp Math
service MathService{
i32 add (1:i32 a, 2:i32 b),
i32 sub (1:i32 a, 2:i32 b),
i32 mul (1:i32 a, 2:i32 b),
i32 div (1:i32 a, 2:i32 b),
i32 mod (1:i32 a, 2:i32 b)
}
@rfaisal
rfaisal / MathServer.cs
Created October 9, 2013 17:18
Simple math library in C#
public class MathServer
{
public MathServer() { }
public int add(int a, int b)
{
Console.WriteLine("Called add({0},{1})={2}", a, b, a+b);
return a + b;
}
public int sub(int a, int b)
{
@rfaisal
rfaisal / MathServer.cs
Created October 9, 2013 17:30
Simple math library implementing thrift interface
public class MathServer : Math.MathService.Iface
{
public MathServer() { }
public int add(int a, int b)
{
Console.WriteLine("Called add({0},{1})={2}", a, b, a+b);
return a + b;
}
public int sub(int a, int b)
{
@rfaisal
rfaisal / Program.cs
Created October 9, 2013 18:14
main for Thrift math server
class Program
{
static void Main(string[] args)
{
try
{
MathServer handler = new MathServer();
MathService.Processor processor = new MathService.Processor(handler);
TServerTransport serverTransport = new TServerSocket(9095);
TServer server = new TSimpleServer(processor, serverTransport);
@rfaisal
rfaisal / MathClient.java
Created October 9, 2013 20:05
Thrift java client for the math library
public class MathClient {
public static void main(String[] args) throws TException{
TTransport transport = new TSocket("localhost", 9095);
transport.open();
TProtocol protocol = new TBinaryProtocol(transport);
MathService.Client client = new MathService.Client(protocol);
System.out.println(client.add(10, 20));
System.out.println(client.sub(10, 20));
System.out.println(client.mul(10, 20));
System.out.println(client.div(10, 20));
@rfaisal
rfaisal / LittleElephantAndBooks.java
Created October 10, 2013 13:35
Little Elephant from the Zoo of Lviv has a bunch of books. You are given a int[] pages. Each element of pages is the number of pages in one of those books. There are no two books with the same number of pages. You are also given a int number. As a homework from the teacher, Little Elephant must read exactly number of his books during the summer.…
public class LittleElephantAndBooks {
public static int getNumber(int[] pages, int number){
int littleElephantMax=find(pages,number);//selection algorithm, O(n) worst case
int lazyElephantMax=0;
int sum=0;
for(int i=0;i<pages.length;i++){ //O(n)
if(pages[i]<=littleElephantMax){
if(pages[i]>lazyElephantMax && pages[i]!=littleElephantMax){
lazyElephantMax=pages[i];
}
@rfaisal
rfaisal / FoxAndClassroom.java
Last active December 26, 2015 08:08
Fox Ciel is now in high school. The seats in her classroom are arranged into an n by m matrix. The rows are numbered from 0 to n-1 (front to back) and the columns from 0 to m-1 (left to right). At the beginning, Ciel can choose any of the seats. Then, at the end of each week Ciel will shift one row to the back and one column to the right, wrappi…
public class FoxAndClassroom {
public static String ableTo(int m, int n){
HashSet<Point> overallVisited=new HashSet<Point>();
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
Point cur=new Point(i,j);
if(!overallVisited.contains(cur)){
HashSet<Point> visited=new HashSet<Point>();
while(!visited.contains(cur) && visited.size() < m*n){
visited.add(cur);
public class BoundaryTraversal {
public static ArrayList<TNode<Integer>> perform(TNode<Integer> root){
ArrayList<TNode<Integer>> ret= new ArrayList<TNode<Integer>>();
ret.addAll(traverseLeftSideTopDown(root));
ret.addAll(traverseBottomSideLeftRight(root));
if(root!=null){
ret.addAll(traverseRightSideBottomUp(root.right));//for excluding root
}
return ret;
}
public class MoveZerosToEnd {
public static int[] move(int[] arr){
int current=0;
for(int i=0;i<arr.length;i++){
if(arr[i]!=0)
arr[current++]=arr[i];
}
while(current<arr.length)
arr[current++]=0;
return arr;