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

enter image description here

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

Indices constraints


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])


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 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_):
        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())
        '{0} {1} {2} {3}\n'
        '{3} {2} {1} {0}\n'
        '{1} {0} {3} {2}\n'
        '{2} {3} {0} {1}\n'

if __name__ == '__main__':
  • \$\begingroup\$ Thank you, but should '{3} {2} {2} {0}\n' be '{3} {2} {1} {0}\n'? \$\endgroup\$ Jul 13, 2019 at 23:14
  • \$\begingroup\$ @TravisWells You are correct, I have updated the answer to account for this. \$\endgroup\$
    – Peilonrayz
    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\$ 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\$
    – Peilonrayz
    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\$ Jul 13, 2019 at 23:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.