# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
# MASTER ONLY

# %load_ext prog_edu_assistant_tools.magics
from prog_edu_assistant_tools.magics import report, autotest
# -

# GLOBAL CONTEXT
import numpy as np


# ```
# # ASSIGNMENT METADATA
# assignment_id: "5-3"
# ```
#
# ## 練習
#
# 引数に整数 $n$ を取り、$i$ から始まる連番の整数からなる配列を$i$番目 ($i\ge 0$) の行として持つ $n\times n$ の2次元配列を返す関数 `range_square_matrix()` を、`arange()` を用いて定義してください。
#
# たとえば、`range_square_matrix(3)`は、
# ```
# [[0 1 2]
#  [1 2 3]
#  [2 3 4]]
# ```
# と印字されるような2次元配列を返します。
#
# ```
# # EXERCISE METADATA
# exercise_id: "ArangeSquareMatrix"
# ```

# %%solution
def arange_square_matrix(n):
    # BEGIN SOLUTION
    return np.array([np.arange(i, n+i) for i in range(n)])
    # END SOLUTION


# 以下のセルを実行して、`True` が表示されることを確認してください。

# + tags=["raises-exception"]
print(all(map(all,(arange_square_matrix(3) == np.array([[0,1,2],[1,2,3],[2,3,4]])))))

# + tags=[]
# %%inlinetest AutograderTest_ArangeSquareMatrix

assert "arange_square_matrix" in globals(), "Did you define a function named arange_square_matrix?"
assert str(arange_square_matrix.__class__) == "<class 'function'>", f"Did you define 'arange_square_matrix' as a function? There was a {arange_square_matrix.__class__} instead"

def test_arange_square_matrix(n, expected):
    actual = arange_square_matrix(n)
    assert actual is not None, f"Did you return a value from the function? Called arange_square_matrix({n}), Got {actual}"
    assert str(actual.__class__) != "<class 'list'>", f"Did you return a numpy array value? Got a Python list instead. You can convert a Python list to a numpy array using np.array"
    assert str(actual.__class__) == "<class 'numpy.ndarray'>", f"Did you return a numpy array value? Got {actual.__class__} instead"
    assert len(actual.shape) == 2, f"Did you return a 2-dimensional array? Got a {len(actual.shape)}-dimensional array instead"
    assert actual.shape == (n, n), f"Did you return the array as a square matrix? Called arange_square_matrix({n}). Expected an array with size {n}x{n}, got size {actual.shape[0]}x{actual.shape[1]} instead"
    assert all(map(all,(expected == actual))), f"Does the matrix returned have correct values? Called arange_square_matrix({n}).\n\nExpected:\n{expected}\n\nGot this instead:\n{actual}"

test_arange_square_matrix(2, np.array([[0, 1], [1, 2]]))
test_arange_square_matrix(5, np.array([[0, 1, 2, 3, 4], [1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7], [4, 5, 6, 7, 8]]))
# -

# Check the canonical solution
result, log = %autotest AutograderTest_ArangeSquareMatrix
assert result.results['passed']
report(AutograderTest_ArangeSquareMatrix, results=result.results)


# %%submission
def arange_square_matrix(n):
    return np.array([np.arange(0, n) for i in range(n)])


result, log = %autotest AutograderTest_ArangeSquareMatrix
print(result.results)
assert not result.results['passed']
report(AutograderTest_ArangeSquareMatrix, results=result.results)


# ## 練習の解答

def arange_square_matrix(n):
    return np.array([np.arange(i, n+i) for i in range(n)])

StarcoderdataJupyterScriptsDedupFiltered