Posted By

a7xrturo on 11/27/13


Tagged

python square latin


Versions (?)

Latin Square


 / Published in: Python
 

Generates a Latin Square given the top left number and the order of the square

  1. # take inputs
  2.  
  3. sides = input('Enter a number to make a n*n latin square > ')
  4. while True:
  5. if sides.isdigit():
  6. if int(sides) in range(0, 10):
  7. break
  8. else:
  9. sides = input('Enter a number to make a n*n latin square > ')
  10. else:
  11. sides = input('Enter a number to make a n*n latin square > ')
  12.  
  13. topleft = input('Enter a number to be the first in the square > ')
  14. while True:
  15. if topleft.isdigit():
  16. if int(topleft) in range(0, 10):
  17. break
  18. else:
  19. topleft = input('Enter a number to be the first in the square > ')
  20. else:
  21. topleft = input('Enter a number to be the first in the square > ')
  22.  
  23. # generates first row
  24. sides = int(sides)
  25. topleft = int(topleft)
  26.  
  27. a = list(range(topleft, sides + 1)) + list(range(1, topleft))
  28. n = len(a)
  29. sqr = [[a[i - j] for i in range(n)] for j in range(n, 0, -1)]
  30.  
  31. for sublist in sqr:
  32. print('')
  33. for num in sublist:
  34. print(str(num), end = ' ')

Report this snippet  

You need to login to post a comment.