The cmd is taking forever to run a python program - python

For some reason, the cmd is taking 2 minutes to run a 18 line python program. I ran it again but it did not do anything.
Can anyone tell me why it is taking that long?
from array import *
file = open("IntegerArray.txt" , "r")
input_array = array('i')
for line in file:
c = int(line)
input_array.append(c)
top_array = input_array[:len(input_array)//2]
bottom_array = input_array[len(input_array)//2:]
inversion = 0
max_index = len(top_array)
for i in range(0, max_index):
for j in range(i + 1, max_index):
if top_array[i] > top_array[j]:
temp = top_array[i]
top_array[i] = top_array[j]
top_array[j] = temp
inversion = inversion + 1
print "inversion = ", inversion

Not related to the time, but you can simplify this:
temp = top_array[i]
top_array[i] = top_array[j]
top_array[j] = temp
to
top_array[i], top_array[j] = top_array[j], top_array[i]

Related

how can I run this code with two loops faster? Can I run it without using for?

I wanna run this code for a wide range instead of this range. So I wanna make it better to run faster.
Is it impossible to use something else instead of these loops?
z1=3
z2=HEIGHT-1
def myfunction(z1,z2):
for l in range(z1):
vector = np.zeros(WIDTH)
vector[WIDTH//2] = 1
result = []
result.append(vector)
for i in range(z2):
vector = doPercolationStep(vector, PROP, i)
result.append(vector)
result = np.array(result)
ss = result.astype(int)
ss = np.where(ss==0, -1, ss)
ww = (ss+(ss.T))/2
re_size = ww/(np.sqrt(L))
matr5 = re_size
np.savetxt('F:/folder/matr5/'+str(l)+'.csv', matr5)
and doPercolationStep is:
WIDTH = 5
HEIGHT = 5
L=5
PROP = 0.6447
def doPercolationStep(vector, PROP, time):
even = time%2 # even is 1 or 0
vector_copy = np.copy(vector)
WIDTH = len(vector)
for i in range(even, WIDTH, 2):
if vector[i] == 1:
pro1 = random.random()
pro2 = random.random()
if pro1 < PROP:
vector_copy[(i+WIDTH-1)%WIDTH] = 1 # left neighbour of i
if pro2 < PROP:
vector_copy[(i+1)%WIDTH] = 1 # right neighbour of i
vector_copy[i] = 0
return vector_copy

Program does not print the return value of a function

I have a piece of code to calculate one matrix from another. But when I try to print the output of the function the code runs without any errors but does not print anything. Furthermore, if I try to print the input matrix to the function and then call the function, the original input does not get printed. It prints fine when I don't call this function inf_mat. Does anybody know what might be the reason?
import numpy as np
from itertools import permutations
def inf_mat(adjacency, l_max):
## Influence matrix - Returns the influence matrix corresponding to the input adjacency matrix
inf_arr = np.zeros(np.shape(adjacency))
print(inf_arr)
norm_mat = adjacency
for i in range(np.shape(norm_mat)[0]):
for j in range(np.shape(norm_mat)[1]):
if norm_mat[i,j] != 0:
norm_mat[i,j] = 1
for i in range(np.shape(inf_arr)[0]):
for j in range(np.shape(inf_arr)[1]):
ind_lists = []
for k in range(l_max-1):
all_ind = [m for m in range(np.shape(adjacency)[1])]
all_ind.remove(i)
if j != i:
all_ind.remove(j)
ind_lists.append(list(permutations(all_ind, k+1)))
pairwise_list = []
for p in range(len(ind_lists)):
pairs = []
for q in range(len(ind_lists[p])):
mult_list = [i]
for r in range(len(ind_lists[p][q])):
mult_list.append(ind_lists[p][q][r])
mult_list.append(j)
pairs.append(mult_list)
pairwise_list.append(pairs)
inf_sum = adjacency[i,j]
norm_sum = norm_mat[i,j]
for t in range(len(pairwise_list)):
for t_ind in range(len(pairwise_list[t])):
inf_ele = 1
norm_ele = 1
for s in range(len(pairwise_list[t_ind])-1):
inf_ele *= adjacency[s,s+1]
norm_ele *= norm_mat[s,s+1]
inf_sum += inf_ele
norm_sum += norm_ele
inf_arr[i,j] = inf_sum/norm_sum
return inf_arr/l_max
def readFile(Filename):
fileObj = open(Filename, "r")
words = fileObj.read().splitlines()
fileObj.close()
return words
topo_list = readFile("EMT_RACIPE.topo")
topo_list.remove(topo_list[0])
gene_list = []
for i in range(len(topo_list)):
topo_list[i] = topo_list[i].split()
for j in range(2):
if gene_list.count(topo_list[i][j]) == 0:
gene_list.append(topo_list[i][j])
adjacency = np.zeros(shape = (len(gene_list),len(gene_list)))
for i in range(len(gene_list)):
indices = []
for j in range(len(topo_list)):
if topo_list[j].count(gene_list[i]) > 0:
indices.append(j)
for k in range(len(indices)):
if topo_list[indices[k]][0] == gene_list[i]:
if topo_list[indices[k]][2] == '1':
adjacency[i, gene_list.index(topo_list[indices[k]][1])] = 1
if topo_list[indices[k]][2] == '2':
adjacency[i, gene_list.index(topo_list[indices[k]][1])] = -1
if topo_list[indices[k]][1] == gene_list[i]:
if topo_list[indices[k]][2] == '1':
adjacency[gene_list.index(topo_list[indices[k]][0]), i] = 1
if topo_list[indices[k]][2] == '2':
adjacency[gene_list.index(topo_list[indices[k]][0]), i] = -1
N22E82 = adjacency
print(N22E82)
inf_N22E82 = inf_mat(N22E82, 10)
print(inf_N22E82)

Why is my breadth first search algorithm so slow?

I am implementing a breadth first search algorithm in Python to solve the 8 puzzle game http://mypuzzle.org/sliding. I was told that this algorithm should not take more than a few minutes to run, but its been running for over an hour. Is there something wrong with my algorithm or implementation? Or was the person who told me this could run in a few minutes mistaken?
Here is my code:
Edit: You can run this in the command line with python driver.py bfs <nine numbers separated by commas> I'm using python driver.py bfs 9,3,7,5,0,6,4,1,2
driver.py
import sys
from SearchAlgorithms import bfs#, dfs, ast, ida
if __name__=='__main__':
board = [int(s) for s in sys.argv[2].split(',')]
if(sys.argv[1]=='bfs'):
output = bfs(board)
elif(sys.argv[1]=='dfs'):
output = bfs(board)
elif(sys.argv[1]=='ast'):
output = bfs(board)
elif(sys.argv[1]=='ida'):
output = bfs(board)
else:
raise IOError
print(output)
SearchAlgorithms.py
from queue import Queue
import math
import time
import copy
class Board:
def __init__(self, data, path=[]):
self.data = data
self.path = path
def neighbors(self):
neighbors = []
board_width = int(math.sqrt(len(self.data)))
if self.data.index(0) - board_width >= 0:
new_data = copy.deepcopy(self.data)
new_data[self.data.index(0)], new_data[self.data.index(0) - board_width] = self.data[self.data.index(0) - board_width], 0
neighbors.append(Board(new_data, self.path + ['Up']))
if self.data.index(0) + board_width < len(self.data):
new_data = copy.deepcopy(self.data)
new_data[self.data.index(0)], new_data[self.data.index(0) + board_width] = self.data[self.data.index(0) + board_width], 0
neighbors.append(Board(new_data, self.path + ['Down']))
if self.data.index(0) - 1 >= 0:
new_data = copy.deepcopy(self.data)
new_data[self.data.index(0)], new_data[self.data.index(0) - 1] = self.data[self.data.index(0) - 1], 0
neighbors.append(Board(new_data, self.path + ['Left']))
if self.data.index(0) + 1 < len(self.data):
new_data = copy.deepcopy(self.data)
new_data[self.data.index(0)], new_data[self.data.index(0) + 1] = self.data[self.data.index(0) + 1], 0
neighbors.append(Board(new_data, self.path + ['Right']))
return neighbors
def bfs(board):
before = time.time()
goal = sorted(board)
graph = Board(board)
frontier = [graph]
explored = set()
count = 0
while frontier:
count +=1
if count % 100 == 0:
print(count)
state = frontier.pop(0)
explored.add(state)
if state.data == goal:
return {'path_to_goal': reversed(state.path),
'cost_of_path': len(state.path),
'nodes_expanded': len(explored),
'fringe_size': len(frontier),
'running_time': time.time() - before
}
for neighbor in state.neighbors():
if neighbor not in set().union(frontier, explored) :
frontier.append(neighbor)
return False
Thank you for your help!

Memory overflow in Python

I have 67000 files, I need to read them and extract similarities between the words, but when I run the code my laptop becomes much slower, I can't open any other application, and then a memory overflow error shows up (even when I run on around 10 000 of the files). Is there a way to clear the memory after every for loop maybe, or will running the code on all files be impossible to do? Below is the code:
def isAscii(s):
for c in s:
if c not in string.printable:
return False
return True
windowSize = 2
relationTable = {}
probabilities = {}
wordCount = {}
totalWordCount = 0
def sim(w1, w2):
numerator = 0
denominator = 0
if (w1 in relationTable) and (w2 in relationTable):
rtw1 = {}
rtw2 = {}
rtw1 = relationTable[w1]
rtw2 = relationTable[w2]
for word in rtw1:
rtw1_PMI = rtw1[word]['pairPMI']
denominator += rtw1_PMI
if(word in rtw2):
rtw2_PMI = rtw2[word]['pairPMI']
numerator += (rtw1_PMI + rtw2_PMI)
for word in rtw2:
rtw2_PMI = rtw2[word]['pairPMI']
denominator += rtw2_PMI
if(denominator != 0):
return float(numerator)/denominator
else:
return 0
else:
return -1
AllNotes = {}
AllNotes = os.listdir("C:/Users/nerry-san/Desktop/EECE 502/MedicalNotes")
fileStopPunctuations = open('C:/Users/nerry-san/Desktop/EECE 502/stopPunctuations.txt')
stopPunctuations = nltk.word_tokenize(fileStopPunctuations.read())
for x in range (0, 10):
fileToRead = open('C:/Users/nerry-san/Desktop/EECE 502/MedicalNotes/%s'%(AllNotes[x]))
case1 = fileToRead.read()
text = nltk.WordPunctTokenizer().tokenize(case1.lower())
final_text = []
for index in range(len(text)):
word = text[index]
if (word not in stopPunctuations):
final_text.append(word)
for index in range (len(final_text)):
w1 = final_text[index]
if(isAscii(w1)):
for index2 in range(-windowSize, windowSize+1):
if (index2 != 0):
if ( index + index2 ) in range (0, len(final_text)):
w2 = final_text[index + index2]
if(isAscii(w2)):
totalWordCount += 1
if (w1 not in wordCount):
wordCount[w1] = {}
wordCount[w1]['wCount'] = 0
try:
wordCount[w1][w2]['count'] += 1
wordCount[w1]['wCount'] += 1
except KeyError:
wordCount[w1][w2] = {'count':1}
wordCount[w1]['wCount'] += 1
for word in wordCount:
probabilities[word]={}
probabilities[word]['wordProb'] = float (wordCount[word]['wCount'])/ totalWordCount
for word in wordCount:
relationTable[word] = {}
for word2 in wordCount[word]:
if ( word2 != 'wCount'):
pairProb = float(wordCount[word][word2]['count'])/(wordCount[word]['wCount'])
relationTable[word][word2] = {}
relationTable[word][word2]['pairPMI'] = math.log(float(pairProb)/(probabilities[word]['wordProb'] * probabilities[word2]['wordProb']),2)
l = []
for word in relationTable:
l.append(word)
for index in range (0, len(l)):
word = l[index]
simValues = []
for index2 in range (0, len(l)):
word2 = l[index2]
if(word!= word2):
simVal = sim(word,word2)
if(simVal > 0):
simValues.append([word2, simVal])
simValues.sort(key= operator.itemgetter(1), reverse = True)
Every time you open a file, use the "with" statement. This will ensure the file is closed when the loop finishes (or rather when the with block is exited.

Why my code is getting NZEC run time error?

Question source: SPOJ.. ORDERS
def swap(ary,idx1,idx2):
tmp = ary[idx1]
ary[idx1] = ary[idx2]
ary[idx2] = tmp
def mkranks(size):
tmp = []
for i in range(1, size + 1):
tmp = tmp + [i]
return tmp
def permutations(ordered, movements):
size = len(ordered)
for i in range(1, size): # The leftmost one never moves
for j in range(0, int(movements[i])):
swap(ordered, i-j, i-j-1)
return ordered
numberofcases = input()
for i in range(0, numberofcases):
sizeofcase = input()
tmp = raw_input()
movements = ""
for i in range(0, len(tmp)):
if i % 2 != 1:
movements = movements + tmp[i]
ordered = mkranks(sizeofcase)
ordered = permutations(ordered, movements)
output = ""
for i in range(0, sizeofcase - 1):
output = output + str(ordered[i]) + " "
output = output + str(ordered[sizeofcase - 1])
print output
Having made your code a bit more Pythonic (but without altering its flow/algorithm):
def swap(ary, idx1, idx2):
ary[idx1], ary[idx2] = [ary[i] for i in (idx2, idx1)]
def permutations(ordered, movements):
size = len(ordered)
for i in range(1, len(ordered)):
for j in range(movements[i]):
swap(ordered, i-j, i-j-1)
return ordered
numberofcases = input()
for i in range(numberofcases):
sizeofcase = input()
movements = [int(s) for s in raw_input().split()]
ordered = [str(i) for i in range(1, sizeofcase+1)]
ordered = permutations(ordered, movements)
output = " ".join(ordered)
print output
I see it runs correctly in the sample case given at the SPOJ URL you indicate. What is your failing case?

Categories

Resources