+ 1
Why test case 3 in divisible task fails
You need to know if a number is divisible by each of a group of other numbers. If you are given the number and the group of other numbers, you will test to make sure that it is divisible by all of them. Task: Test your number against all of the other numbers that you are given to make sure that it is divisible by them. Input Format: Two inputs: an integer value (the number you are testing) and a string of variable length of the integers that you should test against separated by spaces. Output Format: A string that says 'divisible by all' or 'not divisible by all'. Sample Input: 100 2 5 10 Sample Output: divisible by all Here is my code https://code.sololearn.com/cADH83KQIg1Q/?ref=app
9 odpowiedzi
+ 6
You should only perform the divisibility check at line 23 when <divisors> length is non-zero.
Remember how you erase <divisors> in the while...loop? <divisors> could be empty by the time execution exits the loop. You shouldn't do the check when <divisors> is empty ...
+ 2
This is how the line looked for me, with pre-condition added. I put the original condition in parentheses.
if( divisors.length() && ( stoi( divisors ) == 0 || dividend % stoi( divisors) != 0 ) )
{
isDivisible = false;
}
What are you trying to do with line 15 ~ 19?
+ 2
I only made a change by adding a check for <divisors> length on that conditional block after the while..loop, so it's very much like your original version.
I posted the code as comment in your original code in case you want to see it ...
Good job!
+ 1
Thank you for your comments. But I think <divisors> could only be empty if the space would be at the end of the input in the string.
+ 1
Did you try to add the <divisors> length check pre-condition at line 23? it solved the 3rd case for me, Did it work for you too?
+ 1
Yes, I tried, but it does't work for me.
https://code.sololearn.com/cRQ6QgN04u6F/?ref=app
+ 1
It works now, thanks
0
I don't know what is in test case 3 so I thought about two spaces next to each other
0
Could you show me your whole code?