Thursday, February 6, 2014

More Code Puzzles for Ruby... Interview Cake examples part 2

More Code Puzzles for Ruby...


(from Interview cake)

Given a sorted array of integers , write a method to check if a number is in the array. Focus on doing this quickly.






        This is essentially asking us to implement a binary search. You could approach this with more of a brute force approach, by iterating through the entire array and checking each value to see if it matches the value you are looking for, however with binary search we cut the array in half and then half again and then half again... (recursion!). We define a function that takes in our array, and the number we are searching for. The function also starts with two default arguments 'from' and 'to'. We assign the value of 'from' as 0 initially, because this is telling us where in the array to start. 'to' is initially set to nil, because we will dynamically assign this to represent the last index of the array. The first thing we do in the array is check the value of 'to', if it is nil then we reassign it to be the last index of our array. Next we assign a 'mid' variable. This 'mid' variable is intended to represent the index of the value that is in the middle of the array. Next we check to see if our value is greater than the value at our 'mid' index. If it is we recursively call our function, this time passing in our array, our number , and then 'mid+1', and our 'to' value. What this does is, runs through our code again, but this time it has a new starting position to begin our check. We've essentially cut our array in half. Since we know that the the array is sorted and the number we are looking for is greater than the mid point, there is no need to look at anything from array[0] to array[mid]. The same logic is at play if our value is less than the 'mid' value. Each time the method is recursively called, we divide our array in half, until the number we are looking for is equal to our value of our 'mid' index. That index is then returned. 



    More Code Puzzles for Ruby...


    (from Interview cake)

    Given and array of Numbers 1 to n (1..n) . Assume all the numbers in the array are unique, except for one number which occurs twice. Write a method to return the non-unique number.





          We start this one off by declaring a method which takes and array as an argument. The first step in our method is to declare an empty hash. My strategy here is assign each number from our array as a key in the hash and store the number of times our number occurs in the array as the corresponding value. We'll then check the values in the hash and if any value is greater than 1, we'll know that corresponding key occurred more than once in our array and should be returned as our non-unique number. Viola! If you've got an alternative solution, please post it bellow! 

      Tuesday, February 4, 2014

      Ruby Puzzles Part Two of n

      Basic array manipulation in Ruby cont...


      Start with an array of integers 1 through 5.Square each number in the array, then return the sum of all the squared numbers.




            To accomplish this I've written a small function called "squared_sum", which takes an array as an argument. The first step in the method is to set up a sum variable and set it to zero. This will act as a container for our summed numbers. Next we iterate over the array with the .each method to create a loop. For each number(num) we square that number with **2 and assign that to a variable. For each pass in the loop we add the sq_num value to the sum variable with +=. Once we have passed over every number in the array we print out the value of sum. At the very end of the code sample above (outside the method), we are simply creating the array by calling the .to_a method on a range, and then calling our squared_sum method and passing in the array we just created.

        Print out all prime numbers between 1 and n . 




             This one is a little more involved, so please hang in there. My strategy here is to create two methods. One that simply takes in a number and returns true if the number is prime. The second method passes each number of an array into the first method and then adds that number to a list of primes if true is returned. The primes list is then printed. Viola! 
            First is_prime. This method takes a number and then creates a range of number between 2 and the square root of that number. It then takes our number and using the modulo(%) determines if any of the numbers in the newly created range can be evenly divided into our number. If so our number is not a prime, and the method returns false. Otherwise it is a prime and returns true.
            Now check_array_primes. We pass in our array of numbers. The first step creates and empty array of primes. We will push any number that returns with true from the is_prime method into this array. Next we use the .each method to create a loop and iterate each number in the array. Each number is passed to the is_prime method. If that return true, it is added to our list of arrays, otherwise nothing is done.  
           Take a look at this second method. How can it be refactored? Its a pretty easy fix.

        Ruby Puzzles Part One of n

        
        
        
        
        
        
        With a slew of technical interviews on the horizon, I figured it would be worth the time to work through a series of basic ruby concepts and coding problems and Basic array manipulation in Ruby
        
        
        stuff = [:dog,:cat,:orange,:banana]
        How can you slice this array to create a new array [:cat,:orange]



            This can be done in a few different ways, however the most direct is to use the slice method, which is shown here. The slice method takes two parameters. The first is the index you would like to start at, and the second is the number of elements you would like to slice out of the array.



        Add the element :apple on to the end of the array.
           Now take :apple back off again
           Add the element :fish to the start of the array.
           Now remove the element :fish.


             For this step I'll use the .push , .pop, .unshift and .shift methods. The .push method takes a parameter and adds it to the end of the array. The converse of that is .pop. It removes the last item of the array and returns it. The .unshift and .shift methods work in the very same why, except that they preform the actions on the beginning of the array.