Last active
December 22, 2015 10:02
-
-
Save bravegnu/03acfa2473c97911d252 to your computer and use it in GitHub Desktop.
Find and Replace First Dot.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
matching = { | |
'{': '}', | |
'(': ')' | |
} | |
def find_first_dot(line): | |
paren_stack = [] | |
for pos, ch in enumerate(line): | |
if ch in matching: | |
paren_stack.append(ch) | |
else: | |
if paren_stack: | |
if ch == matching[paren_stack[-1]]: | |
paren_stack.pop() | |
else: | |
if ch == '.': | |
return pos | |
return -1 | |
for line in open("sample-text.txt"): | |
print line | |
pos = find_first_dot(line) | |
if pos != -1: | |
line = list(line) | |
line[pos] = '#' | |
line = "".join(line) | |
print line | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment