Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Code Critic Thread

Name: Anonymous 2013-01-04 23:49

# Post code, mercilessly attack code others have posted.

# Here's a function I wrote for a morpheme segmenter I'm writing.
# I think it's terrible, but I don't really want to write it again. Thoughts?

def get_min_concatenation_of(needle,haystack,inherited = []):
   
    # Gotta prevent an infinite loop
    assert "" not in haystack
   


    if needle == "":
        return inherited
    strings, compos = [""],[[]]
   
    def grow_candidates():
        ret_strings, ret_compos = [""],[[]]
        for posx,x in enumerate(strings):
            for y in haystack:
                new_string = x+y
                new_compo = compos[posx]+[y]
                if len(needle) < len(new_string):
                    pass
                if new_string == needle[:len(new_string)]:
                    if len(new_string) == len(needle):
                        return True,new_compo
                    ret_strings.append(new_string)
                    ret_compos.append(new_compo)
        return ret_strings,ret_compos
    
    waiting_for_solution = True

    while waiting_for_solution:
        strings,compos = grow_candidates()
        if strings == True:
            return compos
        if strings == []:
            return False

Name: Anonymous 2013-01-05 0:13


Don't change these.
Name: Email:
Entire Thread Thread List