If the message contains a pattern like ABA, then two consecutive values will be the same. Your code generates the wrong values in this case (because gcd = value and dividing out the gcd gives 1 instead of one of the primes), and probably puts more than 26 values into prime_list, which leads to alpha_id exceeding the length of alphabets later.
On Sun, Apr 7, 2019 at 5:20 PM Nicholas Sadjoli <nick.sadj...@gmail.com> wrote: > Hello everyone, > > I have tried to solve the Cryptopangram problem for the recent CodeJam, > but I have no idea why my code is giving me a Runtime Error. The code > already passed the sample test case, though do note that I have not tried > with any other test cases just yet. > > I have analyzed for any possible mis-indent or any possible zero-division > or mathematically invalid operations in the code. However, it is still > producing the Runtime Error on the CodeJam interface. > > Any discussions for helping me understand the problem is greatly > appreciated, and looking forward to your feedback on my code. > > cryptopangrams.py: > > import string > > #find GCD between 2 integers > def gcd_euclid(n1, n2): > > #n2 being 0 means remainder == 0 > if n2 == 0: > return n1 > return gcd_euclid(n2, n1 % n2) > > test_cases = int(input()) > > for T in range(0, test_cases): > N, L = map(int, input().rstrip().split(' ')) > cipher_values = list(map(int, input().rstrip().split(' '))) > > #get the prime numbers that make up cipher_values > prime_list = [] > for i in range(0, L - 1): > > #find prime value commonly shared by current and next cipher value > common_prime = gcd_euclid(cipher_values[i], cipher_values[i+1]) > > #edge case for i at start of cipher_value list > if i == 0: > prime_list.append(int(cipher_values[i] / common_prime)) > > prime_list.append(common_prime) > > #edge case for when i at end of cipher_value list > if i == (L - 2): > prime_list.append(int(cipher_values[i + 1] / common_prime)) > > #get all alphabets into a string > alphabets = string.ascii_uppercase > alpha_id = 0 > > #prepare dictionary for mapping between prime and alphabets > prime_dict = {} > > #get a sorted prime_list first > prime_list_sorted = sorted(prime_list.copy()) > > #map the sorted prime numbers to the appropriate alphabets > for p in prime_list_sorted: > if p not in prime_dict: > prime_dict[p] = alphabets[alpha_id] > alpha_id += 1 > > #Use mapping to get alphabetical values of all primes in the > prime_list > plaintext = '' > for t in range(0, L + 1): > plaintext += prime_dict[prime_list[t]] > > print("Case #{}: {}".format(T + 1, plaintext)) > > -- > You received this message because you are subscribed to the Google Groups > "Google Code Jam" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to google-code+unsubscr...@googlegroups.com. > To post to this group, send email to google-code@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/google-code/0d1a6381-969e-4e16-be4e-7e334446a7dc%40googlegroups.com > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Google Code Jam" group. To unsubscribe from this group and stop receiving emails from it, send an email to google-code+unsubscr...@googlegroups.com. To post to this group, send email to google-code@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/google-code/CAMAzhzhay1s58oNL4Uw4NFYi43FPSOsRr0thBttLwCMi%3D3%3D8xw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.