NZEC error in python 2.7 MAXCOUNT codechef - python-2.7

BELOW is the code for the problem MAXCOUNT from codechef.com. The code on submission shows NZEC error.
The code takes first input t as no. of test cases, then for each test case takes input n a integer and the next line consists of space separated n integers Basically I need to return the maximum occuring integer and its count as output.
import numpy as np
import sys
t = int(raw_input())
for i in xrange(t):
n = raw_input()
n = int(n)
a = []
a = map(int, raw_input().split())
print a
count = np.bincount(a)
print np.argmax(count),np.max(count)
sys.exit(0)
Someone please help me out with this error.

The answer to your question is the use of the numpy module, which is not part of the standard library used on CodeChef. If you need to check numpy or another module in an online programming judge, a good way is to use a code sample that you know works and then add the import statement to the top before resubmitting.
For CodeChef in particular, try the basic test with the following code, with and without the import statement:
#Test for modules
import numpy
number_in = int(raw_input())
while number_in != 42:
print number_in
number_in = int(raw_input())
As a suggestion, the Counter() function from the collections module will work on CodeChef, you may want to try that instead of numpy. I find however that for many of the problems on that site without numpy or using PyPy it can be quite difficult to meet the timing requirements for solutions.

Related

How do I eliminate even numbers from this python code without an infinite loop occurring?

I have tried different ways to try print odd numbers only but its only causing an infinite loop to occur. Could you please assist me?
import sys
i = 1
while i < len(sys.argv):
print sys.argv[i]
i = i + 1
Your question is little bit confusing.
You should be processing all arguments and then decide which one you should print.
import sys
i = 1
while i < len(sys.argv):
number = int(sys.argv[i])
if number % 2 == 1:
print number
i = i + 1
In python, sys.argv contains only one item 'main.py' in index number 0.
So, if you run the program from index 1, you will get nothing as output. If you set it from index 0, you will get 'main.py' printed as output.
If this answer is not satisfactory, please clarify your issue with this code.

Can someone tell me whats wrong with this python code?

Note: I'm using Python 2.7
I'm not very experienced at Python, but I decided to make a small simple program. Here is the code:
import random
while True:
randomNumber = random.randrange(1, 3)
print randomNumber
guessedNumber = raw_input("Choose a number between 1 and 3 ")
if randomNumber == guessedNumber:
print 'Yay! You got it right!'
else:
print 'You got it wrong :( The number was:',randomNumber
#The first print is just for testing.
But when I try to run it I get this:
IDLE after i used the program a few times
Can someone tell me what i need to change or what is wrong with the code?
raw_input returns a string to guessedNumber, and your program compares a string (guessedNumber) to an integer (randomNumber), so if randomNumber == guessedNumber never evaluates to True.
The solution is to convert guessedNumber to an int and then compare the two values.

Testing for an item in lists - Python 3

As part of a school project we are creating a trouble shooting program. I have come across a problem that I cannot solve:
begin=['physical','Physical','Software','software',]
answer=input()
if answer in begin[2:3]:
print("k")
software()
if answer in begin[0:1]:
print("hmm")
physical()
When I try to input software/Software no output is created. Can anybody see a hole in my code as it is?
In Python, slice end values are exclusive. You are slicing a smaller list than you think you are:
>>> begin=['physical','Physical','Software','software',]
>>> begin[2:3]
['Software']
>>> begin[0:1]
['physical']
Use begin[2:4] and begin[0:2] or even begin[2:] and begin[:2] to get all elements from the 3rd to the end, and from the start until the 2nd (inclusive):
>>> begin[2:]
['Software', 'software']
>>> begin[2:4]
['Software', 'software']
>>> begin[:2]
['physical', 'Physical']
>>> begin[0:2]
['physical', 'Physical']
Better yet, use str.lower() to limit the number of inputs you need to provide:
if answer.lower() == 'software':
With only one string to test, you can now put your functions in a dictionary; this gives you the option to list the various valid answers too:
options = {'software': software, 'physical': physical}
while True:
answer = input('Please enter one of the following options: {}\n'.format(
', '.join(options))
answer = answer.lower()
if answer in options:
options[answer]()
break
else:
print("Sorry, {} is not a valid option, try again".format(answer))
Your list slicing is wrong, Try the following script.
begin=['physical','Physical','Software','software',]
answer=input()
if answer in begin[2:4]:
print("k")
software()
if answer in begin[0:2]:
print("hmm")
physical()

scikits.bvp_solver generates ValueError: too many values to unpack

I will start with a very simple ODE, which generates the same error code by using scikits.bvp_solver as I applied to complex ODE. Below is the ODE and the boundary conditions:
f''''(x)=f(x), f(0)=0, f'(1)=1, f''(0)=1, f'''(1)=1
The code for solving this ODE:
import numpy
import scikits.bvp_solver as bvp
def Fode(x,y):
return numpy.array([y[1],y[2],y[3],y[0]])
def Fbc(ya,yb):
return (numpy.array([ya[0]]),numpy.array([yb[1]-1]),
numpy.array([ya[2]-1]),numpy.array([yb[3]-1]))
guess=numpy.array([0.0,0.0,0.0,0.0])
problem=bvp.ProblemDefinition (num_ODE=4,num_parameters=0,
num_left_boundary_conditions=2,
boundary_points=(0,1),
function=Fode,boundary_conditions=Fbc)
solution=bvp.solve(bvp_problem=problem,solution_guess=guess)
when I run this code, I receive:
I can not figure out what is the problem, since the same code works well for 2nd order ODE. Any comment is appreciated.
Your Fbc function should return exactly two arrays, one containing function value differences for boundary conditions at the left, the other one containing the values at the right.
See https://pythonhosted.org/scikits.bvp_solver/tutorial.html#defining-the-problem
https://pythonhosted.org/scikits.bvp_solver/examples/examples.example4.html

What is the python equivalent for Matlab's stdfilt() function?

I was trying to convert a piece of code in Matlab to python. There I could find a Standard deviation filter function "stdfilt()" for which I could not find any equivalent API in python.
The Matlab code is given below,
Ig_med = medfilt2(Input_Image);
h_gauss = fspecial('gaussian',11,2);
h_avg = fspecial('average',121);
I = imfilter(Ig_med,h_gauss,'corr','replicate');
P = stdfilt(I,ones(121));
P = P.^2;
Q = imfilter(P,h_avg,'corr','replicate');
Can anyone help me out in implementing the above piece of code in Python ?
Thanks in Advance
I found something on
https://nickc1.github.io/python,/matlab/2016/05/17/Standard-Deviation-(Filters)-in-Matlab-and-Python.html
from scipy.ndimage.filters import generic_filter
import numpy as np
I_filt = generic_filter(I, np.std, size=3)
maybe it can help!
(not you I guess, given the 3 years delay, but someone on the net)