Complete "longest common ..." examples (#100)
* no else return * fix var ref * fix importing/requiring dependencies * complete longest common examples
This commit is contained in:
committed by
Aditya Bhargava
parent
c23ca90b83
commit
5b675cc2e8
@@ -1,11 +1,11 @@
|
|||||||
const countdown = (i) => {
|
const countdown = i => {
|
||||||
console.log(i);
|
console.log(i);
|
||||||
// base case
|
// base case
|
||||||
if (i <= 0) {
|
if (i <= 0) {
|
||||||
return null;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
countdown(i - 1);
|
countdown(i - 1);
|
||||||
return null;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
countdown(5);
|
countdown(5);
|
||||||
|
|||||||
@@ -6,9 +6,9 @@ func countdown(i int) {
|
|||||||
fmt.Println(i)
|
fmt.Println(i)
|
||||||
if i <= 0 {
|
if i <= 0 {
|
||||||
return
|
return
|
||||||
} else {
|
|
||||||
countdown(i - 1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
countdown(i - 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ import "fmt"
|
|||||||
func fact(x int) int {
|
func fact(x int) int {
|
||||||
if x == 1 {
|
if x == 1 {
|
||||||
return 1
|
return 1
|
||||||
} else {
|
|
||||||
return x * fact(x-1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return x * fact(x-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ function countdown(i) {
|
|||||||
// base case
|
// base case
|
||||||
if (i <= 0) {
|
if (i <= 0) {
|
||||||
return;
|
return;
|
||||||
} else {
|
|
||||||
countdown(i-1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
countdown(i - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
countdown(5);
|
countdown(5);
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
function fact(x) {
|
function fact(x) {
|
||||||
if (x === 1) {
|
if (x === 1) {
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
|
||||||
return x * fact(x-1);
|
|
||||||
}
|
}
|
||||||
|
return x * fact(x - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(fact(5));
|
console.log(fact(5));
|
||||||
|
|||||||
@@ -3,16 +3,12 @@
|
|||||||
* @param {Array} arr Array of numbers
|
* @param {Array} arr Array of numbers
|
||||||
* @return {number} Sum of the numbers
|
* @return {number} Sum of the numbers
|
||||||
*/
|
*/
|
||||||
const sumLoop = ( arr ) => {
|
const sumLoop = arr => {
|
||||||
let result = 0;
|
let result = 0;
|
||||||
|
for (let i = 0; i < arr.length; i++) {
|
||||||
for ( let i = 0; i < newArr.length; i++ ) {
|
result += arr[i];
|
||||||
result += newArr[i];
|
}
|
||||||
}
|
return result;
|
||||||
|
|
||||||
return result;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let arr = [1, 2, 3, 4];
|
console.log(sumLoop([1, 2, 3, 4])); // 10
|
||||||
|
|
||||||
console.log( sumLoop( arr ) ); // 10
|
|
||||||
|
|||||||
@@ -3,14 +3,6 @@
|
|||||||
* @param {Array} arr Array of numbers
|
* @param {Array} arr Array of numbers
|
||||||
* @return {number} Sum of the numbers
|
* @return {number} Sum of the numbers
|
||||||
*/
|
*/
|
||||||
const sumReduce = ( arr ) => {
|
const sumReduce = arr => arr.reduce((curr, prev) => curr + prev);
|
||||||
let result = newArr.reduce( ( curr, prev ) => {
|
|
||||||
return curr + prev;
|
|
||||||
} );
|
|
||||||
|
|
||||||
return result;
|
console.log(sumReduce([1, 2, 3, 4])); // 10
|
||||||
};
|
|
||||||
|
|
||||||
let arr = [1, 2, 3, 4];
|
|
||||||
|
|
||||||
console.log( sumReduce( arr ) ); // 10
|
|
||||||
|
|||||||
@@ -3,14 +3,10 @@
|
|||||||
* @param {Array} arr Array of numbers
|
* @param {Array} arr Array of numbers
|
||||||
* @return {number} Sum of the numbers
|
* @return {number} Sum of the numbers
|
||||||
*/
|
*/
|
||||||
function sumReduce( arr ) {
|
function sumReduce(arr) {
|
||||||
var result = newArr.reduce( ( curr, prev ) => {
|
return arr.reduce(function(curr, prev) {
|
||||||
return curr + prev;
|
return curr + prev;
|
||||||
} );
|
});
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var arr = [1, 2, 3, 4];
|
console.log(sumReduce([1, 2, 3, 4])); // 10
|
||||||
|
|
||||||
console.log( sumReduce( arr ) ); // 10
|
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
const arr = [1, 2, 3, 4];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sums values in array recursively
|
* Sums values in array recursively
|
||||||
* @param {Array} arr Array of numbers
|
* @param {Array} arr Array of numbers
|
||||||
* @return {number} Sum of the numbers
|
* @return {number} Sum of the numbers
|
||||||
*/
|
*/
|
||||||
const sumRecursive = ( arr ) => {
|
function sumRecursive(arr) {
|
||||||
if ( arr.length == 1 ) return arr[0];
|
if (arr.length == 1) {
|
||||||
return arr[0] + sumRecursive( arr.slice( 1 ) );
|
return arr[0];
|
||||||
};
|
}
|
||||||
|
return arr[0] + sumRecursive(arr.slice(1));
|
||||||
|
}
|
||||||
|
|
||||||
console.log( sumRecursive( arr ) ); // 10
|
console.log(sumRecursive([1, 2, 3, 4])); // 10
|
||||||
|
|||||||
@@ -1,18 +1,19 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
function quicksort(array) {
|
function quicksort(array) {
|
||||||
if (array.length < 2) {
|
if (array.length < 2) {
|
||||||
// base case, arrays with 0 or 1 element are already "sorted"
|
// base case, arrays with 0 or 1 element are already "sorted"
|
||||||
return array;
|
return array;
|
||||||
} else {
|
|
||||||
// recursive case
|
|
||||||
let pivot = array[0];
|
|
||||||
// sub-array of all the elements less than the pivot
|
|
||||||
let less = array.slice(1).filter(function(el) { return el <= pivot; });
|
|
||||||
// sub-array of all the elements greater than the pivot
|
|
||||||
let greater = array.slice(1).filter(function(el) { return el > pivot; });
|
|
||||||
return quicksort(less).concat([pivot], quicksort(greater));
|
|
||||||
}
|
}
|
||||||
|
// recursive case
|
||||||
|
let pivot = array[0];
|
||||||
|
// sub-array of all the elements less than the pivot
|
||||||
|
let less = array.slice(1).filter(function(el) {
|
||||||
|
return el <= pivot;
|
||||||
|
});
|
||||||
|
// sub-array of all the elements greater than the pivot
|
||||||
|
let greater = array.slice(1).filter(function(el) {
|
||||||
|
return el > pivot;
|
||||||
|
});
|
||||||
|
return quicksort(less).concat([pivot], quicksort(greater));
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(quicksort([10, 5, 2, 3])); // [2, 3, 5, 10]
|
console.log(quicksort([10, 5, 2, 3])); // [2, 3, 5, 10]
|
||||||
|
|||||||
@@ -3,6 +3,6 @@ const book = {};
|
|||||||
book.apple = 0.67;
|
book.apple = 0.67;
|
||||||
// milk costs $1.49
|
// milk costs $1.49
|
||||||
book.milk = 1.49;
|
book.milk = 1.49;
|
||||||
book.avokado = 1.49;
|
book.avocado = 1.49;
|
||||||
|
|
||||||
console.log(book); // { apple: 0.67, milk: 1.49, avocado: 1.49 }
|
console.log(book); // { apple: 0.67, milk: 1.49, avocado: 1.49 }
|
||||||
|
|||||||
@@ -31,10 +31,11 @@ func search(name string) bool {
|
|||||||
if person_is_seller(person) {
|
if person_is_seller(person) {
|
||||||
println(person + " is mango seller!")
|
println(person + " is mango seller!")
|
||||||
return true
|
return true
|
||||||
} else {
|
|
||||||
search_queue = append(search_queue, graph[person]...)
|
|
||||||
searched = append(searched, person)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
search_queue = append(search_queue, graph[person]...)
|
||||||
|
searched = append(searched, person)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
|||||||
@@ -39,10 +39,10 @@ func search(name string) bool {
|
|||||||
if PersonIsSeller(person) {
|
if PersonIsSeller(person) {
|
||||||
fmt.Println(person + " is the mango seller!")
|
fmt.Println(person + " is the mango seller!")
|
||||||
return true
|
return true
|
||||||
} else {
|
|
||||||
search_queue = append(search_queue, graph[person]...)
|
|
||||||
searched = append(searched, person)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
search_queue = append(search_queue, graph[person]...)
|
||||||
|
searched = append(searched, person)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
function person_is_seller(name) {
|
function person_is_seller(name) {
|
||||||
return name[name.length-1] === 'm';
|
return name[name.length - 1] === "m";
|
||||||
}
|
}
|
||||||
|
|
||||||
const graph = {};
|
const graph = {};
|
||||||
@@ -14,7 +12,6 @@ graph["peggy"] = [];
|
|||||||
graph["thom"] = [];
|
graph["thom"] = [];
|
||||||
graph["jonny"] = [];
|
graph["jonny"] = [];
|
||||||
|
|
||||||
|
|
||||||
function search(name) {
|
function search(name) {
|
||||||
let search_queue = [];
|
let search_queue = [];
|
||||||
search_queue = search_queue.concat(graph[name]);
|
search_queue = search_queue.concat(graph[name]);
|
||||||
@@ -25,17 +22,16 @@ function search(name) {
|
|||||||
// Only search this person if you haven't already searched them
|
// Only search this person if you haven't already searched them
|
||||||
if (searched.indexOf(person) === -1) {
|
if (searched.indexOf(person) === -1) {
|
||||||
if (person_is_seller(person)) {
|
if (person_is_seller(person)) {
|
||||||
console.log(person + ' is a mango seller!');
|
console.log(person + " is a mango seller!");
|
||||||
return true;
|
return true;
|
||||||
} else {
|
|
||||||
search_queue = search_queue.concat(graph[person]);
|
|
||||||
// Marks this person as searched
|
|
||||||
searched.push(person);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
search_queue = search_queue.concat(graph[person]);
|
||||||
|
// Marks this person as searched
|
||||||
|
searched.push(person);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
search("you"); // thom is a mango seller!
|
||||||
search('you'); // thom is a mango seller!
|
|
||||||
|
|||||||
@@ -1,7 +1,54 @@
|
|||||||
if (word_a[i] === word_b[j]) {
|
function createMatrix(rows, cols) {
|
||||||
// The letters match
|
const matrix = new Array(rows);
|
||||||
cell[i][j] = cell[i - 1][j - 1] + 1;
|
|
||||||
} else {
|
for (let i = 0; i < matrix.length; i++) {
|
||||||
// The letters don't match
|
matrix[i] = new Array(cols).fill(0);
|
||||||
cell[i][j] = Math.max(cell[i - 1][j], cell[i][j - 1]);
|
}
|
||||||
|
|
||||||
|
return matrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function substring(a, b) {
|
||||||
|
const cell = createMatrix(a.length + 1, b.length + 1);
|
||||||
|
let lcs = 0;
|
||||||
|
let lastSubIndex = 0;
|
||||||
|
|
||||||
|
for (let i = 1; i <= a.length; i++) {
|
||||||
|
for (let j = 1; j <= b.length; j++) {
|
||||||
|
if (a[i - 1] === b[j - 1]) {
|
||||||
|
cell[i][j] = cell[i - 1][j - 1] + 1;
|
||||||
|
|
||||||
|
if (cell[i][j] > lcs) {
|
||||||
|
lcs = cell[i][j];
|
||||||
|
lastSubIndex = i;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
cell[i][j] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return a.slice(lastSubIndex - lcs, lastSubIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
substring("vista", "hish"); // "is"
|
||||||
|
substring("fish", "hish"); // "ish"
|
||||||
|
|
||||||
|
function subsequence(a, b) {
|
||||||
|
const cell = createMatrix(a.length + 1, b.length + 1);
|
||||||
|
|
||||||
|
for (let i = 1; i <= a.length; i++) {
|
||||||
|
for (let j = 1; j <= b.length; j++) {
|
||||||
|
if (a[i] === b[j]) {
|
||||||
|
cell[i][j] = cell[i - 1][j - 1] + 1;
|
||||||
|
} else {
|
||||||
|
cell[i][j] = Math.max(cell[i - 1][j], cell[i][j - 1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return cell[a.length][b.length];
|
||||||
|
}
|
||||||
|
|
||||||
|
subsequence("fish", "fosh"); // 3
|
||||||
|
subsequence("fort", "fosh"); // 2
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
const initializeMatrix = (rows, cols) => {
|
export const initializeMatrix = (rows, cols) => {
|
||||||
const matrix = [];
|
const matrix = [];
|
||||||
for (let i = 0; i < rows.length; i += 1) {
|
for (let i = 0; i < rows.length; i += 1) {
|
||||||
matrix.push(Array(cols.length).fill(0));
|
matrix.push(Array(cols.length).fill(0));
|
||||||
}
|
}
|
||||||
return matrix;
|
return matrix;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default initializeMatrix;
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import base from './base';
|
import { initializeMatrix } from "./base";
|
||||||
|
|
||||||
const diff = (firstWord, secondWord) => {
|
const diff = (firstWord, secondWord) => {
|
||||||
const arr1 = firstWord.split('');
|
const arr1 = firstWord.split("");
|
||||||
const arr2 = secondWord.split('');
|
const arr2 = secondWord.split("");
|
||||||
const matrix = initializeMatrix(arr1, arr2);
|
const matrix = initializeMatrix(arr1, arr2);
|
||||||
for (let i = 0; i < arr1.length; i += 1) {
|
for (let i = 0; i < arr1.length; i += 1) {
|
||||||
for (let j = 0; j < arr2.length; j += 1) {
|
for (let j = 0; j < arr2.length; j += 1) {
|
||||||
|
|||||||
@@ -1,11 +1,53 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
func createMatrix(rows, cols int) [][]int {
|
||||||
|
cell := make([][]int, rows)
|
||||||
func main() {
|
for i := range cell {
|
||||||
if word_a[i] == word_b[j] {
|
cell[i] = make([]int, cols)
|
||||||
cell[i][j] = cell[i-1][j-1] + 1
|
|
||||||
} else {
|
|
||||||
cell[i][j] = max(cell[i-1][j], cell[i][j-1])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return cell
|
||||||
|
}
|
||||||
|
|
||||||
|
func substring(a, b string) string {
|
||||||
|
lcs := 0
|
||||||
|
lastSubIndex := 0
|
||||||
|
cell := createMatrix(len(a)+1, len(b)+1)
|
||||||
|
|
||||||
|
for i := 1; i <= len(a); i++ {
|
||||||
|
for j := 1; j <= len(b); j++ {
|
||||||
|
if a[i-1] == b[j-1] {
|
||||||
|
cell[i][j] = cell[i-1][j-1] + 1
|
||||||
|
|
||||||
|
if cell[i][j] > lcs {
|
||||||
|
lcs = cell[i][j]
|
||||||
|
lastSubIndex = i
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
cell[i][j] = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return a[lastSubIndex-lcs : lastSubIndex]
|
||||||
|
}
|
||||||
|
|
||||||
|
func subsequence(a, b string) int {
|
||||||
|
cell := createMatrix(len(a)+1, len(b)+1)
|
||||||
|
|
||||||
|
for i := 1; i <= len(a); i++ {
|
||||||
|
for j := 1; j <= len(b); j++ {
|
||||||
|
if a[i-1] == b[j-1] {
|
||||||
|
cell[i][j] = cell[i-1][j-1] + 1
|
||||||
|
} else {
|
||||||
|
cell[i][j] = cell[i-1][j]
|
||||||
|
|
||||||
|
if cell[i][j] < cell[i][j-1] {
|
||||||
|
cell[i][j] = cell[i][j-1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return cell[len(a)][len(b)]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestSubstring(t *testing.T) {
|
||||||
|
var stests = []struct {
|
||||||
|
name, a, b, expected string
|
||||||
|
}{
|
||||||
|
{"hish-vista", "vista", "hish", "is"},
|
||||||
|
{"hish-fish", "fish", "hish", "ish"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range stests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
actual := substring(tt.a, tt.b)
|
||||||
|
|
||||||
|
if actual != tt.expected {
|
||||||
|
t.Errorf("Expected %s but received %s", tt.expected, actual)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSubsequence(t *testing.T) {
|
||||||
|
var stests = []struct {
|
||||||
|
name, a, b string
|
||||||
|
expected int
|
||||||
|
}{
|
||||||
|
{"fosh-fish", "fish", "fosh", 3},
|
||||||
|
{"fosh-fort", "fort", "fosh", 2},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range stests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
actual := subsequence(tt.a, tt.b)
|
||||||
|
|
||||||
|
if actual != tt.expected {
|
||||||
|
t.Errorf("Expected %d but received %d", tt.expected, actual)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
export default function initialize_matrix(rows, cols){
|
module.exports = function initialize_matrix(rows, cols) {
|
||||||
let matrix = [];
|
let matrix = [];
|
||||||
for (let i = 0; i < rows.length; i++){
|
for (let i = 0; i < rows.length; i++) {
|
||||||
matrix.push(Array(cols.length).fill(0));
|
matrix.push(Array(cols.length).fill(0));
|
||||||
}
|
}
|
||||||
return matrix;
|
return matrix;
|
||||||
}
|
};
|
||||||
|
|||||||
@@ -1,25 +1,25 @@
|
|||||||
import initialize_matrix as base from "./base.js";
|
const initialize_matrix = require("./base.js");
|
||||||
|
|
||||||
export default function diff( firstWord, secondWord ){
|
function diff(firstWord, secondWord) {
|
||||||
let arr1 = firstWord.split('');
|
let arr1 = firstWord.split("");
|
||||||
let arr2 = secondWord.split('');
|
let arr2 = secondWord.split("");
|
||||||
let matrix = initialize_matrix(arr1, arr2);
|
let matrix = initialize_matrix(arr1, arr2);
|
||||||
for (let i = 0; i < arr1.length; i++){
|
for (let i = 0; i < arr1.length; i++) {
|
||||||
for (let j = 0; j < arr2.length; j++){
|
for (let j = 0; j < arr2.length; j++) {
|
||||||
if( arr1[i] == arr2[j] ){
|
if (arr1[i] == arr2[j]) {
|
||||||
if( i > 0 && j > 0){
|
if (i > 0 && j > 0) {
|
||||||
matrix[i][j] = matrix[i - 1][j - 1] + 1;
|
matrix[i][j] = matrix[i - 1][j - 1] + 1;
|
||||||
}else{
|
} else {
|
||||||
matrix[i][j] = 1;
|
matrix[i][j] = 1;
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if( i > 0 && j > 0){
|
|
||||||
matrix[i][j] = Math.max(matrix[i - 1][j], matrix[i][j - 1]);
|
|
||||||
}else{
|
|
||||||
matrix[i][j] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if (i > 0 && j > 0) {
|
||||||
|
matrix[i][j] = Math.max(matrix[i - 1][j], matrix[i][j - 1]);
|
||||||
|
} else {
|
||||||
|
matrix[i][j] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return matrix[arr1.length-1][arr2.length-1];
|
}
|
||||||
}
|
return matrix[arr1.length - 1][arr2.length - 1];
|
||||||
|
}
|
||||||
|
|||||||
@@ -14,5 +14,5 @@ This repo also contains every image in Grokking Algorithms, in hi-res. These ima
|
|||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
- The examples in this book are in Python, but I'd like to get examples in Ruby, Javascript, C, and other languages too. Please add examples in other languages!
|
- The examples in this book are in Python, but I'd like to get examples in Ruby, JavaScript, C, and other languages too. Please add examples in other languages!
|
||||||
- I'm pretty responsive to PRs. That is the quickest way to contribute to this repo.
|
- I'm pretty responsive to PRs. That is the quickest way to contribute to this repo.
|
||||||
|
|||||||
Reference in New Issue
Block a user