man luke

Greenhorn

Posts: 3

Norman Radder

Ranch Hand

Posts: 146

4

man luke

Greenhorn

Posts: 3

posted 10 months ago

What do you mean by "nth happen"?

And BTW, considering that the digit is *not* always equals to the position (unless it is something that counts down like "987654321"), I think it is safe to say that your results for many cases would be incorrect.

Henry

man luke wrote:

if i input position(12354,2) nth happen

What do you mean by "nth happen"?

And BTW, considering that the digit is *not* always equals to the position (unless it is something that counts down like "987654321"), I think it is safe to say that your results for many cases would be incorrect.

Henry

posted 10 months ago

To write a correct program, you need to fully understand what every part of your program is telling the computer to do. The program you have written so far says this:

If the digit in the number's ones place is the digit we're looking for, then the position is 1 (lines 4-7)

otherwise, if the number is negative, then the position is 0 (lines 9-12)

otherwise, the position is the digit we're looking for (line 14)

Only the first part of is correct. The rest makes no sense at all.

A correct solution here will have three key elements; you already have the first one which is a way to check the first digit of the number. You're still missing the other two, namely:

1. Checking all the other digits in the number in turn

2. Keeping track of how many digits you've checked so far

Here's an analogy that might give you an idea for solving this: Imagine a line of soldiers and a sergeant who wants to inspect each soldier's uniform. The most natural way this happens is for the soldiers to all stand at attention as the sergeant slowly makes his way down the line, inspecting each soldier in turn. Now, what if that sergeant is feeling kind of lazy and doesn't feel like walking down the line. Instead, the sergeant just stands at one end of the line and as he gets done inspecting the soldier there, the sergeant dismisses him and has the line move so that the next soldier is now standing in front of him. He does this repeatedly until he has inspected all the soldier's uniforms.

So, you're the sergeant and the number is the line of soldiers you want to inspect. You already figured out how to inspect the soldier in front of you, now figure out how to get the rest of the soldiers in line to move towards you and how to repeat the process until you've inspected everyone.

If the digit in the number's ones place is the digit we're looking for, then the position is 1 (lines 4-7)

otherwise, if the number is negative, then the position is 0 (lines 9-12)

otherwise, the position is the digit we're looking for (line 14)

Only the first part of is correct. The rest makes no sense at all.

A correct solution here will have three key elements; you already have the first one which is a way to check the first digit of the number. You're still missing the other two, namely:

1. Checking all the other digits in the number in turn

2. Keeping track of how many digits you've checked so far

Here's an analogy that might give you an idea for solving this: Imagine a line of soldiers and a sergeant who wants to inspect each soldier's uniform. The most natural way this happens is for the soldiers to all stand at attention as the sergeant slowly makes his way down the line, inspecting each soldier in turn. Now, what if that sergeant is feeling kind of lazy and doesn't feel like walking down the line. Instead, the sergeant just stands at one end of the line and as he gets done inspecting the soldier there, the sergeant dismisses him and has the line move so that the next soldier is now standing in front of him. He does this repeatedly until he has inspected all the soldier's uniforms.

So, you're the sergeant and the number is the line of soldiers you want to inspect. You already figured out how to inspect the soldier in front of you, now figure out how to get the rest of the soldiers in line to move towards you and how to repeat the process until you've inspected everyone.

*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]