Skip to content

Instantly share code, notes, and snippets.

View rfaisal's full-sized avatar

Faisal Rahman rfaisal

  • Facebook
  • Vancouver, BC
View GitHub Profile
public class SimpleSpamDetectorInefficient {
public static boolean doesDuplicatingMatch(String s,String p){
if(p.length()>s.length()) //if pattern is larger than the original string
return false;
else if(s.length()==0 && p.length()==0) //if both empty
return true;
else if(s.length()==0 || p.length()==0) //if one empty
return false;
else if(Character.toLowerCase(s.charAt(0)) == Character.toLowerCase(p.charAt(0)))
return doesDuplicatingMatch(s.substring(1),p.substring(1))
public class SimpleSpamDetectorSlightlyInefficient {
public static boolean doesDuplicatingMatch(String s,int s_i, int s_j, String p,int p_i, int p_j){
if(p_j-p_i>s_j-s_i) //if pattern is larger than the original string
return false;
else if(s_j-s_i==0 && p_j-p_i==0) //if both empty
return true;
else if(s_j<s_i||p_j<p_i)
return false;
else if(Character.toLowerCase(s.charAt(s_i)) == Character.toLowerCase(p.charAt(p_i)))
return doesDuplicatingMatch(s,s_i+1,s_j,p,p_i+1,p_j)
public class SimpleSpamDetector {
public static boolean doesDuplicatingMatch(String s,int s_i, int s_j, String p,int p_i, int p_j){
if(p_j-p_i>s_j-s_i) //if pattern is larger than the original string
return false;
else if(s_j-s_i==0 && p_j-p_i==0) //if both empty
return true;
else if(s_j<s_i||p_j<p_i)
return false;
else if(Character.toLowerCase(s.charAt(s_i)) == Character.toLowerCase(p.charAt(p_i)))
return doesDuplicatingMatch(s,s_i+1,s_j,p,p_i+1,p_j)
public class CompletingBrackets {
public static String complete(String text){
StringBuilder result= new StringBuilder();
int count=0;
for(int i=0;i<text.length();i++){
if(text.charAt(i)=='[') count++;
else if(text.charAt(i)==']') count--;
}
if(text.charAt(0)==']'){
result.append('[');
@rfaisal
rfaisal / FewestFactors.java
Created July 13, 2013 08:56
You will be given some decimal digits in a int[] digits. Build an integer with the minimum possible number of factors, using each of the digits exactly once (be sure to count all factors, not only the prime factors). If more than one number has the same (minimum) number of factors, return the smallest one among them.
public class FewestFactors {
public static int number(int[] digits){
LinkedList<Integer> adapter= new LinkedList<Integer>();
for(int i=digits.length-1;i>=0;i--)
adapter.addFirst(digits[i]);
HashSet<LinkedList<Integer>> combinitions=combinate(adapter);
int min=Integer.MAX_VALUE;
int min_num=0;
for(LinkedList<Integer> c:combinitions){
int num=convertToInteger(c);
@rfaisal
rfaisal / largest_palendrome_product.c
Created July 20, 2013 04:46
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two n-digit numbers.
#include<math.h>
#include<stdlib.h>
int is_palindrome(int n){
int len=0;
while(n/(int)pow(10,len)) len++;
while(len>1){
int mul=pow(10,len-1);
int d1=n/mul;
int d2=n%10;
if(d1!=d2) return 0;
@rfaisal
rfaisal / lcm_gcd.c
Last active December 20, 2015 00:48
#include<stdlib.h>
#include<stdio.h>
long int gcd(long int a, long int b){
if(b==0) return a;
else return gcd(b,a%b);
}
long int lcm(long int a, long int b){
return a*b/gcd(a,b);
}
#include<stdio.h>
#include<math.h>
int is_prime(int p){
if(p<=1) return 0;
int i=2;
int sqrt_p=sqrt(p);
while(i<=sqrt_p)
if(p%i++==0)
return 0;
return 1;
@rfaisal
rfaisal / FactorialDigitSum.java
Last active December 20, 2015 00:48
Find the sum of the digits in the number 100!
import java.math.BigInteger;
public class FactorialDigitSum {
public FactorialDigitSum() {
}
public static int calculate(int n){
return digitSum(factorial(new BigInteger(""+n))).intValue();
}
private static BigInteger digitSum(BigInteger n){
if(n.compareTo(BigInteger.TEN)==-1) return n;
else return n.mod(BigInteger.TEN).add(digitSum(n.divide(BigInteger.TEN)));
@rfaisal
rfaisal / total_number_of_bits_between.c
Last active December 20, 2015 01:09
One of the basics of Computer Science is knowing how numbers are represented in 2’s complement. Imagine that you write down all numbers between A and B inclusive in 2’s complement representation using 32 bits. How many 1’s will you write down in all ?
#include<stdlib.h>
#include<stdio.h>
int get_left_most_1(int n){
int pos=0;
while(n>1){
n>>=1;
pos++;
}
return pos;
}