The 14th offline real-time writing reference problem with Python

How to write offline in real time 14th (September 28th) http://atnd.org/events/43076 Reference problem "Crossing on the circumference" http://nabetani.sakura.ne.jp/hena/ord14crosscircle/ Implementation example.

With unfamiliar python.

Examples of answers in other languages http://qiita.com/Nabetani/items/66806c9dc14a96f2fd42 You can follow from.

so.

#coding:utf-8
#tested with Python 2.7.5 and Python 3.3.2

import re

def solve( src ):
    return str(len(
            [ 0
                for a in range( 0, len(src) )
                for b in range( 0, a )              
                for c in range( 0, b ) if src[a]==src[c] 
                for d in range( 0, c ) if src[b]==src[d] ] ))

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   aabbca1bcb  14  
1   111ZZZ  0   
2   v   0   
""")

As usual, most of the test data is omitted.

A terribly naive implementation. The worst is O (N ** 4), but this is enough for this problem.

Choose 4 points. If the first and third have the same name and the second and last have the same name, there is an intersection. Try them all and you're done.

I don't think it should be the case to use list comprehension, but I want to use it, so I use it without hesitation. I decided to use it, but I wasn't interested in the contents of the list, so I decided to fill it with 0, which made me feel a little strange (I feel like it, but it's not Pythonista, so I wonder if it's really strange. I'm not sure).

Changing the order makes the typical case faster, but the worst case doesn't, so I left it as it is.

Recommended Posts

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 10th offline real-time writing reference problem. Implementation example by Python.
The 11th offline real-time writing reference problem. Implementation example by 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 twelfth offline real-time writing reference problem. Implementation by 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 16th offline real-time how to write problem was solved with Python
The 17th offline real-time how to write reference problem implemented in Python
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
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"
Solving the Python knapsack problem with the greedy algorithm
Try to solve the internship assignment problem with Python
The first algorithm to learn with Python: FizzBuzz problem
I tried to solve the problem with Python Vol.1
ABC163 C problem with python3
Call the API with python3.
ABC188 C problem with python3
ABC187 C problem with python
How to write offline real-time Solving E05 problems with Python
How to write offline real time I tried to solve the problem of F02 with Python
Extract the xz file with python
[AtCoder commentary] Win the ABC165 C problem "Many Requirements" with Python!
Get the weather with Python requests
Get the weather with Python requests 2
Offline real-time how to write Python implementation example of E15 problem
Find the Levenshtein Distance with python
Hit the Etherpad-lite API with Python
Install the Python plugin with Netbeans 8.0.2
Reading and writing NetCDF with Python
I liked the tweet with python. ..
Solve the Python knapsack problem with a branch and bound method
Master the type with Python [Python 3.9 compatible]
Part 1 I wrote the answer to the reference problem of how to write offline in real time in Python
Reading and writing CSV with Python
Solve the subset sum problem with a full search in Python
Make the Python console covered with UNKO
[Python] Set the graph range with matplotlib
Algorithm learned with Python 9th: Linear search
Behind the flyer: Using Docker with Python
Algorithm learned with Python 7th: Year conversion
Check the existence of the file with python
Algorithm learned with Python 8th: Evaluation of algorithm
[Python] Get the variable name with str
[Python] Round up with just the operator
Display Python 3 in the browser with MAMP
Algorithm learned with Python 4th: Prime numbers
Let's read the RINEX file with Python ①
Working with OpenStack using the Python SDK
Algorithm learned with Python 19th: Sorting (heapsort)
Reading and writing JSON files with Python
Download files on the web with Python
Algorithm learned with Python 6th: Leap year
Python: I tried the traveling salesman problem