Skip to content

Instantly share code, notes, and snippets.

@cchoi34
Last active February 13, 2020 17:49
Show Gist options
  • Save cchoi34/1459758abba5be2c06d71bf3adf099c0 to your computer and use it in GitHub Desktop.
Save cchoi34/1459758abba5be2c06d71bf3adf099c0 to your computer and use it in GitHub Desktop.

Act I:

Romeo and Juliet begins as the Chorus introduces two feuding families of Verona: the Capulets and the Montagues. On a hot summer's day, the young men of each faction fight until the Prince of Verona intercedes and threatens to banish them. Soon after, the head of the Capulet family plans a feast. His goal is to introduce his daughter Juliet to a Count named Paris who seeks to marry Juliet.

Montague's son Romeo and his friends (Benvolio and Mercutio) hear of the party and resolve to go in disguise. Romeo hopes to see his beloved Rosaline at the party. Instead, while there, he meets Juliet and falls instantly in love with her. Juliet's cousin Tybalt recognises the Montague boys and forces them to leave just as Romeo and Juliet discover one another.


Prompt

Testing and assertion libraries like Jasmine, Mocha, Chai, Sinon etc. have a feature called spies. Spies are funcitons which accept functions as arguments and return modified functions. While the returned function maintians all of the functionality of the original, when returned it is wrapped in new code which allows the spy to keep track of how the function is used: whether it is called, how many times, what arguments it is called with, what it returns, whether it throws errors, etc.

For this REACTO problem, implement a spyOn function which does the following:

  • Takes a function func as its parameter
  • Returns a spy function spy that takes any number of arguments
  • spy calls func with the given arguments and returns what func returns
  • spy has the following methods:
    • .getCallCount(): returns the number of times spy has been called
    • .wasCalledWith(val): returns true if spy was ever called with val, else returns false
    • .returned(val): returns true if spy ever returned val, else returns false

Examples

function romeo(name) { 
  if(name === 'Juliet') return 'Romeo <3 Juliet';
  else return 'Meh'
}

const romeoSpy = spyOn( romeo );
romeoSpy('Juliet'); // 'Romeo <3 Juliet'

romeoSpy.getCallCount() ; // 1
romeoSpy('Rosalina'); // 'Meh'
romeoSpy(3, 5); // returns 'Meh'
romeoSpy.getCallCount(); // 2
romeoSpy.wasCalledWith('Rosalina'); // true
romeoSpy.wasCalledWith('Capulet'); // false
romeoSpy.wasCalledWith(5); //true
romeoSpy.returned('Meh'); // true
romeoSpy.returned('Capulet'); // false



Solution

Link to gist and solution: https://gist.github.com/nmestrada/223e8441864e939823f17700de00f906 && slides https://docs.google.com/presentation/d/1xOa-r15_2pXfdEUAO5bixazekM5moouu9ghYpwPBeFo/edit?usp=sharing




Act II:

Romeo lingers near the Capulet house to talk with Juliet when she appears in her window. The pair declare their love for one another and intend to marry the next day. With the help of Juliet's Nurse, the lovers arrange to marry when Juliet goes for confession at the cell of Friar Laurence. There, they are secretly married (talk about a short engagement).


Prompt

Right before they get married, Romeo forgot what he originally planned to say in his vows! He quickly goes to the bathroom to come up with something clever and meaningful. Romeo suddenly remembers that before Juliet was in love with him, she was in love with Math! He has this vague memory of her saying something about how much she loves numbers that are happy?

Write a function that determines if a number is considered "happy". Per Juliet's description, a happy number is a positive integer that if the sum each individual digit is squared, the result will eventually become 1. Any other number that repeats into an endless cycle is not considered happy and should return false. Complicated right? Juliet must really love math...


Examples

// Input: 19
happyNum(19); // should return true

// Explanation
/* 
1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1 and therefore happy
*/

// Input: 12
happyNum(12); // should return false

// Explanation
/*
1^2 + 2^2 = 5
5^2 = 25
2^2 + 5^2 = 29
2^2 + 9^2 = 85
8^2 + 5^2 = 89 <-- 
8^2 + 9^2 = 145
1^2 + 4^2 + 5^2 = 42
4^2 + 2^2 = 20
2^2 + 0^2 = 4
4^2 = 16
1^2 + 6^2 = 37
3^2 + 7^2 = 58
5^2 + 8^2 = 89 // REPEAT and therefore not happy :(
*/

Some happy numbers for testing purposes...

1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100, 103, 109, 129, 130, 133, 139, 167, 176, 188, 190, 192, 193, 203, 208, 219, 226, 230, 236, 239, 262, 263, 280, 291, 293, 301, 302, 310, 313, 319, 320, 326, 329, 331, 338, 356, 362, 365, 367, 368, 376, 379, 383, 386, 391, 392, 397, 404, 409, 440, 446, 464, 469, 478, 487, 490, 496, 536, 556, 563, 565, 566, 608, 617, 622, 623, 632, 635, 637, 638, 644, 649, 653, 655, 656, 665, 671, 673, 680, 683, 694, 700, 709, 716, 736, 739, 748, 761, 763, 784, 790, 793, 802, 806, 818, 820, 833, 836, 847, 860, 863, 874, 881, 888, 899, 901, 904, 907, 910, 912, 913, 921, 923, 931, 932, 937, 940, 946, 964, 970, 973, 989, 998, 1000




Solution

Link to the gist and solution: https://gist.github.com/jsmney/fcdb5b3a84b7a6e9844c506910235813




Act III:

starry heart

Following the secret marriage, Juliet's cousin Tybalt sends a challenge to Romeo. Romeo refuses to fight, which angers his friend, Mercutio, who then fights with Tybalt. Mercutio is accidentally killed as Romeo intervenes to stop the fight. In anger, Romeo pursues Tybalt, kills him, and is banished by the prince.

Juliet is anxious when she learns of these happenings. Friar Laurence arranges for Romeo to spend the night with Juliet before he leaves for Mantua. Meanwhile, Lord Capulet retaliates by re-scheduling Juliet and Count Paris' marriage to the next day. Juliet's parents are angry when Juliet doesn't want to marry Paris, but they don't know about her secret marriage to Romeo.

pub?

Prompt

Count Paris loves the idea of getting married the next day to Juliet, but Juliet is having none of it -- he's gross. As they check in to a nearby inn, Paris asks for one room for the two of them. Juliet counters back, saying she wants her own separate room. The innkeeper, trying to be as unbiased as possible, says he'll award the room arrangements to the one who can solve the following problem:

100 people are at a party. The people who didn’t know each other shook hands. There will always be 2 who know each other out of any 3. What is the largest number of handshakes that occur at the party?


tom wisdoms as count paris paris gif


Juliet solves the problem, so they sleep in separate rooms.

Actual Prompt

Count Paris is excited about his impending marriage to Juliet, so he naturally spends his night planning their future. How can he ensure a contingency plan for every outcome of their sure-to-be fruitful and illustrious matrimony?

Implement an immutable binary search tree class.

The class constructor should accept (at least) a single argument which will represent the value for its root node. Each instance should have two methods: insert, which takes a numerical value and returns a new binary search tree containing that value, and contains, which takes a numerical value and returns true or false based on whether the tree contains it.

The existing binary search tree shall not be mutated by insert. That is:

const bstA = new ImmutableBST(5);
const bstB = bstA.insert(6);
console.log(bstA); // contains only 5, NOT 6
console.log(bstB); // contains 5 and 6

Follow-up: build a remove method, which takes a value and returns a new binary search tree that does not include that value.

Read more here: https://en.wikipedia.org/wiki/Persistent_data_structure#Trees and/or here: https://medium.com/@dtinth/immutable-js-persistent-data-structures-and-structural-sharing-6d163fbd73d2#.7xy1ccvr0.

Examples

const bst = new ImmutableBST(5);
const bstMore = bst.insert(4).insert(3).insert(1).insert(10).insert(11).insert(15).insert(2).insert(100);
bstMore.contains(5); // true
bstMore.contains(3); // true
bstMore.contains(11); // true
bstMore.contains(12); // false
console.log(bst === bstMore); // false, because of immutability






Solution

Link to the gist and solution: https://gist.github.com/qualalia/855e1f9eb8e24c4bc565fa86da0aa553




Act IV:

Friar Laurence helps Juliet by providing a sleeping draught that will make her seem dead. When the wedding party arrives to greet Juliet the next day, they believe she is dead. The Friar sends a messenger to warn Romeo of Juliet's plan and bids him to come to the Capulet family monument to rescue his sleeping wife.

The vital message to Romeo doesn't arrive in time because the plague is in town (so the messenger cannot leave Verona). Hearing from his servant that Juliet is dead, Romeo buys poison from an Apothecary in Mantua. He returns to Verona and goes to the tomb where he surprises and kills the mourning Paris. Romeo takes his poison and dies, while Juliet awakens from her drugged coma. She learns what has happened from Friar Laurence, but she refuses to leave the tomb and stabs herself. The Friar returns with the Prince, the Capulets, and Romeo's lately widowed father. The deaths of their children lead the families to make peace, and they promise to erect a monument in Romeo and Juliet's memory.


Prompt

Romeo and Juliet are actually still alive, and managed to convince everyone that they did in fact, die. In secrecy, they go off to start a brand new life with each other. As they are frolicking away, Juliet starts getting tired from all that exercise, and asks Romeo to carry her. As Romeo says okay, he starts to realize that the next town over is actually a ways away! Romeo notices that if he were to make a grid of the area he is traversing through, the next town over would be at the bottom right of this grid, and he would be at the top left.

Given a grid of size "c" (columns) and "r" (rows), and the fact that Romeo carrying Juliet can only move down or right at any point in time, write a function that returns the number of possible unique paths there are to the next town over.

UniquePaths5

Note: Romeo knows for sure that the distance to the next town over (r or c) can't possibly be more than 100 (kilometers).


Examples

// Input: c = 3, r = 2
uniquePaths(3, 2) // should return 3 (grid of 2x3)
// right -> right -> down
// right -> down -> right
// down -> right -> right


// Input: c = 7, r = 3
uniquePaths(7, 3); // should return 28



Solution

Link to gist and solution: https://gist.github.com/cchoi34/75fd82cfea03cc13f69299de773e1f7d




Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment