python - Given a String, What is the Length of the One of the Longest WFF in Polish Notation? -


i'm trying to write version of popular count-a-wff section of wff 'n proof game (no copyright infringement intended) in python. alright, not popular.

i think have , running desired case of 4 letter string.

def maximum_string(s): if cs(s) == true:     return len(s) elif len(s) == 2:     l1 = [cs(s[0]), cs(s[1])]     if true in l1:         return len(s) - 1     else:         return 0 elif len(s) == 3:     first = s[0] + s[1]     second = s[0] + s[2]     third = s[1] + s[2]     l1 = [cs(first), cs(second), cs(third)]     if true in l1:         return len(s) - 1     l2 = [cs(s[0]), cs(s[1]), cs(s[2])]     if true in l2:         return len(s) - 2     else:         return 0 elif len(s) == 4:     first = s[0]+s[1]+s[2]     second = s[0]+s[1]+s[3]     third = s[1]+s[2]+s[3]     fourth = s[0]+s[2]+s[3]     l1 = [cs(first), cs(second), cs(third), cs(fourth)]     if true in l1:         return 3     first = s[0] + s[1]     second = s[0] + s[2]     third = s[0] + s[3]     fourth = s[1] + s[2]     fifth = s[1] + s[3]     sixth = s[2] + s[3]     l2 = [cs(first), cs(second), cs(third), cs(fourth), cs(fifth), cs(sixth)]     if true in l2:         return 2     first = s[0]     second = s[1]     third = s[2]     fourth = s[3]     l3 = [cs(first), cs(second), cs(third), cs(fourth)]     if true in l3:         return 1     else:         return 0  def cs(string): global length_counter, counter, letter counter = 1 length_counter = 0 letters_left = len(string) while letters_left != 0 , length_counter < len(string):     letter = string[length_counter]     if letter == 'c' or letter == 'a' or letter == 'k' or letter == 'e' or letter == "k":         counter += 1      elif letter == 'n':         counter += 0     else:         counter -= 1       length_counter += 1     letters_left -= 1 if counter == 0 , len(string) == length_counter:     return true else:     return false 

the maximum_string helper function intended to, given string s, find length of 1 of longest possible wffs can make letters of s. of course, can continue pattern have maximum_string helper function length of 13. but, combinatorial explosion evident. thus, there more elegant way finish off maximum string helper function?

in effect 1 of functions had earlier return distance of how far away string having permutation in polish notation. surprisingly simpler fix expected. here's looking for:

def maximum_string(string):     global length_counter, counter, letter     counter = 1     length_counter = 0     letters_left = len(string)     while letters_left != 0 , length_counter < len(string):         letter = string[length_counter]         if letter == 'c' or letter == 'a' or letter == 'k' or letter == 'e' or letter == "k":             counter += 1          elif letter == 'n':             counter += 0         else:             counter -= 1           length_counter += 1         letters_left -= 1     if ('p' in string) or ('q' in string) or ('r' in string) or ('s' in string) or ('t' in string) or ('u' in string):         return len(string) - abs(counter)     else:         return 0 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -