[Tutor] Modulus Operator ? SOLVED
Ken Hammer
kfh777 at earthlink.net
Fri Nov 20 21:54:58 EST 2015
Thanks, Danny
Got it.
Ken
In <CAGZAPF7O5LTq7q2DUpuoB7HqmpdcKd5Ks=_J9gDEr1_vOYo22Q at mail.gmail.com>, on 11/20/15
at 01:39 PM, Danny Yoo <dyoo at hashcollision.org> said:
>>>On Thu, Nov 19, 2015 at 1:11 PM, Ken Hammer <kfh777 at earthlink.net> wrote:
>>
>>>> y = 49%13
>>>> print y
>>>> 10
>>
>>>Actually, let me pretend for a moment that I don't know what the modulus
>>>operator is. Why do we get 10 here? Can you verbalize the reason?
>>
>> 49 contains 13 3 times and leaves 10 to be divided.
>>
>>
>>>Can you modify this example above to use the modulus operator with "10" on
>>>the right hand side? What do you expect it computes? What do you see?
>>
>> I see these. Is this what you mean? In view of the third float entry I don't understand why the first two are showing me the digits of the dividend. Why doesn't y = 49%10 deliver 4 as an answer and with 100 as the divisor why don't I get 0?
>>
>>>>> y = 49%10
>>>>> print y
>> 9
>>>>> y= 49%100
>>>>> print y
>> 49
>>>>> 49/13.0
>> 3.7692307692307692
>Ok, I think I might see where're you getting stuck.
>You're misinterpreting what you read about the modulus operator. Contrary
>to what you've read, you don't use it a single time to get *all* the digits
>of the dividend all at once. Rather, what it does do is let you probe at
>the *very last* digit in the number. In that sense, when we're doing:
> 49 % 10
>What you're asking is what's left to be when we divide 49 by 10.
> 49 contains 10 4 times and leaves 9 to be divided.
>and that's why 49 % 10 == 9: it gives us the last, rightmost digit of the
>number.
>So you might be feeling a little let down. What was it trying to say?
> I think the text was trying to say, when it talks about getting all the
>digits of a number, is that you can do *repeated* uses of division and
>modulus can get the individual digits.
>In a sense, division and modulus are like measuring tools that allow us to
>shift and probe at an object to figure out what it's made of. To make this
>concrete, imagine holding something underneath a microscope. For our
>purposes, let's do this:
>############
>thing = 123456
>############
>But close your eyes for a moment: pretend for a minute that you don't know
>what this "thing" looks like. What can we do to inspect it, if we don't
>want to look at it all at once?
>We can probe and shift it, to inspect portions of it.
>#############################
>def probe(x):
> """Return the last digit of x."""
> return x % 10
>def shift(x):
> """Move x a little to the right, dropping the last digit."""
> return x / 10
>#############################
>Here, we'll have two functions that we'll use on our thing. But why do we
>call these things "probe" and "shift"?
>Because we can do things like this. We can get the last digit...
>########################
>>>> probe(thing)
>6
>########################
>And now we know that this thing ends with a 6. And we can move the thing
>around and probe again, to get the second to last digit...
>########################
>>>> probe(shift(thing))
>5
>########################
>So now we know this thing looks something like "...56"
>And we can repeat. Shift the thing twice, and probe it to get the third to
>the last digit...
>########################
>>>> probe(shift(shift(thing)))
>4
>########################
>... and so on.
>Why is this useful? In general: there are times when we're dealing with
>things that are REALLY large, things that we truly can't look at all at
>once, and knowing how to deal with just things by progressively poking and
>moving through it is a skill we can use to navigate through our data.
>You'll hear the term "iteration", which is essentially what this is.
>Exploring a number, using division and modulo operators, just happens to be
>a really simple toy example of this in action.
--
-----------------------------------------------------------
K.F.Hammer Associates Ken Hammer
management consultations Saint Johnsbury, VT 05819
-----------------------------------------------------------
More information about the Tutor
mailing list