31 Days of Algorithms — October 2017

Erica Millado
Yay It’s Erica
Published in
12 min readOct 24, 2017

October has been a month of lots of CodeFights problems. I really like this site because of their Tournament feature. In a tournament, you have ten minutes to solve five problems. Three out of the five problems are coding problems and the remaining two problems are bug fix / recovery (fill in the empty line of code).

Things I’ve Practiced Through Algorithms This Month:

  • Using ternary conditional operators multiple times
  • Refactoring solutions by using less constants within functions
  • Thinking through an initial solution, implementing it as ugly as possibly in code, seeing if it works AND THEN cleaning it up

Oct 1 (from Codefights)

Given the angle measure, find its type. Here's how different angles are called:angles less than 90° are called "acute" angles;
an angle equal to 90° is called a "right" angle;
angles between 90° and 180° are called "obtuse" angles;
an angle equal to 180° is called a "straight" angle.
Example:For measure = 125, the output should be
angleType(measure) = "obtuse";
For measure = 72, the output should be
angleType(measure) = "acute".

Oct 2 (from CodeFights)

Determine number of decimal digits in an unsigned integer. For example, 9 is a single digit, 66 has 2 digits and 128685 has 6 digits. Be careful to avoid overflows/underflows.Extra challenge - Try optimize your implementation by avoiding slow operation like heap allocation and string formatting.

Oct 3 (from CodeFights)

Uber is building a Fare Estimator that can tell you how much your ride will cost before you request it. It works by passing approximated ride distance and ride time through this formula:(Cost per minute) * (ride time) + (Cost per mile) * (ride distance)where Cost per minute and Cost per mile depend on the car type.You are one of the engineers building the Fare Estimator, so knowing cost per minute and cost per mile for each car type, as well as ride distance and ride time, return the fare estimates for all car types.Example:Forride_time = 30,
ride_distance = 7,
cost_per_minute = [0.2, 0.35, 0.4, 0.45] and
cost_per_mile = [1.1, 1.8, 2.3, 3.5],
the output should
befareEstimator(ride_time, ride_distance, cost_per_minute, cost_per_mile) = [13.7, 23.1, 28.1, 38].
Since:30 * 0.2 + 7 * 1.1 = 6 + 7.7 = 13.7
30 * 0.35 + 7 * 1.8 = 10.5 + 12.6 = 23.1
30 * 0.4 + 7 * 2.3 = 12 + 16.1 = 28.1
30 * 0.45 + 7 * 3.5 = 13.5 + 24.5 = 38

Oct 4 (from Codewars)

John loves arrays. He calls an array a dense if there exist some numbers x and y such that a contains only consecutive numbers x, x + 1, x + 2, ..., y, in any order. His parents know that he loves arrays and gave him one for his birthday. Now John wants to check whether this array a is dense or not.Example:For a = [3, 4, 2, 1], the output should be
isArrayDense(a) = true.
This array contains the consecutive numbers 1, 2, 3, and 4.For a = [5, 3, 1, 2], the output should be
isArrayDense(a) = false.

Oct 5 (from CodeFights)

GoDaddy makes a lot of different top-level domains available to its customers. A top-level domain is one that goes directly after the last dot ('.') in the domain name, for example .com in example.com. To help the users choose from available domains, GoDaddy is introducing a new feature that shows the type of the chosen top-level domain. You have to implement this feature.To begin with, you want to write a function that labels the domains as "commercial", "organization", "network" or "information" for .com, .org, .net or .info respectively.For the given list of domains return the list of their labels.Example:For domains = ["en.wiki.org", "codefights.com", "happy.net", "code.info"], the output should bedomainType(domains) = ["organization", "commercial", "network", "information"].

Oct 6 (from Codewars)

Johnny is a farmer and he annually holds a beet farmers convention "Drop the beet".Every year he takes photos of farmers handshaking. Johnny knows that no two farmers handshake more than once. He also knows that some of the possible handshake combinations may not happen.However, Johnny would like to know the minimal amount of people that participated this year just by counting all the handshakes.Help Johnny by writing a function, that takes the amount of handshakes and returns the minimal amount of people needed to perform these handshakes (a pair of farmers handshake only once).

Oct 7 (from Codewars)

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in.Note: If the number is a multiple of both 3 and 5, only count it once.

Oct 8 (from Codewars, I wasn’t able to pass all tests 😳 for this)

My friend John and I are members of the "Fat to Fit Club (FFC)". John is worried because each month a list with the weights of members is published and each month he is the last on the list which means he is the heaviest.I am the one who establishes the list so I told him: "Don't worry any more, I will modify the order of the list". It was decided to attribute a "weight" to numbers. The weight of a number will be from now on the sum of its digits.For example 99 will have "weight" 18, 100 will have "weight" 1 so in the list 100 will come before 99. Given a string with the weights of FFC members in normal order can you give this string ordered by "weights" of these numbers?Example:"56 65 74 100 99 68 86 180 90" ordered by numbers weights becomes: "100 180 90 56 65 74 68 86 99"When two numbers have the same "weight", let us class them as if they were strings and not numbers: 100 is before 180 because its "weight" (1) is less than the one of 180 (9) and 180 is before 90 since, having the same "weight" (9) it comes before as a string.All numbers in the list are positive numbers and the list can be empty.

Oct 9 (from CodeFights)

Consider the following template for an equation:a ? b ? c ? dHere each letter stands for an integer value, and ? stands for either an equals sign or a multiplication operator.You have an array values that contains four integers. Can you fill the template with the integers, two multiplication operators, and one equals sign, such that the resulting equation will be correct?ExampleFor values = [2, 4, 3, 6], the output should be
equationTemplate(values) = true.
Here is how the template can be filled to result in a correct equation: 2 * 6 = 3 * 4.For values = [2, 3, 30, 5], the output should be
equationTemplate(values) = true.
Here is one of the ways to fill the template to get a correct equation: 30 = 2 * 3 * 5.For values = [2, 3, 5, 5], the output should be
equationTemplate(values) = false.
There is no way to fill the template that will result in a correct equation.

Oct 10 (from CodeFights)

You have some tasks in your Asana account. For each ith of them you know its deadlines i, which is the last day by which it should be completed. As you can see in your calendar, today's date is day. Asana labels each task in accordance with its due date:If the task is due today or it's already overdue, it is labeled as Today;If the task is due within a week but not today - that is, its deadline is somewhere between day + 1 and day + 7 both inclusive - it is labeled as Upcoming;All other tasks are labeled as Later;Given an array of deadlines and today's date day, your goal is to find the number of tasks with each label type and return it as an array with the format [Today, Upcoming, Later], where Today, Upcoming and Later are the number of tasks that correspond to that label.ExampleFor deadlines = [1, 2, 3, 4, 5] and day = 2, the output should be
tasksTypes(deadlines, day) = [2, 3, 0].
Today is day 2, so tasks with deadlines at 1 and 2 are labeled as Today. The other three tasks should be completed within a week, so they should be labeled as Upcoming. There are no tasks labeled as Later.For deadlines = [1, 2, 4, 2, 10, 3, 1, 4, 5, 4, 9, 8] and day = 1, the output should be
tasksTypes(deadlines, day) = [2, 8, 2].
Today is day 1, which means that the two tasks with a deadline of 1 are labeled as Today. Tasks with deadlines 10 and 9 are labeled as Later, since their deadlines are more than 7 days ahead. The other eight tasks are labeled as Upcoming.

Oct 11 (from CodeFights)

Define l-r-segment number as the number formed by concatenating all the digits from l to r in ascending order.Given l and r (l ≤ r), return the l-r-segment number.ExampleFor l = 2 and r = 4, the output should be
lrSegmentNumber(l, r) = 234.

Oct 12 (from CodeFights)

Find the perimeter of a square with given sides.ExampleFor n = 1, the output should be
squarePerimeter(n) = 4.

Oct 13 (from CodeFights)

Find the number of even digits in the given integer.ExampleFor n = 1010, the output should be
numberOfEvenDigits(n) = 2;
For n = 123, the output should be
numberOfEvenDigits(n) = 1

Oct 14 (from CodeFights)

Given two version strings composed of several non-negative decimal fields separated by periods ("."), both strings contain equal number of numeric fields. Return true if the first version is higher than the second version and false otherwise.The syntax follows the regular server ordering rules:1.0.5 < 1.1.0 < 1.1.5 < 1.1.10 < 1.2.0 < 1.2.2< 1.2.10 < 1.10.2 < 2.0.0 < 10.0.0There are no leading zeros in any of the numeric fields, i.e. you do not have to handle inputs like 100.020.003 (it would instead be given as 100.20.3).ExampleFor ver1 = "1.2.2" and ver2 = "1.2.0", the output should be
higherVersion(ver1, ver2) = true;
For ver1 = "1.0.5" and ver2 = "1.1.0", the output should be
higherVersion(ver1, ver2) = false.

Oct 15 (from CodeFights)

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].

Oct 16 (from CodeFights)

Given a string, output its longest prefix which contains only digits.ExampleFor inputString="123aa1", the output should be
longestDigitsPrefix(inputString) = "123".

Oct 17 (from CodeFights)

Check whether the given string is a subsequence of the plaintext alphabet.ExampleFor s = "effg" or s = "cdce", the output should be
alphabetSubsequence(s) = false;
For s = "ace" or s = "bxz", the output should be
alphabetSubsequence(s) = true.

Oct 18 (from CodeFights)

Check if all digits of the given integer are even.ExampleFor n = 248622, the output should be
evenDigitsOnly(n) = true;
For n = 642386, the output should be
evenDigitsOnly(n) = false.

Oct 19 (from CodeFights)

n children have got m pieces of candy. They want to eat as much candy as they can, but each child must eat exactly the same amount of candy as any other child. Determine how many pieces of candy will be eaten by all the children together. Individual pieces of candy cannot be split.ExampleFor n = 3 and m = 10, the output should be
candies(n, m) = 9.
Each child will eat 3 pieces. So the answer is 9.

Oct 20 (from CodeFights)

Find the largest digit of the given number.ExampleFor n = 111, the output should be
maxDigit(n) = 1;
For n = 132, the output should be
maxDigit(n) = 3.

Oct 21 (from CodeFights)

You and your friends love drinking, but hate D.U.I's. So you decided to make an algorithm to determine the most each of you can drink, and still be ok to drive to work.BAC = Blood Alcohol Content.Given the number of hours until a person has to leave for work, the number the person's BAC decreases per hour, and the number the person's BAC increase per serving of their favorite drink, determine how much they can drink, such that their BAC will be at most 0.08 when they have to leave for work.Assume all drinks are drunk immediately at once, giving them hours hours to process all the alcohol.Solve the task using floats, but round the final number of drinks down to the closest integer, just to be safe.Also, make sure the person's BAC is never higher than 0.45. A BAC of higher than 0.45 will result in death, and we don't want that!ExampleFor hours = 9, bacDecreasePerHour = 0.03 and bacIncreasePerDrink = 0.05, the output should bebooze(hours, bacDecreasePerHour, bacIncreasePerDrink) = 7.
7 (drinks) · 0.05 (bacIncreasePerDrink) = 0.35;
9 (hours to process) · 0.03 (bacDecreasePerHour) = 0.27.
0.35 - 0.27 = 0.08 => OK to drive to work

Oct 22 (from CodeFights)

Bob is trying to keep track of his credit card balance, and he would like to know how many days he is into the billing cycle in order to gauge his spending. Given the current date and the day of the month on which the billing cycle ends (the "cycle day"), calculate the number of days he is into his credit cycle.Day 1 is the day of the month after the cycle day. The cycle day is the last day of the cycle.ExampleFor month = 8, day = 26, year = 2017 and cycleDay = 20, the output should becreditCycle(month, day, year, cycleDay) = 6.For August 26, 2017 and a cycle date of the 20th of the month, Bob is 6 days into the cycle. Day 1 is August 21, so the 26th is day 6.

Oct 23 (from CodeFights)

Concatenate strings using a specific separator.ExampleFor arguments = ["Code", "Fight", "On", "!"] and separator = "/", the output should bemyConcat(arguments, separator) = "Code/Fight/On/!/".

Oct 24 (from CodeFights)

Given two chars each representing a one-digit number, return a string which represents the sum of these numbers.ExampleFor ch1 = '2' and ch2 = '5', the output should be
digitCharactersSum(ch1, ch2) = "7".

Oct 25 (from CodeFights)

Find the smallest integer not less than the given limit which is not divisible by any of the integers from the given array.ExampleFor divisors = [2, 3, 4] and start = 14, the output should be
firstNotDivisible(divisors, start) = 17.

Oct 26 (from CodeFights)

Transform a number into the given base.ExampleFor base = 2 and n = 13, the output should be
fromDecimal(base, n) = "1101".

Oct 27 (from CodeFights)

Determine if an input is an uppercase Latin letter.ExampleFor symbol = 'A', the output should be
isUppercase(symbol) = true;
For symbol = 'a', the output should be
isUppercase(symbol) = false;
For symbol = '0', the output should be
isUppercase(symbol) = false.

Oct 28 (from CodeFights)

Check the existence of a triangle with the given side lengths. A necessary and sufficient condition for triangle existence is that the sum of any two of its sides must be strictly greater than the third side.ExampleFor sides = [1, 2, 10], the output should be
triangleExistence(sides) = false;
For sides = [6, 2, 5], the output should be
triangleExistence(sides) = true.

Oct 29 (from CodeFights)

You found two items, for each of them you know their weight. You have a max weight capacity of maxW. If your weight capacity allows you to take with you both items, return "both". If you can take whichever one you want, return "either". If you cannot take any items, return "none". Otherwise, return "first" or "second" based on the index of the item which you can take.ExampleFor weight1 = 5, weight2 = 4 and maxW = 8, the output should be
knapsackLight2(weight1, weight2, maxW) = "either";
For weight1 = 10, weight2 = 4 and maxW = 9, the output should be
knapsackLight2(weight1, weight2, maxW) = "second".

Oct 30 (from Interview Cake)

Given a list of integers, find the highest product you can get from three of the integers.The input list_of_ints will always have at least three integers.

Oct 31 (from CodeFights)

Given timestamps time1 and time2, return true if time1 is strictly earlier than time2 and false otherwise.ExampleFor time1 = [22, 30] and time2 = [10, 59], the output should be
isEarlier(time1, time2) = false;
For time1 = [0, 5] and time2 = [1, 0], the output should be
isEarlier(time1, time2) = true.

If you’re reading this and would prefer to read these problems in Git gists, let me know and I’ll make sure that November’s problems are formatted as gists.

Happy coding!

--

--