Skip to main content

Posts

Cash Register Algorithm

Goal: Given a price, cash tendered, and two dimensional array of change ordered by denomination (pennies, dimes, quarters, etc.), return an array of exact change. Exceptions: return "insufficient funds" if there isn't exact change; return "closed" if exact change is equivalent to the contents of the array; return a warning if the price is greater than the cash tendered. Analysis First, check the difference between the price and cash tendered. If the price is greater than cash tendered, return an error. If the price equals the cash tendered, no change is needed. From this point, we assume the price is less than the cash tendered. It helps to consider a simpler case where the cash tendered and the change on hand is all pennies and dimes.  You could proceed like this: (1) see if all of the change can be given in pennies.  If it can't (2) see if the remainder can be made up with dimes.  We could get the following cases: A. All your pennies and som...

Flattening An Array

How do you flatten an array?  That is, how do you take something like this: [1, [2, [3, [4]]]] and change it into something like this? [1, 2, 3, 4] The first thing that occurs to me is that you would go through each element of the given array and check to see whether it is an array.  So we go through: 1 (NO) [2, [3, [4]]] (YES) We're done with the non-arrays -- we can keep them somewhere (another array) but the arrays require further processing.  We process them in exactly the way we processed the first array.  The trick is figuring out how to do that organically: we need the computer to keep processing elements of the array and members of those members recursively (!) on down to the elements. What I think you would do is create a general function that processes an array into elements, then call that function not only on the array itself, but on the members of that array.  How would that look? Well, if we have an element, we don't call it. ...

How Does A Calculator Work?

A calculator has 11 numeric entry buttons (0-9 and the decimal), 4 operation buttons (addition, multiplication, division, subtraction), two clear buttons (clear entry, clear everything), and a result button (equals). At the start of operations, the entry reads "0". The user then presses one of the buttons.  If the user presses "0", the entry continues to read "0".  If the user presses any other digit, the entry displays that digit.  The user can then compound that digit into a numeral by pressing additional digits. The user might choose, however, to press a non-numeral button.  Let's think about what happens if the user presses one of the operation buttons.  To make things more concrete, let's list some actions the user might take in a sequence.  (For the sake abbreviation, I will include entire numerals as the end-product of a sequence of actions (entering digits) rather than the individual entries by which the numeral is compounded.) So the...

What Is Success, For Me?

The secret to career success is self-branding: deciding what you represent, realizing those values, and communicating your achievement to colleagues and potential employers. Deciding what you represent can be the most difficult but is also the most important part of the entire process.  Success has many dimensions -- financial, social, philosophical (for lack of a better word), even moral; carefully determining what you value will help you to synthesize all these dimensions so that you can present a united front to people in your network – and so that in the end you can achieve something you will feel proud of.  Your values help to tell your story: accreditations, experience, accomplishments, and transitions are not so many bullet points to put on a resume but represent something for those who know what they’re about.  Those who don’t, on the other hand, become lists and amalgamations; they follow the market, get certificates, get an MBA, not because they feel this is the...

JavaScript Function For Symmetric Difference

EDIT: Note that the code below works (at least as far as I know) so long as you assume each array is formatted like a set.  If either of the arrays have repeating entries -- i.e. behave the way arrays are allowed to behave -- the algorithm won't work and is seriously in error.  Oh well -- back to the drawing board. The symmetric difference of two sets is the difference between their union and their intersection. Consider the following sets: A = {1, 2, 3, 4, 5} B = {1, 3, 5, 7, 9}  The union of A and B is {1, 2, 3, 4, 5, 7, 9}. The intersection of A and B is {1, 3, 5}. So the symmetric difference of A and B is {1, 2, 3, 4, 5, 7, 9} - {1, 3, 5} = {2, 4, 7, 9}. My job was to construct a two argument JavaScript function that would correctly return this result.  I decided to proceed as follows: 1. Concatenate the two arrays to produce their "union" (with duplicate values). 2. Remove any value that has a duplicate. Here is my code: function diffArray(...

Databased

I decided to add code to store the results from my scraper into a database instead of just printing them on the screen.  I also added a bit to capture the full review text as well as the title.  In the process, I learned a few things about SQL calls: You can't use WHERE with INSERT INTO If you want to add something to an existing row, you have to use UPDATE...WHERE I spent a lot of time trying to insert the review into the same row as its associated title.  At first, I was creating a new row for each review.  Then I tried to link the review with my i  counter and insert it into the i- th row.  But that went nowhere because (A) the database wasn't associating any numbers with the rows and (B) i  counts review pages.  I fumbled around a lot, got my count INTEGER NOT NULL PRIMARY KEY  (probably overkill -- I wanted to AUTOINCREMENT  too, but I guess I was putting in the wrong command), and then after watching dubiously as the databas...

Rudimentary Scraper

I'm toying with the idea of attending a coding camp, so I was looking at a review website .  I noticed I couldn't really read all the reviews I was looking at on one page or (as far as I could tell) sort them by ranking.  I thought how useful it would be if I could just figure out a way to download all the review data and put it into a database. Scanned the HTML for a bit, but didn't make any progress figuring out where the data I wanted was.  Did a few searches that turned up some complicated looking stuff (a tutorial from a company that devotes itself exclusively to HTML scraping), gave up, came back, and finally found this .  Unfortunately, the tutorial was written for use with Python 2, so I had to figure out how to modify the libraries -- now at least I understand a bit more about why Chuck's class imports urlopen .  After that, it was as easy as mousing-over the element I wanted on the review page and requesting the text. Needs further development in a...