Tuesday, August 9, 2016

My First Lazy Programmer Moment

One of the ways I have been trying to improve my programming abilities has been to go to a website called Project Euler and do some of their coding challenges.  In one of the problems you are asked to write a program that goes over 400 numbers in a 20x20 array and manipulate the numbers
                12 07 44 
                38 90 13
                25 64 53 (something like this, but you know, bigger)
After cutting and pasting these numbers into Sublime (a coding text editor), I attempted to turn this grid of numbers into an array that I could manipulate for the rest of the challenge.  (for those less familiar an array is a way for a computer program to store a large number of pieces of information)

The easiest to make an array in ruby is to put square brackets [] around the pieces of information you are trying to work on.  in code it would look something like 

new_array = [12, 07, 44, 38, 90, 13,25, 64, 53]

For whatever reason my computer did not to recognize the array I made as actual numbers, (well it did but it was trying to treat the numbers like they were base 8, the 9s really confused it)

One tedious solution came to mind turn all of these numbers into strings.  (a string is a collection of symbols that you would see on your keyboard they are enclosed by "quotation" marks, a number in string form is treated differently than a pure number)
Within a few minutes of trying to make the array an array of strings.  Tediously adding quotes around each pair of digits another option came to mind.  Instead of turning each number into a unique string, turn the entire array into a string that can be used.  In the awesome weirdness of programming, computer languages like ruby will act as if strings are an array, with each individual letter/number/symbol acting as a different spot in the array.  With this in mind I wrote a new program, that would turn one massive string into all of the different elements of the array that I needed.

In code this solution looks something like this
number_source = "12 07 44 
                               38 90 13
                               25 64 53"
@storage=[]

def make_into_numbers(target_string)
biggest_size = target_string.length
counter = 0
while counter < biggest_size
@storage.push(target_string[counter..(counter+1)].to_i)
counter += 3
end
end

The way the program works is as follows.  The method make_into_numbers takes in a source string, in this case, our giant grid of number, and moves those numbers into a new array.  Because the numbers are always 2 digits long and followed by a space, it seemed easier to have the program jump three places grab the first and second number from that start point and continue on.  For a sanity check, I had am output that would displace particular positions in the array that I could visually confirm that I was generating an appropriate array.  Once I confirmed that it worked, that part of the code was removed.

I know for more experienced programmers something cleaner might have been used, where the numbers were extracted by looking for the next space in the grid, or even cleaner, but this approach took me less than 5 minutes to write and it made me a bit happy.  Suggestions for better approaches are always welcome.




  We have something called a method (methods are the pieces of code people write in ruby to get the various tasks they want done).  The method is called "make_into_numbers" in the parentheses we have "target_string" this is a stand in phrase allowing the computer to know what data to manipulate while allowing the programmer to more readily re-use the code with only changing a few words.  

No comments:

Post a Comment