Codechef - NZEC Error in python code - python

The code runs fine on my machine, but when i compile it on codechef it gives a NZEC(Runtime Error).
The link to the problem : https://www.codechef.com/problems/PPTEST
About my solution: I have calculated the percentile of each test case based on their time and point values. Then I have sorted the entries in each test case based on the percentile.
import sys
def check(x):
if not(x in range(1,100)):
sys.exit(1)
T = input()
check(T)
N_W = []
C_P_T = {}
tp = []
tt = []
for i in range(0,T):
tp.append(0)
tt.append(0)
N_W.append(map(int, raw_input().split()))
check(N_W[i][0])
check(N_W[i][1])
C_P_T[i] = []
for j in range(0,N_W[i][0]):
C_P_T[i].append(map(int, raw_input().split()))
check(C_P_T[i][j][0])
check(C_P_T[i][j][1])
check(C_P_T[i][j][2])
C_P_T[i][j].append(N_W[i][1]-C_P_T[i][j][2])
C_P_T[i][j].append(C_P_T[i][j][1]*C_P_T[i][j][0])
C_P_T[i][j].pop(0)
C_P_T[i][j].pop(0)
C_P_T[i][j].pop(0)
tp[i]+= C_P_T[i][j][1]
tt[i]+=C_P_T[i][j][0]
for i in range(0,T):
C_P_T[i].sort(key = lambda x : x[0] , reverse = True)
item_time = C_P_T[i][0][0]
percentile_time = (C_P_T[i][0][0]/float(tt[i]))*((len(C_P_T[i])-1)/float(len(C_P_T[i])))
for j in range(0,N_W[i][0]):
if C_P_T[i][j][0] == item_time:
C_P_T[i][j].append(percentile_time)
else:
item_time = C_P_T[i][j][0]
percentile_time = (C_P_T[i][j][0]/float(tt[i]))*((len(C_P_T[i])-j-1)/float(len(C_P_T[i])))
C_P_T[i][j].append(percentile_time)
for i in range(0,T):
C_P_T[i].sort(key = lambda x : x[1] , reverse = True)
item_points = C_P_T[i][0][1]
percentile_points = (C_P_T[i][0][1]/float(tp[i]))*((len(C_P_T[i])-1)/float(len(C_P_T[i])))
for j in range(0,N_W[i][0]):
if C_P_T[i][j][1] == item_points:
C_P_T[i][j].append(percentile_points)
else:
item_points = C_P_T[i][j][1]
percentile_points = ((C_P_T[i][j][1])/float(tp[i]))*((len(C_P_T[i])-j-1)/float(len(C_P_T[i])))
C_P_T[i][j].append(percentile_points)
C_P_T[i][j].append(C_P_T[i][j][2]+C_P_T[i][j][3])
C_P_T[i][j].append(N_W[i][1]-C_P_T[i][j][0])
C_P_T[i][j].pop(2)
C_P_T[i][j].pop(2)
C_P_T[i].sort(key = lambda x : x[2],reverse = True)
for i in range(0,T):
points = 0
for j in range(0,N_W[i][0]):
if N_W[i][1]-C_P_T[i][j][3] >= 0:
points+=C_P_T[i][j][1]
N_W[i][1]-=C_P_T[i][j][3]
print points

NZEC means "non-zero exit code", so that is probably happening in sys.exit(1) in your check() function. What you are receiving from input() is either not an integer or not in the right range.
Update: I notice that you use range(1, 100) for validity testing.
But the problem description at codechef states that 1 ≤ T ≤ 100. That is equivalent to range(1, 101)
So, codechef could be passing your code a perfectly valid 100, and your code would reject it, probably with the exact error you are seeing.

Related

LEETCODE 1779. Find Nearest Point That Has the Same X or Y Coordinate

https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/description/
#x = 3
#y = 4
nums = [x,y]
#print("nums =", nums)
#points = [[1,2],[3,1],[2,4],[2,3],[4,4]]
list1 = []
distance =[]
for i in range(len(points)):
if points[i][0]==x:
#print(points[i])
list1.append(points[i])
elif points[i][1] == y:
#print(points[i])
list1.append(points[i])
#print("list1 =",list1)
for i in range(0,len(list1)):
y = abs(nums[0] - list1[i][0]) + abs(nums[1] - list1[i][1])
#print(y)
distance.append(y)
#print(distance)
#print(distance.index(min(distance)))
#print(points.index(list1[distance.index(min(distance))]))
t = min(distance)
a = distance.index(t)
b = list1[a]
c = points.index(b)
print(c)
please note : # - lead lines in the above code are for debugging reference
I AM ABLE TO RECIEVE CORRECT OUTPUTS FOR ANY TESTCASES ON PYCHARM IDE successfully, BUT WHILE RUNNING THIS CODE ON LEETCODE, ITS displaying the following error
error displayed ::
I am unable to understand why the compiler on leetcode is interpreting "distance []" as empty sequence.
Please respond, if you know something that can help.

I want to convert the following MATLAB code into python? Is this the correct way?

How can I change this part [AIF,j]=get_AIF_j(InterpFact) and [~,j_index] = min(InterpFact-AIF_vect) correctly? And what about the remaining code? Thanks in advance.
%Matlab code
InterpFact = (fs_h/2/2)/(fd_max);
[AIF,j]=get_AIF_j(InterpFact);
function [AIF,j] = get_AIF_j (InterpFact)
j_vect = 1:10;
AIF_vect = floor(j_vect*InterpFact)./j_vect;
[~,j_index] = min(InterpFact-AIF_vect);
j = j_vect(j_index);
AIF = AIF_vect(j_index);
end
#Python code
InterpFact = (fs_h/2/2)/(fd_max)
[AIF,j]=get_AIF_j(InterpFact)
def get_AIF_j (InterpFact):
j_vect =np.arange(1,11)
AIF_vect = np.floor(j_vect*InterpFact)/j_vect
[~,j_index] = min(InterpFact-AIF_vect)
j = j_vect[j_index]
AIF = AIF_vect[j_index];
return AIF,j
This MATLAB:
[~,j_index] = min(InterpFact-AIF_vect);
would be translated to Python as:
j_index = np.argmin(InterpFact-AIF_vect)
Also, …/(fd_max) can only be translated the way you did if fd_max is a scalar. A division with a matrix in MATLAB solves a system of linear equations.
I strongly recommend that you run the two pieces of code side by side with the same input, to verify that they do the same thing. You cannot go by guesses as to what a piece of code does.
Try this to see if it delivers what it should (I am not sure here as I am not fluent in matlab):
#Python code
import numpy as np
def get_AIF_j (InterpFact):
j_vect = np.arange(1,11)
AIF_vect = np.floor(j_vect*InterpFact)/j_vect
j_index = int( min(InterpFact-AIF_vect) )
print(j_index)
j = j_vect[j_index]
AIF = AIF_vect[j_index];
return AIF, j
fs_h = 24; fd_max = 1
InterpFact = (fs_h/2/2)/(fd_max)
AIF, j = get_AIF_j(InterpFact)
print(AIF,j)
gives:
0
6.0 1

How can I fix Runtime error for log in python

num_runs = 100000
p_values = []
for i in range(1, num_runs+1):
ornek1 = 2*np.random.normal(0,1,5)
ornek2 = 3*np.random.normal(0,2,5)
ornek3 = 10*np.random.normal(0,5,5)
levene = sp.levene(ornek1,ornek2,ornek3, center = 'mean')
if levene[1] < 0.05:
ornek1 = np.log10(ornek1)
ornek2 = np.log10(ornek2)
ornek3 = np.log10(ornek3)
else:
continue
anova = sp.f_oneway(ornek1,ornek2,ornek3)
p_values.append(anova[1])
hesap = sum(map(lambda x: x<0.05, p_values))
print(hesap/100000)
RuntimeWarning: invalid value encountered in log10
ornek1 = np.log10(ornek1)
How can I fix this problem ? I can't find anything. I want to transform my samples after levene test but ı can't. Is it about to numpy ?
Just as a sample:
>>> np.random.normal(0, 1, 5)
array([ 1.68127583, -0.82660143, -1.82465141, 0.60495851, -0.90369304])
You're taking the log of negative numbers. What are you expecting to happen?

Is there any way to replace (input) zeros with a function of (1/2)?

I am trying to replace the value of 0 with .5, or 1/2 when it has been input initially.
For example, I am trying to have it done before the functions are added. I need to redefine the value of 0 for the input only, and only for the single instance of 0 itself. Not values of 10+.
Here is the project information:
IN = input("Enter IN: ")
N = input("Enter N: ")
NP = input("Enter NP: ")
### These two lines are the part I can't get to work:
if digit == float(0):
digit = float(.5)
###
init = (float(IN)*(float(1)/float(2)))
baselimiter = - (float(N)*(float(1)/float(2))) + ((float(IN)*
(float(1)/float(2))) * (float(NP)*(float(1)/float(2))))
lset = init + baselimiter
limitconverto1 = (lset / init) * (init / lset)
infalatetoinput = (((init * float(IN))) / init )
limit = limitconverto1 * infalatetoinput
result = limit
print(result)
So here is a code that does what you want.
Now to be honest, it works but I don't understand WHY you do that. You do a bunch of weird calculations like multiplying and dividing by the same number...
IN = float(input("Enter IN: "))
N = float(input("Enter N: "))
NP = float(input("Enter NP: "))
# The part that interests you.
IN = 0.5 if IN == 0 else IN
N = 0.5 if N == 0 else N
NP = 0.5 if NP == 0 else NP
init = IN * 1/2
baselimiter = -N*1/2 + IN*1/2*NP*1/2 # Removed all the superfluous float() and parenthesis.
lset = init + baselimiter
limitconverto1 = (lset / init) * (init / lset) # That's just always 1. What is intended here?
infalatetoinput = (((init * float(IN))) / init ) # That's always IN. Same question?
limit = limitconverto1 * infalatetoinput # Equivalent to 1 x IN...
result = limit
print(result) # Your result is always IN...
You can use one-liners when you declare the variables:
IN = (float(input("...")) if float(input("...")) != 0 else .5)
One-liners are for loops or if statements (or both) that are in one line instead of multiple lines when declaring variables. They can only be used for the declaration of variables. The one-liner I suggested is would be in multiple lines:
if float(input("...")) != 0:
IN = float(input("..."))
else:
IN = .5 #You don't need to say float(.5) since .5 is a float anyway.
More about One-Liners: One-Liners - Python Wiki
I hope this edit of my previous answer fully answers your question, for more clarification I will be available on the comments

python access array errors

im working on a an assignment but the few lines keeps on giving me errors that i cannot understand
pick=0
final=0
while True:
if (min_weight>0):
if (sort_ratio[pick]["Weight"]<min_weight):
final = final + sort_ratio[pick]["Cost"]
min_weight = min_weight - sort_ratio[pick]["Weight"]
if (sort_ratio[pick]["Weight"]>min_weight):
pick = pick + 1
if (min_weight == 0):
print (final)
return
the only problem is when the program pass "pick = pick+ 1" it stops and gives "index out of range" error.
Instead of using the increment method i tried to edit the array instead using
sort_ratio.remove(sort_ratio[0]);
but it gives me "cannot interpreted as integer problem"
present code
from operator import itemgetter
import math
raw_input=input;
test_case = int(raw_input());
for inp1 in range (test_case):
min_weight = int(raw_input());
candy_types = int(raw_input());
candy = [];
for inp2 in range (candy_types):
can_weight,can_cost = map(int,raw_input().split());
ratio_candy = (can_cost / int(can_weight));
candy.extend([{"Cost": can_cost, "Ratio": ratio_candy, "Weight": can_weight}]);
sort_ratio = sorted ( candy, key=itemgetter('Ratio'));
pick = 0;
final = 0;
while True:
if (min_weight>0):
if (sort_ratio[pick]["Weight"]<min_weight):
final = final + sort_ratio[pick]["Cost"]
min_weight = min_weight - sort_ratio[pick]["Weight"]
print (final)
print (min_weight)
if (sort_ratio[pick]["Weight"]>min_weight):
pick = pick + 1
if (min_weight==0):
print (final)
break

Categories