30 Days of Algorithms — November 2017

Erica Millado
Yay It’s Erica
Published in
7 min readNov 27, 2017

For this month, I solved only white-boarding problems from CodeFights.

I definitely feel like my daily algorithm practice has helped me:

  • become comfortable using for-loops
  • use range arrays
  • understand that it’s okay to use a constant because .flatMap.map.filter.reduce can get messy

For each problem, I may have identified a strategy (i.e. FlatMap, Dictionary) that I used in my solution. If you utilized a different strategy/tool for a problem, please share with me! I want to learn from other solutions.

Nov 1 (Set)

Given an array of integers, we need to fill in the "holes" such that it contains all the integers from some range.ExampleFor sequence = [6, 2, 3, 8], the output should be
makeArrayConsecutive(sequence) = [4, 5, 7].

Nov 2 (Ternary conditional)

Given array of integers lengths, create an array of arrays output such that output[i] consists of lengths[i] elements and output[i][j] = j.ExampleFor lengths = [1, 2, 0, 4], the output should be
create2DArray(lengths) = [[0],[0, 1],[],[0, 1, 2, 3]]

Nov 3

Given a point and a circle, determine if the point lies strictly inside the circle.ExampleFor xa = 0, ya = 0, xc = 0, yc = 0 and rc = 1, the output should be
isInsideTheCircle(xa, ya, xc, yc, rc) = true.

Nov 4 (Filter)

Define GCPD (Greatest Common Prime Divisor) as the largest prime number that divides both given positive integers. Your task is to find GCPD of the given integers a and b.ExampleFor a = 12 and b = 18, the output should be
greatestCommonPrimeDivisor(a, b) = 3;
For a = 12 and b = 13, the output should be
greatestCommonPrimeDivisor(a, b) = -1.

Nov 5 (FlatMap)

Implement a function that can divide two fractions and produce a reduced fraction.ExampleFor a = [2, 3] and b = [5, 6], the output should be
fractionDivision(a, b) = [4, 5].

Nov 6 (FlatMap)

Find the leftmost digit that occurs in a given string.ExampleFor inputString = "var_1__Int", the output should be
firstDigit(inputString) = '1';
For inputString = "q2q-q", the output should be
firstDigit(inputString) = '2';
For inputString = "0ss", the output should be
firstDigit(inputString) = '0'.

Nov 7 (For loop)

Given an array of integers, find the rightmost round number in it and output its position in the array (0-based). If there are no round numbers in the given array, output -1.ExampleFor inputArray = [0, 5, 10, 15], the output should be
rightmostRoundNumber(inputArray) = 2;
For inputArray = [1, 2, 3, 4, 5], the output should be
rightmostRoundNumber(inputArray) = -1.

Nov 8 (Ternary conditional)

Determine if a given year is leap or not.ExampleFor year = 2000, the output should be
leapYear(year) = true.
2000 is divisible by 400, so the answer is yes.

Nov 9 (Filter)

Let's call a string cool if it is formed only by Latin letters and no two lowercase and no two uppercase letters are in adjacent positions. Given a string, check if it is cool.ExampleFor inputString = "aQwFdA", the output should be coolString(inputString) = true;For inputString = "aAA", the output should be
coolString(inputString) = false;
For inputString = "q q", the output should be
coolString(inputString) = false.

Nov 10 (Range array)

Find the sum of all divisors of a given integer.ExampleFor n = 12, the output should be sumOfDivisors(n) = 28. 
Explanation: 1 + 2 + 3 + 4 + 6 + 12 = 28.

Nov 11

Given two integers a and b, find the remainder of a when divided by b.ExampleFor a = 5 and b = 3, the output should be findTheRemainder(a, b) = 2.

Nov 12 (Range array)

Find the sum of cubes of all integers starting from 1 up to the given integer n, inclusive.ExampleFor n = 3, the output should be sumOfCubes(n) = 36. 
Because 13 + 23 + 33 = 1 + 8 + 27 = 36.

Nov 13

Find the area of a rectangle with given sides.ExampleFor a = 2 and b = 3, the output should be rectangleArea(a, b) = 6.

Nov 14 (Dictionary)

Given array of integers, find its mode.ExampleFor sequence = [1, 3, 3, 3, 1], the output should be arrayMode(sequence) = 3;For sequence = [1, 3, 2, 1], the output should be arrayMode(sequence) = 1.

Nov 15

A number of people are applying for a place in some team. Each of them has his own proficiency level (p.l.), which is represented as a positive integer, and the higher the level is - the better.There are only n places in the team, and the recruiter hires only those who have their p.l. not less than m.All applicants are standing in a queue and if the current one satisfies the required conditions, he is taken into the team right away. Once the team is full, all the remaining people are denied regardless of their p.l.What is the total p.l. of the applicants that get be hired? It is guaranteed that all n places can be taken.ExampleFor a = [4, 2, 3, 6, 2, 5, 4], n = 3 and m = 4, the output should be summaryProficiency(a, n, m) = 15.Explanation: 4 + 6 + 5 = 15.

Nov 16 (Dictionary)

Remove all characters from a given string that appear more than once in it.ExampleFor str = "zaabcbd", the output should be removeDuplicateCharacters(str) = "zcd".

Nov 17 (Dictionary)

Given a string, replace each its character by the next one in the English alphabet (z would be replaced by a).ExampleFor inputString = "crazy", the output should be alphabeticShift(inputString) = "dsbaz".

Nov 18 (Map)

One of the thresholding operations applied to images is truncation. Consider each pixel of some image one by one. If the intensity of a pixel is higher than threshold then it is set to threshold. Otherwise, it isn't changed.Apply this operation to the given image.ExampleFor image = [[0, 100, 100],[1, 255, 103]] and threshold = 102, the output should be imageTruncation(image, threshold) = [[0, 100, 100],[1, 102, 102]]

Nov 19 (Filter)

A password is complex enough, if it meets all of the following conditions:its length is at least 5 characters;
it contains at least one capital letter;
it contains at least one small letter;
it contains at least one digit.
Determine whether a given password is complex enough.ExampleFor inputString = "my.Password123", the output should be
passwordCheckRegExp(inputString) = true;
For inputString = "my.password123", the output should be
passwordCheckRegExp(inputString) = false.

Nov 20

Given an array of integers, find the maximal difference among all possible pairs of its elements.ExampleFor inputArray = [19, 32, 11, 23], the output should be
arrayMaximalDifference(inputArray) = 21.

Nov 21 (Reduce)

For a = [2, 8, 121, 42, 222, 23], the output should be
uniqueDigitProducts(a) = 3.
Here are the products of the array's elements:2: product(2) = 2;
8: product(8) = 8;
121: product(121) = 1 * 2 * 1 = 2;
42: product(42) = 4 * 2 = 8;
222: product(222) = 2 * 2 * 2 = 8;
23: product(23) = 2 * 3 = 6.
As you can see, there are only 3 different products: 2, 6 and 8.

Nov 22 (Set)

Given a string, find the number of different characters in it.ExampleFor s = "cabca", the output should be
differentSymbolsNaive(s) = 3.
There are 3 different characters a, b and c.

Nov 23

Implement a function that can sum two reduced fractions and produce a new one.ExampleFor a = [3, 5] and b = [7, 5], the output should be
fractionSum(a, b) = [2, 1].
3 / 5 + 7 / 5 = 10 / 5 = 2 / 1, so the answer is [2, 1].

Nov 24

Given two integers a and b, find the remainder of a when divided by b.ExampleFor a = 5 and b = 3, the output should be
findTheRemainder(a, b) = 2.

Nov 25 (Filter)

Determine if the given string contains at least one English letter.ExampleFor input = "a_ _2", the output should be
latinLettersSearchRegExp(input) = true;
For input = "W2", the output should be
latinLettersSearchRegExp(input) = true;
For input = "_1111 ", the output should be
latinLettersSearchRegExp(input) = false.

Nov 26 (Filter)

For given n and k find the sum of all multiples of k that are not greater than n.ExampleFor n = 7 and k = 2, the output should be
sumOfMultiples(n, k) = 12.
Explanation: 2 + 4 + 6 = 12.

Nov 27 (Filter)

Given array of integers sequence and some integer fixedElement, output the number of even values in sequence before the first occurrence of fixedElement or -1 if fixedElement is not contained in sequence.ExampleFor sequence = [1, 4, 2, 6, 3, 1] and fixedElement = 6, the output should beevenNumbersBeforeFixed(sequence, fixedElement) = 2.

Nov 28 (Range array)

Return a sorted array of all non-negative numbers less than the given n which are divisible both by 3 and 4.ExampleFor n = 30, the output should be
threeAndFour(n) = [0, 12, 24].

Nov 29 (Set)

Find the number of elements that are contained in both of the given arrays.ExampleFor a = [1, 2, 3] and b = [3, 4, 5], the output should be
sameElementsNaive(a, b) = 1.

Nov 30

Given a string, check whether it has length 3.ExampleFor inputString = "abs cd", the output should be
checkData(inputString) = false;
For inputString = "s ", the output should be
checkData(inputString) = true.

--

--