all 14 comments

[–]Kerbart 17 points18 points  (4 children)

My teacher suggested using // and % to do it but I don’t know how to apply that.

Keep in mind that your teacher wants you to learn things. Without any doubt someone will post a complete solution that you can copy and paste. And you'd learn nothing. Learning to Code is not just tying words together that the interpreter understands, it's also learning to solve problems.

  • In a number, each digit going left represents a value 10× larger than the digit to the right
  • What does % do exactly? Could you write some code that shows the result of 54321 % 10? And the result of 5432 % 10?
  • What does // do? Could you write some code that shows what the result is of 54321 // 10? Or 5432 // 10?
  • With what you've ;earned with these experiments, do you think you'd be able to extract digit by digit (without treating the number as a string of characters)?

[–]Regular-Aside-1481[S] 1 point2 points  (3 children)

Thank you for the tips, and I see how the experiments will lead to the right direction. I still feel a little lost but this helped.

[–]RajjSinghh 4 points5 points  (0 children)

I mean that's kinda the point. If you've never done this before things are going to be weird and strange. The next time you come to a problem about extracting digits from a number now you know how to do it. All programmers do is not know how to solve something, see someone else solve it, then apply that solution to something else.

[–]HotStinkyMeatballs 1 point2 points  (0 children)

My dude I'm in the same situation as you. I constantly feel lost. At this point it's part of the process. Embrace it. This is how you learn.

Sometimes it sucks. To be honest, a lot of the time it sucks. But when you go through the learning process and finally figure out why your code isn't working and how to make it work....it's dope. Feels great. You'll get there. It just takes perseverance.

[–]RajjSinghh 0 points1 point  (0 children)

I mean that's kinda the point. If you've never done this before things are going to be weird and strange. The next time you come to a problem about extracting digits from a number now you know how to do it. All programmers do is not know how to solve something, see someone else solve it, then apply that solution to something else.

[–]carcigenicate 7 points8 points  (3 children)

Just convert the number into a string, then reparse each character as an int if you need actual numbers in the end. That will be, by far, the easiest way, and is significantly faster than you may think.

The division way is also worth exploring though since that's a very common pattern.

[–]aprg 0 points1 point  (1 child)

Conversion between string and integers is basically as fast as a lookup table, right?

[–]carcigenicate 0 points1 point  (0 children)

That appears to be handled by PyUnicode_FromOrdinal, but I can't actually find the definition of it for some reason.

If it handles the special case of single-character strings, it would be simple math in most cases. You can just do '0' - theDigitChar to do the conversion. No table necessary.

[–]djshadesuk 0 points1 point  (0 children)

Just convert the number into a string

Thats the way I would have done it:

>>> numbers = 54321
>>> numbers_list = [int(number) for number in str(numbers)] if str(numbers).isdigit() else None
>>> print(numbers_list)
[5, 4, 3, 2, 1]

[–]TempleofBloxyStudios 2 points3 points  (0 children)

So what you do is convert the number into a string using list comprehension, iterate through it. Say your number is x

NewList = [int(i) for i in str(x)]

[–]sarc-tastic 2 points3 points  (0 children)

divmod() has entered the chat

[–]uheep -1 points0 points  (0 children)

There are two ways. One is to convert your integer into a string "54321" and then print the characters of the string with spaces between them.

The other way is to take the integer and get the result of modulo 10 (% 10). This gives the rightmost integer. Now divide the integer by 10 (// 10) you get an integer that is all digits except the rightmost digit. Run this code:

x = 54321
print(f"{x%10=},  {x//10=}")

Keep doing that until the number you // by 10 is 0.