From 09a8115325e227eefef31783d85da9b8d1ae2451 Mon Sep 17 00:00:00 2001 From: Kevin Nguyen Date: Thu, 16 Jun 2016 10:10:02 -0700 Subject: [PATCH 01/10] code for chapter 2 in javascript --- .../javascript/01_selection_sort.js | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 02_selection_sort/javascript/01_selection_sort.js diff --git a/02_selection_sort/javascript/01_selection_sort.js b/02_selection_sort/javascript/01_selection_sort.js new file mode 100644 index 0000000..1ece0be --- /dev/null +++ b/02_selection_sort/javascript/01_selection_sort.js @@ -0,0 +1,27 @@ +'use strict'; + +// Finds the smallest value in an array +function findSmallest(arr) { + let smallest = arr[0]; // Stores the smallest value + let smallest_index = 0; // Stores the index of the smallest value + for (let i = 1; i < arr.length; i++) { + if (arr[i] < smallest) { + smallest = arr[i]; + smallest_index = i; + } + } + return smallest_index; +} + +// Sort array +function selectionSort(arr) { + const newArr = []; + for (let i = 0, length = arr.length; i < length; i++) { + // Finds the smallest element in the array and adds it to the new array + let smallest = findSmallest(arr); + newArr.push(arr.splice(smallest, 1)[0]); + } + return newArr; +} + +console.log(selectionSort([5, 3, 6, 2, 10])); // [2, 3, 5, 6, 10] From df2775353a650ccdb48fd9862a996a7f76b3cdff Mon Sep 17 00:00:00 2001 From: Kevin Nguyen Date: Thu, 16 Jun 2016 22:37:11 -0700 Subject: [PATCH 02/10] code from chapter 3 in javascript --- 03_recursion/01_countdown.js | 11 +++++++++++ 03_recursion/02_greet.js | 16 ++++++++++++++++ 03_recursion/03_factorial.js | 9 +++++++++ 3 files changed, 36 insertions(+) create mode 100644 03_recursion/01_countdown.js create mode 100644 03_recursion/02_greet.js create mode 100644 03_recursion/03_factorial.js diff --git a/03_recursion/01_countdown.js b/03_recursion/01_countdown.js new file mode 100644 index 0000000..a93a3a6 --- /dev/null +++ b/03_recursion/01_countdown.js @@ -0,0 +1,11 @@ +function countdown(i) { + console.log(i); + // base case + if (i <= 0) { + return; + } else { + countdown(i-1); + } +} + +countdown(5); diff --git a/03_recursion/02_greet.js b/03_recursion/02_greet.js new file mode 100644 index 0000000..5161a08 --- /dev/null +++ b/03_recursion/02_greet.js @@ -0,0 +1,16 @@ +function greet2(name) { + console.log('how are you, ' + name + '?'); +} + +function bye() { + console.log('ok bye!'); +} + +function greet(name) { + console.log('hello, ' + name + '!'); + greet2(name); + console.log('getting ready to say bye...'); + bye(); +} + +greet('adit'); diff --git a/03_recursion/03_factorial.js b/03_recursion/03_factorial.js new file mode 100644 index 0000000..9ae69ad --- /dev/null +++ b/03_recursion/03_factorial.js @@ -0,0 +1,9 @@ +function fact(x) { + if (x === 1) { + return 1; + } else { + return x * fact(x-1); + } +} + +console.log(fact(5)); From 040cfe510b5b07bc16e5762b5e4ce5959c942d1d Mon Sep 17 00:00:00 2001 From: Kevin Nguyen Date: Thu, 16 Jun 2016 23:00:05 -0700 Subject: [PATCH 03/10] code from chapter 4 in javascript --- 04_quicksort/01_loop_sum.js | 11 +++++++++++ 04_quicksort/02_recursive_sum.js | 10 ++++++++++ 04_quicksort/03_recursive_count.js | 10 ++++++++++ 04_quicksort/04_recursive_max.js | 11 +++++++++++ 04_quicksort/05_quicksort.js | 18 ++++++++++++++++++ 5 files changed, 60 insertions(+) create mode 100644 04_quicksort/01_loop_sum.js create mode 100644 04_quicksort/02_recursive_sum.js create mode 100644 04_quicksort/03_recursive_count.js create mode 100644 04_quicksort/04_recursive_max.js create mode 100644 04_quicksort/05_quicksort.js diff --git a/04_quicksort/01_loop_sum.js b/04_quicksort/01_loop_sum.js new file mode 100644 index 0000000..d2f70fd --- /dev/null +++ b/04_quicksort/01_loop_sum.js @@ -0,0 +1,11 @@ +'use strict'; + +function sum(arr) { + let total = 0; + for (let x = 0; x < arr.length; x++) { + total += arr[x]; + } + return total; +} + +console.log(sum([1, 2, 3, 4])) // 10 diff --git a/04_quicksort/02_recursive_sum.js b/04_quicksort/02_recursive_sum.js new file mode 100644 index 0000000..b0712f2 --- /dev/null +++ b/04_quicksort/02_recursive_sum.js @@ -0,0 +1,10 @@ +'use strict'; + +function sum(list) { + if (list.length === 0) { + return 0; + } + return list[0] + sum(list.slice(1)); +} + +console.log(sum([1, 2, 3, 4])) // 10 diff --git a/04_quicksort/03_recursive_count.js b/04_quicksort/03_recursive_count.js new file mode 100644 index 0000000..139acfc --- /dev/null +++ b/04_quicksort/03_recursive_count.js @@ -0,0 +1,10 @@ +'use strict'; + +function count(list) { + if (list.length === 0) { + return 0; + } + return 1 + count(list.slice(1)); +} + +console.log(count([0, 1, 2, 3, 4, 5])); // 6 diff --git a/04_quicksort/04_recursive_max.js b/04_quicksort/04_recursive_max.js new file mode 100644 index 0000000..fe457e9 --- /dev/null +++ b/04_quicksort/04_recursive_max.js @@ -0,0 +1,11 @@ +'use strict'; + +function max(list) { + if (list.length === 2) { + return list[0] > list[1] ? list[0] : list[1]; + } + let sub_max = max(list.slice(1)); + return list[0] > sub_max ? list[0] : sub_max; +} + +console.log(max([1, 5, 10, 25, 16, 1])); // 25 diff --git a/04_quicksort/05_quicksort.js b/04_quicksort/05_quicksort.js new file mode 100644 index 0000000..c3c61ae --- /dev/null +++ b/04_quicksort/05_quicksort.js @@ -0,0 +1,18 @@ +'use strict'; + +function quicksort(array) { + if (array.length < 2) { + // base case, arrays with 0 or 1 element are already "sorted" + 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)); + } +} + +console.log(quicksort([10, 5, 2, 3])); // [2, 3, 5, 10] From bad0a2ac3d4ed2f0314ee956b3e00f2234b9196c Mon Sep 17 00:00:00 2001 From: Kevin Nguyen Date: Fri, 17 Jun 2016 10:03:11 -0700 Subject: [PATCH 04/10] code from chapter 5 in javascript --- 05_hash_tables/01_price_of_groceries.js | 10 ++++++++++ 05_hash_tables/02_check_voter.js | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 05_hash_tables/01_price_of_groceries.js create mode 100644 05_hash_tables/02_check_voter.js diff --git a/05_hash_tables/01_price_of_groceries.js b/05_hash_tables/01_price_of_groceries.js new file mode 100644 index 0000000..a95d3d2 --- /dev/null +++ b/05_hash_tables/01_price_of_groceries.js @@ -0,0 +1,10 @@ +'use strict'; + +const book = {}; +// an apple costs 67 cents +book['apple'] = 0.67; +// milk costs $1.49 +book['milk'] = 1.49; +book['avocado'] = 1.49; + +console.log(book); // { apple: 0.67, milk: 1.49, avocado: 1.49 } diff --git a/05_hash_tables/02_check_voter.js b/05_hash_tables/02_check_voter.js new file mode 100644 index 0000000..288eba6 --- /dev/null +++ b/05_hash_tables/02_check_voter.js @@ -0,0 +1,16 @@ +'use strict'; + +const voted = {}; +function check_voter(name) { + if (voted[name]) { + console.log('kick them out!'); + } else { + voted[name] = true; + console.log('let them vote!'); + } +} + + +check_voter("tom"); // let them vote! +check_voter("mike"); // let them vote! +check_voter("mike"); // kick them out! From 96c766f9d9552e4f973fa223c7da7ef450d974b2 Mon Sep 17 00:00:00 2001 From: Kevin Nguyen Date: Fri, 17 Jun 2016 10:03:11 -0700 Subject: [PATCH 05/10] code from chapter 5 in javascript --- .../javascript/01_price_of_groceries.js | 10 ++++++++++ 05_hash_tables/javascript/02_check_voter.js | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 05_hash_tables/javascript/01_price_of_groceries.js create mode 100644 05_hash_tables/javascript/02_check_voter.js diff --git a/05_hash_tables/javascript/01_price_of_groceries.js b/05_hash_tables/javascript/01_price_of_groceries.js new file mode 100644 index 0000000..a95d3d2 --- /dev/null +++ b/05_hash_tables/javascript/01_price_of_groceries.js @@ -0,0 +1,10 @@ +'use strict'; + +const book = {}; +// an apple costs 67 cents +book['apple'] = 0.67; +// milk costs $1.49 +book['milk'] = 1.49; +book['avocado'] = 1.49; + +console.log(book); // { apple: 0.67, milk: 1.49, avocado: 1.49 } diff --git a/05_hash_tables/javascript/02_check_voter.js b/05_hash_tables/javascript/02_check_voter.js new file mode 100644 index 0000000..288eba6 --- /dev/null +++ b/05_hash_tables/javascript/02_check_voter.js @@ -0,0 +1,16 @@ +'use strict'; + +const voted = {}; +function check_voter(name) { + if (voted[name]) { + console.log('kick them out!'); + } else { + voted[name] = true; + console.log('let them vote!'); + } +} + + +check_voter("tom"); // let them vote! +check_voter("mike"); // let them vote! +check_voter("mike"); // kick them out! From 4ad9398ab27e9970cb39a3fd9eab0a77b90fdedc Mon Sep 17 00:00:00 2001 From: Kevin Nguyen Date: Thu, 16 Jun 2016 23:00:05 -0700 Subject: [PATCH 06/10] code from chapter 4 in javascript --- 04_quicksort/javascript/01_loop_sum.js | 11 +++++++++++ 04_quicksort/javascript/02_recursive_sum.js | 10 ++++++++++ 04_quicksort/javascript/03_recursive_count.js | 10 ++++++++++ 04_quicksort/javascript/04_recursive_max.js | 11 +++++++++++ 04_quicksort/javascript/05_quicksort.js | 18 ++++++++++++++++++ 5 files changed, 60 insertions(+) create mode 100644 04_quicksort/javascript/01_loop_sum.js create mode 100644 04_quicksort/javascript/02_recursive_sum.js create mode 100644 04_quicksort/javascript/03_recursive_count.js create mode 100644 04_quicksort/javascript/04_recursive_max.js create mode 100644 04_quicksort/javascript/05_quicksort.js diff --git a/04_quicksort/javascript/01_loop_sum.js b/04_quicksort/javascript/01_loop_sum.js new file mode 100644 index 0000000..d2f70fd --- /dev/null +++ b/04_quicksort/javascript/01_loop_sum.js @@ -0,0 +1,11 @@ +'use strict'; + +function sum(arr) { + let total = 0; + for (let x = 0; x < arr.length; x++) { + total += arr[x]; + } + return total; +} + +console.log(sum([1, 2, 3, 4])) // 10 diff --git a/04_quicksort/javascript/02_recursive_sum.js b/04_quicksort/javascript/02_recursive_sum.js new file mode 100644 index 0000000..b0712f2 --- /dev/null +++ b/04_quicksort/javascript/02_recursive_sum.js @@ -0,0 +1,10 @@ +'use strict'; + +function sum(list) { + if (list.length === 0) { + return 0; + } + return list[0] + sum(list.slice(1)); +} + +console.log(sum([1, 2, 3, 4])) // 10 diff --git a/04_quicksort/javascript/03_recursive_count.js b/04_quicksort/javascript/03_recursive_count.js new file mode 100644 index 0000000..139acfc --- /dev/null +++ b/04_quicksort/javascript/03_recursive_count.js @@ -0,0 +1,10 @@ +'use strict'; + +function count(list) { + if (list.length === 0) { + return 0; + } + return 1 + count(list.slice(1)); +} + +console.log(count([0, 1, 2, 3, 4, 5])); // 6 diff --git a/04_quicksort/javascript/04_recursive_max.js b/04_quicksort/javascript/04_recursive_max.js new file mode 100644 index 0000000..fe457e9 --- /dev/null +++ b/04_quicksort/javascript/04_recursive_max.js @@ -0,0 +1,11 @@ +'use strict'; + +function max(list) { + if (list.length === 2) { + return list[0] > list[1] ? list[0] : list[1]; + } + let sub_max = max(list.slice(1)); + return list[0] > sub_max ? list[0] : sub_max; +} + +console.log(max([1, 5, 10, 25, 16, 1])); // 25 diff --git a/04_quicksort/javascript/05_quicksort.js b/04_quicksort/javascript/05_quicksort.js new file mode 100644 index 0000000..c3c61ae --- /dev/null +++ b/04_quicksort/javascript/05_quicksort.js @@ -0,0 +1,18 @@ +'use strict'; + +function quicksort(array) { + if (array.length < 2) { + // base case, arrays with 0 or 1 element are already "sorted" + 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)); + } +} + +console.log(quicksort([10, 5, 2, 3])); // [2, 3, 5, 10] From 29a9e7c131cf73b03c517f6d027fd34b73bd08a5 Mon Sep 17 00:00:00 2001 From: Kevin Nguyen Date: Fri, 17 Jun 2016 21:54:01 -0700 Subject: [PATCH 07/10] code for chapter 6 in javascript --- .../javascript/01_breadth-first_search.js | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 06_breadth-first_search/javascript/01_breadth-first_search.js diff --git a/06_breadth-first_search/javascript/01_breadth-first_search.js b/06_breadth-first_search/javascript/01_breadth-first_search.js new file mode 100644 index 0000000..8daa592 --- /dev/null +++ b/06_breadth-first_search/javascript/01_breadth-first_search.js @@ -0,0 +1,41 @@ +'use strict'; + +function person_is_seller(name) { + return name[name.length-1] === 'm'; +} + +const graph = {}; +graph["you"] = ["alice", "bob", "claire"]; +graph["bob"] = ["anuj", "peggy"]; +graph["alice"] = ["peggy"]; +graph["claire"] = ["thom", "jonny"]; +graph["anuj"] = []; +graph["peggy"] = []; +graph["thom"] = []; +graph["jonny"] = []; + + +function search(name) { + let search_queue = []; + search_queue = search_queue.concat(graph[name]); + // This array is how you keep track of which people you've searched before. + const searched = []; + while (search_queue.length) { + let person = search_queue.shift(); + // Only search this person if you haven't already searched them + if (searched.indexOf(person) === -1) { + if (person_is_seller(person)) { + console.log(person + ' is a mango seller!'); + return true; + } else { + search_queue = search_queue.concat(graph[person]); + // Marks this person as searched + searched.push(person); + } + } + } + return false; +} + + +search('you'); // thom is a mango seller! From d41a255b641d86cdef7329611548515e2ffddb14 Mon Sep 17 00:00:00 2001 From: Kevin Nguyen Date: Tue, 21 Jun 2016 20:09:41 -0700 Subject: [PATCH 08/10] code for chapter 7 in javascript --- .../javascript/01_dijkstras_algorithm.js | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 07_dijkstras_algorithm/javascript/01_dijkstras_algorithm.js diff --git a/07_dijkstras_algorithm/javascript/01_dijkstras_algorithm.js b/07_dijkstras_algorithm/javascript/01_dijkstras_algorithm.js new file mode 100644 index 0000000..c050162 --- /dev/null +++ b/07_dijkstras_algorithm/javascript/01_dijkstras_algorithm.js @@ -0,0 +1,75 @@ +'use strict'; + +// the graph +const graph = {} +graph["start"] = {} +graph["start"]["a"] = 6 +graph["start"]["b"] = 2 + +graph["a"] = {} +graph["a"]["fin"] = 1 + +graph["b"] = {} +graph["b"]["a"] = 3 +graph["b"]["fin"] = 5 + +graph["fin"] = {} + +// The costs table +const costs = {}; +costs['a'] = 6 +costs['b'] = 2; +costs['fin'] = Infinity; + +// the parents table +const parents = {}; +parents['a'] = 'start'; +parents['b'] = 'start'; +parents['fin'] = null; + +let processed = []; + + +function find_lowest_cost_node(costs) { + let lowest_cost = Infinity; + let lowest_cost_node = null; + + // Go through each node + for (let node in costs) { + let cost = costs[node]; + // If it's the lowest cost so far and hasn't been processed yet... + if (cost < lowest_cost && (processed.indexOf(node) === -1)) { + // ... set it as the new lowest-cost node. + lowest_cost = cost; + lowest_cost_node = node; + } + } + return lowest_cost_node; +} + +let node = find_lowest_cost_node(costs); + +while (node !== null) { + let cost = costs[node]; + // Go through all the neighbors of this node + let neighbors = graph[node]; + Object.keys(neighbors).forEach(function(n) { + let new_cost = cost + neighbors[n]; + // If it's cheaper to get to this neighbor by going through this node + if (costs[n] > new_cost) { + // ... update the cost for this node + costs[n] = new_cost; + // This node becomes the new parent for this neighbor. + parents[n] = node; + } + }); + + // Mark the node as processed + processed = processed.concat(node); + + // Find the next node to process, and loop + node = find_lowest_cost_node(costs); +} + +console.log("Cost from the start to each node:"); +console.log(costs); // { a: 5, b: 2, fin: 6 } From 4a98a14bc13f65c7b5ab1dfbaa0b5adbfcb5893b Mon Sep 17 00:00:00 2001 From: Kevin Nguyen Date: Tue, 21 Jun 2016 21:48:53 -0700 Subject: [PATCH 09/10] code for chapter 8 in javascript --- .../javascript/01_set_covering.js | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 08_greedy_algorithms/javascript/01_set_covering.js diff --git a/08_greedy_algorithms/javascript/01_set_covering.js b/08_greedy_algorithms/javascript/01_set_covering.js new file mode 100644 index 0000000..a2bb41e --- /dev/null +++ b/08_greedy_algorithms/javascript/01_set_covering.js @@ -0,0 +1,31 @@ +'use strict'; + +// You pass an array in, and it gets converted to a set. +let states_needed = new Set(["mt", "wa", "or", "id", "nv", "ut", "ca", "az"]); + +const stations = {}; +stations["kone"] = new Set(["id", "nv", "ut"]); +stations["ktwo"] = new Set(["wa", "id", "mt"]); +stations["kthree"] = new Set(["or", "nv", "ca"]); +stations["kfour"] = new Set(["nv", "ut"]); +stations["kfive"] = new Set(["ca", "az"]); + +const final_stations = new Set(); + + +while (states_needed.size) { + let best_station = null; + let states_covered = new Set(); + for (let station in stations) { + let states = stations[station]; + let covered = new Set([...states_needed].filter((x) => states.has(x))); + if (covered.size > states_covered.size) { + best_station = station; + states_covered = covered; + } + } + states_needed = new Set([...states_needed].filter((x) => !states_covered.has(x))); + final_stations.add(best_station); +} + +console.log(final_stations); // Set { 'kone', 'ktwo', 'kthree', 'kfive' } From 8181b2609fa3624efde3888ffb94f231e5769fbf Mon Sep 17 00:00:00 2001 From: Kevin Nguyen Date: Tue, 21 Jun 2016 21:50:04 -0700 Subject: [PATCH 10/10] code for chapter 9 in javascript --- .../javascript/01_longest_common_subsequence.js | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 09_dynamic_programming/javascript/01_longest_common_subsequence.js diff --git a/09_dynamic_programming/javascript/01_longest_common_subsequence.js b/09_dynamic_programming/javascript/01_longest_common_subsequence.js new file mode 100644 index 0000000..f1054d0 --- /dev/null +++ b/09_dynamic_programming/javascript/01_longest_common_subsequence.js @@ -0,0 +1,7 @@ +if (word_a[i] === word_b[j]) { + // The letters match + cell[i][j] = cell[i-1][j-1] + 1; +} else { + // The letters don't match + cell[i][j] = Math.max(cell[i-1][j], cell[i][j-1]); +}