Skip to main content
8 of 8
edited body
Chau Giang
  • 1.3k
  • 1
  • 11
  • 16

How many 1's we get

Description

Count how many occurrences there are of the digit 1 between two given numbers \$[a, b]\$, inclusive.

For example, from 1 to 100 it should be 21:

1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 31, 41, 51, 61, 71, 81, 91, 100

The number 1 is repeated 21 times.

Rules

  1. Each number in the input list is guaranteed is an integer in the range \$0 \leq a \leq b < 2^{32}\$.
  2. The shortest answer in bytes wins.

Test cases

[1, 100] -> 21
[11, 200] -> 138
[123, 678] -> 182

Example

Here is my code using bash

eval echo {$1..$2}|grep -o 1|wc -l
Chau Giang
  • 1.3k
  • 1
  • 11
  • 16