Created
May 12, 2013 13:40
-
-
Save xtoss/5563596 to your computer and use it in GitHub Desktop.
Python solution for google code jam 2013 Round 1C - Problem A - Consonants: https://code.google.com/codejam/contest/2437488/dashboard#s=p0
This file contains hidden or 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
""" | |
Used to solve google code jam 2013 Round 1C - Problem A - Consonants | |
Owner: Jamie Xu - [email protected] | |
""" | |
def Consonants(): | |
in_f = open('A-small-attempt0.in', 'r') | |
out_f = open('output.txt', 'w') | |
num_of_case = int(in_f.readline().rstrip('\n')) | |
for i in range(1, num_of_case+1): | |
solve_case(in_f, out_f, i) | |
N = 0 | |
def solve_case(in_f, out_f, case_index): | |
print "start handling case #{}".format(case_index) | |
case_info = in_f.readline().rstrip('\n').split(" ") | |
name = case_info[0] | |
N = int(case_info[1]) | |
cleaned_name = clean_name(name) | |
n = get_n(cleaned_name, N) | |
out_f.write("Case #{}: {}\n".format(case_index, n)) | |
def get_n(name, N): | |
length = len(name) | |
if length < N: | |
return 0 | |
temp = 0 | |
for i in xrange(N, length+1): | |
if is_valid(name[0:i], N): | |
temp = temp + 1 | |
return temp + get_n(name[1:], N) | |
def is_valid(str, N): | |
parts = str.split("1") | |
for part in parts: | |
if len(part) >= N: | |
return True | |
return False | |
def clean_name(name): | |
return name.replace("a", "1").replace("e", "1").replace("i", "1").replace("o", "1").replace("u", "1") | |
if __name__ == '__main__': | |
Consonants() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unfortunately, this only solved the small data set.
Current solution is calculating how many valid ones are starting with current character then add the total to the recursive function with current character being removed.
I was thinking about a potential faster way but failed to complete:
name_piece = name.split("1")
for piece in name_piece
, calculate many valid ones are reaching the boarder of the piece, i.e. those having the ability to "grow" to the outside of the current piece.num_gained_from_growing
will be something likelen(name) - len(piece)
Failed to find a formular for this :<