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 initialize_square_list_of_lists(size): result = [] for i in range(size): result.append([]) for i in range(size): for j in range(size): result[i].append(0) return result def columns(input_list): result = initialize_square_list_of_lists(len(input_list)) size = len(input_list) if not is_a_square_list_of_lists(input_list): return "not square_lol" for i in range(size): for j in range(size): result[i][j] = input_list[j][i] return result def is_symmetrical_list(input_list): return input_list == columns(input_list) def test(): test_cases = [ ([[1, 2, 3], [2, 3, 1], [3, 1, 2]], True), ([[1, 2, 3], [2, 3, 1], [3, 1, 10-8]], True), ([[1, 2, 3], [1, 2, 3], [1, 2, 3]], True), ([[1]], True), ([['a']], True), ([[[0, 1, 2]]], True), ([[('name', 'rank', 'serial number')]], True), ([[]], 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