-1

I am converting letters in a string to their corresponding numbers and printing YES if the result is divisible by 6 or NO. For eg, ab is 12 and will give YES. The program is working fine for small strings but gives wrong answer for very long inputs. I tried to change the datatype for integers to long but nothing changed.

The test case for which it did not work: here#1

Edit: Input restrictions allow only lowercase characters 'a' to 'i' Original problem link: https://www.hackerearth.com/problem/algorithm/encoded-strings-3/

str = raw_input()
n = len(str)
value = 0L
str = str[::-1]

for i in arange(n):
    value = value*1L + (10L**i)*(ord(str[i])-96)

if value%6 == 0:
    print "YES"
else:
    print "NO"
  • neither of those look like "test cases" they look more like the full on challenge, does it work for shorter **test** cases, it worked for several tests that I put it through. – Tadhg McDonald-Jensen Jun 05 '16 at 04:54
  • just managed to actually run the code with both of your cases, `value%6` was `4` for the first case and `3` for the second, I don't think either are **suppose** to print `YES`. – Tadhg McDonald-Jensen Jun 05 '16 at 05:09
  • @TadhgMcDonald-Jensen Yes, it worked for shorter test cases. I have posted an answer with new code that works. Most probably, I think the issue is that the string after conversion is too long for long int. The answer is YES for both. – Rachit Ajitsaria Jun 05 '16 at 05:15
  • Just tested it again differently using `str.replace('a','1')` etc. then casting to int, got same result as above so those are not test cases that are devisable by 6, the second is pretty obviously not devisable by 6 because the last letter is `i` which is `9`, so the number isn't even therefore cannot be devisable by 6. – Tadhg McDonald-Jensen Jun 05 '16 at 05:17

3 Answers3

1

Your code seems to work as in "tests correctly whether a number is divisible by 6" if the input does not have any extra character like whitespace, CR, tab, etc. The URL you supplied is not working, so I could not see the failing test.

Two major errors in your code:

1) you are subtracting 96 - why? If ord('0') is equal to 48. If you want to find the true integer value of str, you should substract 48. Since the difference of 96 and 48 is itself divisible by 6, this error still does not break the "divisible by 6" test, but I don't see any advantage, either.

2) your code should ignore non-number characters, otherwise these characters will throw the conversion off-beam. For example, a simple whitespace will add -64 to your final value, which is not divisible by 6 and will break the test.

I suggest you simply stick with int(str) to convert string to int (the long integer is generated automatically for very long numbers), and capture the ValueError exception for invalid str's.

epx
  • 1,066
  • 9
  • 17
0
dic = {}
for i in xrange(97,123):
    dic[chr(i)] = str(i-96)

str = raw_input()
new_str = ""
for i in xrange(len(str)):
    new_str += dic[str[i]]

val = 0
for i in xrange(len(new_str)):
    val = ( (val*10)%6 + ((int(new_str[i]))%6) )%6 # without modular arithmetic val will turn to long data type

print "NO" if val else "YES"

Using long data type and applying operations on it will take time. So, first decode your original string and store the integer form in string data type. Then apply the basic modular arithmetic i.e (a + b)%m = (a%m + b%m)%m.

-1

The problem with the code appeared to be that the long int is possibly not long enough to hold the integer value of the string, decoded as given.

So, the problem can be simplified by applying the mathematical rules of divisibility.

Check if the corresponding digit for the last letter of the string is even. If not print 'NO' and exit.

Then check for the divisibility by 3, using the sum divisibility property.

Here's the code.

    #Title: String decoding and divisibility by 6
    #Author: Rtg
    #Date: 29-05-16

    str = raw_input()
    n = len(str)

    if (ord(str[n-1])-96)%2:
        print "NO"
        raise SystemExit

    value = 0
    for i in xrange(n):
        value = value + ord(str[i])-96

    if value%3 == 0:
        print "YES"
    else:
        print "NO"