The 11th offline real-time writing reference problem. Implementation example by python.

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.

Recommended Posts

The 10th offline real-time writing reference problem. Implementation example by Python.
The 11th offline real-time writing reference problem. Implementation example by python.
The twelfth offline real-time writing reference problem. Implementation by python
The 14th offline real-time writing reference problem with Python
The 18th offline real-time writing problem in Python
The 19th offline real-time writing problem in Python
The 15th offline real-time how to write reference problem in Python
The 14th offline real-time how to write reference problem in python
The 18th offline real-time how to write reference problem in Python
The 17th offline real-time how to write reference problem implemented in Python
The 16th offline real-time how to write reference problem to solve with Python
The 19th offline real-time how to write reference problem to solve with Python
The 17th Offline Real-time How to Solve Writing Problems in Python
Offline real-time how to write Python implementation example of E15 problem
The 15th offline real-time how to write problem was solved with python
The 15th offline real-time I tried to solve the problem of how to write with python
Offline real-time how to write Python implementation example of E14
13th Offline Real-time How to Solve Writing Problems in Python
Answer to "Offline real-time writing F04 problem"
Answer to "Offline real-time writing F05 problem"
Answer to "Offline Real-Time Writing E12 Problem"
[Implementation example] Read the file line by line with Cython (Python) from the last line
20th Offline Real-time How to Write Problems in Python
Part 1 I wrote the answer to the reference problem of how to write offline in real time in Python
An example of the answer to the reference question of the study session. In python.