Sunday, January 31, 2016

Euclidean Algorithm for GCD

The Euclidean algorithm is a cool little way to find the GCD of two integers. 

Say you have 46 and 90...

Then 95 = 46 * 2 + 3
And 46 = 3 * 15 + 1
3 = 1 * 3 + 0

so the GCD of 95 and 46 is 1. Maybe not the best example, but I'm hungover so you can google it.

Friday, January 29, 2016

Memory And Hex

As you may know I'm taking a class in C and C++ this semester (Spring of 2016) and have found C to be the best language ever (revised 2017: it's great and all, but not THIS great). It's really fun because of it's simplicity, the lack of functionality and user interface makes it easy and demanding of creativity. 

I just started reading up on pointers and memory addresses when I got curious about just how memory addresses are stored in pointers. Try compiling and running the simple function below to see how memory addresses are stored as hex values.


Monday, January 25, 2016

Probably Handy

Since I'm taking a class in C now I think the table below that I found here will come in useful at some point...

PrecedenceOperatorDescriptionAssociativity
1++ --Suffix/postfix increment and decrementLeft-to-right
()Function call
[]Array subscripting
.Structure and union member access
->Structure and union member access through pointer
(type){list}Compound literal(C99)
2++ --Prefix increment and decrementRight-to-left
+ -Unary plus and minus
! ~Logical NOT and bitwise NOT
(type)Type cast
*Indirection (dereference)
&Address-of
sizeofSize-of
_AlignofAlignment requirement(C11)
3* / %Multiplication, division, and remainderLeft-to-right
4+ -Addition and subtraction
5<< >>Bitwise left shift and right shift
6< <=For relational operators < and ≤ respectively
> >=For relational operators > and ≥ respectively
7== !=For relational = and ≠ respectively
8&Bitwise AND
9^Bitwise XOR (exclusive or)
10|Bitwise OR (inclusive or)
11&&Logical AND
12||Logical OR
13[note 1]?:Ternary conditional[note 2]Right-to-Left
14=Simple assignment
+= -=Assignment by sum and difference
*= /= %=Assignment by product, quotient, and remainder
<<= >>=Assignment by bitwise left shift and right shift
&= ^= |=Assignment by bitwise AND, XOR, and OR
15,CommaLeft-to-right

Thursday, January 21, 2016

Two's Compliment

Today I'm reading about two's complement and the difference between signed and unsigned integers. In the near future I'm expecting to know a bunch of random stupid (and probably useful) info about ascii, utf, and hex representations of different characters... Starting by reading here.

Saturday, January 16, 2016

Calculator

Remember that shunting yard algorithm post? Well I turned it into a calculator demo on my website. I made this alongside the implementation of redux that I've been posting about.

check it out here

Tuesday, January 12, 2016

Javascript Objects

Often, Javascript objects are used as a hashtable with a string being the key value. I was curious if this is how they were implemented behind the scenes and found that I was clearly not the first one. Check out this quora question... 

I think it's pretty cool they're not done with hashtables in the V8 engine.

Monday, January 11, 2016

Instanceof String

I was playing with javascript error handling and understanding where to use try/catch today when I ran into an interesting problem. As part of my error handling I tried to include a type check on the key portion of a key-value pair, just to ensure that the key is actually a string and not null, undefined, numeric... 


That's when I noticed the following...


According to MDN (ctrl + f "Primitives and string objects") javascript handles string primitives differently from string objects. When creating a string with single or double quotes, or the global String object, javascript returns a primitive string. However, when creating a string with the new keyword, javascript returns a new string object. This makes sense when you read it, but if you're unaware or unfamiliar it could easily bite you. 

Finally, the documentation does say that "JavaScript automatically converts primitives to String objects, so that it's possible to use String object methods for primitive strings". The problem is that Javascript only does this for String object methods, but instanceof is not a string object method. Instead, since instanceof is a global relational operator, the string created by quotes remains a primitive string and fails the instanceof String test.

Saturday, January 9, 2016

TrainShunting

For ages now I've been trying to implement the Shunting Yard Algorithm to take an expression and evaluate it. Yesterday I finally did it! I completed an iterative Shunting Yard Algorithm along with an evaluation function. The current calculator application is just using the built in javascript eval() function, but now that my shunting yard algo is finished I can use that!

The reason my algorithm is special is that it doesn't just return the prefix statement, but also returns the operator stack and output stack after each pass through the loop! This was actually a little tricky because I needed to make copies of the array at each stage rather than just make a reference to the array. If I had not used Array.from() to make a copy at each stage, I would end up with an array of references to the same object. In other words I would end up with a javascript object containing the final prefix expression, an array of multiple copies of the final prefix expression, and an array of empty arrays.

Another problem I ran into was that, since I was doing this algorithm so infrequently, I never actually took the time out to learn it. I was trying to implement it from pure pseudo-code rather than understand what was actually happening. Understanding what was happening wasn't easy either though, I had never heard of operator associativity or precedence (maybe they're worth a short post in and of themselves). Once I finally had a grasp on it thought, the implementation was fast.

One improvement I want to make is to my evaluate function. I think there must be a way to evaluate the prefix expression recursively (like a recursive in order tree traversal), but right now I'm evaluating it iteratively.

Here's the code, it's got a lot of cleaning up and commenting to do, but what I've got now is in functioning order. Also notice that it currently doesn't handle sin, cos, tan functions.

Friday, January 8, 2016

FFR: Pizza Theorem

In the case that two people are sharing a pizza in which no cut divides the pizza directly in half, apply the Pizza Theorem. Alternate slices chosen to ensure people eating get exactly half the pie.