Learning Recursion in Ruby

“In order to understand recursion, you must first understand recursion.” – Source Unknown

How it Clicked For Me

The key to understanding recursion is to get the base case.

To find the base case, define the easiest result.

Then think about the second easiest result.

As soon as you can get the first, second and third value, you have the solution for infinity.

It’s important to focus on the simplest case because when learning recursion, your mind can wander in the possibilities.

In the subsets example one can find a lot of strange patters when looking at all the subsets. Things like:

• Could we work on this based on array length?
• Is there a pattern in the order?
• Perhaps we could shuffle the arrays and ask if they are in the range?

These sorts of thoughts can go for infinity. I think this is what recursion is seen as such a challenging thing. There is a lot of room for thinkers to develop ideas that get more and more complex as the program gets longer.

In the end, recursion will win.

As I work my way though the AppAcademy.io practice problems, I’m going to keep notes here.

I’m almost certain that there will be mistakes and inaccuracies with these notes.

Please keep in mind that these are notes.

I make them public in the hopes that my insights may provide some value to someone in a similar position to me right now.

0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20

Notes and Understandings From Individual Problems:

Upon opening the file and beginning the lessons, I’m pretty sure that the gist of this is going to be the same that I document in the notes on the introduction.

There will be a lot of learning code and then repeating it under different circumstances, again and again, until it’s committed to functional memory.

00-reverse.rb

This test is all about taking a string and spelling it backwards.

Sure, you could use < “string”.reverse > and be done with it:

This doesn’t really help because we develop our skills by finding interesting arrays of ways to do the same thing. Quick note, this was the first time I typed a method into the practice problems and received all true responses without a single drop of input from the outside world. It was cheating… but it is a start. 😀

OK – So as I began this process, I hit a road block. I started thinking outside the box and writing code that may have solved the problem. When I ran that creative code, I got nothing but errors. Not helpful errors which explained where the problem in the code was, but errors that just say vague stuff like this:

So this means that it’s just not working. I spent a few minutes trying to debug but I got the same errors. With my discouragement, I went to facebook and checked my email. Essentially, I stopped progressing.

How I’ll proceed through the practice problems:

So from now on, I’m just going to remember the solutions to the practice problems.

I’m going to copy the solutions symbol for symbol. Then I’ll run them to ensure I got it right, then delete it all and try to write it from memory. I’ll keep doing this until I can write it three times without making an error. It’ll be a slog.

There are 20 practice problems so I expect this exercise to take about a week or so to complete.

I don’t know exactly how I’ll document it… but I’ll keep notes and thoughts on this page.

Updated Dec 15th, 2015

Practice Problems

The way I’m doing these practice problems is just like I illustrated in the Ruby notes in previous note sheets. First I read the problem and give it as good a shot as I can. Generally, I fail miserably. Not a problem though. I’ll dig into the solutions and doctor up my code until it works. I try to make mental notes and connections as to why each element is needed and why each piece is where it is.

Then I will try again to write the code by memory/intuition. This helps me test my learning pace and it keeps me moving. The worst thing I’ve noticed is when I hit a roadblock and stop working.

I’ve been having a lot of fun with the practice problems. The first three were great for learning… but not so mentionable here because they are so self evident. This one is worth mentioning because the solution is so elegant. Here’s the problem:

Write a method that takes in an integer `num` and returns the sum of all integers between zero and num, up to and including `num`.
Essentially we’re writing a program that does this:
if x = 5

5 + 4 + 3 + 2 + 1 = 15

x needs to be interchangeable.

At first I put a lot of effort into working out the code for (num – 1) + (num – 2) + (num – 3)…. but it wasn’t working well because it’s hard to write the index code necessary to add all the elements without knowing how big the numbers would get. Also, we want the computer to answer the questions if we ask for the answer if the number is 1,000,000 and that becomes a challenge in terms of code size.

The answer is quite elegant though… and I can tell in describing the beauty of this, that I’m turing into a geeky coder. Here’s the answer:

``````def sum_nums(num)
result = 0
i = 0
while i <= num
result += i
puts(i)
i += 1
end

return result
end``````

January 5, 2015

Note: I just now learned how to save the indentation data via WordPress. Unfortunately, I’m writing this long after I learned about the <pre> code. I’ve don’t all but one of the problems in this a/A problem set so I probably won’t go through and fix all of the problems, but going forward, I’ll post it right. Back to the learning:

So the code works up

0
0 + 1
0 + 1 + 2
0 + 1 + 2 + 3

until it reaches the requested number limit

etc….

cool stuff. Here’s how it looks with the code open and if I slip a puts for the i value in there to see how the computer runs the code:

Updated Dec 16, 2015

01-factorial.rb

Write a method that multiplies a number by all the numbers before it.

Example: 7 would be 7 * 6 * 5 * 4 * 3 * 2 * 1

On a calculator it looks like this 7!

One way to do this is to take the number, and create another number by subtracting one from that number. Then multiply the numbers like that over and over again until you’re at zero.

That program looks like this:

```def factorial(n) result = 1 while n > 0 result = result * n n = n - 1 end return result end```

Another way to do it is to multiply 1 * 2 * 3 * 4…. up until you hit the number… but I don’t know how to do that right now.

But does Ruby offer a *= to add via multiplication?

I ran this program as a test:

```num = 3 print(num *= num)```
It printed 9 so it seems to work.

So I wrote this program as a method for defining

```def factorial(n) start_num = 1 result = 1 while start_num < n result = result * start_num start_num = start_num + 1 result *= result end return result end```
Which printed:

1
1
1
4
144
factorial(0) == 1: true
factorial(1) == 1: true
factorial(2) == 2: false
factorial(3) == 6: false
factorial(4) == 24: false
1 – 1 – 4 – 144

So, this is confusing and I’ve lost focus. I feel like the above answer is valid… and I’m confident there is a way to do this, but I’ve been thinking about this too much and I don’t feel like I’m making lots of progress so I’m going to move on.

Update: I figured it out. Here’s how to get factorial of a number by working up (eg. 1 * 2 * 3 * 4….):
```def factorial(num) idx = 1 result = 1 while num > idx - 1 result *= idx idx += 1 end return result end```

Working down:
```def factorial(n) result = 1 while n > 0 result = result * n n = n - 1 end return result end```

Differences Between working up and working down:

Counting from 0 up towards the sum or the factorial requires an extra variable to do the counting. This makes it more complex.

Counting back from the selected number is more elegant of a solution, though the degradation of the original variable could be an issue in more complex programs.

02-longest-word.rb

This problem is easier because it doesn’t involve lots of counting or math. We just write a program that moves through a string comparing each word by word length and saving the longest word. Then we print the longest word.
```def longest_word(sentence) idx = 0 longest_word = "Huzzah!" words = sentence.split(" ") while words.length > idx current_word = words[idx] if longest_word == "Huzzah!" longest_word = current_word elsif current_word.length > longest_word.length longest_word = current_word end idx += 1 end return longest_word end```

This one seems fairly straight forward to me. The learning aspect was in seeing how to do this in an iterative way. While writing the code, we first break down the sentence into words. Then we write a process that will execute for the number of words in the sentence. With each execution, the program selects the current word and compares it with the longest word. The longest word is replaced by the current word when the current word has more letters than the word that was previously assumed to be the longest word. This keeps going until we reach the number of words in the sentence. The program finally returns the longest word and we’ve got a cool program.

03-sum-nums.rb

This one was a bit of a challenge due to it’s similarity to problem 01-factorials.rb. While learning it, I would mix the notation behind these two methodologies because they are similar in their iterative way of seeking solutions.

The sum of the numbers leading up to 4 is 4 + 3 + 2 + 1 = 10
The factorial of the numbers leading up to 4 is 4 * 3 * 2 * 1 = 24

So they are similar, but the way we do it is much different.

Here’s how to get the sum nums:
```def sum_nums(num) result = 0 idx = 0 while idx < num result += num num -= 1 end return result end```

This is how to do it by working from the high number down. So for num = 4 the program adds 4 + 3 + 2 + 1

Alternatively, we could use the same system we learned in factorials and work our way up

```def sum_nums(num) sum = 0 idx = 0 while idx <= num sum += idx idx += 1 end return sum end```

04-time-conversion.rb

This was a problem I was able to solve in a separate ways from what the solutions read without the support. The thing that threw me off was converting the integer feedback to a string. Specifically, listing the seconds when less than ten. I didn’t think to add the string in as zero with the if/else statement. It’s obvious now, but at the time I didn’t think of it.

I used modulo to get a true reading via the code below.

```def time_conversion(minutes) hours = minutes / 60 minutes = minutes % 60 hours_disp = hours.to_s if minutes < 10 min_disp = "0" + minutes.to_s else min_disp = minutes.to_s end return hours_disp + ":" + min_dip end```

My own above solution is different from the solution that is provided by a/A. They used a different attack method in which they subtract out minutes and add up the hours based on how many loops can be run through the number of minutes provided.
```def time_conversion(minutes) hours = 0 while minutes >= 60 hours += 1 minutes -=60 end if minutes < 10 disp_minutes = "0" + minutes.to_s else minutes >= 10 disp_minutes = minutes.to_s end return hours.to_s + ":" + disp_minutes end```

I don’t know which system is better. In fact I think they both work well. Perhaps my system would be faster because the code only runs through 2 mathematical processes before converting to strings and feeding the correct answer. It will run the same number of processes in any situation… The code above will have to work harder and harder the larger the minutes.

I’m unsure of the exact science behind all this… but playing these guessing games helps me to cement the code and the syntax into my functional memory.

05-count-vowels.rb

I did this one wrong a bunch of times. My initial reaction was to try to do it with an array that had all the vowel strings inside, but I couldn’t make it work at the time of writing this (I’m confident it’s possible, I just don’t have the proper syntax yet.)

Anyways, I can’t remember the first way I solved the problem. I’ve since committed this way of doing it to memory so I write it up like this now:

```def count_vowels(string) vowel_count = 0 idx = 0 while idx < string.length if string[idx] == "a" || string[idx] == "e" || string[idx] == "i" || string[idx] == "o" || string[idx] == "u" vowel_count += 1 end idx += 1 end return vowel_count end```

What’s this doing? Essentially, it’s just moving through the string 1 position at a time asking, is this a vowel. When it is, the vowel count goes up one. When it isn’t, the vowel count doesn’t change and we move onto the next position in the string. Easy peasy.

06-palindrome.rb

A palindrome is a word which is spelled the same forwards as it is backwards. “Hello” is not a palindrome, “hellolleh” is a palindrome. Got it?

So how do we write a program that can tell us if a string is a palindrome? Well, if the string is the same in reverse as it is forwards, then we have a palindrome.

Here’s how to write that program:
```def palindrome?(string) reverse_string = string.reverse if reverse_string == string return true end return false end```

This isn’t the way that the solutions from a/A describe how to do this test.

The solution given in the material does it by writing a looping program which runs through each position of the string forward and backwards comparing them.

Here’s an example. For the string “hello” the program takes the first and last letter of the word and asks if they are the same. Is “h” and “o” the same? because they aren’t, the program returns false. For the string “hellolleh” the program would ask if the first and last string are the same. Because they are both “h”, the program continues on to the second position and the second to last position. Because they are both “e”, the program keeps going until the position work all the way to compare the 9th to last and the 9th position. Once we get to the end of the string, the program doesn’t negate the question and the loop ends. Then, because we can’t prove it’s not false, we return true. Here’s how the program works:
```def palindrome?(string) idx = 0 while string.length > idx if string[idx] != string[(string.length - 1) - idx] return false end idx += 1 end return true end```

So which of these programs is more efficient? I think it’s obvious that my first impression is the faster, more elegant program. However the skill of comparing string positions in an iterative way is helpful as a learning process.

Also, the (string.length-1) – idx is a bit of code that could be used a lot in the future when you want to work from the end of an array towards the beginning. This could be used to spell something backwards too.

Note: This could be another solution to the 00-reverse.rb program we did first.
```def reverse(string) reverse_string = "" idx = 0 while idx < string.length reverse_string += string[(string.length - 1) - idx] idx += 1 end return reverse_string end```

This is a cool example of why it’s valuable to learn to do things inefficiently at the beginning. We’re not actually trying to solve these problems as much as we’re trying to develop the critical thinking tools to create new things with existing tools. Counting backwards using this string[(string.length-1) – idx] in a while loop will be useful for the life of a programmer I would guess.

Of course, we could even write another way to do this problem based upon the lessons learned in 00.reverse.rb. The code we used for that could be implemented here to solve the problem too:
```def palindrome?(string) rev_string = "" idx = 0 while string.length > idx rev_string += string[(string.length - 1) - idx] idx += 1 end if rev_string == string return true end return false end```

In the above example, we’re writing the string backwards and then comparing the reverse to the original. If they are the same, we have a palindrome, if not we don’t. This is the same as my initial solution, but it’s in a longer, more complex program.

December 31st, 2015

I’m just running through the problems now, doing them all quickly and in 2-3 different methods of getting the same answers. I noticed the following lesson:
```def palindrome?(string) if string == string.reverse return true else return false end end```

The above code does NOT work. Why? Because I’m trying to execute a method in a conditional statement. These need to be separated by declaring new variables. Here’s the code for how to do it so it works:

```def palindrome?(string) rev_string = string.reverse if string == rev_string return true else return false end end```

This is just a quick lesson that I think will prove endlessly valuable in the future.

07-most-letters.rb

In this one we’re given a string and asked to return true if there is an “a” within in the string a “z” within 3 positions of that a.

Here’s the program/method that my brain defaults to when I just write it from scratch:
```def nearby_az(string) idx = 0 while idx < string.length if string[idx] == "a" && string[idx + 1] == "z" || string[idx + 2] == "z" || string[idx + 3] == "z" return true end idx += 1 end return false end```

It prints true so it’s a good solution. But after comparing this program to the solution provided, mine is quite different.

Mine just moves through the positions of the string one by one asking if the letter is “a” and it the next three positions are “z”. This works great… but the solution they provide has lessons in thought process that I think will be important to a computer programmer, so I’m going to write them out and define how they work.
```def nearby_az(string) idx1 = 0 while idx1 < string.length if string[idx1] != "a" idx1 += 1 next end idx2 = idx1 + 1 while (idx2 < string.length) && (idx2 <= idx1 + 3) if string[idx2] == "z" return true end idx2 += 1 end idx1 += 1 end return false end```

This program is different in a few ways:

• It separates loops in seeking string[idx] == “a” and string[idx] == “z”.
• Next syntax: If string[idx] != “a” then jump out of the loop and move on. This skips looking for “z” when string[idx] != “a”
• while (idx2 < string.length) && (idx2 <= idx1 + 3) – > This is a cleaner way of asking the follow up questions. If the string doesn’t go any longer, it’s smarter to not ask if that blank space is “z”.
• Another note is that we need to list idx += 1 twice in this method because the next breaks us out. If we don’t do it outside of the next sub-loop, then we get an infinite loop.
• idx2 then loops through the next 3 letters to check to see if they are “z”

This is the kind of problem I have a hard time with because I just barely understand why each part does what. For that reason, I make mistakes when writing the program from memory. Why is this >= rather than just >. When we don’t know the answers to questions like this, writing the program becomes more a function of memory, rather than cause and effect understanding. These two ways of writing are critically different. Memory creates boxes, understanding cause and effect creates tools to do anything.

I’m really close with this problem. I can write it both ways right now, but I still make errors with the second way. I’ll have to come back to it in a little bit.

I’m not sure if I know this, or if I’ve just remembered it now.

Will come back another day and write the code again.

Updated December 27th 2015

08-two-sum.rb

Ok – Here we go on two sums.

This one is immediately challenging because I don’t know how to return arrays off the top of my head right now.

```def two_sum(nums) i1 = 0 while i1 < nums.length i2 = i1 + 1 while i2 < nums.length if nums[i1] + nums[i2] == 0 return [i1, i2] end i2 += 1 end i1 += 1 end return nil end```

I almost was able to write this without the help. My thinking process was the same as described in the solution set, but my ability to code it wasn’t there because I didn’t call to memory the return array segment notation.

The thinking behind this is that we start with a while loop which contains another loop. The first loop starts off saying “are we at the end of the string.” If no, then the second loop kicks in asking “is the first value plus the second value equal to zero?” If the answer is yes we print the answer. If it’s no, we move on to asking if the first value plus the third value is equal to zero. We keep this up until there the question hits the end, then we move the first value.

It’s very similar to the last problem in which we moved across the string. The string was essentially treated as an array.

It think this one is straight forward and it builds on the lessons learned from the last problem.

Again, I can write this code really well and I understand it… but I’m not sure at this moment if I’ve just remembered to the code, or if I have fictional grasp over the lesson.

09-is-power-of-two.rb

I’m quite proud of myself today. I’m finally seeing these problems as interesting puzzles to solve with tools I have. That’s a big improvement over how I felt about this all a week ago. A week ago, I was screwing up on syntax and didn’t even know how to approach these problems. I actually created an answer for this problem without seeking any assistance from outside the question it’s self.

First I’ll provide the answer I developed myself, then I’ll work to understand the solution which was provided and then I’ll summarize both the solutions. First, my code:
```def is_power_of_two?(num) if num == 0 return false end idx = 1 while num >= idx if idx ** 2 == num return true end idx += 1 end return false end```

Ok, now I’m going to dissect the solution provided:

```def is_power_of_two?(num) if num <= 0 return false end while true if num == 1 return true elsif num % 2 == 0 num = num / 2 else return false end end end```

This is fascinating. Though both answers work well, mine is really wasteful. Let me explain:

With my response, we ask a million questions.

The Million Dumb Questions Approach:

Him: “Is 78 a power of two?”
Her: “Is 1 ** 2 equal to 78?”
Him: “No. It’s 2.”
Her: “Ok… how about 2 ** 2?”
Him: “Nope, that’s 4”

(2 hours and 76 attempts later.)

Her: “Ok… how about 77 ** 2?”
Him: “HELLL NO! That’s 5,929!”
Her: “Ok… how about 78 ** 2?”
Him: “You’re driving me crazy! That’s 6,084!”
Her: “Great, I can tell you that 78 is not a multiple of 2”

This is a dumb way to do it.
There are a lot of different ways to solve this problem more efficiently listed here on Wikipedia.

December 31st, 2015

Update: Because I still this approach for it’s simplicity in process rather than rigor in math, I found a way to do it better. The idea is that once the results of squaring ‘idx’ become greater than num, it makes no sense to keep it going. So we write a conditional statement that stops asking if idx ** 2 becomes greater than n.

```def is_power_of_two?(num) if num < 1 return false end idx = 0 while idx <= num if (idx) ** 2 == num return true elsif (idx) ** 2 > num return false end idx += 1 end return false end```

The way the solutions says is to do this:

Him: “Is 78 a power of 2?”
Her: “Well, 78 divided by 2 is 39. And 39 divided by 2 is 19.5. As soon as we see fractions in this math, we know that the number is not a multiple of 2.”

So this is a powerful lesson. I’m happy that I was able to solve the solution myself without the aid of the solution set. Also, I now have a greater understanding of how using more efficient algorithms can make a big difference in processor demands.

Revised December 28, 2015

Upon revisiting the problem, I didn’t get their version right away. It had to do with my understanding of syntax, but also my understanding of the math of powers of two.

I now understand it even deeper. Here’s what I learned. The key is to take each num provided and repeatedly divide by 2. Do this until you hit 1. If you don’t hit 1 on the dot (meaning you get a fraction near one) then you don’t have a power of 2. That’s the heart of the code provided by a/A. So when thinking about their solution. num/2 to infinity. Keep going until you hit exactly 1. If you hit exactly 1 it’s true, if not, the result will go below 1 and any calculation resulting in < 1 == false.

December 29, 2015

I came back to this problem but couldn’t remember how to write the modulo system of finding if a number is a square. After doing battle for an hour, I could another way to get the right answer. Here it is:

```def is_power_of_two?(num) while num > 1 num = num / 2.0 end if num == 1 return true end if num < 1 return false end end```

It is cool that I found a new way to do the problem. That said, it’s no cause to celebrate. The reality is I couldn’t remember the syntax of the a/A solution which I committed to memory a few days ago. It’s the “while true” element that threw me off.

I just committed the a/A solution to memory again with a deeper understanding of the problem and the syntax. With luck, it’ll stick this time around.

10-third-greatest.rb

This one is really tough. I don’t know how to order arrays based on the value of the integers… It’s easy to print the 3rd position once the array is in order from longest to shortest… but how do we organize an array of numbers from longest to shortest?

Bubble sort method is the way to do it… I saw it on wikipedia once but don’t know exactly how to do it with ruby syntax.

so if num[idx] > num[idx + 1] then have them switch.

Side note: This one could be solved in the same way that we found the longest value in an array from earlier problems… I’ll revisit that one to see if I can remember the way we did that.

-Back- Yes, from looking back at the 02-longest-word.rb exercise we learn that we can solve this problem the same way that we solved the longest word problem. I actually wrote the code to print out the new arrays in order here:
```def longest_word(sentence) words = sentence.split(" ") longest_word = nil inbetween_word = nil shortest_word = nil idx = 0 while idx < words.length current_word = words[idx] if longest_word == nil || longest_word.length > current_word.length shortest_word = inbetween_word inbetween_word = longest_word longest_word = current_word elsif inbetween_word == nil || inbetween_word.length > current_word.length shortest_word = inbetween_word inbetween_word = current_word else shortest_word == nil || shortest_word.length < current_word.length shortest_word = current_word end idx += 1 end return [longest_word.to_s, inbetween_word.to_s, shortest_word.to_s] end```

Nope, I take that back. What the above method does is organize the words, but this doesn’t work for that array because the array may have less values than 3 in it. The above method does not account for arrays that have less than 3 values and thus can’t provide the right answer.

December 29th, 2015

Update: Yes, I was able to solve the problem using the methodology from the previous problem in which we sought out the longest word. The code that I wrote in the above note is embarrassing. It displays some wacky thinking and I don’t quite understand what I was doing. Anyways, here’s the code that works for this problem, but using the methodology we learned when seeking the longest word.

```def third_greatest(nums) first = nil second = nil third = nil idx = 0 while idx < nums.length current_num = nums[idx] if (first == nil) || (current_num > first) third = second second = first first = current_num elsif (second == nil) || (current_num > second) third = second second = current_num elsif (third == nil) || (current_num > third) third = current_num end idx += 1 end return third end```

It’s fun when you check the code in the solution and it’s essentially the same. This is good news, I’m starting to really get this.

11-most-common-letter.rb

I’ve put a LOT of time into understanding this. All day yesterday and much of the day today.

Yesterday, I spent all my time understanding the code. Today I was able to write it all up, but I screwed it up a bit and needed the solution to get the method to work.

```def most_common_letter(string) most_common_letter = nil most_common_letter_count = nil idx = 0 while idx < string.length current_letter = string[idx] current_letter_count = 0 idx2 = 0 while idx2 < string.length if string[idx2] == current_letter current_letter_count += 1 end idx2 += 1 end if (most_common_letter_count == nil) || (current_letter_count > most_common_letter_count) most_common_letter = current_letter most_common_letter_count = current_letter_count end idx += 1 end return [most_common_letter, most_common_letter_count] end```

Essentially for this problem we wrap a loop in another loop. The first loop does two things:

• Selects the current letter
• Assigns the results of the second loop if it should

The second loop takes the current letter and asks if each position on the string contains the same letter. When the same letters are there, the second loop counts them.

So this isn’t so hard to understand now, but the syntax can be a challenge to nail.

I’m feeling great about loops now.

December 30, 2014

Today I was able to write out the whole code but I screwed up on the positioning of the initial definition of the count. It turns out, if you put the “count = 0” code in the wrong loop, the count just keeps adding up. It never resets to zero with the new loop. Lesson learned! I got this now.

12-dasherize-number.rb

So this one really threw me off. There were two things that threw me off:

1. Double Hashes between odd number pairs
2. When odd numbers are at the end of the words

Today I was able to get a grip on how to solve the problem. More importantly, I realized an important lesson. Simply working the problem over and over again wasn’t especially effective. I spent hours coding it backwards and forwards using the tools I had. I never did get it right on my own and I found out later that the method was something I hadn’t ever been exposed to. Therefore, I wasted a lot of time trying to get it to work.

Going forward, if I don’t get the problem intuitively within 15 minutes, I’ll glance at the solution provided by a/A to get the gist of it. That will help me skip the slow, hopeless process of trial and error when my systems won’t work.

Anyways, dehasherize the odd numbers was useful because it showed me a different way to work backwards through an array.

It’s midnight and I’ve got a big day of coding tomorrow so I’m off to bed.

January 4th, 2015

So I was able to write my own way to solve this problem. Essentially the idea is that you run the problem, adding each string one after the other. If either value is odd, the loop fills in a “-“. The problem is at the end. If the last letter is odd, the program wants to add a “-” to the end which leads to us getting false results. So I wrote a quick if statement. If the string ends in a dash, the if statement sets the array to not include the dash. Pretty simple right? Here’s the code.

```def dasherize_number(num) num_string = num.to_s result = "" idx1 = 0 while idx1 < (num_string.length) idx2 = idx1 + 1 result += num_string[idx1] if (num_string[idx1].to_i % 2 == 1) || (num_string[idx2].to_i % 2 == 1) result += "-" end idx1 += 1 end if result[-1] == "-" result = result[0...-1] end return result end```

This is not the a/A solution to this problem. Their provided answer is much more elegant and I like it more. With theirs, it just doesn’t run the hash process unless idx is greater than 0. That’s really smart. The cool thing about this is that my solution works fine. It might be a little less elegant than theirs, but I sat and retyped the problem 3 times each with slight variations in both methods. Now I have a solid understanding of this so I can use the ideas in other problems.

What’s next?!

13-capitalize-words.rb

I love splitting, manipulating and joining arrays. It makes more sense than anything else. This one is elegant and simple.

14-scramble-string.rb

Something very interesting has happened here. I’m keeping my mouth shut for now, but the code below has the meaning if you look closely. This is my code and I’m certain it’s correct:
```def scramble_string(string, positions) pair_array = [""] idx = 0 while idx < string.length paired = positions[idx].to_s + string[idx] pair_array = pair_array.push(paired) idx += 1 end pair_array = pair_array.sort final_array = [] idx = 0 while idx < pair_array.length individuals = pair_array[idx] final_array = final_array.push(individuals[1]) idx += 1 end final_array = final_array.join("") return final_array end```

15-is-prime.rb

This has to be one of the more interesting problems because of the distraction the answer led me to. Let me explain:

All computer programming is iterative (at least so far for me). That means everything is done in a step by step way. Do this, then this.

In order to find prime numbers using a computer program, we seek numbers that are divisible nothing other than themselves and 1.

The first person credited with identifying this process was known as Eratosthenes. His Sieve of Eratosthenes is still the key to identifying prime numbers by iterative negation. That means, we identify a number that we want to know is prime, then we count up using negation. 4 is not prime because it’s divisible by 2

Example:

Is 21 a prime number?

Is 21 divisible by 2? | 21/2 = 10.5 | No

Is 21 divisible by 3? |  21/3 = 7 | Yes

Then the answer is, no. 21 is not a prime number

Alternatively, is 11 a prime number?

Is 11 divisible by 2? | 11/2 = 5.5 | No

Is 11 divisible by 3? |  11/3 = 3.66 | No

(Continue this on)

Is 11 divisible by 9? | 11/9 = 1.22  | No

Is 11 divisible by 10? |  11/10 = 1.1 | No

Then the answer is, yes. 11 is a a prime number because if we divide all the numbers [1-10] that lead up to 11, we get fractions.

Note: Eratosthenes is also a fascinating character. I’d love to do more research into his life. Wikipedia says he was one of the fist to measure the axis of the earth relative to the sun and provide surprisingly accurate estimates for the size of the sun and the earth. Sorry US History books, not everyone pre-Columbus thought the world was flat.

The solution to this problem is a mathematical exact. I think we could use arrays and more complex ways of writing a program to solve this problem, but this one seems quite elegant to me:
```def is_prime?(number) idx = 2 while number > idx if number % idx == 0 return false end idx += 1 end return true end```

Essentially this program says, count up dividing your count by the number and asking if the result is a fraction. Start at 2 and keep going until you hit the number. If you find a result without a fraction, it’s not prime. If you do, do this.

Updated December 23th, 2015

17-longest-palindrome.rb

Updated January 7th, 2015

This one is fun and quite easy because we learned both processes earlier. We know how to get true/false of a palindrome and we know how to work through a string to get the longest words. This is just putting the two together. The important lesson s is that we create two methods to solve this problem. In method 2 we use method 1 within the code.

In my method, I threw in a puts order to get a cool vision readout of how the computer iterates through the string (see blow highlighted in yellow):

18-greatest-common-factor.rb

Feeling quite accomplished after figuring out the answer to this problem all on my own. Here’s my code. After I save my code here, I’ll look to see the solution provided by a/A. They are generally have far more elegant than me, but I’m still happy to get a win. Here’s my code:

``````def create_factor_arr(n)
factors = []

idx = 1
while idx <= n
if n % idx == 0
factors = factors.push(idx)
end

idx += 1
end

return factors
end

def greatest_common_factor(number1, number2)
n1_factors = create_factor_arr(number1)
n2_factors = create_factor_arr(number2)
greatest_common_factor = nil

idx = 0
while idx < n1_factors.length
factor1 = n1_factors[idx]

idx2 = 0
while idx2 < n2_factors.length factor2 = n2_factors[idx2] if factor1 == factor2 current_factor =              factor1
if (greatest_common_factor == nil) || (current_factor > greatest_common_factor)
greatest_common_factor = factor1
end
end

idx2 += 1
end

idx += 1
end

return greatest_common_factor
end``````

Now I’ll dive into their code.

Ugh. Yes, their solution is clearly an example of more efficient thinking.

Essentially it works like this => Which number is lower. Set idx integer to that number. Count down from there asking constantly if the idx is a factor of the other number. Yes, that is far easier. I’ve put the solution away and will now try to write it from memory.

After reading a/A solution I was able to write a similar code from scratch which was far less complex than my original code. Anyways, I didn’t get it quite as elegant as the solution, but this solution works too:

``````def greatest_common_factor(n1, n2)
greatest_common_factor = 1
i = 0
if n1 < n2 i = n1 elsif i = n2 end while i > 0
if (n1 % i == 0) && (n2 % i == 0)
current_factor = i
if greatest_common_factor < current_factor
greatest_common_factor = current_factor
end
end
i -= 1
end

return greatest_common_factor
end``````

The solution code was 17 lines. My first version was 29 lines of code. My second version had 24 lines of code. I’m getting better, but it’s still not so good. Now I’ll re-examine their solution and try to get it even tighter.

I set idx to the lower number and then just iterated down to zero. That’s an inefficient way to do it because the first number it finds is going to be the greatest common factor. In my version, comparative variables were critical to ensure lower factors wouldn’t override the greatest common factor. But of course, the greatest common factor is going to be the first one we find. This time, I’ll set the loop to end on the first variable it finds in common between the two. I hope I can figure it out by memory. Here goes:

This time I wrote it out perfectly like they did. The lesson I learned here is, Go straight for the number you’re hunting and return it immediately.

``` def greatest_common_factor(n1, n2) i = nil if n1 <= n2 i = n1 else i = n2 end while true if (n1 % i == 0) && (n2 % i == 0) return i end i -= 1 end end ```

19-caesar-cipher.rb

January 7th, 2015

Here’s the code I created before looking at a/A solution:

``````def caesar_cipher(offset, string)
offset_string = []

idx = 0
while idx < string.length
character_num = string[idx].ord

if character_num < 97 || character_num > 122
character_num = character_num.chr
offset_string = offset_string.push(character_num)
elsif (character_num >= 97) && (character_num <= 122)
character_num = (((character_num - 97 + 3) % 26) + 97).chr
offset_string = offset_string.push(character_num)
end

idx += 1
end

return offset_string.join("")
end``````

The key to understanding this code was learning the letters positioning on the ASCII chart. Also, the key is to subtract your 97 (the number that references to “a” and starts the alphabet) first. Then you can add to the string as the offset suggests. After you’ve got that, the modulo at 26 will take the reference numbers off the top and move them to the beginning (therefore what was a z (122) had gone off the charts (125) so when we modulo it it becomes (3).

Then we bring back the reference space (so we add 97) and runs the .chr on it.

That’s the key to this problem. The key is understanding how to use the leftovers and convert them back.

I can’t imagine they have some dramatically different way to do this. It’s not like identifying primes numbers or anything right? It’s just moving around digits and arithmetic. Anyways, I’ll dissect their solution, but a quick notes:

This is an excellent computer science exercise.

Why? Because it draws connections from the base of computing to the level we’re at now. The ASCII chart can tell you the 7 digit sets of | and 0 that create letters.

With these letters we can create code already which answers intense math questions and can lead us on a narrative storyline. So this brings a LOT into perspective. From Ada to now, it’s all starting to become more clear.

20-num-repeats.rb

January 5, 2015

I’m very happy to write that I was able to find a solution to this problem without the aid of any outside sources. It took a long time to sort out the specifics and I can’t imagine this is the most efficient way to do this, but here’s the code I came up with to solve the problem:

``````def num_repeats(string)
num_letters_appearing_more_than_once = 0
current_letter = ""
counted_letters = []

idx1 = 0
while idx1 < string.length
current_letter = string[idx1]

idx2 = 0
idx3 = 0

while idx3 <= counted_letters.length
if counted_letters[idx3] == current_letter
idx2 += string.length + 1
end
idx3 += 1
end

compare_letter = ""
compare_count = 0

while idx2 < string.length
compare_letter = string[idx2]
if compare_letter == current_letter
compare_count += 1
if (compare_count == 2)
counted_letters = counted_letters.push(current_letter)
num_letters_appearing_more_than_once += 1
end
end

idx2 += 1
end

idx1 += 1
end

return num_letters_appearing_more_than_once
end``````

Here is what the above code does: First we set a loop to run through the first string comparing individual letters. Then we set another string to run through each individual letters a second time. We compare each letter against each letter to see if the frequency goes above 2. We use 2 because every result will come up with 1 because each letter in the string appears once. Once the repeating letter is identified, we push it onto an array of letters that we have counted. Moving forward, before counting a new string, we check the new potential double letter against the array of letters that have already been counted. If the current letter is not in that array and has been counted twice, we count it too. This gives us the proper answer.

Phew! Ok I can already think of a few other ways to do this. For now, I’d like to check the a/A result to see if they look at it totally different. That’s what I’ll do now.

Their code is 37 lines deep and mine is 43 so they aren’t dramatically different. Their’s works like this:

1. It loops the string and iterates through by letter
2. They assign each letter to an array (with a side array, which is the first time I’ve seen this) in order of appearance
3. Then they simply walk through the array asking each if there are values above 1

Good news is, their process and my own solution work in very similar ways.

Note: I want to make a quick note on how I deconstruct the code of others. The key is to slide in put commands in places where you don’t understand what’s happening. Then you run the program and can read the output of the inner workings of various programs. Here is a screen shot of what I’m talking about (some code is hidden so the critical elements can be seen on a single screen:

I like that their code utilizes a process of breaking the code into useful arrays. One could imagine that if you did this process to a huge database, you could use the output to get more understanding using the arrays created from identifying this. That said, I feel like if you really wanted to search a large amount of data, my code would be more useful because it uses more negation and therefore less required processing to move through strings.

Either way, this assignment of strings to arrays and connecting their letters to values is a tool which will be useful one day. I’ll write their code a few times to learn the process of breaking strings into frequency arrays.

Update January 16th, 2015

Running through the code again and I wrote this program in a different way. The fundamental difference is that the code below uses two method whereas the code above uses one. The second code is dependent upon the first now, but the first one could be used anytime you want to identify if a string is in an array. This makes the code simpler to understand. I think this one is better than my first version.

``````def letter_counted?(string, arr)
idx = 0
while idx < arr.length
if arr[idx] == string
return true
end
idx += 1
end
return false
end

def num_repeats(string)
num_of_appearing_letters = 0
counted_letters = []

idx = 0
while idx < string.length
current_letter = string[idx]
letter_count = -1

idx2 = 0
while idx2 < string.length
compare_letter = string[idx2]

if (current_letter == compare_letter)
letter_count += 1
if letter_count == 1 && (letter_counted?(current_letter, counted_letters) == false)
num_of_appearing_letters += 1
counted_letters = counted_letters.push(current_letter)
end
end

idx2 += 1
end

idx += 1
end

return num_of_appearing_letters
end``````

Wrapping up – Application Time

January 24, 2015

I’ve been studying Ruby for a month and a half. Really trying to get as good as I could. Today, I found myself diving into really complex problems at CodeWars.com and I realized, I’ll never be more ready for the a/A coding test. To be honest, I’m procrastinating because I want to ensure success.

So I went back to a/A application process and ran the practice problems. We’re supposed to solve them within 45 minutes. I opened them up and solved all three in 13 minutes.

Here’s the code it took for me to solve the problems:

``````# start 15:22 Jan 24 - 2015
# complete 15:35 Jan 24 - 2015
# Time to Complete = 13 minutes

def lucky_sevens?(numbers)

idx = 0
while (idx < numbers.length) && (idx + 2 < numbers.length)
if numbers[idx] + numbers[idx + 1] + numbers[idx + 2] == 7
return true
end
idx += 1
end

return false
end

puts lucky_sevens?([2,1,5,1,0])
puts lucky_sevens?([0,-2,1,8])
puts lucky_sevens?([7,7,7,7])
puts lucky_sevens?([3,4,3,4])

def oddball_sum(numbers)
sum = 0

idx = 0
while idx < numbers.length
if numbers[idx] % 2 != 0
sum += numbers[idx]
end
idx += 1
end

return sum
end

puts oddball_sum([1,2,3,4,5]).to_s + " should be 9"
puts oddball_sum([0,6,4,4]).to_s + " should be 0"
puts oddball_sum([1,2,1]).to_s + " should be 2"

def disemvowel(string)
vowelless_string = ""

idx = 0
while idx < string.length
if string[idx] != "a" && string[idx] != "e" && string[idx] != "i" && string[idx] != "o" && string[idx] != "u"
vowelless_string += string[idx]
end
idx += 1
end

return vowelless_string
end

puts disemvowel("foobar").to_s + " should be = fbr"
puts disemvowel("ruby").to_s + " should be = rby"
puts disemvowel("aeiou").to_s + " should be = \"\""``````

So there is no reason for me not to apply right now. That’s what I’m going to do.

Application

January 24, 2015

I spent 15:50 to 16:23 (estimated) which is about 33 minutes. Fairly certain all my solutions were rock solid. In the solutions I left the tests that I ran to ensure my code was correct. I hope that doesn’t hurt my results.

I’ll not share my code here as they request that we don’t share our code regarding this test.

That’s it for this round of studying.

To be honest, that’s pretty much it for this post on learning code too. Whether I’m accepted to a/A or not, I’ll continue to develop this skill until I can start building things on my own.

Though a/A is my first choice of schools, I will also apply to some of the other great coding bootcamps. Hack Reactor, Fullstack Academy look great as well as a few others I’ll begin the application process for.

Ruby Notes – Ch3 AppAcademy.io Coding Test

The following are my notes from the appacademy.io chapter 3 introduction to programming.

• pushadds elements to the end of an array
• unshiftadds elements to the beginning of an array
• shiftremoves an element from the beginning of an array
• popremoves an element from the end of an array

```while cool_things.length < 3 puts("Tell me something cool.") one_cool_thing = gets.chomp cool_things.unshift(one_cool_thing) end puts("Here are your cool things in order:") idx = cool_things.length - 1 while idx >= 0 puts(cool_things[idx]) idx = idx - 1 end```

The above code asks us to do things in order:

1. Limit array – while…
3. Record input to arraygets..
4. Instructs to add input to top of arrayputs(cool_things[idx])
5. Ends the array – end

This prints the three things the user enters in order. How to have users generate an array without knowing how to type the array code ( variable = [“1”, “2”, “3”] ) *This would be a useful tool if you wanted to create arrays quickly without having to type the quotations so often.

Here’s a screen shot of defining the difference between push and unshift. The key in seeing the difference is to look where the code reads, “Here are your cool things in order:”. It does this twice and they are different because push adds the variable to the beginning of the array and unshift adds it to the end.

Side Note: puts(“ringo”.length) #puts integer 5 | Strings can become arrays

Side Note #2: If you type “clear” into the terminal and press enter, it clears the screen. Wish I had known about that a few hours ago….
```ran_num = [1, 2, 3, 4] puts(ran_num[1])```

#Puts the number 2

Update: I’m having a hard time committing this to memory so I’m doing the same memory commitment process I used in the notes for section 2. Essentially, I’m writing programs that ask me 3 things I love and then spit the responses back to me in order.

COMMITTING CONCEPTS TO MEMORY EXERCISE: The idea here is to write each program as well as I can without any outside help from blogs, the internet or the course work.

Once the program is written to the best of my ability, I’ll run it to see if I got it right. If I didn’t get it right, I’ll use the material to fix up the code. Then I’ll try again. I’ll keep trying to write the code until I get a program right all on my own. Then I’ll do it two more times without error.

The hope is that this commits the process to memory… but most valuably, it forces me to nail down specific reasons and connections between each line of code.

• Program 1 – Favorite Song

• Program 2 – Favorite Material

• Program 3 – Favorite Metal

I’m confident I understand this concept of writing code that records a purposeful number of inputs and then can read them out either in order or backward order.

Removing from Arrays:

This is straight forward. I feel the best way to describe this is to show two examples:
```arr = [2, 3, 5, 7] item = arr.pop puts(arr)```
prints: 2 3 5
```arr = [2, 3, 5, 7] item = arr.shift puts(arr)```
Prints: 3 5 7

Setting Positions in an Array

This section is fairly straightforward. Positioning in an array seems like it will play an important part in the ability to code.
``` arr = [1, 2, 3] arr[1] = "Two" puts(arr == [1, "Two", 3]```
This code shows that you can assign a string to a single part of the array and it will print true when run.

Nil

The manual makes it known that if you ask Ruby to print a value that isn’t stored in an array, Ruby will just print nothing (we call this “nil.”)

Strings are Like Arrays

Just like with arrays, you can print individual elements of a string:
```hello_champ = "Champion" puts(hellow_champ[3])```
#prints m

Note: I understand this on a core level so it’s easy to commit to memory. It’s easy for me to read this next code, but it isn’t something I feel like I could write from memory. Check it out:
```string = "12345" idx = 0 while idx < string.length puts(string[idx]) idx = idx + 1 end```

For the above code, I practiced the same memory commitment exercise. I’ll reiterate because it’s been so valuable. I would make up a variable and a string (example = wife = “Veronica”), then I would write the code so that the computer would spell out the individual variables one by one.

``` wife = "Veronica" idx = 0 while idx < wife.length puts(wife[idx]) idx = idx + 1 end```

This prints out each letter of Veronica’s name on a separate line. To commit this to memory, I would write a program like this from memory and run it. It took me 2 tries before I could do it from memory. After I got it right without any errors, I did it again and again once more. Once I could do it three times without a mistake, I feel like I can move on as this has been committed to memory.

`puts("123" == ["1", "2", "3"])`

Just because strings can act similar to arrays, they aren’t exactly the same. The above code works in what it prints out, but if you set them equal, Ruby prints false.

Joining Arrays and Splitting Strings

`puts("hello my friend".split)`
This prints:
```hello my friend```

This is a quick way to break strings into arrays.

```friends = ["Friends ", "are ", "good ", "to ", "have."] friends_string = friends.join div_friends = friends_string.split puts(friends == div_friends)```

This prints false, even though it returns an array that was converted to a string and then back to an array. This is the above lesson in which we learn that they are similar, but not the same.

```friends = ["Friends ", "are ", "good ", "to ", "have."] friends_string = friends.join div_friends = friends_string.split puts(div_friends.join(" ")) puts(friends.join) puts(friends == div_friends)```

For me, the best way to learn the nuances of the code is to play around with different ideas. split and join make more sense the more I play with them. In the above code, I created an array, joined the array as a string, saved that as a variable, then split the string variable back into and array.

I then printed the initial array and the final array which was converted to a string and back to an array. Even though they print the same, if you ask Ruby if they are equal, it says false. This is an interesting nuance of the language which I would love to hear an explanation for. That is for another time…

We want to write our own methods to repeat code multiple times.
```def cube_numbers(how_many) var1 = [] idx = 0 while idx < how_many var1.push(idx * idx * idx) idx = idx + 1 end return var1 end puts ("How many cubed numbers do you want?") wanted_cubes = gets.to_i var1 = cube_numbers(wanted_cubes) idx = 0 while idx < var1.length puts(var1[idx]) idx = idx + 1 end```

While writing the above method, I decided to make it cubed instead of squared like in the example with AppAcademy.io. I also changed all the variables so that while I was typing it out, I had to be very conscious of the connecting variables.

This was important in understanding the code, but I’m not sure that I got it a hundred percent. I can’t write this method system from memory yet.

I’m tempted to move forward without going through the exercise of rewriting it again and again, but the truth is that I need to do this. Methods will be a critical aspect of programming in the future and I need to know them from memory.

Update: This has taken me a while to remember, but I’m even more confident that this is critical to being good at coding. I’ve rewritten the code 4-5 times but still make mistakes. I’ll continue to do so until I can write the blow three programs from memory/understanding.

December 13th, 2015: Still going slow. I’m working to understand the language well enough to complete the practice tests. The day before yesterday, I was able to take the practice tests apart and learn the pieces that went into them for the first time. Before then, the practice test was a foreign language to me. This is slow, lethargic progress, but it is progress none the less. Today I’ll wrap up the notes on this Chapter 3 and get back into the practice test.

Update 2: Dec 14, 2015 – This has taken a long time to remember. There are a few things going on in this code.

1. Define a process to work numbers and return them
2. Requesting how the numbers to be returned
3. Using the request to create an array of requested numbers
4. Redefining the process to print the array

Update 3: Dec 15, 2015 – Though this has taken a few days to get good at, I’ve not been working on just this. I’ve been working through methods on Codecademy and reading and taking notes on Ruby in 100  Minutes. All this cross reference is helping a lot. When I get stuck on one thing (like remembering this) I go around and re-learn concepts put forth in different ways. This helps because hearing the same thing in different ways is excellent for cementing knowledge. I just sat down and for the first time, can write the squares code of this example with no errors. So I’m ready to write out 3 versions of it (singles, trips and cubes) and then I’ll feel confident with creating methods and puts the number of outputs.

Program 1 – Repeating the Requested Single Numbers

Program 2 – Tripling the Requested Numbers

Program 3 – Cubing the Requested Numbers

Note: I slowed this way down to get this method definition and relaying of set numbers syntax committed to memory. Over the course of doing this, I’ve been checking in with the AppAcademy practice tests and I learned that the whole practice test is essentially creating def that make the readout codes work. Therefore, it’s clear that having a powerful grasp of the above concept was critical for passing the tests in the future. Even though this took about 3 days to commit to memory, I feel like it’s been great for my core understanding of how it all works.

Breaking Out of Loops

Ok. Loops will be the same process as above. The good news is that it’s much quicker to learn because the loops code is about 9 lines whereas the code for writing methods was about 19.

Program 1 – What’s the center of the Solar System?

Program 2 – First name of the President of the USA?

Program 3 – the most populous city in the USA

sidenote: though tedious, this repetition is critical. I’m ironing out a lot of kinks with each repetition. Also, I’m learning to correct things. Just a second ago I wrote “get.chomp”, when I ran this through terminal, terminal showed me that it was wrong and I self-corrected. Good times.

Returning Early

Again, a pair of concepts that were much easier to repeat and commit to memory.
```def smallest_square(lower_bound) i = 0 while true square = i * i if square > lower_bound return square end i = i + 1 end end puts(smallest_square(60)) ```
The above code was interesting because it displayed a code that was interesting but not very useful. It asked for the lower bound of a number that if squared would equal something. So you input the number (60 in the above example) and it feeds you back the number that, once squared, would be above the lower bound. This is useless unless you could find the lower bound number to be squared right? Right.

So I added a request to print n at each attempt and the code outputted:
```1 2 3 4 5 6 7 8 64```
That tells em that 8 is the lower square that will devise into 64 which is the lower bound. This was a cool learning bit about where we can type in code to give us different information that can be useful.

That’s it for my Notes On AppAcademy intro to Coding.

If you found these useful, check out my notes for chapter 1 or chapter 2. The source material for this chapter can be found on the appacademy.io site here.

How I Setup my Coding Workspace and Note Taking on WordPress:

My code workspace for this note-taking process is set up with WordPress as my note screen, this site as my learning material, the Terminal for executing programs, and Atom for writing and saving code. I keep a coding folder on the Desktop and call it aawork so it’s easy to find and open. Here’s what my screen looks like:

That’s It

Now it’s time to do the next step. This introduction was quite valuable and I feel I learned a lot from it. Truth is, I feel like I just opened up a door. I’m realizing that there is a whole complex, and interesting world behind it.

My objective is simple right now, to get into AppAcademy later in the year (2016). I don’t need to learn to program now, I just need to learn how to do well on the AppAcademy coding challenge.

If you want more notes like this, please feel free to check my notes for chapter 1 or chapter 2. I’ll probably be making < TK – Enter link to notes here > notes on the Introduction to Programming Summary (source material) too.

It’s hard to know how my notes will go as I work through the practice problems because they are on a different platform (c9.io) which may have a system for organizing though native to the platform. I’ll know more as I move through the notes.

Until then, I’ll use this blog to keep track of my progress. I’ll be taking my own notes on ruby resources and this will be a part of the breadcrumb trail from chapter 1, to the most recent of my Ruby studies and notes.

For the next breadcrumb in this series, please feel free to move onto my notes on Ruby in 100 Minutes (here is the source material). This is a page of notes I’m taking on a piece recommended by AppAcademy.

Ruby Notes – Ch2 AppAcademy.io Coding Test

The following are my notes from the appacademy.io chapter 2 introduction to programming.

Methods:

• +
• *
• /
• chomp
• puts
• gets
• to_i
• to_s

Methods are the verbs of Ruby.

If -> else, and elsif

```if num < 10 puts ("That's not a big number.") end```

If a method is true, Ruby will run the method. If a method is false, Ruby will skip it and move to the next method.

Logical Connectives

If you want to define something in which X is true and Y is true use &&

De Morgan’s LawWiki

The negation of a conjunction is the disjunction of the negations.

The negation of the disjunction is the conjunction of the negations.

This is a strange concept because it’s simple when you play out individual cases, but it get strange when you think of it as a whole. If something is not A, it could still be B, but if it’s B it doesn’t mean it’s not A. See, this is a strange way of speaking. I think the ven diagram on the wikipedia page is the best way to describe it.

Anyways, this is a logical tool to be used in more complex programs.

```number = gets.to_i if (number > 10) && (number < 30 ) puts ("Your number is greater than 30 and less than 10.") else puts("Your number is between 30 and 10.") end```

Negation is the last logical connector

Use the ! mark to negate.

Infinite Loops

while loops can become infinite loops

How to write an infinite loop

```while 1 == 1 puts ("We're looping.") end```

Aside: after writing this program and executing it, I had to learn quickly how to stop the terminal from creating an infinite loop.

To end an infinite loop using the Terminal press: ctrl + c

Arrays

```variable = ["thing1", "thing2", "thing3"] puts (variable[1])```

This code would print thing2

Positioning in the array above:

• 0 = thing1
• 1 = thing2
• 2 = thing3

Important note = arrays start from position 0

Arrays can also be written like this:

```random_array = [ "one", "two", "thee", "four" ] puts(random_array.lenght) idx = 0 while idx < random_array.length puts(random_array[idx]) idx = idx + 1 end puts("Loop Complete") ```

The above code runs like this:

Side Note: idx becomes a way of counting. With the above example it will go up linearly until it reaches the number which is greater than the number of arrays in the array. For the example above, it is 4.

Note on idx: using idx is completely arbitrary. Providing you use a constant variable as your means of counting, you can use any collection of symbols. I ran an array using love instead of idx and it worked exactly the same (except larger file size I’m sure because love is 4 characters while idx is three.)

Note on Learning to Code: When learning to code, writing your notes longhand is a waste of time. It’s better to put it into a blog like this. I already feel like I’m gaining a great understanding of the subject matter.

I was having a really hard time committing the idx value to memory. The key was to do it over and over again. Here’s an exercise to get you practicing coding at a fast level. It’s also a gratitude practice so your killing two birds with one stone.

For the following exercise, do it based off memory. If you can’t do it based off memory and you need to go back to your notes, don’t count that exercise as a success. You want to keep this exercise up until you have 3 consecutive successes with no notes:

Write 3 programs which use idx to systematically list out your arrays. At the end of each array, write, “I think I’m getting this.”

• Program 1 – Places you Love
• Program 2 – Foods you Love
• Program 3 – People you Love

Here’s how my programs went:

Program 1:

Program 2:

Program 3:

After doing this exercise, I know I can write simple Ruby array programs.

Geeking Out on Percentage Change and File Size:

I’m now realizing why programmers so often use really short file names. It’s because we want to type the names of files fast accurately.

In Program 1, I wrote the name of the program to be gp.rb

I could have titled it great_places_that_i_love.rb

…but then I’d be typing 5,400% more per request. <- Note: This is wrong. I’m not doing my statistical calculation right.

```short_var = "gp.rb".length long_var = "great_places_that_i_love.rb".length puts(short_var) puts(long_var) percentage_length = short_var / long_var puts(long_var.fdiv(short_var))```

The above program prints 5.4 which is the percentage of the absolute change in the file name length. <- Note: when I wrote that, I was misunderstanding the rules of statistics.

In order to get the percentage change from a short file name to a long file name as described above, I need to (source) understand the absolute change divided by the number we started out with.

absolute change / starting number = percentage change

I geeked out and wrote a program that will take your short file name and your long file name and return the percentage change in length. Here it is:
```puts("Type your short file name now, please.") short_var = gets.length - 1 puts(short_var) puts("Type your long file name now, please.") long_var = gets.length - 1 puts(long_var) absolute_change = long_var - short_var puts(absolute_change) ac_div_startnum = absolute_change.fdiv(short_var) puts("Percetage change: ") puts(ac_div_startnum * 100)```

Though this was a fairly silly exercise, I really feel like I understand Ruby more than ever.

That’s It

That concludes my notes for chapter 2. If you’d like to continue on with my notes, please select from more options below.

Chapter 1 notes can be found here.

Chapter 3 notes can be found here.

Ruby Notes – Ch1 AppAcademy.io Coding Test

The following are my notes from the appacademy.io chapter 1 introduction to programming.

There are three types of data in Ruby:

1. Numbers
2. Booleans – True or false
3. Strings – Words or phrases

Ruby is case sensitive

puts can be called methods in Ruby

Integers = Whole #s (ex 42)

Floats = Fractional #s (e.g. 3.14)

Never write commas when writing integers. They will change everything.

Notes on Division

Ruby does not divide into integers. It returns whole #s

puts (9.0/2) | #returns 4.5

puts (9.fdive(2)) | #returns 4.5

Modulo n%m -> returns the remainder from division

Defining Variables

ian_loves = “guitar, wifey, surfing”

Variables must:

• consist of letters or/and #s
• first character must be lowerCase
• no spaces (separate words w/ _ )

Gets Method

The gets method allows you to ask users to define variables
```puts("Type in your name, please.") name=gets() puts(name)```
This would return whatever the user entered

Gets for Strings and Integers

Converting Strings and Integers

• to_i = to integer
• to_s = to string

The Chomp Method

chomp makes it so a line break doesn’t happen behind each gets command. It seems like it’s used most of the time. For example:

```puts("Type your name, please.") name = gets name = name.chomp puts ("Hello" + name)```

use gets.chomp rather than reassigning the variables.

Important Note: Feeling good about all of this. I can write program files using Atom and execute the files using terminal.

I went through the Codecademy command line class which was a critical step in understanding how to navigate and execute programs using just typing. This makes it so I can at least pretend to be like the guy in Mr. Robot.

It was today, November 20th, 2015 (these notes were copied her later) that I actually wrote and executed my first computer program. It was a game that said hello.
```puts("Hello. What is your name, please?") name = gets name = name.chomp puts("Hi " + name + ", it's nice to meet you.")```

That’s it

If you’d like to continue to follow along with these notes, I continue the study in my notes in chapter 2.