Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions DanielWagenerSolutions/implementation/bonAppetit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Anna and Brian are sharing a meal at a restuarant and they agree to split the bill equally. Brian wants to order something that Anna is allergic to though, and they agree that Anna won't pay for that item. Brian gets the check and calculates Anna's portion. You must determine if his calculation is correct.

// For example, assume the bill has the following prices: . Anna declines to eat item which costs . If Brian calculates the bill correctly, Anna will pay . If he includes the cost of , he will calculate . In the second case, he should refund to Anna.

// Function Description

// Complete the bonAppetit function in the editor below. It should print Bon Appetit if the bill is fairly split. Otherwise, it should print the integer amount of money that Brian owes Anna.

// bonAppetit has the following parameter(s):

// bill: an array of integers representing the cost of each item ordered
// k: an integer representing the zero-based index of the item Anna doesn't eat
// b: the amount of money that Anna contributed to the bill
// Input Format

// The first line contains two space-separated integers and , the number of items ordered and the -based index of the item that Anna did not eat.
// The second line contains space-separated integers where .
// The third line contains an integer, , the amount of money that Brian charged Anna for her share of the bill.

// Constraints

// The amount of money due Anna will always be an integer
// Output Format

// If Brian did not overcharge Anna, print Bon Appetit on a new line; otherwise, print the difference (i.e., ) that Brian must refund to Anna. This will always be an integer.

// Sample Input 0

// 4 1
// 3 10 2 9
// 12
// Sample Output 0

// 5
// Explanation 0
// Anna didn't eat item , but she shared the rest of the items with Brian. The total cost of the shared items is and, split in half, the cost per person is . Brian charged her for her portion of the bill. We print the amount Anna was overcharged, , on a new line.

// Sample Input 1

// 4 1
// 3 10 2 9
// 7
// Sample Output 1

// Bon Appetit
// Explanation 1
// Anna didn't eat item , but she shared the rest of the items with Brian. The total cost of the shared items is and, split in half, the cost per person is . Because , we print Bon Appetit on a new line.

function bonAppetit(bill, k, b) {
// Create an new array without bill[k], reduce that, then halve it:
const moneyAnnaOwes =
[...bill.slice(0, k), ...bill.slice(k + 1)].reduce(
(acc, val) => acc + val
) / 2;

console.log(b - moneyAnnaOwes || "Bon Appetit");
}
55 changes: 55 additions & 0 deletions DanielWagenerSolutions/implementation/catAndMouse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Two cats and a mouse are at various positions on a line. You will be given their starting positions. Your task is to determine which cat will reach the mouse first, assuming the mouse doesn't move and the cats travel at equal speed. If the cats arrive at the same time, the mouse will be allowed to move and it will escape while they fight.

// You are given queries in the form of , , and representing the respective positions for cats and , and for mouse . Complete the function to return the appropriate answer to each query, which will be printed on a new line.

// If cat catches the mouse first, print Cat A.
// If cat catches the mouse first, print Cat B.
// If both cats reach the mouse at the same time, print Mouse C as the two cats fight and mouse escapes.
// For example, cat is at position and cat is at . If mouse is at position , it is units from cat and unit from cat . Cat will catch the mouse.

// Function Description

// Complete the catAndMouse function in the editor below. It should return one of the three strings as described.

// catAndMouse has the following parameter(s):

// x: an integer, Cat 's position
// y: an integer, Cat 's position
// z: an integer, Mouse 's position
// Input Format

// The first line contains a single integer, , denoting the number of queries.
// Each of the subsequent lines contains three space-separated integers describing the respective values of (cat 's location), (cat 's location), and (mouse 's location).

// Constraints

// Output Format

// For each query, return Cat A if cat catches the mouse first, Cat B if cat catches the mouse first, or Mouse C if the mouse escapes.

// Sample Input 0

// 2
// 1 2 3
// 1 3 2
// Sample Output 0

// Cat B
// Mouse C
// Explanation 0

// Query 0: The positions of the cats and mouse are shown below: image

// Cat will catch the mouse first, so we print Cat B on a new line.

// Query 1: In this query, cats and reach mouse at the exact same time: image

// Because the mouse escapes, we print Mouse C on a new line.

function catAndMouse(x, y, z) {
return Math.abs(x - z) === Math.abs(y - z)
? "Mouse C"
: Math.abs(x - z) < Math.abs(y - z)
? "Cat A"
: "Cat B";
}
62 changes: 62 additions & 0 deletions DanielWagenerSolutions/implementation/countingValleys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Gary is an avid hiker. He tracks his hikes meticulously, paying close attention to small details like topography. During his last hike he took exactly steps. For every step he took, he noted if it was an uphill, , or a downhill, step. Gary's hikes start and end at sea level and each step up or down represents a unit change in altitude. We define the following terms:

// A mountain is a sequence of consecutive steps above sea level, starting with a step up from sea level and ending with a step down to sea level.
// A valley is a sequence of consecutive steps below sea level, starting with a step down from sea level and ending with a step up to sea level.
// Given Gary's sequence of up and down steps during his last hike, find and print the number of valleys he walked through.

// For example, if Gary's path is , he first enters a valley units deep. Then he climbs out an up onto a mountain units high. Finally, he returns to sea level and ends his hike.

// Function Description

// Complete the countingValleys function in the editor below. It must return an integer that denotes the number of valleys Gary traversed.

// countingValleys has the following parameter(s):

// n: the number of steps Gary takes
// s: a string describing his path
// Input Format

// The first line contains an integer , the number of steps in Gary's hike.
// The second line contains a single string , of characters that describe his path.

// Constraints

// Output Format

// Print a single integer that denotes the number of valleys Gary walked through during his hike.

// Sample Input

// 8
// UDDDUDUU
// Sample Output

// 1
// Explanation

// If we represent _ as sea level, a step up as /, and a step down as \, Gary's hike can be drawn as:

// _/\ _
// \ /
// \/\/
// He enters and leaves one valley.

function countingValleys(n, s) {
// Step 1: Convert string to an array of 1's and -1's

// Step 2: Reduce array. If the current elevation is zero and the current value is -1, increment valleyCount

const { valleyCount } = s
.split("")
.map(val => (val === "U" ? 1 : -1))
.reduce(
(acc, val) => ({
elevation: acc.elevation + val,
valleyCount:
!acc.elevation && val === -1 ? acc.valleyCount + 1 : acc.valleyCount
}),
{ elevation: 0, valleyCount: 0 }
);

return valleyCount;
}
59 changes: 59 additions & 0 deletions DanielWagenerSolutions/implementation/dayOfTheProgrammer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,62 @@
// Marie invented a Time Machine and wants to test it by time-traveling to visit Russia on the Day of the Programmer (the day of the year) during a year in the inclusive range from to .

// From to , Russia's official calendar was the Julian calendar; since they used the Gregorian calendar system. The transition from the Julian to Gregorian calendar system occurred in , when the next day after January was February . This means that in , February was the day of the year in Russia.

// In both calendar systems, February is the only month with a variable amount of days; it has days during a leap year, and days during all other years. In the Julian calendar, leap years are divisible by ; in the Gregorian calendar, leap years are either of the following:

// Divisible by .
// Divisible by and not divisible by .
// Given a year, , find the date of the day of that year according to the official Russian calendar during that year. Then print it in the format dd.mm.yyyy, where dd is the two-digit day, mm is the two-digit month, and yyyy is .

// For example, the given . is divisible by , so it is a leap year. The day of a leap year after is September 12, so the answer is .

// Function Description

// Complete the dayOfProgrammer function in the editor below. It should return a string representing the date of the day of the year given.

// dayOfProgrammer has the following parameter(s):

// year: an integer
// Input Format

// A single integer denoting year .

// Constraints

// Output Format

// Print the full date of Day of the Programmer during year in the format dd.mm.yyyy, where dd is the two-digit day, mm is the two-digit month, and yyyy is .

// Sample Input 0

// 2017
// Sample Output 0

// 13.09.2017
// Explanation 0

// In the year , January has days, February has days, March has days, April has days, May has days, June has days, July has days, and August has days. When we sum the total number of days in the first eight months, we get . Day of the Programmer is the day, so then calculate to determine that it falls on day of the month (September). We then print the full date in the specified format, which is 13.09.2017.

// Sample Input 1

// 2016
// Sample Output 1

// 12.09.2016
// Explanation 1

// Year is a leap year, so February has days but all the other months have the same number of days as in . When we sum the total number of days in the first eight months, we get . Day of the Programmer is the day, so then calculate to determine that it falls on day of the month (September). We then print the full date in the specified format, which is 12.09.2016.

// Sample Input 2

// 1800
// Sample Output 2

// 12.09.1800
// Explanation 2

// Since 1800 is leap year. Day lies on 12 September.

function dayOfProgrammer(year) {
const gregorian = year > 1918;
const switchover = year === 1918;
Expand Down
80 changes: 80 additions & 0 deletions DanielWagenerSolutions/implementation/drawingBook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Brie’s Drawing teacher asks her class to open their books to a page number. Brie can either start turning pages from the front of the book or from the back of the book. She always turns pages one at a time. When she opens the book, page is always on the right side:

// image

// When she flips page , she sees pages and . Each page except the last page will always be printed on both sides. The last page may only be printed on the front, given the length of the book. If the book is pages long, and she wants to turn to page , what is the minimum number of pages she will turn? She can start at the beginning or the end of the book.

// Given and , find and print the minimum number of pages Brie must turn in order to arrive at page .

// Function Description

// Complete the pageCount function in the editor below. It should return the minimum number of pages Brie must turn.

// pageCount has the following parameter(s):

// n: the number of pages in the book
// p: the page number to turn to
// Input Format

// The first line contains an integer , the number of pages in the book.
// The second line contains an integer, , the page that Brie's teacher wants her to turn to.

// Constraints

// Output Format

// Print an integer denoting the minimum number of pages Brie must turn to get to page .

// Sample Input 0

// 6
// 2
// Sample Output 0

// 1
// Explanation 0

// If Brie starts turning from page , she only needs to turn page:

// Untitled Diagram(6).png

// If Brie starts turning from page , she needs to turn pages:

// Untitled Diagram(3).png

// Because we want to print the minumum number of page turns, we print as our answer.

// Sample Input 1

// 5
// 4
// Sample Output 1

// 0
// Explanation 1

// If Brie starts turning from page , she needs to turn pages:

// Untitled Diagram(4).png

// If Brie starts turning from page , she doesn't need to turn any pages:

// Untitled Diagram(5).png

// Because we want to print the minimum number of page turns, we print as our answer.

function pageCount(n, p) {
const pagesFromFront = Math.ceil((p - 1) / 2);

// This part works because of the way it is
// Like, I just tinkered with it until it passed the test cases
const pagesFromBack = (() => {
if (n === p) return 0;
if (n % 2) {
return Math.ceil((n - p - 1) / 2);
}
return Math.floor((n - p) / 2) || 1;
})();

return pagesFromFront < pagesFromBack ? pagesFromFront : pagesFromBack;
}
Loading