-2

Although I know there is a built in str() function in python, I'm trying to understand its logic.

def intToStr(i):
    digits = '0123456789'
    if i == 0:
        return '0'
    result = ''
    while i > 0:
        result = digits[i%10] + result
        i = i/10
    return result

what I don't get is the part in the loop where [i%10].

How do I get a remainder when i < 10?

8-Bit Borges
  • 9,643
  • 29
  • 101
  • 198
  • 3
    Why don't you test it and find out or try searching up modulo? – Andrew Li Oct 16 '16 at 05:38
  • Are you sure of what you try to do within this piece of code of yours? While @thatotherguy guy answer is correct (applied to some other question), you do try to query string for _"remainder of divison by 10"_ index. Is that's really your intension ? – agg3l Oct 16 '16 at 05:38
  • In case you specify your indension on more details, SO community will help you much faster. I'm almost sure I understand what you try to achive, but.. You should make youself to right it down properly – agg3l Oct 16 '16 at 05:41
  • 2
    Briefly: `a%b` works exactly the same way when `a` is smaller than `b`. – TigerhawkT3 Oct 16 '16 at 05:43
  • @TigerhawkT3 it's no duplicate. linked question asks when a>b – 8-Bit Borges Oct 16 '16 at 05:53
  • 1
    Linked question simply asked how to get the remainder, just as you did. The modulo operator is the answer, the numbers picked in that question are arbitrary. `a > b`, let a = 10 and b = i. You have the same problem – OneCricketeer Oct 16 '16 at 05:58
  • @cricket_007 don't agree. I have not asked HOW to get the remainder. I thought remainder was the result of a division, where "a" should be larger than "b". an explanatory answer should point that this is not the logic with modulo. that seems the right answer to my question. I had seen the marked question, of course, and it did not answer this – 8-Bit Borges Oct 16 '16 at 06:03
  • I'm still not following why `a` should be greater than `b`. If the divisor is greater than the dividend, then the divisor does not evenly divide, so the remainder is the dividend. – OneCricketeer Oct 16 '16 at 06:10
  • It is exactly the same. Your complaint is similar to someone asking "how do I print a variable that holds a negative number?", getting it closed as a duplicate of a question that asks "how do I print a variable that holds a number?", and insisting that it's not the same because you're specifically interested in negative numbers. Well, `n%a` when `n=a`. It is most certainly a duplicate. Next time, have a go at testing things out in the interactive interpreter. It can be very helpful. – TigerhawkT3 Oct 16 '16 at 06:24
  • @TigerhawkT3 I think it is a poor comparison. you said it yourself that a%b works the same when a < b. thus, it is not self-explanatory. there is a mathematical operation implicit in %, unlike merely printing out a number, be it positive or negative. if you were right, the behavior you pointed out twice would have been stated either in an answer or in a comment in the 'duplicate' at least once, and it haven't. anyway, thank you for your time and patience. – 8-Bit Borges Oct 16 '16 at 06:56
  • It's not really a "behavior" to specifically note, so much as it is basic arithmetic: three divided by seven is zero remainder three in the same way that ten divided by seven is one remainder three. There is simply nothing noteworthy about it. There is also a mathematical operation in negative numbers: the unary `-`, so it's no different from this situation with the modulo operator. You are seeing a unique case where none exist. – TigerhawkT3 Oct 16 '16 at 08:17
  • @TigerhawkT3 nobody learns unary operations at elementary school, but one learns addition, subtraction, multuplication and division. unary it is math operation only as far as computation is xxconcerned. 3/7 = 0 is computational logic. intuitively the mathematical answer is 0.428. so it is a bit counter-intuitive that you should index with 0, then 0, then 0 again, from integers 1 to 9. – 8-Bit Borges Oct 16 '16 at 16:14
  • "3/7 = 0 is computational logic" I don't even know what you're talking about anymore. If you ask someone who's just learned long division what 3/7 is, they'll tell you "0, remainder 3." That's what this is. This issue could have been resolved by spending five seconds with a Python interpreter. Please spend at least a little bit of effort before asking a question. – TigerhawkT3 Oct 16 '16 at 21:07
  • @TigerhawkT3 only robots would answer '0, remainder 3', when prompted to answer the the result of a division, anyone human being who faces % for the first time will mentally divide a number by the other. I dont care whether you agree or not, the question was not a duplicate. you dont own truth, let alone logic reasoning, so it seems. – 8-Bit Borges Oct 16 '16 at 21:11
  • Like I said, ask someone who's just learned long division what 3/7 is. They will attempt to put three into 7, see that it's too small, write "0," see that there's no more to do, and write "remainder 7." That is what integer division and modulo are. It is not complicated. Again, this site expects at least a few seconds of independent research prior to asking a question. Please do so in the future. – TigerhawkT3 Oct 16 '16 at 21:22
  • @TigerhawkT3 sir, any simple calculator gives you a different result from '0, remainder 7', expressed in decimals. people who are not resourceful in computational logic bring certain harwired misconceptions into it. what you should do, intead of assuming wrongly what my time of research amounts to, is be aware of it and help to clarify the subject. which you did, by the way, in your first comment. but what you pointed out was not stated in the linked duplicate, so the question did not help me to clarify. hence, I was asking something else. lets end this here, please. none will convince none. – 8-Bit Borges Oct 16 '16 at 21:35

1 Answers1

1

% means mod, a number mod 10, result in the last number in it.
So 5 % 10, you will get 5.
You should try it yourself first.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
xuqiang
  • 102
  • 1
  • 6