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..
1 Answer 1
- Use
json
to simplify your input handling. - You can use
str.format
to simplify all theprints
. - 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:
data = json.loads(input_)
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()
-
\$\begingroup\$ Thank you, but should '{3} {2} {2} {0}\n' be '{3} {2} {1} {0}\n'? \$\endgroup\$The T– The T2019年07月13日 23:14:47 +00:00Commented Jul 13, 2019 at 23:14
-
\$\begingroup\$ @TravisWells You are correct, I have updated the answer to account for this. \$\endgroup\$2019年07月13日 23:24:17 +00:00Commented Jul 13, 2019 at 23:24
-
\$\begingroup\$ 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 \$\endgroup\$The T– The T2019年07月13日 23:33:08 +00:00Commented Jul 13, 2019 at 23:33
-
1\$\begingroup\$ @TravisWells I accidentally used
!=
rather than==
. The program told you what the problem was tho, so it shouldn't be a 'strange reason'. \$\endgroup\$2019年07月13日 23:38:39 +00:00Commented Jul 13, 2019 at 23:38 -
\$\begingroup\$ yeah, I'm still a novice. I have to keep learning syntax errors. Just trying to be observant. \$\endgroup\$The T– The T2019年07月13日 23:39:50 +00:00Commented Jul 13, 2019 at 23:39