I made a Python program using numpy, sympy, sys which worked well in IDE. Here is the code...
import numpy as np
from sympy import *
import sys
f = open("result.txt", 'w+')
for line in sys.stdin:
f.write(line)
f.close()
f = open("result.txt", 'r')
data = f.read().split("\n")
i = 0
while i < len(data):
data[i] = data[i].split(' ')
i += 1
print(data)
amount = int(data[0][0])
print(amount)
matrix = np.zeros((amount, amount))
i = 0
j = 0
while i < amount:
while j < amount:
matrix[i, j] = int(data[i + 1][j])
j += 1
j = 0
i += 1
i = 0
j = 0
counter = 0
formula = 1
while i < amount:
while j < amount:
if matrix[i, j] == 1:
x = symbols(str(i + 1))
y = symbols(str(j + 1))
if counter == 0:
formula = (~x | ~y)
counter += 1
else:
formula = formula & (~x | ~y)
j += 1
j = 0
i += 1
formula_to_string = pycode(simplify_logic(formula, form='dnf', force=True))
massive_to_parse = formula_to_string.split("or")
k = 1
i = 0
while i < len(massive_to_parse):
print("{", end='')
while k < amount + 1:
try:
massive_to_parse[i].index(str(k))
except ValueError:
print("V",k, sep='', end='')
finally:
k += 1
print("}-максимальное внутренне устойчивое множество")
k = 1
i += 1
Then I made an exe file using pyinstaller with this command...
pyinstaller main.py
but when i launch it, this errors appear.
How can I fix this problem? Because I need exe file for university to launch it from other program on C
Related
So I'm trying to assign values to a Queue from the module multiprocessing.
My code is:
from multiprocessing import Queue
fileBlocks = Queue()
filesDic = dict()
cont = 0
def eDiv(files, k):
global fileBlocks
listLinesAllWithIdx = []
index = 0
bytesCount = 0
blockLines = []
blockLinesAll = []
allBytes = 0
n = 0
lineCount = 0
for file in files:
with open(file) as f:
filesDic[file] = index
for line in f:
listLinesAllWithIdx.append((index,line, utf8len(line)))
allBytes += utf8len(line)
index += 1
while n < allBytes:
bytesCount = 0
blockLines = []
while bytesCount <= k and lineCount < len(listLinesAllWithIdx):
blockLines.append(listLinesAllWithIdx[lineCount])
bytesCount += listLinesAllWithIdx[lineCount][2]
lineCount += 1
n += bytesCount
blockLinesAll.append(blockLines)
for i in blockLinesAll:
fileBlocks.put(i)
print(fileBlocks.empty())
def utf8len(s):
return len(s.encode('utf-8'))
eDiv(["file1.txt","file2.txt"], 10)
I'm expecting fileBlocks.empty() = True but I'm getting False, and I don't understand why this is happening.
I need some help with my code. I need to look for the presence of resistance genes in a water sample. That translates in having a huge file of reads coming from the water sample and a file of resistances genes. My problem is making the code run under 5 minutes, a thing that is not happening right now. Probably the issue relays on discarting reads as fast as possible, on having a smart method to only analyze meaningful reads. Do you have any suggestion? I cannot use any non standard python library
This is my code
import time
def build_lyb(TargetFile):
TargetFile = open(TargetFile)
res_gen = {}
for line in TargetFile:
if line.startswith(">"):
header = line[:-1]
res_gen[header] = ""
else:
res_gen[header] += line[:-1]
return res_gen
def build_kmers(sequence, k_size):
kmers = []
n_kmers = len(sequence) - k_size + 1
for i in range(n_kmers):
kmer = sequence[i:i + k_size]
kmers.append(kmer)
return kmers
def calculation(kmers, g):
matches = []
for i in range(0, len(genes[g])):
matches.append(0)
k = 0
while k < len(kmers):
if kmers[k] in genes[g]:
pos = genes[g].find(kmers[k])
for i in range(pos, pos+19):
matches[i] = 1
k += 19
else:
k += 1
return matches
def coverage(matches, g):
counter = 0
for i in matches[g]:
if i >= 1:
counter += 1
cov = counter/len(res_genes[g])*100
return cov
st = time.time()
genes = build_lyb("resistance_genes.fsa")
infile = open('test2.txt', 'r')
res_genes = {}
Flag = False
n_line = 0
for line in infile:
n_line += 1
if line.startswith("+"):
Flag = False
if Flag:
kmers = build_kmers(line[:-1], 19)
for g in genes:
counter = 18
k = 20
while k <= 41:
if kmers[k] in genes[g]:
counter += 19
k += 19
else:
k += 1
if counter >= 56:
print(n_line)
l1 = calculation(kmers, g)
if g in res_genes:
l2 = res_genes[g]
lr = [sum(i) for i in zip(l1, l2)]
res_genes[g] = lr
else:
res_genes[g] = l1
if line.startswith('#'):
Flag = True
for g in res_genes:
print(g)
for i in genes[g]:
print(i, " ", end='')
print('')
for i in res_genes[g]:
print(i, " ", end='')
print('')
print(coverage(res_genes, g))
et = time.time()
elapsed_time = et-st
print("Execution time:", elapsed_time, "s")
I am stuck in rewriting Python code to compile it in PyPy. My task is to calculate number of inversions in a list using modification of merge sort. Input data brings from input.txt, the result of the code is written in output.txt. My last attempt to do this looks like:
import sys
def merge(a, b, inverse_num):
n, m = len(a), len(b)
i = 0
j = 0
c = []
while i < n or j < m:
if j == m:
c.append(a[i])
i += 1
# inverse_num += 1
elif i < n and a[i] <= b[j]:
c.append(a[i])
i += 1
elif i < n and a[i] > b[j]:
c.append(b[j])
j += 1
inverse_num += n - i
if j == m and i == n - 1:
c.append(a[i])
i += 1
else:
c.append(b[j])
j += 1
return c, inverse_num
def merge_sort(arr, inverse_num=0):
n = len(arr)
if n == 1:
return arr, inverse_num
l = arr[:(n//2)]
r = arr[(n//2):n]
l, inverse_num_l = merge_sort(l, inverse_num)
r, inverse_num_r = merge_sort(r, inverse_num)
inverse_num = inverse_num_l + inverse_num_r
return merge(l, r, inverse_num)
def main():
with open('input.txt') as f:
n = int(f.readline().split()[0])
in_list = list(map(int, f.readline().split()))
output_file = open('output.txt', 'w')
sorted_arr, inverse_num = merge_sort(in_list, inverse_num=0)
output_file.write(str(inverse_num))
output_file.close()
return 0
def target(*args):
return entry_point, None
def entry_point(argv):
main()
return 0
if __name__ == "__main__":
entry_point(sys.argv)
After compilation it with command pypy "C:/pypy_compile/pypy-src/translate.py" Task2.py
error appears:
[translation:ERROR] TypeError: method_split() takes at least 2 arguments (1 given)
Processing block:
block#27[v4...] is a <class 'rpython.flowspace.flowcontext.SpamBlock'>
in (Task2:44)main
containing the following operations:
v6 = getattr(v5, ('split'))
v7 = simple_call(v6)
--end--
Thanks in advance for helping.
https://www.hackerrank.com/challenges/designer-door-mat/problem
Below is my submission:
n = input().split(' ')
rows, columns = int(n[0]), int(n[1])
if(rows % 2 == 1 and columns == 3*rows):
printale = ''
j = 1
k = rows - 7
for i in range(int(rows/2), -1, -1):
printale = '---'*i + '.|.'*j + '---'*i
if(i == 0):
print('-'*int(j+k/2) + 'Welcome' + '-'*int(j+k/2))
j -= 2
else:
print(printale)
j += 2
for l in range(1, int(rows/2)+1):
printale = '---'*l + '.|.'*j + '---'*l
print(printale)
j -= 2
Is there anything wrong with the code?
Yes, there is. The "WELCOME" in the problem statement is all-caps.
I wrote the code below, but it specifies the number of lines in the file. I'm wondering how I can change it so that it will read an unknown number of lines?
n = int(input("instance: "))
tp, tn, fp, fn = 0
for i in range(n):
real, predicted = map(int, input().split(' '))
for num in i:
if real == 1 and predicted == 1:
tp += 1
elif real == 0 and predicted == 0:
tn += 1
elif real == 1 and predicted == 0:
fn += 1
else:
fp += 1
pr = tp / (tp + fp)
rc = tp / (tp + fn)
f1 = 2 * ((pr * rc) / (pr + rc))
print("f1= ", f1)
My code reads these lines and compares numbers on each line with each other, but not with numbers on the other lines.
The input looks like this:
1 1
0 0
0 1
0 1
1 0
0 1
0 0
1 1
0 0
0 0
0 0
0 0
1 1
Keep reading until EOFError is thrown:
tp, tn, fp, fn = 0
i = 0
try:
while True:
real, predicted = map(int, input().split(' '))
for num in i:
if real == 1 and predicted == 1:
tp += 1
elif real == 0 and predicted == 0:
tn += 1
elif real == 1 and predicted == 0:
fn += 1
else:
fp += 1
i += 1
except EOFError:
pass
pr = tp / (tp + fp)
rc = tp / (tp + fn)
f1 = 2 * ((pr * rc) / (pr + rc))
print("f1= ", f1)
Your code has errors too:
multiple variables can't be assigned to the same value using unpacking.
range needs to be used if you want to count up to a number.
This should fix them:
tp = tn = fp = fn = 0
i = 0
try:
while True:
real, predicted = map(int, input().split(' '))
for num in range(i):
if real == 1 and predicted == 1:
tp += 1
elif real == 0 and predicted == 0:
tn += 1
elif real == 1 and predicted == 0:
fn += 1
else:
fp += 1
i += 1
except EOFError:
pass
pr = tp / (tp + fp)
rc = tp / (tp + fn)
f1 = 2 * ((pr * rc) / (pr + rc))
print("f1= ", f1)