Showing posts with label program. Show all posts
Showing posts with label program. Show all posts

Monday, June 6, 2016

a/A Practice Problems: Time Conversion

Back at it again today! My plan is to continue solving these practice problems till I feel that "Oh hell yes!" sense of accomplishment. Let's get on with it.

Your mission: Write a method that will take in a number of minutes and returns a string that formats the number into "hours:minutes"

In keeping with tradition,  think about how to solve this problem before doing any actual coding. I promise it's easier than Phoebe trying to teach Joey guitar cords without a guitar. #friends

The problem is asking us to convert any number of minutes given into the hours:minutes format. In order to do this there will have to be some math involved. Maybe set an hour counter equal to 0 and every time there's at least 60 that counter should go up by one. There should conversely be another counter that starts with the number passed in and decreases by 60 each time. Then it's just an issue of formatting, concatenating variables with strings and whatnot.

1. Let's go ahead and define our function with one argument. It is already provided in Ruby, but for Javascript it would be:

function time_conversion(minutes) {

}


2. I defined two new variables here: hours, which I set equal to 0, and min, which I set equal to the parameter, minutes. (This last step might be repetitive, but it was how I solved the problem and it worked so I'm keeping it for now.)

function time_conversion(minutes) {    
   var hours = 0;
   var min = minutes;
}


3. Every time min is greater than or equal to 60, I want hours to increase by 1 and I want min to decrease by 60. To do this, I used a while loop. Inside the while loop, I want it to do all the math I explained above. Remember to include the counter (i -= 60) or you'll find yourself in an infinite loop. The same three parts of a for-loop are contained in a while loop, just the formatting is different.

function time_conversion(minutes) {
   var hours = 0;
   var min = minutes;
   var i = min;
   while (i >= 60)
      hours++;
      min -= 60;
      i -= 60;
   }
}


4. Almost done! Just need to handle some special cases-- when there is less than ten minutes, you want it to read "06" or "0-whatever" not just "6". The second case you want it to handle is when there are 0 minutes, you want it to say "00". Sounds like an if statement.

function time_conversion(minutes) {
   var hours = 0;
   var min = minutes;
   var i = min;
   while (i >= 60)
      hours++;
      min -= 60;
      i -= 60;
   }
   if (min === 0) {
      min = "00" 
      return hours + ":" + min;
   } 
   else if (min < 10) {
      min = "0" + min;
      return hours + ":" + min;
   }
   else {
      return hours + ":" + min;
   } 
}


This problem sent me for a loop. Yes, pun intended. It literally sent me into an infinite loop multiple times. Despite having to come back to this problem on different days to give it another shot, that little victory kinda made it all worth it. It's a pretty awesome feeling of accomplishment figuring these challenges out, even if it may not be the most concise code or employ best practices.. I have to actually know how do it first, amirite? Here's to the small victories!

P.S. I move to New York in TEN DAYS. (!!!!!!!!!!!!!)

Check out past problems here:

Thursday, February 25, 2016

Learn to Program: 99 Bottles of Beer

I recently found myself with some unanticipated extra time on my hands, so I decided to put it to good use and get through another chapter of Learn to Program by Chris Pine.

If you're a beginner, I highly recommend going through his *free* e-book (also offered as an actual paper book). It.is.wonderful. An actual tutorial that helps you to do more complex problems as opposed to the ol' "Hello, world" and "2 + 2" examples--who knew you could do so much!! (Kidding.)  Really though, his writing and teaching method is so clear and succinct that it  was able to help a noob (me) understand the basics of Ruby AND put it to use.

As promised, I'm going to show you the problem and walk you through my answer. Don't look if you haven't tried this yet yourself!! Apparently that's half the fun...

Here's the prompt:




Here's my program:


















I set bottles to 99 because that's the starting point. We start at 99 and count down from there.

Because of the song's repetitive nature, I knew I would want a loop so I didn't have to manually type out every line of the song. I tried while bottles > 0  and while bottles != 0, and both gave me the answer I wanted.

I put the first two lines of the song first, because we have to start at 99.
If you use double quotations you can use the #{ } inside the quotations to call on the variable bottles, but this will not work with single quotations.
     * Alternatively you could use string concatenation and write:
                                               puts bottles.to_s 'bottles of beer on the wall..'

Then I put bottles = bottles - 1 because the line after the first to has to include one less bottle than what the previous bottles variable stored (99).

The next part is an if/else statement we need to embed in our while loop because one bottle, not one bottles.
I proceeded by saying if bottles ==  then puts "#{ } bottle of beer..."
else (if bottles == anything other than 1(don't need to write)) puts "#{ } bottles of beer..."
Don't forget to put end.

That next little line puts ' '  just makes sure that we have a line break after each iteration of the song so that it's a bit easier to read. It obviously is not necessary.

So now if you try to run this program it will almost work.  We took care of everything except the very last iteration of the song. It's should still say "1 bottles of beer on the wall..."

We need one more if statement that will modify this last part of the song, which is what you see above.  No else needed.

Whew. That was a lot. I know this may seem pretty easy, and once you get a handle on things it really is... but it was also really easy to get tripped up on some little things.  If you have any questions or maybe a better way to do this same thing, send'em my way!

Until next time, friends.