# Hackerrank Day 26: Nested Logic Solution in Ruby:

Took me a while to solve this problem because I didn’t read the entire instructions..  -__- Advertisements

# Hackerrank Day 3: Intro to Conditional Statements

### Task

Given an integer, n, perform the following conditional actions:

If n is odd, print Weird
If n is even and in the inclusive range of 2 to 5, print Not Weird
If n is even and in the inclusive range of 6 to 20, print Weird
If n is even and greater than 20, print Not Weird

My solution: I simplified the solution but it can also be written as four separate conditions for readability.

Link to challenge: here

# Exercism JS: Hello World

Write a function that greets the user by name, or by saying “Hello, World!” if no name is given.

My solutions: Note to self:  Don’t forget ’em brackets, semi-colons, and ‘===’.

Test Cases: I was going to skip this problem since I solved it in Ruby already, but good thing I decided not to because JS syntax is a totally different & confusing beast.

More implementations to mull over here.

Also, here’s how to run exercism tests with jasmine-node:

Change ‘xit’ to ‘it’ when you’re ready to run a test case.

Execute the test file with the following code:

```cd ~/exercism/javascript/bob
jasmine-node bob_test.spec.js
```

# englishNumber

I think another example method would be helpful here. We’ll call this one englishNumber. It will take a number, like 22, and return the english version of it (in this case, the string twenty-two). For now, let’s have it only work on integers from to 9999.  # CS 61A – Homework 1

1. Fill in the blanks in the following function definition for adding `a` to the absolute value of `b`, without calling `abs`. 2. Write a function that takes three positive numbers and returns the sum of the squares of the two larger numbers. Use only a single expression for the body of the function: 3. Let’s try to write a function that does the same thing as an if statement: This function actually doesn’t do the same thing as an if statement in all cases. To prove this fact, write functions c, t, and f such that one of these functions returns the number 1, but the other does not: 4. Douglas Hofstadter’s Pulitzer-prize-winning book, Gödel, Escher, Bach, poses the following mathematical puzzle.

Pick a positive number n
If n is even, divide it by 2.
If n is odd, multiply it by 3 and add 1.
Continue this process until n is 1.
The number n will travel up and down but eventually end at 1 (at least for all numbers that have ever been tried — nobody has ever proved that the sequence will always terminate).

The sequence of values of n is often called a Hailstone sequence, because hailstones also travel up and down in the atmosphere before falling to earth. Write a function that takes a single argument with formal parameter name n, prints out the hailstone sequence starting at n, and returns the number of steps in the sequence: # Longest Word

Write a method that takes in a string. Return the longest word in the string. You may assume that the string contains only letters and spaces.

My solution: Thought Process:

• Sentence = “hello you motherfucker”
• Break sentence into an array of words:  words = “hello, you, motherfucker”
• Set the initial longest word as nil.
• Go through array (while loop).  If the new current word is longer than the previous current word, set the longest word as the new current word

A simpler alternative:

Setting the initializer for longest_word as an empty string, whose length will be 0. # Num Repeats

Write a method that takes in a string and returns the number of letters that appear more than once in the string. You may assume the string contains only lowercase letters. Count the number of letters that repeat, not the number of times they repeat in the string.

Thought Process:

# 0 1 2 3 4
# c a d a c

• Check each letter in string against the rest of the array
• If it appears more than once, count += 1
• Store repeated letters in a hash
• Count how many letters are in hash and return that value

c == a? string == string
c == d? string == string
c == a? string == string
c == c? string == string
a == d? string == string
a == a? string == string
a == c? string == string
d == a? string == string
d == c? string == string
a == c? string == string

Provided Solution: My solution:

Example: “aaa” => 1

1. Create a hash called ‘count’ to store each unique letter and its count.
2. Compare each letter against the rest of the letters in the string by incrementing
idx1 and idx2.

string == string    a == a?
string == string   a == a?
string == string    a == a?

3. If it repeats, count +=1 and store it in the hash.
count[string] += 1 => count = {‘a’ => 3}

4. Count the number of key-value pairs in the count hash by using count.length and return that value. IMPORTANT:

This is how you store key-value pair in a hash:   HASH_NAME[KEY] += VALUE