# is_a_square_list_of_lists # A python procedure which takes one input, and returns the boolean True if: # 1. the input is a list # 2. any member lists contains as many members as there are member lists # For example, these inputs will return True: # [[1]] # [['a', 'b'], ['b', 'c']] # [[37, 'b', ['nesting_OK']], ['b', 'c', '']] def is_a_square_list_of_lists(my_list): if isinstance(my_list, list): size = len(my_list) for row in my_list: if len(row) != size: return False return True else: return False def test(): test_cases = [ ([], True), ([[]], False), # The list contains one list, but the list does not have precisely one member ([[1]], True), ([[1, 2], [1, 2]], True), ([[1, [2]], [1, 2]], True), ([[1, 2, 3], [2, 3, 1], [3, 1, 2]], True), ([[1, 2, 3], [2, 3, 1], [3, 1]], False), (37, False), ('a', False), ] passes = 0 for (args, expected_result) in test_cases: result = is_a_square_list_of_lists(args) if result != expected_result: print "Failed when input was: ", args, ". Expected: ", expected_result, " Got: ", result else: passes = passes + 1 print passes, " out of ", len(test_cases), " test cases passed." test()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question