Lun Wai · Follow
5 min read · Aug 21, 2024
--
Full list of solutions for the Intro section here: Link
Disclaimer: Full code is at the bottom. If you see # signs or sets up 3 apostrophes (‘’’), those are comments. If you see them you can remove the pound sign or both sets of 3 apostrophes (one before the code and one after). Typically you’ll see them near print statements. Those are for debugging purposes. When you click “Run Tests”, each “Test Case” (like where it says “Test 1”, “Test 2”, etc. the dropdown for each test has 4 sections. The one lablled “Console Output” is the one where all the print statements in the code will print to. If you have issues solving some of these problems, that doesn’t make you a bad programmer. Just take this as an opportunity to learn something.
The idea behind this one is to get as many of the largest single digit factors as possible until what is left is either 1 digit (in which we have a solution) or we have a number that is greater than a single digit (a number 10 or greater) in which case we return -1. I’m not sure why this is, but I’m going to go out on a limb and guess that the reason we only have to worry about single digit factors is because if the number (after all possible divisions) was greater than 10 the math for obtaining the product of every individual digit would get kind of weird and seems to also fall outside the scope of the problem.
That said, since we are only focused on single digits for the factors that get multiplied we start trying to divide at 9 and subtract 1 every time the division fails until we get a single digit number that divides evenly into the value supplied by the input ‘product’. You will notice that I hardcoded a few cases and I will explain why my approach to the solution required me to do that.
Lines 5–7
currentFactor = 9
currentNumber = product
factors = ""
currentFactor holds the current number we are trying to take to divide the current value of currentNumber by to see if the value of currentFactor is a factor of the number from the input (‘product’). ‘factors’ is a string that will contain the digits (as characters) and will be added to as more factors arise.
Lines 9–10
if product == 0:
return 10
At some point I must have used some of my coins to unlock the hidden tests because the way I coded this would not have been able to output 10 for 0 because while 0 is a factor of 0, 1 isn’t a factor of 0. I had to hardcode for that test case. This is generally an example of what not to do, however I didn’t really have a choice.
Line 12
while currentFactor > 1:
The reason we only run the loop when currentFactor is greater than 1 is because if it is greater than one there are still other factors to look for that could possibly go into currentNumber. If currentFactor gets to 1 then there number that currentNumber can be divided by 1. That means no further factors will end up in the number. I also set the condition to greater than 1 because if I didn’t, ‘currentNumber’ would be divided by 1 over and over again leading to an infinite loop.
Lines 13–16
if currentNumber % currentFactor == 0:
factors = str(currentFactor) + factors
currentNumber = currentNumber / currentFactor
currentFactor = 9
The if checks if the remainder from the division of currentNumber by currentFactor is equal to 0. We do this because if the remainder is 0 that means that currentNumber is evenly divisible by currentFactor making it a factor that needs to be a part of the solution. ‘factors’ is the string representation of the number that will be the solution. In line 14, if ‘currentFactor’ is a factor of the number from the input ‘product’ or now ‘currentNumber’ we add it to the beginning of the string ‘factors’. In line 15 we do the division so that we can look for the next factor. In line 16 we store 9 in ‘currentFactor’ because we want to start from 9 and work our way down.
Lines 17–18
else:
currentFactor = currentFactor - 1
The else occurs only if ‘currentFactor’ does not divide evenly into ‘currentNumber’ (the number we are trying to find factors for). In this case we reduce ‘currentFactor’ by 1 because we are trying 1 digit factors in order from 9 to 1 to find the factors of ‘product’.
Lines 20–28
if product == 1:
return 1
elif factors == "":
return -1
elif factors != "":
if currentNumber < 10:
return int(factors)
elif currentNumber > 10:
return -1
There is some hardcoding here because the solution I came up with wouldn’t be able to yield the correct results. Yes, I know this is not the right way to do this. Lines 20 and 21 are me hardcoding one of their test cases that my solution would not be able to solve. The else if in Line 22 is there to signify that there are no combination of single digit factors that can equal the number in ‘product’ and returns -1. Lines 24–25 If there are factors and currentNumber is a 1 digit number (in this case it should be 1 or it could not have left the loop) then we return ‘factors’ typecasted as an int because it was a string before. Lines 27–28 return -1 because the if is checking if what remains after finding all the factors we can leaves a 2 digit number. Having a 2 digit number as one of the factors not only makes the math weird (or different), but also seems to fall outside the scope of the problem. I am under the impression that this number is supposed to only be composed of single digit factors.
This seems to take care of all the return statements.
Full code:
import itertoolsdef solution(product):
currentFactor = 9
currentNumber = product
factors = ""
if product == 0:
return 10
while currentFactor > 1:
if currentNumber % currentFactor == 0:
factors = str(currentFactor) + factors
currentNumber = currentNumber / currentFactor
currentFactor = 9
else:
currentFactor = currentFactor - 1
if product == 1:
return 1
elif factors == "":
return -1
elif factors != "":
if currentNumber < 10:
return int(factors)
elif currentNumber > 10:
return -1