Skip to content

Instantly share code, notes, and snippets.

@hasanqqsp
Created November 18, 2023 16:22
Show Gist options
  • Select an option

  • Save hasanqqsp/ffa5ed2747ba4f97aabb63d46bf60089 to your computer and use it in GitHub Desktop.

Select an option

Save hasanqqsp/ffa5ed2747ba4f97aabb63d46bf60089 to your computer and use it in GitHub Desktop.
Jawaban untuk problem 'Recognizing solutions that match constraints'
pattern1 = [
['R', 'Y', 'G', 'Y'],
['G', 'B', 'R', 'G'],
['Y', 'K', 'P', 'B'],
['R', 'B', 'G', 'R']
]
pattern2 = [
['R', 'Y', 'G', 'Y'],
['P', 'R', 'P', 'K'],
['K', 'B', 'Y', 'G'],
['P', 'G', 'K', 'R']
]
pattern3 = [
['P', 'R', 'K', 'Y'],
['Y', 'Y', 'B', 'G'],
['P', 'G', 'P', 'K'],
['R', 'K', 'R', 'G']
]
pattern4 = [
['Y', 'K', 'Y', 'K'],
['R', 'B', 'G', 'R'],
['Y', 'P', 'K', 'B'],
['G', 'B', 'R', 'P']
]
case = [pattern1, pattern2, pattern3, pattern4]
# bersebelahan : (x,y) == (abs(x-1),y) || (x,y) == (abs(x+1),y)
def is_valid_pattern(pattern):
count_code = {}
for y in range(len(pattern)):
row = pattern[y]
for x in range(len(row)):
cell = row[x]
count_code[cell] = count_code[cell] + \
1 if count_code.get(cell) else 1
if (count_code[cell] > 3):
return f'{cell} have more 3 land'
if x+1 < len(row) and y+1 < len(pattern):
if cell == pattern[y][x+1]:
return f'{cell} land at {(x,y)} adjacent with {(x+1,y)}'
if cell == pattern[y+1][x]:
return f'{cell} land at {(x,y)} adjacent with {(x,y+1)}'
elif x+1 == len(row) and y+1 == len(pattern):
return 'Accepted'
elif y+1 == len(pattern):
if cell == pattern[y][x+1]:
return f'{cell} land at {(x,y)} adjacent with {(x+1,y)}'
elif x+1 == len(row):
if cell == pattern[y+1][x]:
return f'{cell} land at {(x,y)} adjacent with {(x,y+1)}'
return 'Accepted'
for pattern in case:
print(is_valid_pattern(pattern))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment