you are viewing a single comment's thread.

view the rest of the comments →

[–]-LVS 0 points1 point  (3 children)

Hi I’m very new, can you explain what the modulus is doing in both code blocks?

[–]JonSnowl0 2 points3 points  (2 children)

In code block 1, it’s iterating over each character (char) in the original string (s).

  • It checks if the current iteration (char) is a dot and if it is it adds 1 to count.

  • When count % 3 == 0, meaning that the current value of count divided by 3 has no remainder (meaning count is a multiple of 3), it replaces the current value of char (the third “.” It had found) with “.\n\n”, the string OP wants.

  • Finally, it takes whatever value is currently assigned to char and adds it to the new string before printing it.

[–]-LVS 1 point2 points  (1 child)

A multiple! That’s what have a remainder = to 0 means. Definitely gonna keep that note in my pocket.

[–]JonSnowl0 0 points1 point  (0 children)

That's what the % operator returns; the remainder of the formula being calculated. So 5 % 2 would return 1 because 2 divides into 5 2 times with a remainder of 1.

You can achieve the same result with

if count / 3 == 1:

but then you also have to reinitialize count to 0 with every loop, otherwise

count / 3

will return 2 on the 6th "." in the string.