Python script takes input of four elements and outputs a valid 4 × 4 Sudoku grid in O(n) time, but only when given non-repeating elements

Introduction

I've found a clever and fun thing to do after solving a n2 × n2 sudoku puzzle. I can take a grid as such as this one and hard-code its indices as constraints to output other 4 × 4 latin squares in poly-time

Now, I took each successive row and hardcoded as indices with successive print statements.

Indices constraints

0123
3210
1032
2301


Working Code

print('enter with [1,2,3...] brackets')
text = input()[1:-1].split(',')
print(text[0], text[1], text[2], text[3])
print(text[3], text[2], text[1], text[0])
print(text[1], text[0], text[3], text[2])
print(text[2], text[3], text[0], text[1])


Question

Being a novice at python I'm asking is there a better way of hardcoding the Sudoku's pattern with fewer lines of code?

Because it would be daunting to have to write larger constraints for larger latin squares.

I would appreciate it to keep it O(n) time because I have a desire to input integers besides just elements 1-4. But, 100-104 and so on..

• Use json to simplify your input handling.
• You can use str.format to simplify all the prints.
• You don't handle incorrect data well. What if I enter 3 or 5 numbers?
• Your code doesn't run in $$\O(n)\$$ time, it runs in $$\O(n^2)\$$ time. I recommend that you ignore $$\O\$$ and just get working code if you're a novice. After you get it working make it readable. Finally here you should time your code to see if you need to then optimize it.
• Use functions, they make your code easier to use.
import json

def handle_input(input_):
try:
except ValueError:
raise ValueError('Invalid format.')

if len(data) != 4:
raise ValueError(f'Incorrect amount of numbers, got {len(data)} not 4.')
return data

def main():
print('Enter four numbers in brackets. E.g. [1, 2, 3, 4]')
data = handle_input(input())
print(
'{0} {1} {2} {3}\n'
'{3} {2} {1} {0}\n'
'{1} {0} {3} {2}\n'
'{2} {3} {0} {1}\n'
.format(*data)
)

if __name__ == '__main__':
main()

• Thank you, but should '{3} {2} {2} {0}\n' be '{3} {2} {1} {0}\n'? – Travis Wells Jul 13 '19 at 23:14
• @TravisWells You are correct, I have updated the answer to account for this. – Peilonrayz Jul 13 '19 at 23:24
• Also for some strange reason. I get an error. Try it and run it in the link below. You'll see. It seems to be fixed if you switch it 4 to a 5 (line 10). And again, I appreciate your answer. That should work. repl.it/repls/SquareLinearConferences – Travis Wells Jul 13 '19 at 23:33
• @TravisWells I accidentally used != rather than ==. The program told you what the problem was tho, so it shouldn't be a 'strange reason'. – Peilonrayz Jul 13 '19 at 23:38
• yeah, I'm still a novice. I have to keep learning syntax errors. Just trying to be observant. – Travis Wells Jul 13 '19 at 23:39