Uncategorized

Checking for balanced parentheses using the stack

Posted on

So this problem, looks to see if there is a balance between round, curly, and square brackets.

First up lets discuss the the brackets:

  • ( ) Round Brackets
  • [ ] Square Brackets
  • { } Curly Brackets

Balance can be like so

  • (){}[]
  • ([{}])
  • (((((((((())))))))))

But this is unbalanced

  • ( ] ) [   notice closing square bracket without partner
  • ( [ ) ]   notice how the curved bracket closes before square bracket closes

If given a string of brackets, how can we tell if it validly pairs up? How can we solve this?

Use an array and every time we we don’t find a matching pair, add the element. If we do find a matching pair of the current element we are looking at and the previous one then pop the match.

What are you talking about?

  • (){}[]
  • ([{}])
  • (((((((((())))))))))

Looking at the above examples, you will eventually come across a pair of matching characters that are next to each other, and as we eliminate them, it frees up other pairs to be matched.

Please explain:

Ok, we will go over this step by step.

Lets loop over the string, removing matches as we find them. Only looking at last added element in array

  • string: ([{}]) . array or “stack” : []
  • string: ({ } ] )  array: []    no match in queue, add to queue
  • string: ( [ { } ] )  array: [ (  ]   no match in queue, add to queue
  • string: ( [ {] )  array: [ ( [  ]   no match in queue, add to queue
  • string: ( [ { } ] )  array: [ ( [ { ]   match in queue, remove from queue
  • string: ( [ { } ] )  array: [ ( [  ]   match in queue, remove from queue
  • string: ( [ { } ] )  array: [ (   ]   match in queue, remove from queue
  • string: ( [ { } ] )  array: [  ]   string traversed nothing in  queue

 

Here is a REPL in case you wanted to play with the code

Hadoop: An introduction

Posted on Updated on

This past week I attended a data conference where the functional piece behind a lot of the software that was being presented was Hadoop.”Hadoop is an open-source software framework for storing data and running applications” (1)

What makes Hadoop special is that it can be run on generic hardware, and the scalability is based on the amount of hardware you have, not the speed of your processor, or disk. Now to describe this in some details in layman’s terms: Imaging you are in your office with 100 employees. You have a laptop as does everyone else. If you want to processing something, you can only process as fast as your processor will go. OK this won’t cut it. You buy a huge Dell server with 16 processors, and each processor has 16 cores, giving you 256 threads. This set you back $250,000. Now you can really process a lot of data. But imagine you took all the laptops in your office and were able to cluster or chain them together. Dell XPS 13 laptops come with 2 cores, so if you chained all of them together, you could have a virtual “system: of 200 threads. At under $1000 each thats $200,000. At this point you might be thinking, ok where is the savings? The other system has more cores for not much more money. But now that we have our thinking going, what if we instead were looking at desktops, which we can buy at $399. Suddenly we have a “200 core” system at $40,000. (100 desktops x 399). $40,000 vs $250,000. Looking a lot better. Now lets scale this this up to a data center’s worth of hardware, and suddenly we are getting supercomputer performance and datacenter prices. This is just a generic example I thought of, but it shows why in scale Hadoop seems like a good choice. Like any infomercial I feel like I need to say “But wait! There more!”

Fault tolerance is built into Hadoop. FT is having the ability to not be affected by hardware failures. If a node, name given to a server or computer point, fails it does not mean that the jobs that were on that node are lost. Data and compute is distributed so jobs are redirected and data is stored on multiple points so the data on that node is also available elsewhere. An example that might make sense would be your iPhoto pictures. If you lose your phone, via iCloud they are still available on your PC, and even in the cloud if you paid for that plan. Your photos are fault tolerant, they don’t depend on your phone alone to exist.

Those are some of the ways that Hadoop makes sense to process big data, and why it exists. As I learn more, I hope I can teach you too.

ECMAScript 2017 padEnd() and padStart()

Posted on

Two new methods implemented in the 2017 version of Javascript are padEnd and padStart. Both methods pad a string with second string, until the result is of the desired indicated length. Given that the return value of padEnd and padStart are new Strings, and the original strings are unmodified, the methods a “pure”, as they do not cause side-effects.  As you can see from the examples below, this is true as str1 in the example is unmodified.

Screen Shot 2018-09-08 at 1.04.49 AM

Now the length of our example string, str1 is 17.  If the target length is less than the size of the primary string, the primary string will be returned. If the target length is less than the size of the primary string and the added string, the added string will be truncated to fit the target length. Example below:

Screen Shot 2018-09-08 at 1.14.54 AM.png

If your browser doesn’t have these methods because it doesn’t support ECMAScript 2017 you can actually write your own methods and add them to the String prototype, extending it. I changed the styling such that it can fit in a smaller picture, so heres an example of padEnd() implemented by me: below. See also the repl link if you would like to play with it

Screen Shot 2018-09-08 at 1.37.56 AM

Screen Shot 2018-09-08 at 1.42.44 AM.png

Binary: an introduction

Posted on

If I told you I would give you 10 dollars for a Five dollar bill, you probably would be excited. But if you received two dollars, you might be livid. “Where is the ten dollars you promised?” But I said 10 dollars not ten dollars. The 10 is binary. In binary 10 is 2. Are you confused? Don’t worry, it should make sense by the end

“ a binary number is a number expressed in the base-2 numeral system or binary numeral system, which uses only two symbols: typically 0 (zero) and 1 (one).”[1]

The number system we are used to is called decimal. The digits consist of 0,1,2,3,4,5,6,7,8,9 and are part of base ten notation. With binary, or base2 notation, the only digits are 0, and 1. While it may seem counterintuitive, every number that can be made with the decimal system can also be made in binary. Coincidentally this is how computers count and add digits, binary is the “native language” of computers.

What does zero to ten look like in binary?

  • Zero     0
  • One     1
  • Two     10
  • Three     11
  • Four    100
  • Five    101
  • Six    110
  • Seven    111
  • Eight    1000
  • Nine    1001
  • Ten    1010

 

Binary addition:

 

Addition in binary is just like decimal addition, adding from right to left, and carrying over carry values.

Lets go through an example: We will add Ten (1010) and eleven (1011)

  • Ten + Eleven = sum
  • Sum is currently 0
  • 1010 + 1011 : sum 0 : carry 0
  • Remember, Start with rightmost digits: Digits we worked on will be replaced with x
  • 1010 + 1011 = 0 + 1 = 1 : sum is 1 : carry is 0
  • 101x + 101x = 1 + 1 = 10: sum is 01 : carry is 1
  • 10xx + 10xx = 0 + 0 + carry(1) : sum is 101 : carry is 0
  • 1xxx + 1xxx = 1 + 1 = 10 : sum is 0101 : carry is 1
  • Sum is 10101
  • 10101 is 21 in decimal, addition complete!

Assembly Language introduction

Posted on Updated on

What is assembly
Assembly is a language that is written as close to the hardware’s understanding while leaving room for a human’s understanding. A lot of the details hidden from view by higher order languages are exposed to the developer, which can be good for bad. “Assembly languages have the same structure and set of commands as machine languages, but they enable a programmer to use names instead of numbers.” [1]

Reasons to use assembly
“Today, assembly language is used primarily for direct hardware manipulation, access to specialized processor instructions, or to address critical performance issues. Typical uses are device drivers, low-level embedded systems, and real-time systems.”[2] Specialized hardware programmers need to address new hardware as its developed. A new piece of hardware will have new abilities that existing drivers (set of instructions) can understand. Another example could be speed optimizations. Compilers that convert higher order languages add a lot of “bloat” to executable files. A similar concept in web programming could be installing JavaScript assets like Bootstrap, when you only needed to change a button. This adds some slight bloat, which will be unnoticeable to us on modern systems. But let’s say we have a budget phone, or even a car sensor. We want minimize the size of the file run for speed returns.

 

 

React – onClick captures children as target rather than parent

Posted on

I placed  some data in the parent div that I wanted to use when the div was clicked.

Using event.target was not working because I was getting the children elements as the target and not the parent div as I wanted.


div  onClick={this.handleClick} name={item.name}  //want this as event target h2 
Welcome h2
h3 {item.name} h3
h4 Item found in: {Item.location} h4
/div

 

The fix was below:


handleClick = (event)=>{
event.preventDefault();
console.log(event.currentTarget);
const target = event.target

 

Uncaught TypeError: Super expression must either be null or a function, not undefined in React app

Posted on Updated on

Running a react app, nothing is showing up.

This is the error I am getting in console:

Uncaught TypeError: Super expression must either be null or a function, not undefined

From bundle.js:22486

In console, I  expand and click on the link to  bundle.js:22486

I get back this line, but the important part is at the end:

    function _inherits(subClass, superClass) { if (typeof superClass !== “function” && superClass !== null) { throw new TypeError(“Super expression must either be null or a function, not ” + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } // import React, {Componenet} from ‘react’

Can you see what was wrong?

I spelled Component wrong. But the error doesn’t directly say that, you need to dig a little.

One I fixed the error, I was able to see my app