Making the best of social distancing

Take social distancing as an opportunity to grow. Make good use of the additional hours that you have right now. Staying active, balanced and productive is the best thing you can do for your physical…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How to Solve Digit Counts Problem?

Description

Count the number of k’s between 0 and n. k can be 0–9.

Example

— — — — — — — — — — — — — — — — — — — -

Step by Step Solution

The step 0 is to get each number from the input, we need to a simple loop.

The next step is to think through how to get the number of each number. We would like to do some experience.

Let’s consider the single-digit number (0–9) with given input k, it is very straightforward thought, just do a simple comparison.

Now let’s consider the “2+ digit” number, the way to extract each digit is to get the reminder with dividing by 10. For example, to extract each number 1,2,3 from the number 123.

We can do the calculation as 123 % 10 = 3; 12 % 10 = 2; 1 % 10 = 1. And after we extract each number, we utilize the same way we do the comparison with single-digit number.

For the above analysis, we could summarize the below steps:

1. Get the reminder with dividing by 10;

2. Do comparison;

3. Renew the number by dividing by 10;

4. Repeat the step 1–3, until the end condition, which is the renewed number is 0;

Now let’s write some pseudocode -

Let’s give it a try but the answer is wrong, let’s add some debug statement and understand what’s going on.

We found out that there is something weird in the line 20, the t is changed as the float value. That’s not we want. Since Javascript is the loose-type language, we would need to manually make it as integer using the Math.floor() operation to make it as integer.

So the final solution is

Add a comment

Related posts:

The Shameless.

It was years before they made any move. Long after we were called family; Not sisters, nor brothers, but friends who wore a stamp of repute. But now I sit, just to wallow, and to beg for bread too…

Driving Positive Change with Sustainable and Diverse Supply Chains

Originally posted at: https://www.tagww.com/insights/driving-positive-change-with-sustainable-and-diverse-supply-chains/ At Tag, we help clients reach their marketing procurement and business goals…

Preventative Health Care for the Outpatient Population

As Americans spend more time at work, the demands placed on them to stay productive have only increased. But the demands of life outside the office have remained largely unchanged. With longer…