#!/usr/bin/python # ETHAN NEFF # 2014.23.01 # PROBLEM # Given that the text file 'input.txt' contains a rectangular block of dot # characters ('.') and two or more hash characters ('#'), write a program 'pather' # which writes out to 'output.txt' the same data with the two '#' characters # joined by asterisks ('*'). The command will be invoked like this: # VISION ''' cat input.txt ........................ ........................ ....#................... ........................ ........................ ........................ ........................ ..................#..... ........................ ........................ ........................ ........................ python pather.py input.txt output.txt cat output.txt ........................ ........................ ....#................... ....*................... ....*................... ....*................... ....*................... ....**************#..... ........................ ........................ ........................ ........................ ''' # CRITERIAS # 1 the program will not make any diagonals # 2 the program will only change direction once # 3 the program starts with a vertical line and then complete with a horizontal line # ASSUMPTIONS # 1 the program parses found hashtags from the top-left to the bottom-right # 2 the program makes the connection to the next sequential hashtag (not based on shortest distances) # 2 the program has strict argument inputs (programName inputFile outputFile) # 4 the program will overwrite the output file # HOW TO USE (command line) # python pather.py input.txt output.txt import sys # initial argument check if len(sys.argv) != 3: print "ERROR. Proper argument format: python pather.py inputFile.txt outputFile.txt" sys.exit() # 2D array to store the output file outputArray = [] # dictionary to store the found hashtags [y,x] hashDict = {} # READ THE INPUT FILE try: inputFile = open(sys.argv[1], 'r') except: print "ERROR. Could not read input file." sys.exit() # LOCATE THE HASHTAGS hashCount = 0; lineCount = 0; for line in inputFile: # clean the line line = line.rstrip('\n' ' ') # parse individual line characters charCount = 0 outputLine = [] for i in range(0,len(line)): outputLine.append(line[i]) if (line[i] == "#"): # find the hashes, and store their y and x coordinates in a dictionary hashDict[hashCount] = [lineCount,charCount] hashCount += 1 charCount += 1 lineCount += 1 outputArray.append(outputLine) inputFile.close() # FILL IN ASTERISKS INBETWEEN HASHTAGS if (len(hashDict) > 1): # go through each hashtag, and find the path to the next hashtag for i in range(0,len(hashDict)-1): # go vertical if (hashDict[i][0] != hashDict[i+1][0]): smallerY = hashDict[i+1][0]+1 largerY = hashDict[i][0]+1 if (smallerY > largerY): smallerY = hashDict[i][0]+1 largerY = hashDict[i+1][0]+1 for j in range(smallerY,largerY): outputArray[j][hashDict[i][1]] = '*' # then horizontal smallerX = hashDict[i+1][1]+1 largerX = hashDict[i][1] if (smallerX > largerX): smallerX = hashDict[i][1]+1 largerX = hashDict[i+1][1] for j in range(smallerX,largerX): outputArray[hashDict[i+1][0]][j] = '*' # WRITE THE OUTPUT FILE outputFile = open(sys.argv[2], "w") for line in outputArray: outputFile.write("".join(line) + "\n") outputFile.close()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question