Skip to content

Instantly share code, notes, and snippets.

@mmloveaa
mmloveaa / Find the next perfect square
Last active February 3, 2024 07:36
Find the next perfect square
// 12/22/2015
// You might know some pretty large perfect squares. But what about the NEXT one?
// Complete the findNextSquare method that finds the next integral perfect square after the one passed as a parameter. Recall that
an integral perfect square is an integer n such that sqrt(n) is also an integer.
// If the parameter is itself not a perfect square, than -1 should be returned. You may assume the parameter is positive.
// Examples:
// findNextSquare(121) --> returns 144
// findNextSquare(625) --> returns 676
@mmloveaa
mmloveaa / Missing Word
Last active November 1, 2023 09:52
Missing Word
Julia and Samantha are playing with strings. Julia has a string S, and Samantha has a string T which is a subsequence of string S. They are trying to find out what words are missing in T.
Help Julia and Samantha to solve the problem. List all the missing words in T, such that inserting them at the appropriate positions in T, in the same order, results in the string S.
Constraints
1 <= |T| <= |S| <= 106, where |X| denotes the length of string X.
The length of each word will be less than 15.
Function Parameter
You are given a function missingWords that takes the strings S and T as its arguments.
@mmloveaa
mmloveaa / Coderbyte - ABCheck
Created March 21, 2016 09:14
Coderbyte - ABCheck
Have the function ABCheck(str) take the str parameter being passed and return the string true if the characters a and b are
separated by exactly 3 places anywhere in the string at least once (ie. "lane borrowed" would result in true because there
is exactly three characters between a and b). Otherwise return the string false.
Use the Parameter Testing feature in the box below to test your code with different arguments.
function ABCheck(str) {
for (var i = 0 ; i < str.length-4 ; i++)
@mmloveaa
mmloveaa / 4-19 - Q6 Modify Price
Last active August 23, 2020 10:11
4-19 - Q6 Modify Price
Michael is a shop owner who keeps n list, L, of the name and sale price for each item in inventory. The store employees record the name and sale price of every item sold. Michael suspects his manager, Alex, of embezzling money and modifying the sale prices of some of the items. Write a program that finds the number of times Alex recorded an incorrect sale price.
Complete the verifyItems function provided in your editor so that it returns the number of incorrect sale prices recorded by Alex. It has 4 parameters:
origItems: An array of strings, where each element is an item name.
origPrices: An array of floating point numbers, where each element contains the original (correct) price of the item in the corresponding index of origItems.
items: An array of strings containing the name of the items with sales recorded by Alex.
prices: An array of floating point numbers, where each element contains the sale price recorded by Alex for the item in the corresponding index of items.
Note: Where required by the langua
@mmloveaa
mmloveaa / aogprotips.txt
Last active April 2, 2020 01:32
<AoGProTips> Synchronize animations with the Text-To-Speech
const callbacks = {
onTtsMark(markName) {
if (markName === ’START_ROAR’) {
beginRoaring();
} else if (markName === ’STOP_ROAR’) {
stopRoaring();
} else {
...
}
}
@mmloveaa
mmloveaa / 3-22 Count the holes
Last active October 24, 2018 09:36
3-22 Count the holes
24 -> 1
1264 -> 2
9899 -> 5
349 -> x
You swiftly determine that the puzzle can be solved by counting the number of holes in the digits of a number. e.g. 1,2,3,5,7 have no holes, 0, 4, 6, 9 have 1 hole each, and 8 has 2 holes. 2 is therefore the value of x to the last line.
@mmloveaa
mmloveaa / 4-26 Free Code Camp remove all falsy
Last active July 8, 2018 13:00
4-26 Free Code Camp remove all falsy
Remove all falsy values from an array.
Falsy values in JavaScript are false, null, 0, "", undefined, and NaN.
Remember to use Read-Search-Ask if you get stuck. Write your own code.
Here are some helpful links:
Boolean Objects
Array.filter()
@mmloveaa
mmloveaa / Balance the Array
Last active May 9, 2018 09:11
Balance the Array
Michael gives an array A={a1, a2,. .., aN} to Dwight. Michael then asks Dwight to find out whether there exists an element
in the array such that the sum of elements on its left is equal to the sum of elements on its right.
In other words, is there an index i such that, a1+a2...ai-1 = ai+1+ai+2...aN.
Complete the function balanceSum to help Dwight answer that question.
Note: If there are no elements to the left or to the right, then that sum is considered to be zero.
Constraints
1 ≤ N ≤ 105
1 ≤ Ai ≤ 2x104 where 1 ≤ i ≤ N
@mmloveaa
mmloveaa / chocolate - 3_21
Last active March 13, 2018 05:41
chocolate
Sam loves chocolates and starts buying them on the 1st day of the year. Each day of the year, x, is numbered from 1 to Y. On days when x is odd, Sam will buy x chocolates; on days when x is even, Sam will not purchase any chocolates.
Complete the calculate function in the editor so that for each day Ni (where 1 ≤ x ≤ N ≤ Y) in array arr, the number of chocolates Sam purchased (during days 1 through N) is printed on a new line. This is a function-only challenge, so input is handled for you by the locked stub code in the editor.
Input Format
The calculate function takes an array of integers as a parameter.
The locked code in the editor handles reading the following input from stdin, assembling it into an array of integers (arr), and calling calculate(arr).
The first line of input contains an integer, T (the number of test cases). Each line i of the T subsequent lines describes the ith test case as an integer, Ni (the number of days).
@mmloveaa
mmloveaa / 4-19 Q5 - Closest Num
Last active December 29, 2017 09:33
4-19 Q5 - Closest Num
Sorting is useful as a first step in many different operations. For example, searching for a specific elements is easier on a sorted data structure. But search is not the only task facilitated by an initial sort.
Given a list of unsorted numbers A={ a1, a2, a3,.. ,aN}, can you find the pair of numbers that has the smallest absolute difference between its elements? If there are multiple pairs that have the same absolute distance as the minimum, find them all.
Constraints
1 ≤ N ≤ 2x105
-107 ≤ x ≤ 107, where x ∈ array
No repetitions: ai≠ aj where 1 ≤ i < j ≤N
Input Format