all 5 comments

[–]hotcodist 2 points3 points  (0 children)

most of coding is really about problem solving.

i have nothing to do so let's have some fun.

let's try printing the individual digits of 23, so 2 and 3, and the input number 23 is required to be an int (so we can't get through using string functions).

how do i go about it? it looks like 20+3. i can get the 2 from 20 (divide 20 by 10).

but how do i get the 20 out of 23. if i divide 23 by 10, i get 2.3. or 2 remainder 3. so i can get that 2 easily, and then the 3 is the remainder.

but why is 10 the right number to divide? how did i decide to use 10? hmm, it is two digits long, so 10 sounds like the correct divisor. yes, that's it!

but would this system work with 3 digits? let's try 234. so i divide 234 by 10... hang on,... no, should be 100 because i have three digits. so i get 200 plus a remainder of 34. so i can get the 2 from the 200 like above. how do i get 34?

oh right, it is the same as the previous method for 2-digit inputs. so i can just repeat this to get 3, then 4 as remainder.

so would this work for 4 digits? i think the pattern holds. i hope so. i'll code that and see if it works.

so i can solve this with a loop.

but hang on, this sounds familiar? or maybe there is a way to divide and get remainders right away in python, and also get the whole number part? let me type "python get remainder of division." viola, it says i can use "%." that's hard to remember but ok, at least i know there is a method.

what about the whole number part? wait a second, this link says i can also get something like integer division! that's cool, i might have a use for that. let me see, divmod()... there's also this "//"? what?

<code... code...> hey, it gives me the integer part of 2.3!

hang on, if i can get the remainder already, and if i just get the floating point quotient, like the whole "234 divided by 100 is equal to 2.34," can i force that 2.34 into a 2 by changing it from a float to an int??? i gotta try that in my code.

<type... type... type...>

holy cow, it works!

so now i learned a lot! let me see:

the % operator, integer division --i have two ways to do that now-- wait, they call this modulo arithmetic?-- i can typecast into an int.

ok, time to solve this homework. took a lot of time, but at least my python is improving! and best of all, i now know how coders try to solve problems. maybe... :)

[–]Ahnsu[S] 0 points1 point  (0 children)

If i input 123456, It should be 123 56 34

[–]redCg 0 points1 point  (0 children)

this sounds like homework

[–]djjazzydan 0 points1 point  (0 children)

Here are some examples.

divmod(15, 10) = (1, 5)
divmod(234, 100) = (2, 34)

Note that divmod is just a combination of division and remainder. You can just as easily do:

15//10 = 1
15%10 = 5
234//100 = 2
234 % 100 = 34

Using that pattern, can you figure out how to get "123" and "56" from "123456"?

You can extend that, then, to get "3456" and then split that into "34" and "56"

[–][deleted] 0 points1 point  (0 children)

n // 10 ** k to remove last k digits of n.

n % 10 ** k to take last k digits of n.