0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

数独であるかどうかの問題チェック

Last updated at Posted at 2017-02-11

先ほども投稿しましたがリンクが規約にあっていなかった可能性があるので削除して再投稿。
指摘点も修正してみました。
前回はwhile文を3つ使って実現しましたがfor文に変更.numpyを使ってやるアドバイスもいただいておりますのでそちらも理解して挑戦してみたいです。

target1 = [[1,2,3],
           [2,3,1],
           [3,1,2]]
target2 = [[1,1,1],
           [2,2,2],
           [3,3,3]]
target3 = [[1,2,3],
           [2,3,1],
           [1,3,2]]
target4 = [[1,2,3,4,5],
           [2,3,4,5,1],
           [3,4,5,1,2],
           [4,5,1,2,3],
           [5,1,2,3,4]]
           
def check(target):
    n = len(target)
    digit = 1
    for i in range(0, n):  # 要素数は3で0から3までをだす、すなわち0,1,2
        row_count = 0
        col_count = 0
        for j in range(0, n):
            if target[i][j] == digit:
                row_count += 1
            if target[j][i] == digit:
                col_count += 1
        if row_count != 1 or col_count != 1:
            return False
        digit += 1
    return True
def test():
    print('数独のフォームの場合')
    assert check(target1) == True
    print('リストの中身が全て同じ数字の場合')
    assert check(target2) == False
    print('最後のリストだけ条件に合ってない場合')
    assert check(target3) == False
    print('5×5の場合の正常系')
    assert check(target4) == True
    print('test ok')
test()

0
1
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?