Here is my simple solution to find the fruit name with PHP:
functionsubtractSum($n){// fruit name "apple"$fruitArray=["kiwi","pear","kiwi","banana","melon","banana","melon","pineapple","apple","pineapple","cucumber","pineapple","cucumber","orange","grape","orange","grape","apple","grape","cherry","pear","cherry","pear","kiwi","banana","kiwi","apple","melon","banana","melon","pineapple","melon","pineapple","cucumber","orange","apple","orange","grape","orange","grape","cherry","pear","cherry","pear","apple","pear","kiwi","banana","kiwi","banana","melon","pineapple","melon","apple","cucumber","pineapple","cucumber","orange","cucumber","orange","grape","cherry","apple","cherry","pear","cherry","pear","kiwi","pear","kiwi","banana","apple","banana","melon","pineapple","melon","pineapple","cucumber","pineapple","cucumber","apple","grape","orange","grape","cherry","grape","cherry","pear","cherry","apple","kiwi","banana","kiwi","banana","melon","banana","melon","pineapple","apple","pineapple",];$number=$n;while(true){$str=(string)$number;$sum=0;for($index=0;$index<strlen($str);$index++){$sum+=(int)$str[$index];}$number=$number-$sum;if($number<=99){break;}}return$fruitArray[$number-1];}
Coding since 11yo, that makes it over 30 years now ~~~
Have a PhD in Comp Sci ~~~
Love to go on bike tours ~~~
I try to stay as generalist as I can in this crazy wide place coding is at now.
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...
// Debug version of the functionfunctionfruitForIndex(i){console.log(i),returnbaseFruitMapping[i]??fruitForIndex([...String(i)].reduce((acc,digit)=>acc-digit,i));}>fruitForIndex(1)1<"kiwi">fruitForIndex(100)100<"pineapple">fruitForIndex(101)10199<"apple">fruitForIndex(200)20019818017116215314413512611710899<"apple"
FRUITS = {
1 => 'kiwi',
...
100 => 'pineapple'
}
def substract_sum(num)
if num >= 10 && num < 10000
sum_of_digits = num.to_s.chars.map(&:to_i).inject(:+)
if FRUITS.keys.include?(sum_of_digits)
puts FRUITS[sum_of_digits]
else
puts "#{sum_of_digits} (not in the list)"
end
else
puts 'Enter valid number'
end
end
substract_sum(10)
substract_sum(1204)
elements=["1-kiwi","2-pear","3-kiwi","4-banana","5-melon","6-banana","7-melon","8-pineapple","9-apple","10-pineapple","...","100-pineapple"]defsubstractSum(n):if10<=n<=10000:sum_n=0fordigitinstr(n):sum_n+=int(digit)result=n-sum_nwhileresult>100:fordigitinstr(n):sum_n+=int(digit)result=n-sum_nprint(n,result,elements[result-1])else:print("{}: Use value between 10 and 10000".format(n))substractSum(9)substractSum(10)substractSum(31)substractSum(110)substractSum(111)substractSum(325)substractSum(1204)substractSum(1316)substractSum(2888)substractSum(10000)substractSum(10001)
Output of the above:
❯ python 260.py
9: Use value between 10 and 10000
10 9 9-apple
31 27 27-apple
110 100 100-pineapple
111 99 99-apple
325 95 95-melon
1204 98 98-pineapple
1316 95 95-melon
2888 80 80-cucumber
10000 100 100-pineapple
10001: Use value between 10 and 10000
// Set of utilities for manipulating fruits.packagefruits// Find a fruit based on its index.funcFindFromInteger(integeruint16)string{ifinteger<9{switch(integer){case1:return"kiwi"case2:return"pear"case3:return"kiwi"case4:return"banana"case5:return"melon"case6:return"banana"case7:return"melon"case8:return"pineapple"}}return"apple"}
Commands for the unit tests.
$touch fruits_test.go
The source-code for unit tests.
packagefruits_testimport"testing"import"fruits"import"fmt"funcTestFindFromInteger(t*testing.T){varvaluesExpectationsmap[uint16]string=map[uint16]string{0:"apple",1:"kiwi",2:"pear",3:"kiwi",4:"banana",5:"melon",6:"banana",7:"melon",8:"pineapple",9:"apple",10:"apple",100:"apple",1_000:"apple",325:"apple",10_000:"apple",}forvalue,expectation:=rangevaluesExpectations{varresultstring=fruits.FindFromInteger(value)ifresult!=expectation{t.Errorf("Expected fruits.FindFromInteger(%d) to equal %s but got %s.",value,expectation,result)}}}funcBenchmarkFindFromInteger(b*testing.B){forindex:=0;index<b.N;index++{fruits.FindFromInteger(10_000)}}funcExampleFindFromInteger(){fmt.Printf("Fruit for index %d is %q.\n",15,"apple")// Output: Fruit for index 15 is "apple".}
Commands for the tests, benchmark and coverage.
$go test-cover-bench.goos: linux
goarch: amd64
pkg: fruits
BenchmarkFindFromInteger-4 482598516 2.58 ns/op
PASS
coverage: 100.0% of statements
ok fruits 1.500s
I graduated in 1990 in Electrical Engineering and since then I have been in university, doing research in the field of DSP. To me programming is more a tool than a job.
withAda.Strings.Unbounded;useAda.Strings.Unbounded;subtypeParameter_TypeisIntegerrange10..9999;subtypeFruit_IndexisIntegerrange1..100;Names:array(Fruit_Index)ofUnbounded_String:=(-- Fill with names);functionSubctractSum(N:Parameter_Type)returnUnbounded_Stringis(ifNinFruit_IndexthenNames(N)else"apple");-- Explanation: -- N can be written as ---- N = a*1000 + b*100 + c*10 + d ---- if N <= 100, we access directly the name array. ---- If N > 100 (which means b >= 1) the new N is---- New_N = a*1000 + b*100 + c*10 + d - (a+b+c+d)-- = a*999 + b*99 + c*9---- If the iteration stops with New_N it must be -- New_N <= 100 which implies a = 0, b=1 -- (it cannot be b=0 otherwise we would had -- not done another iteration) and c=0, that is, -- New_N = 99.---- Therefore, unless N<=100, the result is "apple"
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
I graduated in 1990 in Electrical Engineering and since then I have been in university, doing research in the field of DSP. To me programming is more a tool than a job.
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:
My version, in C# 😄
The helper function to convert the raw list into a usable dictionary:
Here is a solution,
And here is a recursive Python 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):iis 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 ??
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,
Some python-that-can-be-improved:
Output of the above:
Yanother Ruby solution
Rust solution:
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.
Ada solution
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
Base ten is implicit, I suppose.
Python grill:
why I always get apple 😅 ?