I have a program that reads in 5 files, performs a "two sum" algorithm over the arrays in the file, and outputs to a new file if there's a sum of two numbers that matches the target.
I've got the logic to handle everything except if there's no match. If there's no match I need to write "No" to the output file. If I just add else: f.write("No") after the second if then it'll write "No" for every pass that it's not a match. I need it write "No" ONCE at the end of the processing, after it hasn't found a match.
Read 5 "in" files
inPrefix = "in"
outPrefix = "out"
for i in range(1, 6):
inFile = inPrefix + str(i) + ".txt"
with open(inFile, 'r') as f:
fileLines = f.readlines()
target = fileLines[1]
arr = fileLines[2]
Output 5 "out" files
outFile = outPrefix + str(i) + ".txt"
with open(outFile, 'a') as f:
f.write(target)
f.write(arr)
target = int(target)
num_arr = [int(j) for j in arr.split()]
for a in range(len(num_arr)):
for b in range(a, len(num_arr)):
curr = num_arr[a] + num_arr[b]
if num_arr[a]*2 == target:
a = str(num_arr[a])
target = str(target)
answer = "{}+{}={}".format(a,a,target)
f.write("Yes")
f.write("\n")
f.write(answer)
break
if curr == target:
a = str(num_arr[a])
b = str(num_arr[b])
target = str(target)
answer = "{}+{}={}".format(a,b,target)
f.write("Yes")
f.write("\n")
f.write(answer)
break
f.close()
Initialize a variable -- let's call it wrote_yes -- to False at the top of the code.
Anytime you write "Yes" to the file, set that variable to True.
When you reach the end of all the processing, check that variable. If it's still False, then you never wrote "Yes", so now you can write "No".
I'm basically trying to code a simple spell-check program that will prompt you for an input file, then analyze the input file for possible spelling errors (by using binary search to see if the word is in the dictionary), before printing them in the output file.
However, currently, it outputs everything in the input file instead of just the errors...
My code is as follows:
import re
with open('DICTIONARY1.txt', 'r') as file:
content = file.readlines()
dictionary = []
for line in content:
line = line.rstrip()
dictionary.append(line)
def binary_search(array, target, low, high):
mid = (low + high) // 2
if low > high:
return -1
elif array[mid] == target:
return mid
elif target < array[mid]:
return binary_search(array, target, low, mid-1)
else:
return binary_search(array, target, mid+1, high)
input = input("Please enter file name of file to be analyzed: ")
infile = open(input, 'r')
contents = infile.readlines()
text = []
for line in contents:
for word in line.split():
word = re.sub('[^a-z\ \']+', " ", word.lower())
text.append(word)
infile.close()
outfile = open('TYPO.txt', 'w')
for data in text:
if data.strip() == '':
pass
elif binary_search(dictionary, data, 0, len(data)) == -1:
outfile.write(data + "\n")
else:
pass
file.close
outfile.close
I can't seem to figure out what's wrong. :(
Any help would be very much appreciated!
Thank you. :)
I tried replacing len(data) with len(dictionary) as that made more sense to me and it seems to work in my very limited tests.
I think you were passing the length of the word in question as the upper bound on the dictionary. So if you were looking up the word "dog" you were only checking the first 3 words in the dictionary, and since your dictionary is probably very large, almost every word was never found (so every word was in the output file).
My code is not very good but this is a really interesting problem. When looking for a forward slash in a string all are found except for if the forward slash is in the last word in the file. Here is my code.
#!/usr/bin/python
import sys
if len(sys.argv)!=2:
print "usage: %s filename\n" % (sys.argv[0]);
exit(0);
f = open(sys.argv[1]);
lines = [i for i in f.readlines()]
finals = [];
for line in lines:
words = line.split(",");
for word in words:
if word.find("/") != -1:
datefixes = word.split("/")
if datefixes[2].__len__() == 4:
temp = datefixes[2]
word = datefixes[0] + "-" + datefixes[1] + "-" + temp[-2:]
finals += "," + word;
tempstring = ''.join(finals)
finallist = tempstring.split("\r\n")
finalstring = ""
for tmpstrpart in finallist:
if tmpstrpart != "" or tmpstrpart !="\r\n":
finalstring += tmpstrpart[1:] + "\r\n"
print finalstring
and here is a sample input
ACPVBF,1930-729,Z729,12/16/2014,6/10/2008,1/5/2003,44-48-46,39-43-41,35-39-37,29-33-31
ACPVGT,1930-729,Z729,25-29-27,19-23-21,14-18-16,7/11/2009,2/6/2004,48-2-0,42-46-44
ACPUQH,1930-729,Z729,32-40-19,26-34-13,21-29-8,14-22-1,9/17/1946,5/13/1942,49-7-36
ACPVOU,1930-729,Z729,42-0-29,36-44-23,31-39-18,24-32-11,19-27-6,15-23-2,9/17/1946
in the code these lines are split by commas. if the word at the end contains a / the forward slash is not found. but only if it is at the end. the rest work fine.
edit: The output I am currently getting on these lines is:
ACPVBF,1930-729,Z729,12-16-14,6-10-08,1-5-03,44-48-46,39-43-41,35-39-37,29-33-31
ACPVGT,1930-729,Z729,25-29-27,19-23-21,14-18-16,7-11-09,2-6-04,48-2-0,42-46-44
ACPUQH,1930-729,Z729,32-40-19,26-34-13,21-29-8,14-22-1,9-17-46,5-13-42,49-7-36
ACPVOU,1930-729,Z729,42-0-29,36-44-23,31-39-18,24-32-11,19-27-6,15-23-2,9/17/1946
the output that I am trying to get from these lines is:
ACPVBF,1930-729,Z729,12-16-14,6-10-08,1-5-03,44-48-46,39-43-41,35-39-37,29-33-31
ACPVGT,1930-729,Z729,25-29-27,19-23-21,14-18-16,7-11-09,2-6-04,48-2-0,42-46-44
ACPUQH,1930-729,Z729,32-40-19,26-34-13,21-29-8,14-22-1,9-17-46,5-13-42,49-7-36
ACPVOU,1930-729,Z729,42-0-29,36-44-23,31-39-18,24-32-11,19-27-6,15-23-2,[9-17-46]
I want the one with the brackets around it to change also.
final working code based on BrenBarn's answer:
#!/usr/bin/python
import sys
import re
if len(sys.argv)!=2:
print "usage: %s filename\n" % (sys.argv[0]);
exit(0);
f = open(sys.argv[1]);
x = f.read()
f.close()
filename = sys.argv[1]
filename = filename[:-4] + " finished.csv"
f = open(filename, 'w')
f.write(re.sub(r'(\d{1,2})/(\d{1,2})/\d{2}(\d{2})', r'\1-\2-\3', x))
f.close()
Thanks for all the help. Sorry I can't upvote yet.
I'm not sure what the problem is, but I think what you're trying to do can be accomplished way more easily by just using a regular expression.
>>> print x
ACPVBF,1930-729,Z729,12/16/2014,6/10/2008,1/5/2003,44-48-46,39-43-41,35-39-37,29-33-31
ACPVGT,1930-729,Z729,25-29-27,19-23-21,14-18-16,7/11/2009,2/6/2004,48-2-0,42-46-44
ACPUQH,1930-729,Z729,32-40-19,26-34-13,21-29-8,14-22-1,9/17/1946,5/13/1942,49-7-36
ACPVOU,1930-729,Z729,42-0-29,36-44-23,31-39-18,24-32-11,19-27-6,15-23-2,9/17/1946
>>> print re.sub(r'(\d{1,2})/(\d{1,2})/\d{2}(\d{2})', r'\1-\2-\3', x)
ACPVBF,1930-729,Z729,12-16-14,6-10-08,1-5-03,44-48-46,39-43-41,35-39-37,29-33-31
ACPVGT,1930-729,Z729,25-29-27,19-23-21,14-18-16,7-11-09,2-6-04,48-2-0,42-46-44
ACPUQH,1930-729,Z729,32-40-19,26-34-13,21-29-8,14-22-1,9-17-46,5-13-42,49-7-36
ACPVOU,1930-729,Z729,42-0-29,36-44-23,31-39-18,24-32-11,19-27-6,15-23-2,9-17-46
I think the issue with your code is the trailing new line. My guess is it would fail for last word in each line. I would suggest you do a line.strip() before splitting