30 Days of Algorithms —September 2017

Erica Millado
Yay It’s Erica
Published in
10 min readSep 11, 2017

This month, I’ve found a couple new algorithm websites with interesting problems and challenges. Check out Codewars (which call their problems “Katas”) or CodeFights if you haven’t done so already.

Things I’ve Practiced Through Algorithms This Month:

Sept 1 (from Women Who Code NYC May 2017 Algorithms)

Usually when you buy something, you're asked whether your credit card number, phone number or answer to your most secret question is still correct. However, since someone could look over your shoulder, you don't want that shown on your screen. Instead, we mask it.Your task is to write a function maskify, which changes all but the last four characters into '#'.Example:maskify("4556364607935616") == "############5616"maskify(               "1") ==                "1"maskify(                "") ==                 ""// "What was the name of your first pet?"maskify("Skippy") == "##ippy"

Sept 2 (from Codewars)

Your task is to write function toLeetSpeak that converts a regular english sentence to Leetspeak.More about LeetSpeak You can read at wiki -> https://en.wikipedia.org/wiki/LeetConsider only uppercase letters (no lowercase letters, no numbers) and spaces.For example:toLeetSpeak("LEET") returns "1337"In this kata we use a simple LeetSpeak dialect. Use this alphabet:{A : '@',B : '8',C : '(',D : 'D',E : '3',F : 'F',G : '6',H : '#',I : '!',J : 'J',K : 'K',L : '1',M : 'M',N : 'N',O : '0',P : 'P',Q : 'Q',R : 'R',S : '$',T : '7',U : 'U',V : 'V',W : 'W',X : 'X',Y : 'Y',Z : '2'}

Sept 3 (from Ray Wenderlich Swift Algorithm Club)

Greatest Common DivisorThe greatest common divisor (or Greatest Common Factor) of two numbers a and b is the largest positive integer that divides both a and b without a remainder.For example, gcd(39, 52) = 13 because 13 divides 39 (39/13 = 3) as well as 52 (52/13 = 4). But there is no larger number than 13 that divides them both.

Sept 4 (from Leetcode)

The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Given two integers x and y, calculate the Hamming distance.Note:0 ≤ x, y < 231.Example:Input: x = 1, y = 4Output: 2Explanation:1   (0 0 0 1)4   (0 1 0 0)

Sept 5 (from Leetcode)

Given an integer, return its base 7 string representation.Example 1:Input: 100Output: "202"Example 2:Input: -7Output: "-10"

Sept 6 (from Women Who Code NYC Oct 2015 Algorithms)

Imagine you run a restaurant, where the main specialty is a boiled egg. Your particular pot/stove set up allows you to boil up to 8 eggs every 5 minutes. If all of the attendees from tonight's meet up went to your restaurant at once after some awesome algorithm-ing, how could you determine how long it would take before the last person got their egg?

Sept 7 (from Codility)

An integer N is given, representing the area of some rectangle.The area of a rectangle whose sides are of length A and B is A * B, and the perimeter is 2 * (A + B).The goal is to find the minimal perimeter of any rectangle whose area equals N. The sides of this rectangle should be only integers.For example, given integer N = 30, rectangles of area 30 are:(1, 30), with a perimeter of 62,(2, 15), with a perimeter of 34,(3, 10), with a perimeter of 26,(5, 6), with a perimeter of 22.Write a function:public func solution(_ N : Int) -> Intthat, given an integer N, returns the minimal perimeter of any rectangle whose area is exactly equal to N.For example, given an integer N = 30, the function should return 22, as explained above.Assume that:N is an integer within the range [1..1,000,000,000].

Sept 8 (from Codility)

A positive integer D is a factor of a positive integer N if there exists an integer M such that N = D * M.For example, 6 is a factor of 24, because M = 4 satisfies the above condition (24 = 6 * 4).Write a function:public func solution(_ N : Int) -> Intthat, given a positive integer N, returns the number of its factors.For example, given N = 24, the function should return 8, because 24 has 8 factors, namely 1, 2, 3, 4, 6, 8, 12, 24. There are no other factors of 24.Assume that:N is an integer within the range [1..2,147,483,647].

Sept 9 (from Leetcode)

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.Example 1:Input: "Let's take LeetCode contest"Output: "s'teL ekat edoCteeL tsetnoc"Note: In the string, each word is separated by single space and there will not be any extra space in the string.

Sept 10 (from Project Euler)

The sum of the squares of the first ten natural numbers is,1^2 + 2^2 + ... + 10^2 = 385The square of the sum of the first ten natural numbers is,(1 + 2 + ... + 10)2 = 552 = 3025Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

Sept 11 (from Project Euler)

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,a2 + b2 = c2For example, 32 + 42 = 9 + 16 = 25 = 52.There exists exactly one Pythagorean triplet for which a + b + c = 1000.Find the product abc.

Sept 12 (from Codewars)

Given a year, return the century it is in. The first century spans from the year 1 up to and including the year 100, the second - from the year 101 up to and including the year 200, etc.Example:For year = 1905, the output should becenturyFromYear(year) = 20;For year = 1700, the output should becenturyFromYear(year) = 17.

Sept 13 (from CodeFights)

Given the string, check if it is a palindrome.Example:For inputString = "aabaa", the output should becheckPalindrome(inputString) = true;For inputString = "abac", the output should becheckPalindrome(inputString) = false;For inputString = "a", the output should becheckPalindrome(inputString) = true.

Sept 14 (from CodeFights)

Given an array of integers, find the pair of adjacent elements that has the largest product and return that product.Example:For inputArray = [3, 6, -2, -5, 7, 3], the output should beadjacentElementsProduct(inputArray) = 21.7 and 3 produce the largest product.Guaranteed constraints:2 ≤ inputArray.length ≤ 10,-1000 ≤ inputArray[i] ≤ 1000.

Sept 15 (from CodeFights)

Below we will define an n-interesting polygon. Your task is to find the area of a polygon for a given n.A 1-interesting polygon is just a square with a side of length 1. An n-interesting polygon is obtained by taking the n - 1-interesting polygon and appending 1-interesting polygons to its rim, side by side. You can see the 1-, 2-, 3- and 4-interesting polygons in the picture below.For n = 2, the output should beshapeArea(n) = 5;For n = 3, the output should beshapeArea(n) = 13.Guaranteed constraints:1 ≤ n < 104.

Sept 16 (from CodeFights)

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 bemakeArrayConsecutive2(statues) = 3.Ratiorg needs statues of sizes 4, 5 and 7.An array of distinct non-negative integers.Guaranteed constraints:1 ≤ statues.length ≤ 10,0 ≤ statues[i] ≤ 20.

Sept 17 (from my friend Jhantelle Belleza)

*Given an array of values, return only the values that have duplicates in the array***Parameters**Input: arr {Array}Output: {Array}**Constraints**Time: O(N)Space: O(N)**Examples**`[1, 2, 4, 2] --> [2]``[3, 2, 3, 2, 3, 3, 4] --> [3, 2]``[1, 2, 3, 4] --> []`

Sept 18 (from Leetcode)

Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this array.Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.Example:Input:[4,3,2,7,8,2,3,1]Output:[5,6]

Sept 19 (from Codewars)

Convert number to reversed array of digitsGiven a random number:You have to return the digits of this number within an array in reverse order.Example:348597 => [7,9,5,8,4,3]

Sept 20 (from Codewars)

Jaden Smith, the son of Will Smith, is the star of films such as The Karate Kid (2010) and After Earth (2013). Jaden is also known for some of his philosophy that he delivers via Twitter. When writing on Twitter, he is known for almost always capitalizing every word.Your task is to convert strings to how they would be written by Jaden Smith. The strings are actual quotes from Jaden Smith, but they are not capitalized in the same way he originally typed them.Example:Not Jaden-Cased: "How can mirrors be real if our eyes aren't real"Jaden-Cased:     "How Can Mirrors Be Real If Our Eyes Aren't Real"Note that the Java version expects a return value of null for an empty string or null.

Sept 21 (from Codewars)

Write function bmi that calculates body mass index (bmi = weight / height ^ 2).if bmi <= 18.5 return "Underweight"if bmi <= 25.0 return "Normal"if bmi <= 30.0 return "Overweight"if bmi > 30 return "Obese"

Sept 22 (from Codewars)

How much bigger is a 16-inch pizza compared to an 8-inch pizza? A more pragmatic question is: How many 8-inch pizzas "fit" in a 16-incher?The answer, as it turns out, is exactly four 8-inch pizzas. For sizes that don't correspond to a round number of 8-inchers, you must round the number of slices (one 8-inch pizza = 8 slices), e.g.:how_many_pizzas(16) -> "pizzas: 4, slices: 0"how_many_pizzas(12) -> "pizzas: 2, slices: 2"how_many_pizzas(8) -> "pizzas: 1, slices: 0"how_many_pizzas(6) -> "pizzas: 0, slices: 5"how_many_pizzas(0) -> "pizzas: 0, slices: 0"Get coding quick, so you can choose the ideal size for your next meal!

Sept 23 (from Codewars)

Trolls are attacking your comment section!A common way to deal with this situation is to remove all of the vowels from the trolls' comments, neutralizing the threat.Your task is to write a function that takes a string and return a new string with all vowels removed.For example, the string "This website is for losers LOL!" would become "Ths wbst s fr lsrs LL!".

Sept 24 (from Codewars)

Your task in this kata is to implement a function that calculates the sum of the integers inside a string. For example, in the string "The30quick20brown10f0x1203jumps914ov3r1349the102l4zy dog", the sum of the integers is 3635.

Sept 25 (from Codewars)

Write function heron which calculates the area of a triangle with sides a, b, and c.Heron's formula: sqrt (s (s - a) (s - b) * (s - c)), where s = (a + b + c) / 2

Sept 26 (from Codewars)

Your task is to write function findSum.Upto and including n, this function will return the sum of all multiples of 3 and 5.For example:findSum(5) should return 8 (3 + 5)findSum(10) should return 33 (3 + 5 + 6 + 9 + 10)

Sept 27 (from Codewars)

Given a number, return a string with dash'-'marks before and after each odd integer, but do not begin or end the string with a dash mark.Ex:dashatize(274) -> '2-7-4'dashatize(6815) -> '68-1-5'

Sept 28 (from Codewars)

Your task is to make a function that can take any non-negative integer as a argument and return it with it's digits in descending order. Essentially, rearrange the digits to create the highest possible number.Examples:Input: 21445 Output: 54421Input: 145263 Output: 654321Input: 1254859723 Output: 9875543221

Sept 29 (from Codewars)

This function has to be called multiply and needs to take two numbers as arguments, and has to return the multiplication of the two arguments.

Sept 30 (from Codewars)

After a hard quarter in the office you decide to get some rest on a vacation. So you will book a flight for you and your girlfriend and try to leave all the mess behind you.You will need a rental car in order for you to get around in your vacation. The manager of the car rental makes you some good offers.Every day you rent the car costs $40. If you rent the car for 7 or more days, you get $50 off your total. Alternatively, if you rent the car for 3 or more days, you get $20 off your total.Write a code that gives out the total amount for different days(d).

Let me know if you have any good problems you can share — I need some for October!

--

--