# Hackerrank Day 0: Weighted Mean

Solution in Ruby:

I think I could have calculated the numerator with a for loop, but wasn’t sure how to do it.  Need to review other loops  as I usually default to while loop for everything now.

# Hackerrank Day 20: Sorting

My solution in Ruby:

# Exercism JS: Hamming

Write a program that can calculate the Hamming difference between two DNA strands.

By counting the number of differences between two homologous DNA strands taken from different genomes with a common ancestor, we get a measure of the minimum number of point mutations that could have occurred on the evolutionary path between the two strands.

This is called the ‘Hamming distance’.

GAGCCTACTAACGGGAT
CATCGTAATGACGGCCT
^ ^ ^  ^ ^    ^^

The Hamming distance between these two DNA strands is 7.

Another solution that I really like, especially for the error part:

Since I solved this problem in Ruby before, I mostly just spent this time translating it to Javascript.

In brief, you can define a function with the following steps:

1. Create an anonymous function: var Module = function() {};
2. Apply that function to Module.prototype.compute = function(arg1, arg2)
3. Export that anonymous function/module with module.exports
4. Use var  Module = require(‘./filename.js’); in your file to include that modules.

Here’s a quick & easy to understand guide to Module Patterns.

Also learned how to “throw” an error in Javascript:

First you include this function:

function UserException(message) {
this.message = message;
this.name = "UserException";
}

then you customize the error message in your function:

throw new UserException("DNA strands must be of equal length.");

# Hackerrank Day 6: Let’s Review

Given a string, S, of length N that is indexed from 0 to N1, print its even-indexed and odd-indexed characters as 2 space-separated strings on a single line (see the Sample below for more detail).

Input Format

The first line contains an integer, T (the number of test cases).
Each line ii of the T subsequent lines contain a String, S.

Constraints

• ≤ 10
• ≤ length of 10000

Sample Input

2
Hacker
Rank


Sample Output

Hce akr
Rn ak

Explanation

Test Case 0: S=“Hacker”
S[0]=“H”
S[1]=“a”
S[2]=“c”
S[3]=“k”
S[4]=“e”
S[5]=“r”
The even indices are 0, 2, and 4, and the odd indices are 1, 3, and 5. We then print a single line of 2 space-separated strings; the first string contains the ordered characters from S‘s even indices (Hce), and the second string contains the ordered characters from S‘s odd indices (akr).

Test Case 1: S=“Rank”
S[0]=“R”
S[1]=“a”
S[2]=“n”
S[3]=“k”
The even indices are 0 and 2, and the odd indices are 1 and 3. We then print a single line of 2 space-separated strings; the first string contains the ordered characters from S‘s even indices (Rn), and the second string contains the ordered characters from S‘s odd indices (ak).

My solution:

So I had a hard time with this challenge because I misunderstood the problem.  I thought the T and S arguments were independent of each other.  However, they are not.  The value of S is dependent on the value of T.  So for each test case (T), there is one substring (S).  Thus, the trick is to use T to prompt the number of user inputs for S, and then store the value of S in an array.

Took me an entire day and 1 Kevin Liu to figure all of this out.

Original problem here.

# Exercism Ruby: Hamming

Write a program that can calculate the Hamming difference between two DNA strands.

A mutation is simply a mistake that occurs during the creation or copying of a nucleic acid, in particular DNA. Because nucleic acids are vital to cellular functions, mutations tend to cause a ripple effect throughout the cell. Although mutations are technically mistakes, a very rare mutation may equip the cell with a beneficial attribute. In fact, the macro effects of evolution are attributable by the accumulated result of beneficial microscopic mutations over many generations.

The simplest and most common type of nucleic acid mutation is a point mutation, which replaces one base with another at a single nucleotide.

By counting the number of differences between two homologous DNA strands taken from different genomes with a common ancestor, we get a measure of the minimum number of point mutations that could have occurred on the evolutionary path between the two strands.

This is called the ‘Hamming distance’.

It is found by comparing two DNA strands and counting how many of the nucleotides are different from their equivalent in the other string.

GAGCCTACTAACGGGAT
CATCGTAATGACGGCCT
^ ^ ^  ^ ^    ^^


The Hamming distance for the above DNA strands is 7.

This is the test suite that my code has to pass:

My solution:

Took me a while to get used to exercism’s format of defining a class first (don’t give it the same name as the test) and then defining the method with self.compute

The BookKeeping module at the end of the code to keep track of the version of the given test, which is version 3 in this case.

Here are more implementations of this problem.

# Word Unscrambler

Write a function word_unscrambler that takes two inputs: a scrambled word and a dictionary of real words. Your program must then output all words that our scrambled word can unscramble to.

Hint: To see if a string s1 is an anagram of s2, split both strings into arrays of letters. Sort the two arrays. If they are equal, then they are anagrams.

My solution:

Provided solution:

Test Cases:

# Practice Coding Exercise

## lucky_sevens?

Write a function lucky_sevens?(numbers), which takes in an array of integers and returns true if any three consecutive elements sum to 7.

lucky_sevens?([2,1,5,1,0]) == true # => 1 + 5 + 1 == 7
lucky_sevens?([0,-2,1,8]) == true # => -2 + 1 + 8 == 7
lucky_sevens?([7,7,7,7]) == false
lucky_sevens?([3,4,3,4]) == false


Make sure your code correctly checks for edge cases (i.e. the first and last elements of the array).

My solution:

## oddball_sum

Write a function oddball_sum(numbers), which takes in an array of integers and returns the sum of all the odd elements.

oddball_sum([1,2,3,4,5]) == 9 # => 1 + 3 + 5 == 9
oddball_sum([0,6,4,4]) == 0
oddball_sum([1,2,1]) == 2


My solution:

## disemvowel

Write a function disemvowel(string), which takes in a string, and returns that string with all the vowels removed. Treat “y” as a consonant.

disemvowel("foobar") == "fbr"
disemvowel("ruby") == "rby"
disemvowel("aeiou") == ""

My solution:

I did these with 26 mins remaining on the clock! (I was given 45min total.)