NZEC error in Python - SPOJ - python-2.7

def multiply(num1, num2):
result = num1 * num2
print(result)
return result
tc = raw_input("") #testcases
tc = [int(tc)]
#count = [int(count)]
count = 0
while ( count < tc) :
var1, var2 = raw_input("").split()
var1, var2 = [int(var1), int(var2)]
multiply (var1,var2)
print '\n'`**enter code here**`
count = count + 1
I'm getting NZEC on SPOJ . I'm new to python programming .
Please help me. Thanks in advance . Link to the Question on SPOJ

There are two issues in you code:
Line 7: tc = [int(tc)]
You shouldn't wrap variable tv into the list. You have created a singleton with the number of test cases as an element. Variable tc should be a number, therefore, the line should look like this:
tc = int(tc)
Line 14: print '\n'**enter code here**
You should remove the weird part "**enter code here**".
After mentioned changes your code should be a correct solution to the MUL problem on Spoj.

Related

GUI freezing when I use '.get'

Heres the relevant code:
def opencommand():
number=entry1.get()
mydata = csv.reader(open('result.csv','rU'))
card_name = []
for row in mydata:
card_name.append(row[9])
r=0
while r<number:
randomnumbers=[]
counter=0
while counter<5:
randomnumbers.append(randint(1,90))
counter=counter+1
pack1=[]
p=0
while p<5:
pack1.append(card_name[randomnumbers[p]])
p=p+1
print pack1
r=r+1
and....
numpac = Label(options_frame,text='Number of Packs')
entry1 = Entry(options_frame)
numpac.grid(row=0,column=0,sticky=E)
entry1.grid(row=0,column=1)
openbutton = Button(options_frame, text='Open',command=opencommand)
openbutton.grid(row=1,column=0,columnspan=2)
Can anyone tell me why when i include the get part it freezes but if i set it to a fixed number i doesnt?
heres some text as it says theres to much code: vkjberbverihjbvjerhbvjhebvjhervhjberjvhberjhbverhjbvjlerbvjlerbvljerbverjlhbvrejlvhberljvhberljvhberljvbherjlvhberjvlhbevljerbvljerbvlerjhbvelrjbvlerjhbvlejrhbv
Because get() returns a string, not a number. You need to convert it:
number = int(entry1.get())

Concatenate all possible strings using one entry per variable number of lists

It's the variable number of lists that is confusing me. It's easy to (pseudo code)...
for i=1 to list1_size
for ii=1 to list2_size
for iii=1 to list3_size
results_list.add(list1[i]+list2[ii]+list3[iii])
... but I could do with a nudge in the right direction regarding how to do this with a varying number of lists.
I've started with getting a table of the number of entries in each list to work through and it does seem like a bit of recursion is required but from there I'm drawing a blank.
edit: To clarify, what I am looking for is...
Input: A varying number of lists/tables with a varying numbers of entries.
{"A","B","C"} {"1","2","3","4"} {"*","%"}
Output:
{"A1*", "A1%", "A2*", "A2%", "A3*", "A3%", "A4*", "A4%",
"B1*", "B1%", "B2*", "B2%", "B3*", "B3%", "B4*", "B4%",
"C1*", "C1%", "C2*", "C2%", "C3*", "C3%", "C4*", "C4%"}
local list_of_lists = {{"A","B","C"}, {"1","2","3","4"}, {"*","%"}}
local positions = {}
for i = 1, #list_of_lists do
positions[i] = 1
end
local function increase_positions()
for i = #list_of_lists, 1, -1 do
positions[i] = positions[i] % #list_of_lists[i] + 1
if positions[i] > 1 then return true end
end
end
local function get_concatenated_selection()
local selection = {}
for i = 1, #list_of_lists do
selection[i] = list_of_lists[i][positions[i]]
end
return table.concat(selection)
end
local result = {}
repeat
table.insert(result, get_concatenated_selection())
until not increase_positions()

Variable within a number

This code ask for a message and a value to the user and then it modifies it with the given value. The problem is that I want the ASCII codes to not go over 126 or under 33, I tried to do so in the highlighted part of the code but when the value of the ASCII code gets over 126 the code returns me nothing for some reason.
loop = True
def start():
final_word = []
word_to_crypt = str(raw_input("Type a word: "))
crypt_value = int(raw_input("Choose a number to cript yout message with: "))
ascii_code = 0
n = 0
m = len(word_to_crypt)
m = int(m - 1)
while n <= m:
ascii_code = ord(word_to_crypt[n])
ascii_code += crypt_value
############# highlight #############
if 33 > ascii_code > 126:
ascii_code = (ascii_code%94)+33
############# highlight #############
final_word.append(chr(ascii_code))
n += 1
print 'Your crypted word is: ' + ''.join(final_word)
while loop:
start()
Sorry if it's not formatted well or for any mistakes in my explanation but I'm on my phone and I'm not native
Solved thank you very much this site and this community is helping me a lot!
There is no number that is greater than 126 and less than 33 at the same time. It should be:
if 33 < ascii_code < 126:
Edit:
If you want the reversed case, you will have to do it separately:
if ascii_code < 33 or ascii_code > 126:
Or you can just use the in operator and a list:
if ascii_code not in [33,126]:

Loop problems Even Count

I have a beginner question. Loops are extremely hard for me to understand, so it's come to me asking for help.
I am trying to create a function to count the amount of even numbers in a user input list, with a negative at the end to show the end of the list. I know I need to use a while loop, but I am having trouble figuring out how to walk through the indexes of the input list. This is what I have so far, can anyone give me a hand?
def find_even_count(numlist):
count = 0
numlist.split()
while numlist > 0:
if numlist % 2 == 0:
count += 1
return count
numlist = raw_input("Please enter a list of numbers, with a negative at the end: ")
print find_even_count(numlist)
I used the split to separate out the indexes of the list, but I know I am doing something wrong. Can anyone point out what I am doing wrong, or point me to a good step by step explanation of what to do here?
Thank you guys so much, I know you probably have something more on your skill level to do, but appreciate the help!
You were pretty close, just a couple of corrections:
def find_even_count(numlist):
count = 0
lst = numlist.split()
for num in lst:
if int(num) % 2 == 0:
count += 1
return count
numlist = raw_input("Please enter a list of numbers, with a negative at the end: ")
print find_even_count(numlist)
I have used a for loop rather than a while loop, stored the outcome of numlist.split() to a variable (lst) and then just iterated over this.
You have a couple of problems:
You split numlist, but don't assign the resulting list to anything.
You then try to operate on numlist, which is still the string of all numbers.
You never try to convert anything to a number.
Instead, try:
def find_even_count(numlist):
count = 0
for numstr in numlist.split(): # iterate over the list
num = int(numstr) # convert each item to an integer
if num < 0:
break # stop when we hit a negative
elif num % 2 == 0:
count += 1 # increment count for even numbers
return count # return the total
Or, doing the whole thing in one line:
def find_even_count(numlist):
return sum(num % 2 for num in map(int, numlist.split()) if num > 0)
(Note: the one-liner will fail in cases where the user tries to trick you by putting more numbers after the "final" negative number, e.g. with numlist = "1 2 -1 3 4")
If you must use a while loop (which isn't really the best tool for the job), it would look like:
def find_even_count(numlist):
index = count = 0
numlist = list(map(int, numlist.split()))
while numlist[index] > 0:
if numlist[index] % 2 == 0:
count += 1
index += 1
return count

Log base 2 calculation in python

I am trying to calculate the average disorder in ID trees. My code is below:
Republican_yes = yes.count('Republican')
Democrat_yes = yes.count('Democrat')
Republican_no = no.count('Republican')
Democrat_no = no.count('Democrat')
Indep_yes = yes.count('Independent')
Indep_no = no.count('Independent')
disorder_yes= Republican_yes/len(yes)*(math.log(float(Republican_yes)/len(yes),2))+ Democrat_yes/len(yes)*(math.log(float(Democrat_yes)/len(yes),2))+Indep_yes/len(yes)*(math.log(float(Indep_yes)/len(yes),2))
disorder_no= Republican_no/len(no)*(math.log(float(Republican_no)/len(no),2))+Democrat_no/len(no)*(math.log(float(Democrat_no)/len(no),2))+Indep_no/len(no)*(math.log(float(Indep_no)/len(no),2))
avgdisorder = -len(yes)/(len(yes)+len(no))*disorder_yes - len(no)/(len(yes)+len(no))*disorder_no
return avgdisorder
why do I keep getting math domain error?
Check if the lengths are 0 or not, else you will get MathError.
if len(yes):
disorder_yes= Republican_yes/len(yes)*(math.log(float(Republican_yes)/len(yes),2))+ Democrat_yes/len(yes)*(math.log(float(Democrat_yes)/len(yes),2))+Indep_yes/len(yes)*(math.log(float(Indep_yes)/len(yes),2))
if len(no):
disorder_no= Republican_no/len(no)*(math.log(float(Republican_no)/len(no),2))+Democrat_no/len(no)*(math.log(float(Democrat_no)/len(no),2))+Indep_no/len(no)*(math.log(float(Indep_no)/len(no),2))
if len(yes) or len(no):
avgdisorder = -len(yes)/(len(yes)+len(no))*disorder_yes - len(no)/(len(yes)+len(no))*disorder_no
If you want, you can always add the else clause for all 3 if statements as per your requirement.