How to write offline in real time http://atnd.org/events/38770 Reference problem http://nabetani.sakura.ne.jp/hena/ord11arithseq/ I wrote an implementation example in python. It has been confirmed to work with both python2.7 and python3.3.
Examples of answers in other languages http://qiita.com/items/c206fbc645c255cb7de6 You can follow from.
so. Like this.
solver.py
#coding:utf-8
import re
def solve( src ):
def impl( s, seq ):
if 2<len(s) and ( s[1]-s[0] != s[-1]-s[-2] ) :
return 0
if len(seq)==0:
return len(s)
return max( impl( s, seq[1:] ), impl( s+seq[0:1], seq[1:] ) )
nums="0123456789abcdefghijklmnopqrstuvwxyz"
return "%d" % impl( [], [ nums.index(c) for c in list( src ) ] )
def test( samples ) :
for line in samples.splitlines():
a=re.split( "\s+", line ) # num, input, expected
if len(a) <3:
continue
actual = solve( a[1] )
ok=actual==a[2]
print( [ "ok" if ok else "***NG***", a[1:3], actual ] )
test( """
0 12345abcz 5 12345
1 012abku 4 0aku
2 01245689cdeghik 6 048cgk
49 012346abceghjknortv 5 01234 and 2 others""" )
Most of the test data is omitted as usual.
Comment from matarillo who is trying various things in F # http://qiita.com/items/6ab69f5f514cb2d0e7a8#comment-73ec472aa6be28061f68 I wrote it inspired by.
It's a recursion that doesn't result in tail recursion, so it feels like the stack overflows, but it's 36 at most, so it's okay. was.
I'd like to make it look like python, but it's not good just by using one comprehension. Again, I didn't have a chance to use the else in the for statement. Sorry.
I'm new to python, so I made many discoveries.
#coding: utf-8
has an effect. (For python2.7)Recommended Posts