The 16th offline real-time how to write problem was solved with Python

The problem is here. http://nabetani.sakura.ne.jp/hena/ord16boseg/ I didn't have time to put together the redundant processing in the horizontal and vertical directions. I will organize it later.

After organizing: A solve function that uses bitmap operations.

def solve(data):
  h=[int(data[i:i+2],8) for i in range(0,len(data),2)]
  v=[int(''.join(y),2) for y in zip(*[format(x,'06b') for x in h])]
  c=[map(len,format(m[y]^m[y+1],'b').split('0')) for y in range(0,5) for m in(h,v)]
  return ','.join(map(str,[sum(c,[]).count(i) for i in range(1,7)]))

Commentary:

#Bitmap creation:Cut out an octal character string two characters at a time to make a numerical array
h=[int(data[i:i+2],8) for i in range(0,len(data),2)]
  for i in range(0,len(data),2) #Index number generation to cut out two characters at a time
  data[i:i+2]                   #Extract 2 characters at a time
  int(... ,8)                   #Quantify octadecimal strings
  [...]                         #Make an array

#Vertical / horizontal replacement bitmap creation:Make a binary character string array, replace it vertically and horizontally, and then change it to a numeric array again.
v=[int(''.join(y),2) for y in zip(*[format(x,'06b') for x in h])]
  [format(x,'06b') for x in h] #Convert the numeric array to an array of binary 6-digit character strings
  zip(*[...])                  #Argument expansion of array elements(*)Then zip and replace each character vertically and horizontally
  for y in zip(...)            #Array elements('0'Or'1'Is an array of 6)about
  ''.join(...)                 #Concatenate binary characters that are character by character to create a binary character string
  int(... ,2)                  #Digitize binary strings
  [...]                        #Make an array


#XOR the bitmap between each row and make an array of lengths where 1 bit is continuous
c=[map(len,format(m[y]^m[y+1],'b').split('0')) for y in range(0,5) for m in(h,v)]
  for m in(h,v)          #For horizontal and vertical bitmaps
  for y in range(0,5)    #y=0~4, 0-1,1-2,2-3,3-4,4-To take an XOR between 5 lines
  m[y]^m[y+1]            #Take the XOR between the lines and set the bit at the border to 1.
  format(... ,'b')       #Bit string Number to binary string
  .split('0')            #'0'Divided by'1'Make a continuous element of
  map(len, ...)          #'1'Make an array of lengths
  [...]                  #Make an array(An array of arrays, a two-dimensional array)


#From the length array, count the number of appearances for each length,','Returns a string separated by
return ','.join(map(str,[sum(c,[]).count(i) for i in range(1,7)]))
  for i in range(1,7)    #About lengths 1-6
  sum(c,[])              #After flattening c in a two-dimensional array to make it a one-dimensional array
  .count(i)              #Count the number of appearances of length
  [...]                  #Make an array
  map(str, ...)          #Make the number of occurrences from 1 to 6 into an array of character strings
  ','.join(...)          #Number of occurrences string array','Make it a character string connected with
  return ...             #return

Before menstruation: Source written in 1 hour at the venue

#!/usr/bin/env python
#-*- coding:utf-8 -*-

def solve(data):
    data += "00"
    m = [format(int(data[i:i+2],8), "06b")+"0" for i in xrange(0,len(data),2)]
    count = [0]*7
    for x in xrange(0,5):
        n = 0
        last=(0,0)
        for y in xrange(0,7):
            z = (m[y][x], m[y][x+1])
            if z != last:
                count[n] += 1
                n = 0
            if z[0] != z[1]: n += 1
            last = z
    for y in xrange(0,5):
        n = 0
        last=(0,0)
        for x in xrange(0,7):
            z = (m[y][x], m[y+1][x])
            if z != last:
                count[n] += 1
                n = 0
            if z[0] != z[1]: n += 1
            last = z
    return ','.join(map(str,count[1:]))

def test(data, correct):
    answer = solve(data)
    print "OK" if answer==correct else "NG", answer, correct, data

if __name__ == '__main__':
    0, test( "060276724276", "6,2,1,1,0,1" );
    1, test( "770175454177", "2,3,0,3,1,0" );    
    2, test( "743733377170", "9,3,1,0,0,0" );    
    3, test( "724212121273", "5,2,1,1,1,1" );    
    4, test( "100000000000", "3,0,0,0,0,0" );    
    5, test( "000002000000", "4,0,0,0,0,0" );    
    6, test( "003622223600", "0,4,0,4,0,0" );    
    7, test( "520073737070", "8,3,1,1,0,0" );    
    8, test( "770077007700", "0,0,0,0,0,5" );    
    9, test( "555555555514", "2,0,0,0,2,2" );    
    10, test( "764252427600", "4,0,4,0,2,0" );    
    11, test( "774555554177", "3,3,1,3,0,0" );    
    12, test( "674574754557", "11,5,0,1,0,0" );    
    13, test( "000000000000", "0,0,0,0,0,0" );    
    14, test( "777777777777", "0,0,0,0,0,0" );    
    15, test( "774377777577", "6,0,2,0,0,0" );    
    16, test( "070777777777", "0,1,1,0,0,0" );    
    17, test( "373737373737", "0,0,0,0,0,1" );    
    18, test( "603260327725", "30,0,0,0,0,0" );    
    19, test( "466331144663", "30,0,0,0,0,0" );    
    20, test( "000000000242", "3,2,0,0,0,0" );    
    21, test( "567656043772", "18,2,1,0,0,0" );    
    22, test( "200763012420", "15,4,1,0,0,0" );    
    23, test( "400101140052", "14,3,0,0,0,0" );    
    24, test( "764767476476", "13,2,0,1,0,0" );    
    25, test( "001110140110", "12,2,1,0,0,0" );    
    26, test( "765405076527", "16,3,0,1,0,0" );    
    27, test( "377323370373", "8,4,2,0,0,0" );    
    28, test( "250541131216", "11,5,2,0,0,0" );    
    29, test( "744165741476", "12,3,2,0,0,0" );    
    30, test( "042101000300", "10,3,0,0,0,0" );    
    31, test( "002004554101", "11,3,1,0,0,0" );    
    32, test( "371707762706", "15,1,1,0,0,0" );    
    33, test( "130371310175", "7,3,1,2,0,0" );    
    34, test( "212537003613", "13,2,1,1,1,0" );    
    35, test( "157700063411", "15,3,0,0,0,1" );    
    36, test( "011500036007", "6,7,1,0,0,0" );    
    37, test( "743113313517", "17,2,1,0,0,0" );    
    38, test( "174105270405", "13,3,1,1,0,0" );    
    39, test( "427272200311", "13,3,2,0,0,0" );    
    40, test( "725370332237", "12,5,1,1,0,0" );    
    41, test( "005640420046", "12,1,3,0,0,0" );    
    42, test( "700350001101", "14,3,1,0,0,0" );    
    43, test( "577627744076", "16,1,1,1,0,0" );    
    44, test( "620332232007", "10,4,2,1,0,0" );    
    45, test( "260406401000", "15,1,1,0,0,0" );    
    46, test( "737272723276", "5,0,0,0,3,0" );    
    47, test( "000400040444", "7,0,2,0,0,0" );    
    48, test( "370222002177", "13,2,2,0,0,0" );    
    49, test( "372236024656", "9,3,2,0,1,0" );    
    50, test( "276131137003", "11,6,2,0,0,0" );    
    51, test( "742134007240", "13,4,2,0,0,0" );    
    52, test( "777721775571", "13,1,2,0,0,0" );    
    53, test( "700301232233", "11,2,3,0,0,0" );

Recommended Posts

The 16th offline real-time how to write problem was solved with Python
The 15th offline real-time how to write problem was solved with 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 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 15th offline real-time I tried to solve the problem of how to write with python
The 17th offline real-time how to write reference problem implemented in Python
20th Offline Real-time How to Write Problems in Python
The 14th offline real-time writing reference problem with Python
How to write offline real-time Solving E05 problems 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
Answer to "Offline real-time how to write F02 problem"
The 18th offline real-time writing problem in Python
Answer to "Offline Real-time How to Write F01 Problem"
Answer to "Offline Real-time How to Write E13 Problem"
The 19th offline real-time writing problem in Python
How to write offline real time I tried to solve the problem of F02 with Python
Offline real-time how to write Python implementation example of E14
13th Offline Real-time How to Solve Writing Problems in Python
Offline real-time how to write E11 ruby and python implementation example
The 10th offline real-time writing reference problem. Implementation example by Python.
The 11th offline real-time writing reference problem. Implementation example by python.
How to write offline real time Solve F01 problems with Python
How to write offline real time I tried to solve E11 with python
[Introduction to Python] How to write a character string with the format function
Part 1 I wrote the answer to the reference problem of how to write offline in real time in Python
How to write offline real time I tried to solve E12 with python
[Introduction to Python] How to iterate with the range function?
Try to solve the internship assignment problem with Python
The first algorithm to learn with Python: FizzBuzz problem
[Python] How to specify the download location with youtube-dl
I tried to solve the problem with Python Vol.1
[Python] How to rewrite the table style with python-pptx [python-pptx]
Write to csv with Python
Part 1 I wrote an example of the answer to the reference problem of how to write offline in real time in Python
[Note] How to write QR code and description in the same image with python
I tried to simulate how the infection spreads with Python
A story about how to deal with the CORS problem
How to get into the python development environment with Vagrant
[Introduction to Python] How to get data with the listdir function
The twelfth offline real-time writing reference problem. Implementation by python
How to write a Python class
Python: How to use async with
[Python] Write to csv file with Python
How to get the Python version
How to get started with Python
[Python] How to import the library
How to use FTP with Python
How to calculate date with python
[Introduction to Python] How to split a character string with the split function
How to write offline real time Solve E04 problems in Python
[Python] Explains how to use the format function with an example
I wanted to solve the ABC164 A ~ D problem with Python
How to send a request to the DMM (FANZA) API with python
How to deal with the problem that the current directory moves when Python is executed from Atom
Answer to "Offline real-time writing F04 problem"
How to solve the bin packing problem
How to write Python document comments (Docstrings)