코드 2021 Python 솔루션의 출현: Day 9

첫 번째 부분은 크랙하기가 그다지 어렵지 않았지만 여전히 많은 시간이 걸렸습니다. 그러나 두 번째 부분은 까다 롭습니다.

1 부




import numpy as np
data,data1 = get_data(day=9)

dl = len(data1[0])
dt = np.array([int(d) for dt in data1 for d in dt])
dt = dt.reshape(-1, dl)

nums = []
pos = []
dc = len(dt[0])
dr = len(dt)
for r in range(len(dt)):
    for c in range(len(dt[0])):
        if r==0:
            if c==0:
                if dt[r,c]<dt[r+1, c] and dt[r,c]<dt[r, c+1]:
                    nums.append(dt[r,c])
                    pos.append((r,c))
            elif c==dc-1:
                if dt[r,c]<dt[r+1, c] and dt[r,c]<dt[r, c-1]:
                    nums.append(dt[r,c])
                    pos.append((r,c))
            else:
                if dt[r,c]<dt[r+1, c] and dt[r,c]<dt[r, c+1] and dt[r,c]<dt[r, c-1]:
                    nums.append(dt[r,c])
                    pos.append((r,c))
        elif r==dr-1:
            if c==0:
                if dt[r,c]<dt[r-1, c] and dt[r,c]<dt[r, c+1]:
                    nums.append(dt[r,c])
                    pos.append((r,c))
            elif c==dc-1:
                if dt[r,c]<dt[r-1, c] and dt[r,c]<dt[r, c-1]:
                    nums.append(dt[r,c])
                    pos.append((r,c))
            else:
                if dt[r,c]<dt[r-1, c] and dt[r,c]<dt[r, c+1] and dt[r,c]<dt[r, c-1]:
                    nums.append(dt[r,c])
                    pos.append((r,c))
        else:
            if c==0:
                if dt[r,c]<dt[r-1, c] and dt[r,c]<dt[r, c+1] and dt[r,c]<dt[r+1, c]:
                    nums.append(dt[r,c])
                    pos.append((r,c))
            elif c==dc-1:
                if dt[r,c]<dt[r-1, c] and dt[r,c]<dt[r, c-1] and dt[r,c]<dt[r+1, c]:
                    nums.append(dt[r,c])
                    pos.append((r,c))
            else:
                if dt[r,c]<dt[r-1, c] and dt[r,c]<dt[r, c+1] and dt[r,c]<dt[r, c-1] and dt[r,c]<dt[r+1, c]:
                    nums.append(dt[r,c])
                    pos.append((r,c))


nums


2 부



DFS 또는 BFS와 같은 일종의 검색 알고리즘을 사용해야 한다고 생각했지만 NumPy를 사용하여 StackOverflow에서 솔루션을 찾았습니다.

from scipy import ndimage

label, num_label = ndimage.label(dt < 9)
size = np.bincount(label.ravel())

top3 = sorted(size[1:], reverse=True)[:3]
print(np.prod(top3))


왜 더 읽지 않습니까?


  • Gesture Based Visually Writing System Using OpenCV and Python
  • Gesture Based Visually Writing System: Adding Visual User Interface
  • Gesture Based Visually Writing System: Adding Virtual Animationn, New Mode and New VUI
  • Gesture Based Visually Writing System: Add Slider, More Colors and Optimized OOP code
  • Gesture Based Visually Writing System: A Web App
  • Contour Based Game: Break The Bricks
  • Linear Regression from Scratch
  • Writing Popular ML Optimizers from Scratch
  • Feed Forward Neural Network from Scratch
  • Convolutional Neural Networks from Scratch
  • Writing a Simple Image Processing Class from Scratch
  • Deploying a RASA Chatbot on Android using Unity3d
  • Naive Bayes for text classifications: Scratch to Framework
  • Simple OCR for Devanagari Handwritten Text
  • 좋은 웹페이지 즐겨찾기