FCC: Profile Lookup

screen-shot-2017-01-12-at-12-52-42-am

Solution: 

So I thought I got the correct solution pretty quickly but it would only pass 2/5 test cases.  Turned out, it was because I was returning “No such contact” inside the outer if statement..

screen-shot-2017-01-12-at-12-52-23-am

Advertisements
FCC: Profile Lookup

FCC: Nesting For Loops

Instructions:

Write a function multiplyAll so that it multiplies the product variable by each number in the sub-arrays of arr.

Solution: 

  • Iterate through inner subarrays with j.
  • Then iterate through the outer array with i.
  • Multiply each item with the stored result (product) as you iterate.

screen-shot-2017-01-11-at-11-45-30-pm

Test Cases: 

screen-shot-2017-01-11-at-11-46-10-pm

FCC: Nesting For Loops

Hackerrank Day 11: 2D Arrays

screen-shot-2016-12-22-at-7-53-09-pm

screen-shot-2016-12-22-at-7-54-15-pm

My Solution in Ruby:

screen-shot-2016-12-22-at-7-54-59-pm

This problem was a lot easier to tackle once I’ve sketched out the formula for calculating each hourglass.  That’s why I bought a notebook to derive future algorithms!

Also downloaded git and cleaned up my github today!  A few commands to remember when adding files and committing changes:

  1. Stage the file: git add .filename
    • stage a folder with files: git add folder/subfolder/*
  2. Commit: git commit -m “enter notes/messages about the changes
  3. Push to master branch: git push origin master
    • Push to another branch: git push origin BRANCH_NAME
Hackerrank Day 11: 2D Arrays

Hackerrank Day 6: Let’s Review

Task

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.

screen-shot-2016-12-13-at-4-04-17-pm

Original problem here.

Hackerrank Day 6: Let’s Review

Hackerrank Day 5: Loops

Task
Given an integer, n , print its first 10 multiples. Each multiple n x i (where 1≤ 10) should be printed on a new line in the form: n x i = result.

Input Format
A single integer, n.

Output Format
Print lines of output; each line i (where 1≤ 10) contains the of in the form:
n x i = result.

Sample Input

2

Sample Output

2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20

My Solution in Ruby:

screen-shot-2016-12-12-at-11-08-48-am

 

Hackerrank Day 5: Loops

Chess Board

Write a program that creates a string that represents an 8×8 grid, using newline characters to separate lines. At each position of the grid there is either a space or a “#” character. The characters should form a chess board.

Passing this string to console.log should show something like this:

 # # # #
# # # #
 # # # #
# # # #
 # # # #
# # # #
 # # # #
# # # #

When you have a program that generates this pattern, define a variable size = 8 and change the program so that it works for any size, outputting a grid of the given width and height.

Solution:

screen-shot-2016-11-17-at-3-36-41-pm

To know whether to put a space or a hash sign at a given position (or coordinate), check if the sum of x + y is even or odd.  (all the locations with space are even, and hash are odd).

Example:

location where (x, y) are (1, 1):

1 + 1 = 2 % 2 == 0 => true aka it’s even, you add a space there.

location at (2, 1):

2 + 1 = 3 % 2 == 0 => false aka it’s odd.  So you add a hash.

Chess Board