abprime
abprime

Reputation: 76

wheres the Nzec runtime error in this code?

i have a simple python code that keeps on showing NZEC error on CodeChef online judge. The code is for the problem GRANAMA.

Chef has learned a new technique for comparing two recipes. A recipe contains list of ingredients in increasing order of the times they will be processed. An ingredient is represented by a letter 'a'-'z'. The i-th letter in a recipe denote the i-th ingredient. An ingredient can be used multiple times in a recipe.

The technique is as follows. Compare two recipes by comparing their respective lists. If the sets of ingredients used in both recipes are equal and each ingredient is
used the same number of times in both of them (processing order does not matter),
they are declared as granama recipes. ("granama" is the Chef-ian word for "similar".) Chef took two recipes he invented yesterday. He wanted to compare them using the technique. Unfortunately, Chef forgot to keep track of the number of times each ingredient has been used in a recipe. He only compared the ingredients but NOT their
frequencies. More precisely, Chef considers two recipes as granama if there are no ingredients which are used in one recipe and not used in the other recipe. Your task is to report whether Chef has correctly classified the two recipes (as granama or not granama) although he forgot to keep track of the frequencies.

 Input

 The first line of the input contains a single integer T denoting the number of test 
 cases. The description for T test cases follows. Each test case consists of a single 
 line containing two space-separated strings R and S denoting the two recipes.

 Output

 For each test case, output a single line containing "YES" (quotes for clarity) if Chef 
 correctly classified the two recipes as granama or not granama. Otherwise, output a 
 single line containing "NO" (quotes for clarity) if Chef declared two recipes as 
 granama when they actually are not.

 Constraints

 1 ≤ T ≤ 100
 1 ≤ |R|, |S| ≤ 1000
 Example

 Input:

 3
 alex axle
 paradise diapers
 alice bob

 Output:

 YES 
 NO
 YES

 Explanation:

 Example case 1: Chef declared them as granama recipes. They are actually granama 
 because the sets of ingredients and the number of times each ingredient has been used 
 are equal. The Chef got it right!
 Example case 2: Chef declared them as granama recipes because both sets of ingredients 
 are equal. But they are NOT granama since ingredient 'a' has been used twice in the 
 first recipe but only once in the second. The Chef was incorrect!
 Example case 3: Chef declare them as not granama. They are not granama as the sets of 
 ingredients are different. Hence, the Chef was right!

Here's the code:

k=int(raw_input())
for n in range(k):
    recipes=raw_input().split(' ')
    w1=recipes[0]
    w2=recipes[1]
    for i in w1:
        if i in w1:
            w1=w1.replace(i,'')
            w2=w2.replace(i,'')

    if w1=='' and w2=='':
        dict1={}
        dict2={}
        for i in recipes[0]:
            if i in dict1:
                dict1[i]+=1
            else:
                dict1[i]=1
        for i in recipes[1]:
            if i in dict2:
                dict2[i]+=1
            else:
                dict2[i]=1
        flag=True
        for i in dict1:
            if not dict1[i]==dict2[i]:
                print 'NO'
                flag=False
                break
        if flag:
            print 'YES'

    else:
        print 'YES'  

Upvotes: 0

Views: 334

Answers (1)

siddharthlatest
siddharthlatest

Reputation: 2257

I believe the error is here -

for i in w1:
    if i in w2:    # You were checking again in 'w1'
        w1=w1.replace(i,'')
        w2=w2.replace(i,'')

This should solve the NZEC issue. You can see my submitted solution over here. The runtime is 0.23s.

There is a lot of room to make this code compact. Let me pick up some of your code snippets and show how -

w1=recipes[0]
    w2=recipes[1]
    for i in w1:
        if i in w2:
            w1=w1.replace(i,'')
            w2=w2.replace(i,'')

if w1=='' and w2=='':

Python has a set data structure which can come quite handy in cases such as this.

if set(recipes[0]) == set(recipes[1]):    # Check for equality between two sets

For seeing if the frequency of each character matches between the two recipes, you don't need a dictionary. You can simply compare the sorted strings.

if sorted(recipes[0]) == sorted(recipes[1])

So, the 30 lines of code can be replaced by 7 lines.

    if set(recipes[0]) == set(recipes[1]):
        if sorted(recipes[0]) == sorted(recipes[1]):
            print 'YES'
        else:
            print 'NO'
    else:
        print 'YES'

I took input in a slightly different way on the code based on the one above. It runs in 0.09s.

Upvotes: 1

Related Questions