# Flatiron Blogger Magazine

### Select a search typeSearch by title Search by author Search by keyword

Road to The Stored-Program Computer by on 06/20/2018
Linear Search vs Binary Search And Why We Should Approach Any Problem Using Binary Search As A DeveloperLet’s say we are given a sorted array of n elements and asked to search for a given element in the array.A simple and straight forward method is to use linear search, which we start from the first element, compare one by one, and find the element we want. To demonstrate, say we have a sorted array of 10 different numbers from small to large and we want to search for 70:In worst case, it requires us to run through the whole array to find it, that is, when the element is at the last index. If n increases by m, the comparisons/guesses the algorithm needs increases by m as well. So we can say linear search has a worst case run-time complexity of O(n) (more on this below).What about we use binary search? And what is a binary search?From Wikipedia, “Binary search compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until the target value is found. If the search ends with the remaining half being empty, the target is not in the array”. Let’s draw it out to demonstrate:Say we have the same array and we want to search for 70:At first step, we find the midpoint, and divide it in half, so visually we have:We search in the first half, 70 is not in them. So let’s ignore it, and for the second half, we find the midpoint and divide it in half again. And since it has odd number of elements, let’s also include the median number (we can also not include the median number, it won’t make a real difference since the other half will include it then), so now we have:For our new first half, let’s search for 70, it is still not in them. So let’s again ignore it, and find the midpoint and divide it in half on the second half. Now we have:And this time, 70 is in the new first half. We have completed a binary search.As we can see, binary search is much more efficient than linear search, since every time we only need to search through half of the remaining array. In fact, binary search only has a worst case run-time complexity of O(log n) (log by convention is base 2). And let’s compare how different in performance O(n) vs. O(log n) can be — say we have a sorted array of 1,000,000 elements:At worst, linear search needs a run-time of 1000000 guesses to find the target element.At worst, binary search needs a run-time of log(1000000) = 19.93, approximately 20 guesses to find the target element.That’s the difference!Now you may ask, why does binary search has a worst case run-time complexity of O(log n)?To recap, according to Wikipedia: in computer science, big O notation is used to classify algorithms according to how their running time or space requirements grow as the input size grows. It characterizes functions according to their growth rates.Thus for linear search, it is straight forward that as the size of the array grows, the worst-case run-time complexity is O(n).What about binary search?For simplicity reasons, let’s assume we are given an array of 8 number of elements and the one we need to search “happens to” be at the last index (worst case situation). So each time, we divide our array by half until we have only one element left, that is:If we refactor:For n elements, let’s refactor:Let’s put it in logarithmic form since we need k:And that’s why the worst-case run-time complexity for binary search is O(log n). The most amazing thing about binary search is that, every time the size of the array doubles, the number of run time guesses only increases by 1 only!Now think about how we normally approach a problem…By intuition, we usually use linear search, right? But now we know binary search can be so efficient, especially for a big problem…So, as developer, how do we debug?To finish, I want to quote Dan Abramov:Use binary search and scientific method. Not literally in the code, but in how you approach it. Have a bug somewhere between callsites A and B? Put a log in the middle. It’s between A and the midpoint? Put another log in the middle. Something’s wrong with some input? Eliminate half the input. It’s working? Try the other half. Etc. Debugging can feel very arbitrary but it’s straightforward when you do it mechanically. Observe, form a hypothesis, come up with a way to test it, repeat. And cut things in half when there are too many.Reference:What does the time complexity O(log n) actually mean?
Pry Your Eyes Out: Pry Troubleshooting Tips by on 06/16/2018
Web Accessibility Is A Thing by on 06/17/2018
Explaining React Lifecycle Methods by on 06/14/2018
This is…Javascript by on 06/18/2018
Console.log(“tips”) by on 06/13/2018
How To Get Started With Chart.js In React by on 06/18/2018
Ruby by on 06/17/2018
Workflow in Ruby by on 04/18/2018
Why JavaScript? by on 05/06/2018
Micro-Controllers by on 05/28/2018
The Tessel 2So what is a micro-controller?A micro-controller is a small computer that is completely programable. The most common one is Raspberry Pi. People have built some pretty amazing things with micro-controllers and a little bit of hardware know-how. Here are some examples.Media Center built with Raspberry PiArcade CabinetEven RobotsWhy are they important?https://medium.com/media/658096af3110351dde02fefa5838cfc4/hrefLots of people have ideas for inventions. But in the past bringing that idea into reality took knowledge in multiple disciplines. It involved understanding things like this.And a suitable language to code in. But with micro-controllers things are much simpler. All you need now is a little knowledge of soldering and a language.The more popular types of micro-controllers , e.g. Raspberry Pi, support multiple easy to learn languages like Python and Ruby. Because of the ease of using these micro-controllers the world of invention is now much more open to many more people.https://medium.com/media/b16dbe62e7fd78a80e439c6bfb3254c6/href
React.js vs. Vue.js by on 06/17/2018
Coding like Kung Fu by on 06/14/2018
A high level overview of Defensive ProgrammingHow many hacks or epic software failures in history could have been avoided? Are software vulnerabilities mostly a matter of hardware faults or software inefficiencies? How can I define a secure program? Are program failures predictable as edge cases evolve?These were some of the questions that inspired me to search for a high level view of defensive programming. I figured, the answers to the questions above can lead to better nights of sleep for software developers and owners alike.As defined by Wikipedia, defensive programming is “ a form of defensive design intended to ensure the continuing function of a piece of software under unforeseen circumstances”.Informally, I’ve found definitions such as, “ defensive programming is when the programmer makes necessary assumptions and creates code that anticipates potential problems and specification changes”.As with all things that are broad there is a degree of subjectiveness to the views of what actually composes the subject. This vast domain is hotly debated amongst cyber security enthusiasts, researchers and newbies like me who have interest in a rapidly evolving field of large problems &amp; lucrative opportunities.“Distrust and caution are the parents of security.” — Benjamin FranklinMany view defensive programming as a matter of time, either as a time waster or something they do not have enough time for. I hypothesize that these thoughts may be the culprits behind many costly errors and exploitations. Here are a few examples of very bad things that may have been avoided using a defensive programming thought process:Exhibit A: The Dhahran Patriot Missile Detection Failure 1991US Customs Computer Glitch at Los Angeles International 2007Stock Market Algorithms Cause Flash Crash of 2010Through my review I have discovered that defensive thought processes give a program many different “abilities” that provide a sense of security against random events. These include:TestabilityMaintainabilityPortabilitySupportabilityDeployabilityMost importantly, when using defensive coding principles we stand to improve our code’s comprehensibility and position our systems to perform predictably despite unknown risks from inputs or users.As for Kung fu, well, defensive programming may be similar in many more ways than I know of, but it surely shares commonality in the fact that Cyber Security is as diverse of a subject as Martial Arts.While researching, I decided to create a map of the concepts I felt really stuck out as the core elements of defensive programming . It is my goal to explore these concepts further throughout my education and software career.My personal taxononmy of defensive programmingI’ve recently began my defensive programming journey by utilizing validations and unit testing. Validations in particular presents a unique thought exercise that forces me to contemplate security and user experience in unison. This is a lot of fun as I see it as a way to truly build cool things for an end user. This happens all while I am thinking of all the ways to break what I am building. At times it seems like it makes no sense, but be reminded of the events above.Specifically, validations vet user inputs before they are committed or persisted to our data storage systems. Validations can happen at various levels of software and hardware.Ruby Model Validation Helper MethodsAs for web applications using the MVC framework, validations can typically be stored in the model, controllers, views and database itself. These varying levels of validation presents many opportunities to not be a lazy developer and keep the “abilities” of our software in optimal state.JQuery Form Validations in the browserIn the end, I hope to practice, learn and implement enough on my defensive programming journey to rival the challenges of the ever evolving human &amp; digital worlds. I personally find software architecture principles, methods to validate and test my systems very interesting. Then again, who am I?