Skip to content

Instantly share code, notes, and snippets.

View Nicknyr's full-sized avatar

Nick Kinlen Nicknyr

  • South Florida
View GitHub Profile
@Nicknyr
Nicknyr / encloseInBrackets.js
Created April 13, 2020 22:33
CodeSignal - Enclose in Brackets
/*
Given a string, enclose it in round brackets.
Example
For inputString = "abacaba", the output should be
encloseInBrackets(inputString) = "(abacaba)".
*/
function encloseInBrackets(inputString) {
@Nicknyr
Nicknyr / makeArrayConsecutiveTwo.js
Created April 13, 2020 22:27
CodeSignal - Make Array Consecutive 2
/*
Ratiorg got statues of different sizes as a present from CodeMaster for his birthday, each statue having an non-negative integer size. Since he likes to make things perfect, he wants to arrange them from smallest to largest so that each statue will be bigger than the previous one exactly by 1. He may need some additional statues to be able to accomplish that. Help him figure out the minimum number of additional statues needed.
Example
For statues = [6, 2, 3, 8], the output should be
makeArrayConsecutive2(statues) = 3.
Ratiorg needs statues of sizes 4, 5 and 7.
*/
@Nicknyr
Nicknyr / replaceMiddle.js
Created April 13, 2020 22:04
CodeSignal - Replace Middle
/*
We define the middle of the array arr as follows:
if arr contains an odd number of elements, its middle is the element whose index number is the same when counting from the beginning of the array and from its end;
if arr contains an even number of elements, its middle is the sum of the two elements whose index numbers when counting from the beginning and from the end of the array differ by one.
Given array arr, your task is to find its middle, and, if it consists of two elements, replace those elements with the value of middle. Return the resulting array as the answer.
Example
For arr = [7, 2, 2, 5, 10, 7], the output should be
@Nicknyr
Nicknyr / isSmooth.js
Created April 13, 2020 21:47
CodeSignal - Is Smooth
/*
We define the middle of the array arr as follows:
if arr contains an odd number of elements, its middle is the element whose index number is the same when counting from the beginning of the array and from its end;
if arr contains an even number of elements, its middle is the sum of the two elements whose index numbers when counting from the beginning and from the end of the array differ by one.
An array is called smooth if its first and its last elements are equal to one another and to the middle. Given an array arr, determine if it is smooth or not.
Example
For arr = [7, 2, 2, 5, 10, 7], the output should be
@Nicknyr
Nicknyr / removeArrayPart.js
Created April 13, 2020 20:26
CodeSignal - Remove Array Part
/*
Remove a part of a given array between given 0-based indexes l and r (inclusive).
Example
For inputArray = [2, 3, 2, 3, 4, 5], l = 2, and r = 4, the output should be
removeArrayPart(inputArray, l, r) = [2, 3, 5].
*/
function removeArrayPart(inputArray, l, r) {
@Nicknyr
Nicknyr / concatenateArrays.js
Created April 13, 2020 20:15
CodeSignal - Concatenate Arrays
/*
Given two arrays of integers a and b, obtain the array formed by the elements of a followed by the elements of b.
Example
For a = [2, 2, 1] and b = [10, 11], the output should be
concatenateArrays(a, b) = [2, 2, 1, 10, 11].
*/
function concatenateArrays(a, b) {
@Nicknyr
Nicknyr / firstReverseTry.js
Created April 13, 2020 19:58
CodeSignal - First Reverse Try
/*
Reversing an array can be a tough task, especially for a novice programmer. Mary just started coding, so she would like to start with something basic at first. Instead of reversing the array entirely, she wants to swap just its first and last elements.
Given an array arr, swap its first and last elements and return the resulting array.
Example
For arr = [1, 2, 3, 4, 5], the output should be
firstReverseTry(arr) = [5, 2, 3, 4, 1].
@Nicknyr
Nicknyr / arrayReplace.js
Created April 13, 2020 19:42
CodeSignal - Array Replace
/*
Given an array of integers, replace all the occurrences of elemToReplace with substitutionElem.
Example
For inputArray = [1, 2, 1], elemToReplace = 1, and substitutionElem = 3, the output should be
arrayReplace(inputArray, elemToReplace, substitutionElem) = [3, 2, 3].
*/
@Nicknyr
Nicknyr / createArray.js
Created April 13, 2020 19:32
CodeSignal - Create Array
/*
Given an integer size, return array of length size filled with 1s.
Example
For size = 4, the output should be
createArray(size) = [1, 1, 1, 1].
*/
@Nicknyr
Nicknyr / isDigit.js
Created April 5, 2020 02:50
CodeSignal - Is Digit
/*
Determine if the given character is a digit or not.
Example
For symbol = '0', the output should be
isDigit(symbol) = true;
For symbol = '-', the output should be
isDigit(symbol) = false.
*/