Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #42131 > unrolled thread
| Started by | khaosyt@gmail.com |
|---|---|
| First post | 2013-03-28 07:39 -0700 |
| Last post | 2013-03-29 04:17 +1100 |
| Articles | 7 — 4 participants |
Back to article view | Back to comp.lang.python
Help printing the integers of a longer number khaosyt@gmail.com - 2013-03-28 07:39 -0700
Re: Help printing the integers of a longer number Chris Angelico <rosuav@gmail.com> - 2013-03-29 01:48 +1100
Re: Help printing the integers of a longer number Joel Goldstick <joel.goldstick@gmail.com> - 2013-03-28 10:48 -0400
Re: Help printing the integers of a longer number Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-03-28 19:03 +0200
Re: Help printing the integers of a longer number Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-03-28 19:11 +0200
Re: Help printing the integers of a longer number Chris Angelico <rosuav@gmail.com> - 2013-03-29 04:19 +1100
Re: Help printing the integers of a longer number Chris Angelico <rosuav@gmail.com> - 2013-03-29 04:17 +1100
| From | khaosyt@gmail.com |
|---|---|
| Date | 2013-03-28 07:39 -0700 |
| Subject | Help printing the integers of a longer number |
| Message-ID | <7764e61c-e4cc-413a-a76d-2d37f39abc61@googlegroups.com> |
I want to print the individual numbers of a large number using division and modulus division. For example: Enter a positive integer: 54321 5 4 3 2 1
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-03-29 01:48 +1100 |
| Message-ID | <mailman.3877.1364482107.2939.python-list@python.org> |
| In reply to | #42131 |
On Fri, Mar 29, 2013 at 1:39 AM, <khaosyt@gmail.com> wrote: > I want to print the individual numbers of a large number using division and modulus division. > > For example: > > Enter a positive integer: 54321 > 5 > 4 > 3 > 2 > 1 Python has two operators that can help here: // for integer division - returns the quotient without the remainder % for modulus - returns the remainder You'll also want to use a while loop to continue gathering digits so long as there's something left in the number. And if you want the digits to come out in that order, you're probably going to want to gather them into a list and then output them in reverse. But start by ignoring that part and producing something that, for the input 54321, produces 1, 2, 3, 4, and then 5. Finally, when you're asking about homework, please be honest about it. We can tell, you're not putting one over us :) Better still, post your non-working code and explain where you're having trouble; we'll be happy to help you learn, but we won't simply give you the answer. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2013-03-28 10:48 -0400 |
| Message-ID | <mailman.3878.1364482145.2939.python-list@python.org> |
| In reply to | #42131 |
[Multipart message — attachments visible in raw view] — view raw
On Thu, Mar 28, 2013 at 10:39 AM, <khaosyt@gmail.com> wrote: > I want to print the individual numbers of a large number using division > and modulus division. > > For example: > > Enter a positive integer: 54321 > 5 > 4 > 3 > 2 > 1 > > This looks familiar. Make the integer a string and use a for loop to iterate over each item > -- > http://mail.python.org/mailman/listinfo/python-list > -- Joel Goldstick http://joelgoldstick.com
[toc] | [prev] | [next] | [standalone]
| From | Jussi Piitulainen <jpiitula@ling.helsinki.fi> |
|---|---|
| Date | 2013-03-28 19:03 +0200 |
| Message-ID | <qoty5d7h2x6.fsf@ruuvi.it.helsinki.fi> |
| In reply to | #42131 |
khaosyt@gmail.com writes: > I want to print the individual numbers of a large number using > division and modulus division. > > For example: > > Enter a positive integer: 54321 > 5 > 4 > 3 > 2 > 1 Those numbers are called the digits of the large number. With divmod(54321, 10) you get both the number that is "left" after removing the last digit, and the last digit: >>> left, last = divmod(54321, 10) >>> left 5432 >>> last 1 Define a function, print_digits(num), that prints the digits of the non-negative integer num. Zero turns out fine so let's allow zero: def print_digits(num): left, last = divmod(num, 10) if left < 0: print the digits of left print(last) How do you print the digits of left? With print_digits. Why does it work? Because you only call print_digits again when left is closer to zero than num. It's called recursion.
[toc] | [prev] | [next] | [standalone]
| From | Jussi Piitulainen <jpiitula@ling.helsinki.fi> |
|---|---|
| Date | 2013-03-28 19:11 +0200 |
| Message-ID | <qottxnvh2l4.fsf@ruuvi.it.helsinki.fi> |
| In reply to | #42169 |
Jussi Piitulainen writes:
> khaosyt@gmail.com writes:
>
> > I want to print the individual numbers of a large number using
> > division and modulus division.
> >
> > For example:
> >
> > Enter a positive integer: 54321
> > 5
> > 4
> > 3
> > 2
> > 1
>
> Those numbers are called the digits of the large number.
>
> With divmod(54321, 10) you get both the number that is "left" after
> removing the last digit, and the last digit:
>
> >>> left, last = divmod(54321, 10)
> >>> left
> 5432
> >>> last
> 1
>
> Define a function, print_digits(num), that prints the digits of the
> non-negative integer num. Zero turns out fine so let's allow zero:
>
> def print_digits(num):
> left, last = divmod(num, 10)
> if left < 0: print the digits of left
> print(last)
Blush. That should be:
...
if left > 0: ...
...
(Or just "if left" because left will eventually be 0, positive numbers
are true values, and 0 is a false value.)
Sorry about that.
> How do you print the digits of left? With print_digits. Why does it
> work? Because you only call print_digits again when left is closer to
> zero than num.
>
> It's called recursion.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-03-29 04:19 +1100 |
| Message-ID | <mailman.3904.1364491182.2939.python-list@python.org> |
| In reply to | #42170 |
On Fri, Mar 29, 2013 at 4:11 AM, Jussi Piitulainen
<jpiitula@ling.helsinki.fi> wrote:
> Jussi Piitulainen writes:
>
>> khaosyt@gmail.com writes:
>>
>> > I want to print the individual numbers of a large number using
>> > division and modulus division.
>> >
>> > For example:
>> >
>> > Enter a positive integer: 54321
>> > 5
>> > 4
>> > 3
>> > 2
>> > 1
>>
>> Those numbers are called the digits of the large number.
>>
>> With divmod(54321, 10) you get both the number that is "left" after
>> removing the last digit, and the last digit:
>>
>> >>> left, last = divmod(54321, 10)
>> >>> left
>> 5432
>> >>> last
>> 1
>>
>> Define a function, print_digits(num), that prints the digits of the
>> non-negative integer num. Zero turns out fine so let's allow zero:
>>
>> def print_digits(num):
>> left, last = divmod(num, 10)
>> if left < 0: print the digits of left
>> print(last)
>
> Blush. That should be:
>
> ...
> if left > 0: ...
> ...
>
> (Or just "if left" because left will eventually be 0, positive numbers
> are true values, and 0 is a false value.)
>
> Sorry about that.
Sorry, I just nitpicked that very thing, hehe :)
Note that this doesn't work with negative numbers; it'll infinitely
recurse, due to divmod's behaviour. You'd need a special trap in there
to handle that:
if num<0:
print("-")
num=-num
# and continue.
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-03-29 04:17 +1100 |
| Message-ID | <mailman.3903.1364491047.2939.python-list@python.org> |
| In reply to | #42169 |
On Fri, Mar 29, 2013 at 4:03 AM, Jussi Piitulainen <jpiitula@ling.helsinki.fi> wrote: > def print_digits(num): > left, last = divmod(num, 10) > if left < 0: print the digits of left > print(last) > > How do you print the digits of left? With print_digits. Why does it > work? Because you only call print_digits again when left is closer to > zero than num. > > It's called recursion. An elegant solution, but buggy, I'm afraid... fortunately it's a trivial problem. The comparison should be left>0. :) ChrisA
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web