"Design and write a recursive function to find wether a given number p is or not prefix of another number n. Both are natural numbers."
I have wrote the following:
isPrefix(n,p) if n equals p then true else if digits in n < digits in p then false else isPrefix(n/10,p);
and something similar with only one base case
if (n==0) then false; else then n==p AND isPrefix(n/10,p);
Is there anything better you can think of? Something more elegant,more intuitive, etc? Which of both would you suggest? The first is more efficient, but the second is more elegant i guess.
Cheers to all!


Sign In
Create Account

Back to top









