Complete the function which get an input number n such that n >= 10 and n < 10000, then:
Sum all the digits of n.
Subtract the sum from n, a...
For further actions, you may consider blocking this person and/or reporting abuse
Here is my simple solution to find the fruit name with PHP:
Heres my solution in Javascript:
It seems like it always returns apple, pretty neat question huh (Unless im wrong? 😅)
How I found this out:
Some python-that-can-be-improved:
Output of the above:
Ada solution
Here is a solution,
And here is a recursive Python solution,
Rust solution:
My version, in C# 😄
The helper function to convert the raw list into a usable dictionary:
Go
EDIT: just realized this challenge was a trap, but leaving the (wrong) solution here for people wanting to try out Go (it's awesome, try it).
Commands for initializing the project.
The source-code.
Commands for the unit tests.
The source-code for unit tests.
Commands for the tests, benchmark and coverage.
I think this might be called a naive solution...
...and neither do I understand recursion (maximum recursion depth exceeded while calling a Python object) and why it always comes up apple.
Here is Ruby solution,
Yanother Ruby solution
A quick JS one:
First up, set up the base fruit mapping by splitting that string:
The fruit-fetching-function itself:
The guts of that is:
[...String(i)].reduce((acc, digit) => acc - digit, i)
Breaking that up:
String(i)
:i
is a number so to split it into characters, render it into a standard decimal string[...String(i)]
: Easy way to split the string into chars[...String(i)].reduce((acc, digit) => acc - digit, i)
: The reduce function is passed the number itself as its initial value and then goes through the array of digits, subtracting each from the running total.Bit of sanity checking...
Note that it's worth making sure you have a modern JS engine
Caniuse for Object.fromEntries
Caniuse for ??
Solution of this task:
The number minus the sum of its digits is divisible by 9:
N = (number - sum(number's digits)) % 9 == 0
because:
number = d0 + d1 * 10^1 + ... + dm * 10^m
let dk is the k-th digit of the number:
dk * 10^k - dk = dk * 9 * 1..1, (k 1s), for k = 1,.., m
(d0 * 10^0 - d0 = 0)
and
N = d1 * 9 * 1 + d2 * 9 * 11 + ... + dm * 9 * 1...1 = 9(d1 + d2 * 11 + ... + dm * 1...1)
<=>
D = sum(N's digits) % 9 == 0
what is the definition of number divisible by 9
=>
N = 9K, D = 9L (K, L are some integers) and their difference is also divisible by 9:
N - D = 9K - 9L = 9(K - L)
If we repeat the subraction of the sum of the digits of the numbers we are getting smaller differences and they are all divisible by 9
=>
the first number in the interval <1, 100> is also divisible by 9
all items in the basket with the index divisible by 9 are apples
=> the solution is an apple
Python grill:
why I always get apple 😅 ?
Base ten is implicit, I suppose.