text
stringlengths
0
897
if list[i] = x then return i
i := i + 1
return -1
```
This is actually an algorithm called *sequential search*, and is in fact what MiniScript does when you use the `indexOf` intrinsic method. It's the best possible algorithm for an *unsorted* list of items, where the thing you're looking for could be anywhere; you simply have to check each one. But if we know the list ...
```pseudo
function find(x, list)
i := 0
while i < length of list
if list[i] = x then return i
if list[i] > x then return -1
i := i + 1
return -1
```
But it turns out we can do even better, by using the *binary search* algorithm. Instead of keeping track of just one position in the list (like `i` in the pseudocode above), binary search keeps track of two positions, called `min` and `max`. And on each step, it moves one of those by quite a lot, quickly zeroing in o...
Googling "binary search" quickly led me to a nice lesson about at Kahn Academy, which gives the pesudocode as:
D>The inputs are the array, which we call array; the number n of elements in array; and target, the number being searched for. The output is the index in array of target:
D>1. Let min = 0 and max = n-1.
D>2. Compute guess as the average of max and min, rounded down (so that it is an integer).
D>3. If array[guess] equals target, then stop. You found it! Return guess.
D>4. If the guess was too low, that is, array[guess] \< target, then set min = guess + 1.
D>5. Otherwise, the guess was too high. Set max = guess - 1.
D>6. Go back to step 2.
Here's a very different style of pseudocode! In case it wasn't clear already, everybody's pseudocode is a little different. That's OK, because it only needs to be clear to humans, not to computers. We can work with this. Just convert it into MiniScript, step by step.
```miniscript
find = function(array, target)
n = array.len // (no need to pass this as a parameter!)
// step 1:
min = 0
max = n-1
while true // (loop from step 2 to step 6)
// step 2:
guess = floor((max + min)/2)
// step 3:
if array[guess] == target then return guess
// steps 4 and 5:
if array[guess] < target then
min = guess + 1
else
max = guess - 1
end if
end while // step 6
end function
```
Note that while the pseudocode said that the number `n` of elements in the array was to be one of the inputs (i.e. parameters to the function), there is no need for that in MiniScript; a list already knows its own length. So our implementation takes only the list, and the target value. Also, while the pseudocode uses...
Type that in and give it a try on this data:
```terminal
]find [3,6,10,14,19,27], 10
2
```
It works! We asked for the index of value 10, and it returned 2, which is correct since element 2 of our list is indeed 10. But now try this one:
```terminal
]find [3,6,10,14,19,27], 11
```
Uh-oh! This call never returns; it is stuck in an infinite loop. Looking at the pseudocode (or our MiniScript implementation), it is easy to see why: the only `return` statement in it is when the target element is found. If the target is not found, it will never return. So the pseudocode algorithm we found here is ...
When you run into something like this, first, give a cheer for testing! It's so much better to discover things like this right away, rather than later when this code has become some small part of a much larger program. And next, you have two options. Either search for a different variation of the algorithm, and impl...
When a program is stuck in an infinite loop, the first step is usually to get a better understanding of what state it's in when it's stuck. Press Control-C if your program is still stuck, and then insert a `print` statement right after we calculate the guess, on line 9:
{number-from: 9}
```miniscript
print "min:" + min + " max:" + max + " guess:" + guess
```
Now run that same test that failed before. It will very quickly start repeating this line over and over:
```terminal
min:3 max:2 guess:2
```
Now notice something odd about this — the "min" is actually greater than the "max" value! As we move the `min` value up and the `max` value down, we will always either find the target value, or end up in this situation where `min > max`. So, the solution is to just check for that. Change the `while` statement so tha...
Now try both tests again (remember that in Mini Micro or command-line MiniScript, you can press the up arrow to recall a previous command). It should work now.
If you have a very large list of items, and know that this list is always sorted, this algorithm will often be faster than the built-in `indexOf` method. So now you have a handy new tool in your toolbox, *and* you've learned how to fix a flawed algorithm you find on the internet!
## k-Means Clustering
For our last example, we're going to look at a machine learning algorithm called "k-means clustering." Suppose you have a bunch of data points, characterized by X and Y values, and you want to divide those points into groups ("clusters") so that each point is mostly surrounded by other points in the same group. How d...
D> This problem actually came up in my day job quite recently. We were building a large multiplayer game. To keep network traffic down, we wanted to divide all the players into groups of nearby players, so we can send frequent updates only to other players in the same group. k-means clustering is one way to do that!
The k-means clustering algorithm is a fancy name for a simple idea. Each group (or "cluster") is defined by the centroid (average position) of all the data points assigned to it. We simply alternate between assigning each point to the nearest cluster, and moving each cluster to the centroid of its assigned points. I...
```pseudo