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