Hackerrank Day 26: Nested Logic


Solution in Ruby:

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

Screen Shot 2017-01-02 at 5.10.45 PM.png

Hackerrank Day 26: Nested Logic

Hackerrank Day 3: Intro to Conditional Statements


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

Hackerrank Day 3: Intro to Conditional Statements

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
Exercism JS: Hello World

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:



CS 61A – Homework 1

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.

Screen Shot 2016-09-07 at 8.35.04 PM.png


Longest Word

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[0] == string[1]
c == d? string[0] == string[2]
c == a? string[0] == string[3]
c == c? string[0] == string[4]
a == d? string[1] == string[2]
a == a? string[1] == string[3]
a == c? string[1] == string[4]
d == a? string[2] == string[3]
d == c? string[2] == string[4]
a == c? string[3] == string[4]

Provided Solution:

Screen Shot 2016-08-24 at 11.16.36 AM

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[0] == string[1]    a == a?
string[0] == string[2]   a == a?
string[1] == string[2]    a == a?

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

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



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

Num Repeats