JavaScript & Python
Details
Name Kata: Sum of Digits / Digital Root
6kuy
Description: Given n, take the sum of the digits of n. If that value has more than one digit, continue reducing in this way until a single-digit number is produced. The input will be a non-negative integer.
Example:
16 --> 1 + 6 = 7
942 --> 9 + 4 + 2 = 15 --> 1 + 5 = 6
132189 --> 1 + 3 + 2 + 1 + 8 + 9 = 24 --> 2 + 4 = 6
493193 --> 4 + 9 + 3 + 1 + 9 + 3 = 29 --> 2 + 9 = 11 --> 1 + 1 = 2
My Solutions
Python
def digital_root(n: int) -> int:
n_str = str(n)
while len(n_str) > 1:
r = 0
for i in range(len(n_str)):
r = r + int(n_str[i])
n_str = str(r)
return n if (n >= 0 and len(str(n)) == 1) else r
JavaScript(TypeScript4)
export const digitalRoot = (n:number):number => {
return String(n).length == 1
? n : digitalRoot(
String(n).split("").map((n) => Number(n)).reduce(
(previousValue, currentValue) => previousValue + currentValue, 0)
)
};
PHP
Details
Name Kata: You're a square!
7kuy
Description: You like building blocks. You especially like building blocks that are squares. And what you even like more, is to arrange them into a square of square building blocks!
However, sometimes, you can't arrange them into a square. Instead, you end up with an ordinary rectangle! Those blasted things! If you just had a way to know, whether you're currently working in vain⦠Wait! That's it! You just have to check if your number of building blocks is a perfect square.
Task
Given an integral number, determine if it's a square number:
In mathematics, a square number or perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself.
The tests will always use some integral number, so don't worry about that in dynamic typed languages.
Example:
-1 => false
0 => true
3 => false
4 => true
25 => true
26 => false
My Solutions
function isSquare(int $n): bool {
return sqrt($n) == (int)sqrt($n) ? true : false;
}
SQL
Details
Name Kata: SQL Basics: Truncating
7kuy
Description: Given the following table 'decimals':
decimals table schema
id
number1
number2
Return a table with a single column towardzero where the values are the result of number1 + number2 truncated towards zero.
My Solutions
SELECT TRUNC(number1 + number2) AS towardzero FROM decimals
Top comments (0)