Skip to content

Instantly share code, notes, and snippets.

View animatedlew's full-sized avatar

Lewie [m80] animatedlew

View GitHub Profile
package com.company;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class Comparison {
// NOT TESTED!!!
static Set<String> diff(String aa, String bb) {
object pascal {
private def get(rows: List[Long], index: Int): Long = {
if (index < 0 || index >= rows.length) 0L
else rows(index)
}
private def row(prev: List[Long] = List()): List[Long] = {
if (prev.isEmpty) List(1)
object Main extends App {
val lst1 = List(1, 2, 3, 4).toStream
val lst2 = List('A', 'B', 'C', 'D', 'E', 'F').toStream
lst1 zip lst2 foreach { case (a, b) => println(s"pair: ($a, $b)") }
}
package com.company;
import java.util.Arrays;
import java.util.Iterator;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Main {
private static int consume(int a, int b) {
return a + b;
function permute(xs) {
function p(xs, prefix) {
if (!xs.length) return prefix;
else {
let results = [];
for (let i = 0; i < xs.length; i++) {
let rem = xs.slice(0, i) + xs.slice(i+1);
results = results.concat(p(rem, prefix + xs[i]));
}
return results;
object main extends App {
def perm[T](xs: List[T]): Seq[List[T]] = {
val len = xs.size
if (len < 2) List(xs)
else
(0 until len).flatMap { i =>
val (head, tail) = (xs(i), xs.take(i) ++ xs.drop(i + 1))
perm(tail).foldLeft(List[List[T]]())((r, p) => (head :: p) :: r)
}
}
def permute(s):
if len(s) < 2:
yield s
else:
for i in range(len(s)):
head, tail = s[i], s[:i] + s[i+1:]
for p in permute(tail):
yield [head] + p
@animatedlew
animatedlew / isperm.js
Last active February 21, 2017 02:48
Given two strings, write a method to decide if one is a permutation of the other.
function perm(str) {
return _perm(str, "").toString().split(',');
}
function _perm(str, acc) {
if (str.length === 0) return acc;
else {
return str.split('').map((c, i) => {
let s = str.substr(0, i) + str.substr(i+1);
return _perm(s, acc + str[i]);
function isUnique(word) {
// polyfill for Set()
let set = chars => {
return chars.reduce((b, a) => {
if (!b.includes(a)) b.push(a);
return b;
}, []);
};
let sortedWord = word.split('');
let charSet = set(sortedWord);
@animatedlew
animatedlew / uniquechars.cpp
Last active February 21, 2017 01:53
Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures?
/*
* Implement an algorithm to determine if a string has all unique
* characters. What if you cannot use additional data structures?
*/
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include <iostream>
using namespace std;