BryanTownTech
Thursday, February 6, 2014
More Code Puzzles for Ruby... Interview Cake examples part 2
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.
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.
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.
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
How can you slice this array to create a new array
[:cat,:orange]
Add the element
:apple
on to the end of the array.
Now take
Add the element
Now remove the element
:apple
back off againAdd 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.
Subscribe to:
Posts (Atom)