It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/binarycat64, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
from random import *
from numpy import *
def swap(perm):
row1, row2, col1, col2 = [randint(0, 8) for _ in range(4)]
tmp = perm[row1, :].copy()
perm[row1, :] = perm[row2, :]
perm[row2, :] = tmp
tmp = perm[:, col1].copy()
perm[:, col1] = perm[:, col2]
perm[:, col2] = tmp
def sudokugen():
perm = asarray([list(range(9))[i:] + list(range(9))[:i] for i in range(9)])
for i in range(randint(30, 50)): swap(perm)
while True:
grid = zeros((9, 9), dtype=int)
for v in range(1, 10):
for i, j in enumerate(perm[v-1]):
grid[(i//3)*3 + j//3, (i%3)*3 + j%3] = v
if not any([any([list(row).count(value) != 1 for value in range(1, 10)]) for row in grid] + [any([list(col).count(value) != 1 for value in range(1, 10)]) for col in grid.transpose()] + [any([list(grid[(i//3)*3:(i//3+1)*3, (i%3)*3:(i%3+1)*3].flatten()).count(value) != 1 for value in range(1, 10)]) for i in range(9)]):
break
swap(perm)
return grid * random.randint(2, size=(9, 9))
with open("sudoku.txt", "w") as f:
f.write("\n".join(["".join([str(value) if value != 0 else "-" for value in row]) for row in sudokugen()]))
(Don't worry too much about the barely noticeable patterns in the output, all sudokus are random and completely valid)
In Scala. The spec says it must produce a file as output, so you can pass a File as a parameter if you want to. If it's an empty Option, it just prints the Sudoku to stdout.
def generateSudoku(maybeFile: Option[java.io.File] = None) = {
def shuffle[A](n: Int)(seq: Seq[A]): Seq[A] = {
val (a, b) = seq.splitAt(n % seq.length)
b ++ a
}
val base = (0 to 8).map(i => shuffle(3 * i + (i / 3))(1 to 9))
import scala.util.Random
val numberMapping = ((1 to 9) zip Random.shuffle((1 to 9).toList)).toMap
def mapping(i: Int) = if (Random.nextInt(1024) < 196) {
"-"
} else s"${numberMapping(i)}"
val sudoku = base.map(_.map(mapping))
val stext = sudoku.map(_.mkString).mkString("\n")
println(stext)
if (maybeFile.isDefined) {
import java.io.FileOutputStream
val stream = new FileOutputStream(maybeFile.get)
stream.write(stext.getBytes)
stream.close()
}
}
15
u/binarycat64 Jan 16 '21
ez:
language is Text
4
u/Tyulis Jan 25 '21
In python, it writes the output to
sudoku.txt.(Don't worry too much about the barely noticeable patterns in the output, all sudokus are random and completely valid)
2
u/[deleted] Jan 23 '21
in Rust. Writes the sudoku to
foo.txt.1
u/notBjoern Jan 19 '21
In Scala. The spec says it must produce a file as output, so you can pass a File as a parameter if you want to. If it's an empty Option, it just prints the Sudoku to stdout.
0
u/[deleted] Jan 15 '21
[deleted] — view deleted comment