egonschiele / grokking_algorithms Goto Github PK
View Code? Open in Web Editor NEWCode for the book Grokking Algorithms (https://amzn.to/29rVyHf)
License: Other
Code for the book Grokking Algorithms (https://amzn.to/29rVyHf)
License: Other
I've just started to read the book and went across all the repo in search for the example of linked list in JS, so it occurred to me it would be handy for the programming newcomers to have code examples of all the themes that your book covers, not only the main ones.
thanks, for this cool edition. =)
For Charpter 9, 10, 11, Why not provide complete sample code ? For example, Longest common substring, K nearest neighbor algorithm. I really need them !
The longest common substring is wrongly in Python coded and throws an error anytime it runs, this should be prioritized since it is a core part of the book.
dp_table_blue = ["b", "l", "u", "e"]
dp_table_clues = ["c", "l", "u", "e", "s"]
dp_table = [[0 for i in range(len(dp_table_blue))] for i in range(len(dp_table_clues))] # (5,4)
print(dp_table)
for i in range(0, len(dp_table_blue)):
for j in range(0, len(dp_table_clues)):
if dp_table_clues[j] == dp_table_blue[i]:
dp_table[i][j] = dp_table[i-1][i-1] + 1
print(dp_table)
copy this code and try to run it here https://onecompiler.com/python or click this link https://onecompiler.com/python/3znsmgdbr to run it directly to get the errors.
public class BinarySearch {
public static void main(String[] args) {
int[] myList = {87, 21, 45, 93};
//add this line Arrays.sort(myList);
System.out.println(binarySearch(myList, 93));
I might be wrong, I am just a newbie
Hi,
Current
if arr[i] < smallest: smallest_index = I
should be
if arr[i] < smallest: smallest_index = i smallest = arr[i]
Hey, I wanted to point out that I found a discrepancy on the recommended time to resize a hash table. The images are from chapter 5 Hash tables, pages 92 & 94.
I'm guessing that in page 94, the highlighted load factor should also be 0.7
instead of .07
.
Thanks for the amazing work on the book btw! I'm really loving how well concepts are explained.
spelling mistake in 10_knn/README.md
The problem is in the assignment
mid = (low + high) / 2
It can lead to overflow in some language
Can you add the topic of Hacktoberfest, as I'm aiming to supply a few solutions for some languages that aren't fully covered in this book.
Hi guys, I am trying to run the Golang implementation on the graph below and the cost to the stop is the MaxInt set in the beginning.
graph["start"] = map[string]int{}
graph["start"]["b"] = 5
graph["start"]["c"] = 2
graph["b"] = map[string]int{}
graph["b"]["d"] = 1
graph["b"]["e"] = 4
graph["c"] = map[string]int{}
graph["c"]["b"] = 8
graph["c"]["d"] = 7
graph["d"] = map[string]int{}
graph["d"]["stop"] = 1
graph["e"] = map[string]int{}
graph["e"]["d"] = 6
graph["e"]["stop"] = 3
graph["stop"] = map[string]int{}
I have added the following lines in the implementation while looping through the nodes of the lowestCostNode and it seems to now be working but I am not certain if that's the best approach or otherwise.
for lowestCostNode != "" {
for node, cost := range graph[lowestCostNode] {
// The following three lines have been added
if _, isSet := costs[node]; !isSet {
costs[node] = cost
}
newCost := costs[lowestCostNode] + cost
if newCost < costs[node] {
costs[node] = newCost
parents[node] = lowestCostNode
}
}
processed[lowestCostNode] = true
lowestCostNode = findLowestCostNode(costs, processed)
}
......
for station, states in stations.items():
covered = (states_needed & states)
if len(covered) > len(states_covered):
best_station = station
states_covered = covered
......
should be like this:
......
for station, states in stations.items():
covered = (states_needed & states).union(states)
if len(covered) > len(states_covered):
best_station = station
states_covered = covered
......
Thanks for this great work on algorithms book. I now understand many of concepts which seemed difficult to me but now I'm stuck on Quick Sort algorithm and I cannot fully understand how its work.
JavaScript Version of Code (from repository):
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))
}
}
How I understanding parts of work this algorithm:
console.log(quicksort([10, 5, 2, 3]))
first step:
quicksort([10, 5, 2, 3])
pivot = 10 // get first item from array[0]
less = [5, 2, 3] // list of all item less than pivot
greater = [] // no items greater than pivot
quicksort([5, 2, 3]).concat([10], quicksort([]))
now we save this step ☝️ in stack and adding new step to up of stack 👇:
quicksort([5, 2, 3])
pivot = 5
less = [2, 3]
greater = []
quicksort([2, 3]).concat([5], quicksort([]))
and last step:
quicksort([2, 3])
pivot = 2
less = []
greater = [3]
quicksort([]).concat([2], quicksort([3]))
AND now the part I'm not sure I understand correctly:
now we concat array down to up i.e.:
[] + [2] + [3] + [5] + [10]
Why it's confusing for me? Because In "Recursion" chapter says that stack runs from up to down like on following screenshot:
also on last part:
quicksort([]).concat([2], quicksort([3]))
when quicksort function gets array [3]
:
quicksort([3])
but since base case return true if (array.length < 2) { return array }
we just return [3]
array and code stop working.
What happens on this part?
Can anyone help me to understand where I made a mistake in my explanations? What am I wrong about?
If in input list last element is the biggest, the present algorithm is'n count it.
page 59. "Sneak peak at functional programming"
I guess it's Sneak peek rather than Sneak peak.
Hi,
in p. 9 of the book (when you show the binary search algorithm), there is no division after mid = (low + high)
.
Congrats for the wonderful book!
Hey , I also want to contribute , but I am not able to create a branch in repo for pushing my contribution . It is giving Permission Denied error .
The issue was in condition for example: arr = [6,7,8,9,11,13] tar = 11
In this situation code return -1 what was the mistake.
Answer to question 2.2 says - A linked list because Lots of inserts are happening (servers
adding orders), which linked lists excel at. You don’t need search
or random access (what arrays excel at), because the chefs always
take the first order off the queue.
Isn't in queue insert always happens at last and array would be better choice ?
Or this answer is driven by considering factor - "what if required space is not available in existing contiguous block ? "
Should i rewrite all the algorithms in Python 3 ?
Suggestions ?
while
), and this solution doesn't work for all cases (for example, binarySearch([1,2,3,4,5,6,7,8,9,10], 3)
return null);As I read the book, I can create issues and fix them for JavaScript/ES6 code examples.
Hi, I'd like to add examples written in Perl 5, if you don't mind.
At the moment 01_binary_search.pl and 02_selection_sort.pl are ready.
How do you find this idea ?
I would like to contribute C# examples to your project. I have a question though. Do you prefer idiomatic examples or examples that are essentially translations of the Python code? I can do either.
In BinarySearch.java
Since the array myList is unsorted, the binary search algorithm may not produce the correct result. While the algorithm might still run without errors, the output cannot be guaranteed to be accurate because it's searching in an unsorted context.
If you provide me permission I will open the Merge Request for the bug.
selection sort should be in-place, why you create an additional array to keep sorted elements in it?
For the given graph, when I changed the values of A and B, where A < B
. I don't get the desired path.
Here's the link to the code. I have checked for negative-weight edges and found none. I have also implemented a function that shows the final path.
In the book on page 9 there is binary search code written in python.
In the while loop you have assigned mid = (low + high) instead of mid = (low + high) / 2.
This is not a major issue but in your future copies if you correct it then it would be great.
I hope this is useful.
The book is amazing and easy to grasp I am enjoying it.
Just change the union return values to Concat()
int[] myList = {87, 21, 45, 93};
The array is not sorted.
Must be:
int[] myList = {21, 45, 87, 93};
diff('fosh', 'fish') // displays 2, but correct 3
05_binary_search_recursive.py - fails for cases like these,
[1, 3, 5, 7, 9], target = 5
OR
[77, 95, 101, 255, 309, 570, 600], target = 255
A check needed before calling the method recursively.
if arr[mid] == target:
return arr[mid]
There is no solution for typescript
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.