Skip to content

Instantly share code, notes, and snippets.

@arafatkamaal
arafatkamaal / up.py
Created August 25, 2020 14:04
leetcode, 62. Unique Paths
class Solution:
def uniquePathsBruteForce(self, m: int, n: int) -> int:
grid = [[""] * m for _ in range(n)]
def up(grd):
if len(grd) == 0:
return 0
# the last cell
elif len(grd) == 1 and len(grd[0]) == 0:
return 1
@arafatkamaal
arafatkamaal / lcopm.py
Created August 25, 2020 07:10
Leetcode, Letter Combinations of a Phone Number
from typing import List
class Solution:
def __init__(self):
self.letter_dict = {"2": "abc", "3": "def", "4": "ghi", "5": "jkl", "6": "mno", "7": "pqrs", "8": "tuv", "9": "wxyz"}
def letterCombinations(self, digits: str) -> List[str]:
if digits == "" or len(digits) == 0:
@arafatkamaal
arafatkamaal / flatten.py
Created August 13, 2020 16:40
Flattens nested list
def flatten(accumulator, lst):
if len(lst) == 0:
return accumulator
elif type(lst[0]) is list:
result = flatten([], lst[0])
return flatten(accumulator + result, lst[1:])
else:
accumulator.append(lst[0])
return flatten(accumulator, lst[1:])
@arafatkamaal
arafatkamaal / LCS.java
Created November 28, 2019 11:03
Longest Common Sequence.
import java.util.*;
import java.math.*;
public class LCS {
public static void main(String[] args) {
LCS_ l = new LCS_();
System.out.println(l.lCSR("AGGTAB", "GXTXAYB", 0, 0));
System.out.println(l.lCSR("pmjghexybyrgzczy", "hafcdqbgncrcbihkd", 0, 0));
System.out.println(l.lCSR("oxcpqrsvwf","shmtulqrypy", 0, 0));
@arafatkamaal
arafatkamaal / PrefixSum.java
Last active November 23, 2019 07:04
Zero Sum Subarrays
//geeksforgeeks.org/find-if-there-is-a-subarray-with-0-sum/
import java.util.ArrayList;
public class PrefixSum {
public static int[] recoverOriginalArray(int[] array) {
int l = array.length;
int[] result = new int[l];
result[0] = array[0];
@arafatkamaal
arafatkamaal / PrefixSumArray.java
Created November 22, 2019 14:07
Prefix sums in java.
public class PrefixSumArray {
public static int[][] prefixSumArray(int[] array) {
int al = array.length;
int[][] result = new int[al][al];
int sum = 0;
for(int i = 0; i < al; i++) {
for(int j = i; j < al; j++) {
sum += array[j];
result[i][j] = sum;
@arafatkamaal
arafatkamaal / KSubsequences.java
Created November 22, 2019 07:53
Given a array and a number k, find all contigious array sequences that are divisible by k
/*
*
* Given a array and a number k, find all contigious array sequences that are divisible by k
*
*
*
*/
public class KSubsequences {
@arafatkamaal
arafatkamaal / trie.lisp
Created September 24, 2018 12:58
A very simple implementation of trie
(defparameter init-tree
'((a) (b) (c) (d) (e) (f) (g) (h) (i)
(j) (k) (l) (m) (n) (o) (p) (q) (r)
(s) (t) (u) (v) (w) (x) (y) (z)))
(defun add-member (m seq)
(labels ((am (m lst acc found)
(cond
((null lst)
(if (null found)
@arafatkamaal
arafatkamaal / music_practice.p6
Last active September 24, 2015 00:17
To emit notes in pairs in a interval for recorder practice.
my Bool $highs_only = False;
my Bool $lows_only = True;
my Str @notes;
if $highs_only {
@notes = <C C# D Eb E F F# G G# A Bb B>;
} elsif $lows_only {
@notes = <Low-C Low-C# Low-D Low-Eb Low-E Low-F Low-F# Low-G Low-G# Low-A Low-Bb Low-B>;
} else {
@notes = <C C# D Eb E F F# G G# A Bb B Low-C Low-C# Low-D Low-Eb Low-E Low-F Low-F# Low-G Low-G# Low-A Low-Bb Low-B>;
#
# This program scrapes the FIFA Data from the site:
# http://thesoccerworldcups.com/index.php
#
#
import urllib2