#๐Ÿ”’ Need Help Finding A Good Approach

23 messages ยท Page 1 of 1 (latest)

hollow hornet
#

Given an interger N, delete 1 or continuous digits of N to make a new int such that it's divisable by 3 (%3==0)
Find All possible ways to do that
Note: The N number itself if %3==0 also consider 1 solution

lunar cosmosBOT
#

@hollow hornet

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

hollow hornet
#

sample input/output

#

i just need help with the approach to solve this

#

if anyone respond pls ping me

frozen spoke
#

one way to check divisibility by three is to see if the sum of all digits is divisible by three

#

so if the sum of digits isn't divisible by 3, you just have to find what digits can be deleted to make it so

sage hinge
#

wow never knew this trick

#

interesting

frozen spoke
#

for example in 2009, you can delete consecutive digits (2), (2, 0), and (2, 0, 0) => three ways

sage hinge
#

why is the first one output 4

frozen spoke
#

in 1005, it's already divisible by three, so that counts as one way
then you can delete the first zero, the second zero, and both zeros together (since they're consecutive), so 4 ways in total

sage hinge
#

I see

hollow hornet
#

the problems says we can delete the digits but has to be consecutive

sage hinge
#

I imagine the simplest way is to try every combination possible and check the outcome of all of them. The fun would be how to optimize that

hollow hornet
frozen spoke
#

(I haven't tested this, just some ideas)
first find what the remainder is
then iterate through the digits:
if you can find a digit that is that remainder OR gives that remainder when divided by 3, that's one way
if that digit is followed by a number of zeros, each of those counts as a way
else consider the next digit plus the previous one, is that equal to or gives the remainder, and so on

hollow hornet
#

ok i can imagine it now

#

!close

lunar cosmosBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.