debugpythonMinor
"File Fix-it" challenge
Viewed 0 times
fixfilechallenge
Problem
I'm learning Python and have found this problem from Google Code Jam:
How many
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each case begins with a line containing two integers N and M, separated by a space.
The next N lines each give the path of one directory that already exists on your computer. This list will include every directory already on your computer other than the root directory. (The root directory is on every computer, so there is no need to list it explicitly.)
The next M lines each give the path of one directory that you want to create.
Each of the paths in the input is formatted as in the problem statement above. Specifically, a path consists of one or more lower-case alpha-numeric strings (i.e., strings containing only the symbols 'a'-'z' and '0'-'9'), each preceded by a single forward slash. These alpha-numeric strings are never empty.
Output
For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the number of
I've solved it by writing code shown below, and it works correctly, but how can I make it faster?
```
import sys
def split_path(path_file,line_count_to_be_read):
for i in range(line_count_to_be_read):
# Get the Path line
line = path_file.readline()
# Skip the first slash
line = line[1:]
line = line.strip()
splited = line.split('/')
# make each subpaths from the source path
for j in range(1,len(splited)+1):
joined = "/".join(splited[:j])
yield joined
def main():
file_name = ""
try:
file_name = sys.argv[1]
except IndexError:
file_name = "A-small-practice.in"
with open(file_name) as path_file:
# skip the first line
line = path_file.readline()
total_testcases = int(line) # Number of Test Cases
How many
mkdir commands does it take to construct a given directory tree:Input
The first line of the input gives the number of test cases, T. T test cases follow. Each case begins with a line containing two integers N and M, separated by a space.
The next N lines each give the path of one directory that already exists on your computer. This list will include every directory already on your computer other than the root directory. (The root directory is on every computer, so there is no need to list it explicitly.)
The next M lines each give the path of one directory that you want to create.
Each of the paths in the input is formatted as in the problem statement above. Specifically, a path consists of one or more lower-case alpha-numeric strings (i.e., strings containing only the symbols 'a'-'z' and '0'-'9'), each preceded by a single forward slash. These alpha-numeric strings are never empty.
Output
For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the number of
mkdir you need.I've solved it by writing code shown below, and it works correctly, but how can I make it faster?
```
import sys
def split_path(path_file,line_count_to_be_read):
for i in range(line_count_to_be_read):
# Get the Path line
line = path_file.readline()
# Skip the first slash
line = line[1:]
line = line.strip()
splited = line.split('/')
# make each subpaths from the source path
for j in range(1,len(splited)+1):
joined = "/".join(splited[:j])
yield joined
def main():
file_name = ""
try:
file_name = sys.argv[1]
except IndexError:
file_name = "A-small-practice.in"
with open(file_name) as path_file:
# skip the first line
line = path_file.readline()
total_testcases = int(line) # Number of Test Cases
Solution
Rather than using a list, consider approaching the problem from a graph perspective. Build a tree from the root directory using the existing directories, then traverse it with the new directories, and output a result line each time you have to add a leaf to the graph. This should be a little faster than comparing your two sets.
Context
StackExchange Code Review Q#1283, answer score: 5
Revisions (0)
No revisions yet.