[PYTHON] Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Summer 2017 Programming Exam

This is an example of the answer to the 2017 summer hospital exam.

Question theme

--Canvas drawing

Problem statement

(1)

pic = {
    '0': [['*', '*', '*', '*'],
          ['|', ' ', ' ', '|'],
          ['*', ' ', ' ', '*'],
          ['|', ' ', ' ', '|'],
          ['*', '*', '*', '*']],
    '1': [['*'],
          ['|'],
          ['*'],
          ['|'],
          ['*']],
    '2': [['*', '*', '*', '*'],
          [' ', ' ', ' ', '|'],
          ['*', '*', '*', '*'],
          ['|', ' ', ' ', ' '],
          ['*', '*', '*', '*']],
    '3': [['*', '*', '*', '*'],
          [' ', ' ', ' ', '|'],
          ['*', '*', '*', '*'],
          [' ', ' ', ' ', '|'],
          ['*', '*', '*', '*']],
    '4': [['*', ' ', ' ', '*'],
          ['|', ' ', ' ', '|'],
          ['*', '*', '*', '*'],
          [' ', ' ', ' ', '|'],
          [' ', ' ', ' ', '*']],
    '5': [['*', '*', '*', '*'],
          ['|', ' ', ' ', ' '],
          ['*', '*', '*', '*'],
          [' ', ' ', ' ', '|'],
          ['*', '*', '*', '*']],
    '6': [['*', ' ', ' ', ' '],
          [' ', ' ', ' ', ' '],
          ['*', '*', '*', '*'],
          ['|', ' ', ' ', '|'],
          ['*', '*', '*', '*']],
    '7': [['*', '*', '*', '*'],
          [' ', ' ', ' ', '|'],
          [' ', ' ', ' ', '*'],
          [' ', ' ', ' ', '|'],
          [' ', ' ', ' ', '*']],
    '8': [['*', '*', '*', '*'],
          ['|', ' ', ' ', '|'],
          ['*', '*', '*', '*'],
          ['|', ' ', ' ', '|'],
          ['*', '*', '*', '*']],
    '9': [['*', '*', '*', '*'],
          ['|', ' ', ' ', '|'],
          ['*', '*', '*', '*'],
          [' ', ' ', ' ', '|'],
          [' ', ' ', ' ', '*']],
}

def solve1(txt_num):
    nums_array = []
    for i in txt_num:
        nums_array.append(pic[i])
    nums = len(nums_array)
    row_num = 5
    with open('out1.txt', 'w') as f:
        for row in range(row_num):
            row_txt = ''
            for num_array in nums_array:
                for mark in num_array[row]:
                    row_txt += mark
                row_txt += '  '
            f.writelines(row_txt[:-2] + '\n')
            print(row_txt[:-2])

(2)

def toNumber(num_array):
    for key in pic.keys():
        if num_array == pic[key]:
            return key
    return '-'


def solve2(file_path):
    with open(file_path, 'r') as f:
        txt_rows = []
        col = 0
        row = 5
        for i in range(5):
            line = f.readline()
            line = line[:-1]
            col = max(col, len(line))
            txt_rows.append(line)
        #Added 10 blank columns on the right side as sentinel
        canvas = [[' ' for _ in range(col + 10)] for _ in range(row)]
        for i, line in enumerate(txt_rows):
            for j, mark in enumerate(line):
                canvas[i][j] = mark
        x = 0
        ret = ''
        while (x < col):
            num_array_not_one = [[' ' for _ in range(4)] for _ in range(5)]
            for i in range(4):
                for j in range(5):
                    num_array_not_one[j][i] = canvas[j][x + i]
            key = toNumber(num_array_not_one)
            if key != '-':
                ret += key
                x += 6
                continue

            num_array_one = [[' ' for _ in range(1)] for _ in range(5)]
            for i in range(1):
                for j in range(5):
                    num_array_one[j][i] = canvas[j][x + i]
            key = toNumber(num_array_one)
            if key != '-':
                ret += key
                x += 3
                continue
        print(ret)

(3)

class Numobj(object):
    def __init__(self, txt_num, x, y):
        self.txt_num = txt_num
        self.x = x
        self.y = y
    
    def __repr__(self):
        return '{0}, ({1}, {2})'.format(self.txt_num, self.x, self.y)
        
def solve3(txt):
    input_list = txt.split(',')
    input_txt_num = input_list[0]
    nums = len(input_txt_num)
    numobjs = []
    x_cnt = 0
    for i in range(nums):
        txt_num = input_txt_num[i]
        num = int(txt_num)            
        y = int(input_list[1 + i * 2])
        x = x_cnt
        numobjs.append(Numobj(txt_num, x, y))
        if i < nums - 1:                
            x_cnt += int(input_list[2 + i * 2])
            if num == 1:
                x_cnt += 1
            else:
                x_cnt += 4
    col = numobjs[-1].x
    if numobjs[-1].txt_num == '1':
        col += 1
    else:
        col += 4
    row = 5
    tmp_max = 0
    for numobj in numobjs:
        tmp_max = max(numobj.y, tmp_max)
    row += tmp_max
    
    canvas = [[' ' for _ in range(col)] for _ in range(row) ]

    for numobj in numobjs:
        if numobj.txt_num == '1':
            for i in range(1):
                for j in range(5):
                    canvas[j + numobj.y][i + numobj.x] = pic[numobj.txt_num][j][i]
        else:
            for i in range(4):
                for j in range(5):
                    canvas[j + numobj.y][i + numobj.x] = pic[numobj.txt_num][j][i]
    with open('out3.txt', 'w')as f:        
        for row in canvas:
            row_txt = ''
            for mark in row:
                row_txt += mark
            f.writelines(row_txt + '\n')    
            print(row_txt)

(4)

def toNumber(num_array):
    for key in pic.keys():
        if num_array == pic[key]:
            return key
    return '-'

def solve4(file_path):
    with open(file_path, 'r') as f:
        txt_rows = []
        col = 0
        row = 0
        while True:
            line = f.readline()
            if line == '':
                break
            else:
                line = line[:-1]
                row += 1
                col = max(col, len(line))
                txt_rows.append(line)
        #Added 10 blank columns on the right side as sentinel
        canvas = [[' ' for _ in range(col + 10)] for _ in range(row)]
        for i, line in enumerate(txt_rows):
            for j, mark in enumerate(line):
                canvas[i][j] = mark 

        x = 0        
        ret = ''
        while (x < col):
            for y in range(row - 4):
                num_array_not_one = [[' ' for _ in range(4)] for _ in range(5)]
                for i in range(5):
                    for j in range(4):
                        num_array_not_one[i][j] = canvas[y + i][x + j]                       
                
                key = toNumber(num_array_not_one)        
                if key != '-':
                    ret += key
                    x += 4
                    break
                    
                num_array_one = [[' ' for _ in range(1)] for _ in range(5)]
                for i in range(5):
                    for j in range(1):
                        num_array_one[i][j] = canvas[y + i][x + j]
                        
                key = toNumber(num_array_one)    
                if key != '-':
                    ret += key
                    x += 2
                    break
                
            x += 1
        print(ret)

(5)

def toNumberNotOne(num_array):    
    #Degree of coincidence
    score = 0
    ret = '-'
    for key in pic.keys():
        tmpscore = 0
        if key == '1':
            continue
        pic_array = pic[key]
        for i in range(4):
            for j in range(5):
                if pic_array[j][i] == num_array[j][i]:
                    tmpscore += 1
        if tmpscore > score:
            score = tmpscore
            ret = key
    #The best match 15/If it matches 20 or more, it is judged to be that number
    if score > 14:
        print(score, ret)
        return ret
    else:
        return '-'

def toNumberOne(num_array):
    score = 0
    ret = '-'
    one_array = pic['1']
    for i in range(1):
        for j in range(5):
            if one_array[j][i] == num_array[j][i]:
                score += 1
    if score > 2:
        return '1'
    else:
        return '-'

def solve5(file_path):
    with open(file_path, 'r') as f:
        txt_rows = []
        col = 0
        row = 0
        while True:
            line = f.readline()
            if line == '':
                break
            else:
                line = line[:-1]
                row += 1
                col = max(col, len(line))
                txt_rows.append(line)
        #Added 10 blank columns on the right side as sentinel
        canvas = [[' ' for _ in range(col + 10)] for _ in range(row)]
        for i, line in enumerate(txt_rows):
            for j, mark in enumerate(line):
                canvas[i][j] = mark                
        x = 0        
        ret = ''
        while (x < col):
            for y in range(row - 4):
                num_array_not_one = [[' ' for _ in range(4)] for _ in range(5)]
                for i in range(4):
                    for j in range(5):
                        num_array_not_one[j][i] = canvas[y + j][x + i]
                key = toNumberNotOne(num_array_not_one)
                if key != '-':
                    ret += key
                    x += 4
                    break
                    
                num_array_one = [[' ' for _ in range(1)] for _ in range(5)]
                for i in range(1):
                    for j in range(5):
                        num_array_one[j][i] = canvas[y + j][x + i]
                key = toNumberOne(num_array_one)    
                if key != '-':
                    ret += key
                    x += 2
                    break
                
            x += 1
        print(ret)

Impressions

――I didn't have much time, and the accuracy of (5) became messy as a result ... -In (5), it was decided when the score was higher than the score in a certain area, so as a point to fix, even when y is larger (score comparison even in vertical search), whether it is 1 or not I think you can improve the accuracy of (this looks at 5 rows and 2 columns, and determines whether the front and back of the area are empty columns).

Recommended Posts

Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Summer 2016 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Summer 2018 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Summer 2011 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Summer 2014 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Summer 2008 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Summer 2017 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Summer 2013 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Summer 2019 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Summer 2015 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Summer 2007 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Winter 2010 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Winter 2014 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Winter 2013 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Winter 2006 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Winter 2015 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Winter 2007 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Winter 2012 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Winter 2011 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Summer 2012 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Summer 2018 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Summer 2011 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Summer 2014 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Summer 2008 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Summer 2017 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Summer 2013 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Summer 2019 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Summer 2015 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Summer 2007 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Winter 2010 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Winter 2014 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Winter 2013 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Winter 2006 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Winter 2015 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Winter 2007 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Winter 2012 Programming Exam
Graduate School of Information Science and Technology, The University of Tokyo, Department of Creative Informatics, Winter 2011 Programming Exam