PythonでFind Sequence

f:id:g_YUYUYU:20140501113546j:plain

コード

import math

def check_hc(matrix,option):
    if option == 1:
        matrix = zip(*matrix)

    s = [x for i in matrix for x in i]
    for i in range(len(s)-3):
       if s[i] == s[i+1] == s[i+2] == s[i+3]:
            return 1
    else:
        return 0

def check_Diagonal(matrix,option):
    if option == 1:
        rev_s = []
        for i in matrix:
            i.reverse()
            rev_s.append(i)
        else:
            matrix = rev_s

    s = [x for i in matrix for x in i]
    t = int(math.sqrt(len(s)))
    for i in range((t**2)-(3*t)-3):
        if s[i] == s[i+t+1] == s[i+(t*2)+2] ==s[i+(t*3)+3]:
            return 1
    else:
        return 0

        

def checkio(matrix):
    b = [0,0,0,0]
    b[0] = check_hc(matrix,0)          #check horizon
    b[1] = check_hc(matrix,1)          #check certical
    b[2] = check_Diagonal(matrix,0)    #check diagonal L->
    b[3] = check_Diagonal(matrix,1)    #check diagonal R->
    if sum(b) > 0:
        print "OKOK"
    else:
        print "NONO"
    print "---------------------"
if __name__ == '__main__':
 #   checkio([[7, 1, 1, 8, 1, 1],[1, 1, 7, 3, 1, 5], [2, 3, 1, 2, 5, 1],[1, 1, 1, 5, 1, 4],[4, 6, 5, 1, 3, 1],[1, 1, 9, 1, 2, 1]])# == True, "Diagonal"
 #   checkio([[1, 2, 1, 1],[1, 1, 4, 1],[1, 3, 1, 6],[1, 7, 2, 5]])# == True, "Vertical"
 #   checkio([[2, 1, 1, 6, 1],[1, 3, 2, 1, 1],[4, 1, 1, 3, 1],[5, 5, 5, 5, 5],[1, 1, 3, 1, 1]])
#    checkio([[7, 1, 4, 1],[1, 2, 5, 2],[3, 4, 1, 3],[1, 1, 8, 1]])
#    checkio([[2,7,6,2,1,5,2,8,4,4],[8,7,5,8,9,2,8,9,5,5],[5,7,7,7,4,1,1,2,6,8],[4,6,6,3,2,7,6,6,5,1],[2,6,6,9,8,5,5,6,7,7],[9,4,1,9,1,3,7,2,3,1],[5,1,4,3,6,5,9,3,4,1],[6,5,5,1,7,7,8,2,1,1],[9,5,7,8,2,9,2,6,9,3],[8,2,5,7,3,7,3,8,6,2]])
 #   checkio([[2,6,2,2,7,6,5],[3,4,8,7,7,3,6],[6,7,3,1,2,4,1],[2,5,7,6,3,2,2],[3,4,3,2,7,5,6],[8,4,6,5,2,9,7],[5,8,3,1,3,7,8]])
  #  checkio([[1,9,7,8,9,3,6,5,6,2],[4,9,4,8,3,4,8,8,5,9],[2,8,5,5,7,8,6,1,3,6],[6,4,7,6,9,1,4,5,7,8],[4,7,7,9,8,8,8,8,4,4],[3,7,3,2,1,9,1,8,9,1],[4,7,2,4,8,1,2,3,6,2],[4,4,1,3,3,3,9,2,6,7],[8,6,1,9,3,5,8,1,7,5],[7,3,6,5,3,6,6,4,8,2]])
    checkio([[2,3,6,5,6,2,8,3,7,4],[6,9,5,9,7,6,8,5,1,6],[6,8,2,6,1,9,3,6,6,4],[5,8,3,2,3,8,7,4,6,4],[2,3,1,4,5,1,2,5,6,9],[5,4,8,7,5,5,8,4,9,5],[9,7,9,9,5,9,9,8,1,2],[5,1,7,4,8,3,4,1,8,8],[5,3,3,2,6,1,4,3,8,8],[4,8,1,4,5,8,8,7,4,7]])

一言

  • 一問を解くための時間が増えてきている。
  • アルゴリズムというよりも数学的な解法だと思う。
  • 二次配列を一次元配列化して、簡略化できたと思う

PythonでStriped Words

f:id:g_YUYUYU:20140501014649j:plain

コード2

http://www.checkio.org/mission/striped-words/publications/RRRQ/python-3/first/
からの引用。
すげー綺麗なコードで感動した。

VOWELS = "AEIOUY"
CONSONANTS = "BCDFGHJKLMNPQRSTVWXZ"
PUNCTUATION = ",.!?"
 
def checkio(text):
    text = text.upper()
    for c in PUNCTUATION:
        text = text.replace( c, " " )
    for c in VOWELS:
        text = text.replace( c, "v" )
    for c in CONSONANTS:
        text = text.replace( c, "c" )
 
    words = text.split( " " )
     
    count = 0
    for word in words:
        if len( word ) > 1 and word.isalpha():
            if word.find( "cc" ) == -1 and word.find( "vv" ) == -1:
                count += 1
 
    return count

コード1

VOWELS = "AEIOUY"
CONSONANTS = "BCDFGHJKLMNPQRSTVWXZ"

def checkio(text):
	s = text.upper()
	s = s.encode("utf-8")
	s = s.replace(","," ")
	s = s.replace("."," ")
	s = s.replace("!"," ")
	s = s.replace("?"," ")
	s = s.split()
	count = 0
	b = []
	print s
	for i in s:
		if len(i) == 1:
			continue
		print i
		for x in i:
			if x in VOWELS and b != "V":
			 check = 1
			 b = "V"
			 print x,1
			elif x in CONSONANTS and b != "C":
				check = 1
				b = "C"
				print x,2
			elif b == "V" and x in CONSONANTS and check == 1:
				check = 1
				print x,3
			elif b == "C" and x in VOWELS and check == 1:
				check = 1
				print x,4
			else:
				check = 0
				b = []
				print x,5
				break
		else:
			print 6
			print check
			if check == 1:
				count += 1
			check = 0
			b = []
		print "-----------"

		
	print "Count is ",count



#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
#	checkio(u"My name is ...") #== 3, "All words are striped"
#	checkio(u"Hello world")# == 0, "No one"
#	checkio(u"A quantity of striped words.")# == 1, "Only of"
#	checkio(u"A quantity of striped words")# == 3, "Dog, cat and human"
	checkio(u"To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it?")

PythonでBrackets

f:id:g_YUYUYU:20140430012157j:plain

#coding utf-8
import string

def checkio(expression):
 	s = expression
 	s = s.encode("utf-8")
 	s = s.translate(string.maketrans("",""),"+-*/0123456789")
 	if s == "": return True

 	t = 0
 	while t < len(expression):
	 	s = s.replace("()","").replace("{}","").replace("[]","")
 	 	t+=1
 	 	if s == "": return True
 	return False

if __name__ == '__main__':
#	checkio(u"((5+3)*2+1)")# == True, "Simple"
#	checkio(u"{[(3+1)+2]+}")# == True, "Different types"
#	checkio(u"(3+{1-1)}")# == False, ") is alone inside {}"
#	checkio(u"[1+1]+(2*2)-{3/3}")# == True, "Different operators"
#	checkio(u"(({[(((1)-2)+3)-3]/3}-3)") #== False, "One is redundant"
#	checkio(u"2+3") #== True, "No brackets, no problem"
#	checkio("({[3]})-[4/(3*{1001-1000}*3)/4]")
#	checkio("((5+3)*2+1)")
#	checkio("({[3]})-[4/(3*{1001-1000}*3)/4]")
	checkio("2+3")

一言

綺麗に書けたし、綺麗に解けたと思える納得の出来。