class Solution:
def subarraysDivByK(self, A, K):
"""
:type A: List[int]
:type K: int
:rtype: int
"""
l = len(A)
div = [[0 for col in range(l)] for row in range(l)]
sums = [[None for col in range(l)] for row in range(l)]
global cnt
cnt = 0
for i in range(l):
if A[i]%K == 0:
div[i][i] = 1
cnt = cnt+1
def impl(self,st,en):
print(st,en)
if sums[st][en]!=None:
return sums[st][en]
if A[st] == A[en]:
if A[st]%K == 0:
sums[st][st] = A[st]
div[st][st] = 1
print(st,en,sums[st][en])
if sums[st][en]%k == 0:
div[st][en] = 1
cnt = cnt+1
return sums[st][st]
elif st+1==en:
sums[st][en] = A[st]+A[en]
print(st,en,sums[st][en])
if sums[st][en]%K == 0:
div[st][en] = 1
cnt = cnt+1
return sums[st][en]
else:
if sums[st+1][en]==None:
sums[st][en] = impl(self,st+1,en)+A[st]
if sums[st][en-1]==None:
sums[st][en] = impl(self,st,en-1)+A[en]
#sums[st][en] = sums[st+1][en]+ A[st]
print(st,en,sums[st][en])
if sums[st][en]%K == 0:
div[st][en] = 1
cnt = cnt+1
return sums[st][en]
impl(self,0,len(A)-1)
print(cnt)
#print(sums)
You should add global cnt inside the function impl so that the global variable will be visible.
def impl(self,st,en):
global cnt
...
For more information, please check: enter link description here.
Related
Trying to get this code to sort topological, but I get the error mentioned in the topic. How do I solve this?
PS: I do also have a method for adding edges and variables.
def __init__(self):
self.edges = defaultdict(lambda: [])
self.variables = {}
def sorted_nodes(self):
indegree = [0]*self.variables
for i in self.edges:
for j in self.edges[i]:
indegree[j] += 1
Q = []
for i in range(self.variables):
if indegree[i] == 0:
Q.append(i)
count = []
L = []
while Q:
start = Q.pop()
L.append(start)
for i in self.edges[start]:
indegree[i] -= 1
if indegree[i] == 0:
Q.append(i)
count += 1
if count != self.variables:
return False
else:
return L
I have a problem with my perceptron codes.I receive this when I execute my code. I checked my two txt files and I am pretty sure the two of them are definitely ok. So can someone help? Thanks a lot
Traceback (most recent call last):
File "perceptron.py", line 160, in <module>
test()
File "perceptron.py", line 133, in test
w,k,i = p.perceptron_train('train.txt')
TypeError: 'NoneType' object is not iterable
Here is my code
import numpy as np
import matplotlib.pyplot as plt
class Data():
def __init__(self,x,y):
self.len = len(x)
self.x = x
self.y = y
class Perceptron():
def __init__(self,N,X):
self.w = np.array([])
self.N = N
self.X =X
def prepare_training(self,file):
file = open(file,'r').readlines()
self.dic = set([])
y = []
vocab = {}
for i in range(len(file)):
words = file[i].strip().split()
y.append(int(words[0])*2-1)
for w in set(words[1:]):
if w in vocab:
vocab[w].add(i)
if i < self.N and len(vocab[w]) >= self.X:
self.dic.add(w)
elif i < self.N:
vocab[w] = set([i])
x = np.zeros((len(file),len(self.dic)))
self.dic = list(self.dic)
for i in range(len(self.dic)):
for j in vocab[self.dic[i]]:
x[j][i] = 1
self.training = Data(x[:self.N],y[:self.N])
self.validation = Data(x[self.N:],y[self.N:])
return x,y
def update_weight(self,x,y):
self.w = self.w + x * y
def perceptron_train(self,data):
x,y = self.prepare_training(data)
self.w = np.zeros(len(self.dic),int)
passes = 0
total_passes = 100
k = 0
while passes < total_passes:
print('passes:',passes)
mistake = 0
for i in range(self.N):
check = y[i] * np.dot(self.w,x[i])
if (check == 0 and (not
np.array_equal(x[i],np.zeros(len(self.dic),int)))) or (check < 0):
self.update_weight(x[i],y[i])
mistake += 1
k += 1
passes += 1
print('mistake:',mistake)
if mistake == 0:
print('converge at pass:',passes)
print('total mistakes:', k)
return self.w, k, passes
def perceptron_error(self,w,data):
error = 0
for i in range(data.len):
if data.y[i] * np.dot(w,data.x[i]) < 0:
error += 1
return error/data.len
def test(self,report):
x = np.zeros(len(self.dic),int)
for i in range(len(self.dic)):
if self.dic[i] in report:
x[i] = 1
if np.dot(self.w,x) > 0:
return 1
else:
return 0
def perceptron_test(self,data):
test = open(data,'r').readlines()
y = []
mistake = 0
for t in test:
y0 = int(t.strip().split()[0])
report = set(t.strip().split()[1:])
r = self.test(report)
y.append(r)
if (y0 != r):
mistake += 1
return y,mistake/len(test)
def predictive_words(self):
w2d = {}
for i in range(len(self.dic)):
try:
w2d[self.w[i]].append(self.dic[i] + " ")
except:
w2d[self.w[i]] = [self.dic[i] + " "]
key = list(w2d.keys())
key.sort()
count = 0
most_positive = ""
most_negative = ""
for i in range(len(key)):
for j in range(len(w2d[key[i]])):
most_negative += w2d[key[i]][j]
count += 1
if count == 5:
break
if count == 5:
break
count = 0
for i in range(len(key)):
for j in range(len(w2d[key[len(key)-i-1]])):
most_positive += w2d[key[len(key)-i-1]][j]
count += 1
if count == 5:
break
if count == 5:
break
return most_positive,most_negative
def test():
p = Perceptron(500,30)
w,k,i = p.perceptron_train('train.txt')
print(p.perceptron_error(w,p.validation))
normal,abnormal = p.predictive_words()
print('Normal:\n',normal)
print('Abnormal:\n',abnormal)
print(p.perceptron_test('test.txt'))
def plot_error():
x = [100,200,400,500]
y = []
for n in x:
p = Perceptron(n,10)
w,k,i = p.perceptron_train('train.txt')
y.append(p.perceptron_error(w,p.validation))
plt.plot(x,y)
plt.show()
def plot_converge():
x = [100,200,400,500]
y = []
for n in x:
p = Perceptron(n,10)
w,k,i = p.perceptron_train('train.txt')
y.append(i)
plt.plot(x,y)
plt.show()
test()
perceptron_train has the implicit return value None if mistakes!=0, so that's what you're seeing here.
My code:
class Persistence:
num = 0
counter1 = 0
counter2 = 0
def __init__(self, num):
self.num = num
#num = input("Enter a non-negative number:: ")
if num < 0:
raise NameError("Negative")
#test else:
#print "ok!"
num_list = []
def digitize(self, num):
num_list = []
n = str(num)
for digit in n:
num_list.append(int(digit))
return num_list
def sum_digits(self, num):
the_list = self.digitize(num)
the_sum = 0
for digit in the_list:
the_sum = the_sum + digit
return the_sum
def times_digits(self, num):
the_list = self.digitize(num)
the_product = 0
for digit in the_list:
the_product = the_product * digit
return the_product
def additive(self, num):
global counter1
sum1 = self.sum_digits(num)
list1 = []
list1 = self.digitize(sum1)
if list1.__len__() > 1:
global counter1
counter1 = counter1 + 1
self.additive(sum1)
return sum1, counter1
def multiplicative(self, num):
global counter2
prod1 = self.times_digits(num)
list1 = []
list1 = self.digitize(prod1)
if list1.__len__() > 1:
global counter1
counter2 = counter2 + 1
self.multiplicative(prod1)
return prod1, counter2
c = Persistence(5)
print c.additive(5)
print c.multiplicative(5)
Not sure why I'm getting this error? It seems to me that I have defined the global variable counter1. I'm also getting this error for counter2, and the only way I've been able to resolve the error is by inserting counter1 = 0 (or any other number) a single line above the return statement on the additive() method. Help would be much appreciated!!
One of the solutions is to move the counters out of class like:
# global variables
num = 0
counter1 = 0
counter2 = 0
class Persistence:
....
....
Leave the rest of your code unmodified.
counter1 and counter2 are defined as class attributes currently, if you want to keep it that way, just access them as classname.attrname:
class Persistence:
num = 0
counter1 = 0
counter2 = 0
def __init__(self, num):
self.num = num
#num = input("Enter a non-negative number:: ")
if num < 0:
raise NameError("Negative")
#test else:
#print "ok!"
num_list = []
def digitize(self, num):
...
def sum_digits(self, num):
...
def times_digits(self, num):
...
def additive(self, num):
sum1 = self.sum_digits(num)
list1 = []
list1 = self.digitize(sum1)
if list1.__len__() > 1:
Persistence.counter1 = Persistence.counter1 + 1
self.additive(sum1)
return sum1, Persistence.counter1
def multiplicative(self, num):
prod1 = self.times_digits(num)
list1 = []
list1 = self.digitize(prod1)
if list1.__len__() > 1:
Persistence.counter2 = Persistence.counter2 + 1
self.multiplicative(prod1)
return prod1, Persistence.counter2
c = Persistence(5)
print c.additive(5)
print c.multiplicative(5)
The appropriate way of solving this issue will be to move the 3 class variables to init method. These variable will become associated with the object can be accessed using 'self'
class Persistence(object):
def __init__(self):
self.counter1 = 0
self.counter2 = 0
def digitize(self, num):
return [int(digit) for digit in str(num)]
def sum_digits(self, num):
return sum(self.digitize(num))
def times_digits(self, num):
the_list = self.digitize(num)
the_product = 0
for digit in the_list:
the_product = the_product * digit
return the_product
def additive(self, num):
sum1 = self.sum_digits(num)
list1 = []
list1 = self.digitize(sum1)
if len(list1) > 1:
self.counter1 += 1
self.additive(sum1)
return sum1, self.counter1
def multiplicative(self, num):
prod1 = self.times_digits(num)
list1 = []
list1 = self.digitize(prod1)
if len(list1) > 1:
self.counter2 += 1
self.multiplicative(prod1)
return prod1, self.counter2
c = Persistence()
print c.additive(5)
print c.multiplicative(5)
You don't num in the constructor if you are not actually using it.
The purpose of this code is to create the shortest path that connect each point. I've tried to implement the Kruskals algorithm in Python, but there is a problem and it occurs when the function findSet(x) is called. It seems like the program enters a infinite loop. I've tried to print x, x.dad and x.dad.dad and x in the crucial moment returns the initial x. I really don't know where is the problem.
class Point:
def __init__(self,x = None, y = None):
self.x = x
self.y = y
self.dad = None
self.rank = None
class Edge:
def __init__(self, firstPoint = None, secondPoint = None, value = None):
self.firstPoint = firstPoint
self.secondPoint = secondPoint
if self.firstPoint != None and self.secondPoint != None:
self.value = abs(firstPoint.x-secondPoint.x) + abs(firstPoint.y - secondPoint.y)
else:
self.value = float("inf")
def makeSet(node):
node.dad = node
node.rank = 0
def findSet(x):
print x, x.dad,x.dad.dad
if x == x.dad:
return x
x.dad = findSet(x.dad)
return x.dad
def unionSet(node1,node2):
root1 = findSet(node1)
root2 = findSet(node2)
if root1.rank < root2.rank:
root1.dad = root2
if root1.rank > root2.rank:
root2.dad = root1
else:
root2.dad = root1
root1.rank += 1
def merge(A, p, q, r):
L = A[p:q+1]
R = A[q+1:r+1]
infinite = Edge()
L.append(infinite)
R.append(infinite)
temp = []
i = 0
j = 0
for k in range(p,r+1):
if L[i].value <= R[j].value:
temp.append(L[i])
i += 1
else:
temp.append(R[j])
j += 1
A[p:r+1] = temp
def mergeSort(A,p,r):
A2 = A
if p < r:
q = int((p+r)/2)
mergeSort(A2, p, q)
mergeSort(A2, q+1, r)
merge(A2, p, q, r)
def readPoint(A):
temp = []
for i in range(0,len(A), 2):
tempPoint = Point(A[i],A[i+1])
makeSet(tempPoint)
temp.append(tempPoint)
return temp
def calculateEdges(A):
tempArray = []
for i in range(0,len(A)):
for j in range(i+1,len(A)):
edge = Edge(A[i], A[j])
tempArray.append(edge)
return tempArray
def Kruskal(N, E):
mergeSort(E,0,len(E)-1)
j = 0
i = 0
total = 0
while j < N:
if findSet(E[i].firstPoint) != findSet(E[i].secondPoint):
unionSet(E[i].firstPoint,E[i].secondPoint)
j += 1
total += E[i].value
if i < len(E) - 1:
i += 1
return total
Ar = readPoint([18, 2, 99, 68])
E = calculateEdges(Ar)
print Kruskal(len(Ar),E)
I am not sure why do i get this error from the console:
<<
print stirngp[state[i][j]]
^
SyntaxError: invalid syntax
<<
Furthermore it seems that the IDE put a red close on the following code line
line = raw_input("Enter:")
I am not sure what did i do wrong, the following code is as shown below
def isTerminal(state):
stateT = zip(*state)
for i in range(3):
if all(state[i][0] == j for j in state[i]) and state[i][0] != 0:
return state[i][0]
if all(stateT[i][0] == j for j in stateT[i]) and stateT[i][0] != 0:
return stateT[i][0]
if (state[0][0] == state[1][1] == state[2][2]) or \
(state[0][2] == state[1][1] == state[2][0]):
if state[1][1] != 0:
return state[1][1]
for i in range(3):
if 0 in state[i]:
return None
return 0
def succ(state):
# print state
countX = 0
countO = 0
for i in range(3):
for j in range(3):
if state[i][j] == 1: countX = countX + 1
if state[i][j] == -1: countO = countO + 1
if countX > countO:
player = "MIN"
else:
player = "MAX"
succList = []
v = {"MIN":-1,"MAX":1}
for i in range(3):
for j in range(3):
if state[i][j] == 0:
succ = [k[:] for k in state]
succ[i][j] = v[player]
succList = succList + [succ]
# print succList
return succList
def nextplay(player):
if player == "MIN":
return "MAX"
else:
return "MIN"
def minimax(state,alpha,beta,player):
value = isTerminal(state)
if value != None:
# print "TERMINAL:", state, value, player
return value
if player == "MIN":
for y in succ(state):
beta = min(beta, minimax(y,alpha,beta,"MAX"))
if beta <= alpha: return alpha
return beta
if player == "MAX":
for y in succ(state):
alpha = max(alpha, minimax(y,alpha,beta,"MIN"))
if alpha >= beta: return beta
return alpha
def printing(state):
p = {-1:"O",0:".",1:"X"}
for i in range(3):
for j in range(3):
print p[state[i][j]],
print ""
print ""
def main():
state = [[0,0,0],
[0,0,0],
[0,0,0]]
val = isTerminal(state)
while val == None:
line = raw_input("Enter:")
x = line.split(",")
a = int(x[0]); b = int(x[1])
state[a][b] = 1
if isTerminal(state) != None:
printing(state)
return
# determine which successive state is better
succList = succ(state)
succValList = []
for i in succList:
succValList = succValList + [(minimax(i,-1,1,"MAX"),i)]
succValList.sort(key=lambda x:x[0])
state = succValList[0][1] # can also randomly choose other states of the same minimax value
printing(state)
val = isTerminal(state)
if __name__ == '__main__':
main()
As far as i know you can't use raw_input() in Python 3. It's been changed to just input()
http://docs.python.org/dev/whatsnew/3.0.html
also what is stringp? is it an existing list?
if so then state[i][j] MUST return an integer so you can retrieve the item at that index of stringp[]