Working with Digits

Is using `str` the correct idiom for working with digits in Python

This has always been my approach and it's worked fine, though I've never done much testing for speed. It works particularly well when needing to iterate over permutations/combinations of digits, since you can build up such strings with the functions in the itertools package.

There are certainly other methods that involve less straightforward mathematical operations, but unless speed is absolutely crucial I feel the string method is the most Pythonic.


Here, for example, is a more mathematical approach, where a and b are indexed from the right (ie ones place is 0, tens place is 1, etc):

def getSubdigits(n, a, b):
n %= 10 ** a
n //= 10 ** b
return n

For this to work with the same indexing as string slicing, you'd need to find the total number of digits first, and the function becomes:

def getSubdigits2(n, a, b):
l = int(math.ceil(math.log10(n)))
n %= 10 ** (l - a)
n //= 10 ** (l - b)
return n

And the string slicing equivalent:

def subDigits3(n, a, b):
return int(str(n)[a:n])

Here's timing results:

  • subDigits: 0.293327726114
  • subDigits2: 0.850861833337
  • subDigits3: 0.990543234267

My takeaway from that result is that the slicing method is fine unless you really care about speed, in which case you need to use the first method and think about the indices in the other direction.

Working with digits

Need to look first Kernel#sprtintf :

The syntax of a format sequence is follows.

 %[flags][width][.precision]type

A format sequence consists of a percent sign, followed by optional flags, width, and precision indicators, then terminated with a field type character. The field type controls how the corresponding sprintf argument is to be interpreted, while the flags modify that interpretation.

The field width is an optional integer, followed optionally by a period and a precision. The width specifies the minimum number of characters that will be written to the result for this field.

Now coming to your example : "%012d" % 10.

"%012d" called format string. The type d means - Convert argument as a decimal number.
012 means you are specifying 12 as a minimum number of characters that will be written to the result for this field.

Now look at the documentation of String#%

Format—Uses str as a format specification, and returns the result of applying it to arg. If the format specification contains more than one substitution, then arg must be an Array or Hash containing the values to be substituted. See Kernel::sprintf for details of the format string.

Working with digits in numbers in Swift 3

I think this is easier to do with just integer manipulation:

let number = 643

let last2Digits = number % 100 // 43
let lastDigit = number % 10 // 3

let result = last2Digits - lastDigit //40

Tesseract doesn't seem to work with digits

As mentioned in tesseract github issue you can't black or whitelist characters with tesseract 4.0 LSTM, instead you should train LSTM with characters you expect on your image.

Thanks to Shreeshrii you can try his 'experimantal' digits traineddata from here

Please note that Tesseract 4.0 is still in alpha stage and if you want - you can still use 3.* versions of tesseract which support your needs from the box.
Tesseract v 3.4 tessdata is located here, library for windows can be downloaded from here

Return error 3221225725 when working with digits of pi

The most likely reason for your program crashing is a stack overflow.

On Microsoft Windows, the maximum stack size is set to about 1 megabyte, by default. This can be changed in the linker settings.

By putting 10 million characters on the stack, you are exceeding this limit.

A simple solution is to change the line

const char pi[] = "INSERT 10 MILLION CHARACTERS";

to

const char *pi = "INSERT 10 MILLION CHARACTERS";

so that the 10 million characters are no longer stored directly on the stack. Instead, you will only have a single pointer on the stack, which points to the string literal in read-only memory.

How to take the nth digit of a number in python

First treat the number like a string

number = 9876543210
number = str(number)

Then to get the first digit:

number[0]

The fourth digit:

number[3]

EDIT:

This will return the digit as a character, not as a number. To convert it back use:

int(number[0])

Getting each individual digit from a whole integer

You use the modulo operator:

while(score)
{
printf("%d\n", score % 10);
score /= 10;
}

Note that this will give you the digits in reverse order (i.e. least significant digit first). If you want the most significant digit first, you'll have to store the digits in an array, then read them out in reverse order.

Digit Frequency calculating code in C not working

To write a correct and reasonable digit-counting program:

  • Do not allocate any buffer for this.
  • Create an array to count the number of times each digit occurs. The array should have ten elements, one for each digit.
  • Initialize the array to zero in each element.
  • In a loop, read one character at a time.
  • Leave the loop when the read routine (such as getchar) indicates end-of-file or a problem, or, if desired, returns a new-line or other character you wish to use as an end-of-input indication.
  • Inside the loop, check whether the character read is a digit. If the character read is a digit, increment the corresponding element of the array.
  • After the loop, execute a new loop to iterate through the digits.
  • Inside that loop, for each digit, print the count from the array element for that digit.


Related Topics



Leave a reply



Submit