0

So right now, I'm making a sudoku solver. You don't really need to know how it works, but one of the checks I take so the solver doesn't break is to check if the string passed (The sudoku board) is 81 characters (9x9 sudoku board). An example of the board would be: "000000000000000000000000000384000000000000000000000000000000000000000000000000002"

this is a sudoku that I've wanted to try since it only has 4 numbers. but basically, when converting the number to a string, it removes all the '0's up until the '384'. Does anyone know how I can stop this from happening?

deebah
  • 3
  • 1
  • You mean converting string to int? And there is nothing you can do about it... There is no meaning for leading zeros in an int. What is your end-goal? – Tomerikoo Dec 06 '20 at 16:00
  • 3
    integers dont have leading zeros. Neither do floats. – CogitoErgoCogitoSum Dec 06 '20 at 16:00
  • 1
    It is a string, so there is no reason to ever consider it a number. – Thierry Lathuille Dec 06 '20 at 16:00
  • Does this answer your question? [Why does Python remove leading zeroes when converting from string to int?](https://stackoverflow.com/questions/22597716/why-does-python-remove-leading-zeroes-when-converting-from-string-to-int) or https://stackoverflow.com/questions/13499033/how-to-retain-leading-zeros-of-int-variables – Tomerikoo Dec 06 '20 at 16:01
  • maybe you are looking for a list of ints – Chris Dec 06 '20 at 16:01

1 Answers1

2

There is no way to prevent it from happening, because that is not what is happening. Integers cannot remember leading zeroes, and something that does not exist cannot be removed. The loss of zeroes does not happen at conversion of int to string, but at the point where you parse the character sequence into a number in the first place.

The solution: keep the input as string until you don't need the original formatting any more.

Amadan
  • 191,408
  • 23
  • 240
  • 301