14
0
Task
The prepend,append-Sequence is defined recursively, like this
- a(1) = 1
- a(n) = a(n-1).n , if n is even
- a(n) = n.a(n-1) , if n is odd
where the . represents an integer concatenation.
So the first few terms are: 1,12,312,3124,53124,531246,7531246,...
This is A053064.
Your task is, given an integer a > 0 to return n, such that the nth element in the prepend,append-Sequence is equal to a and if no such n exists return 0, a negative number or error out etc.
Rules
- Input can be taken as an integer, string, list of characters/digits etc.
- Output can be printed to STDOUT or returned (integer, string etc. is fine)
- On invalid input & in the case no such n exists your program may do anything but return a positive integer (eg. loop forever, return 0 etc.)
- You may choose to use 0-indexing, but then the output in case no n exists cannot be 0
Test cases
1 -> 1
12 -> 2
21 -> 0
123 -> 0
312 -> 3
213 -> 0
211917151311975312468101214161820 -> 21
2119171513119753102468101214161820 -> 0
333129272523211917151311975312468101214161820222426283031 -> 0
999795939189878583817977757371696765636159575553514947454341393735333129272523211917151311975312468101214161820222426283032343638404244464850525456586062646668707274767880828486889092949698100 -> 100
More formal:
a(n-1)*(int(log(n))+1)+n
andn*(int(log(n))+1)+a(n-1)
? – Mr. Xcoder – 2017-08-07T16:19:17.9371@Mr.Xcoder I would call that less formal :P – Post Rock Garf Hunter – 2017-08-07T16:20:11.327
@JonathanAllan That is already in the question for ~10 minutes. – Mr. Xcoder – 2017-08-07T16:27:10.947
2I suggest allowing errors for invalid inputs. – user41805 – 2017-08-07T16:46:27.310
I suggest allowing undefined behaviour for invalid inputs. – Mr. Xcoder – 2017-08-07T16:46:59.973
Can we take input as a list of digits? – Okx – 2017-08-07T16:58:29.473
@Okx Yes, that's mentioned in the question. – ბიმო – 2017-08-07T17:01:22.990