Created
April 11, 2012 18:08
-
-
Save progval/2361040 to your computer and use it in GitHub Desktop.
Papou papa
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
Générateur de virelangue |
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
# -*- coding: utf8 -*- | |
def papoupapa(level, peut_etre_non_papou=False, peut_etre_pas_papa=True, est_un_pou=False, papa_connu=False, pluriel=False): | |
if level == 0: | |
tree = {} | |
else: | |
tree = {} | |
suffixe = 's' if pluriel else '' | |
if peut_etre_non_papou: | |
tree.update({u'papou' + suffixe: {}, u'pas papou' + suffixe: {}}) | |
if not est_un_pou: | |
tree.update({u'à poux': papoupapa(level-1, peut_etre_non_papou=True, est_un_pou=True, pluriel=True), u'pas à poux': {}}) | |
if peut_etre_pas_papa: | |
for type_ in ('papou', 'pas papou'): | |
tree.update({u'papa d\'un' + (' pou' if est_un_pou else ''): papoupapa(level-1, peut_etre_non_papou=True, est_un_pou=est_un_pou, papa_connu=True, pluriel=pluriel), u'pas papa' + suffixe: {}}) | |
if level > 1 and not papa_connu: | |
tree.update({u'à papa' + suffixe: papoupapa(level-1, peut_etre_non_papou=True, peut_etre_pas_papa=False, est_un_pou=est_un_pou, pluriel=pluriel)}) | |
return tree | |
def to_string(tree, concatener=True): | |
if tree == {}: | |
return '' | |
else: | |
strings = [] | |
for key, subtree in tree.items(): | |
subtree = to_string(subtree) | |
strings.append(key + subtree) | |
if concatener: | |
return ' ' + (' '.join(strings)) | |
else: | |
return strings | |
if __name__ == '__main__': | |
tree = papoupapa(5) | |
print u'En Papouasie, il y a les papous ' + (u', les papous '.join(to_string(tree, False))) + '.' |
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
# -*- coding: utf8 -*- | |
# modes : (l'ordre est important) | |
PAPOU = 0 | |
A_POUX = 1 | |
PAPA = 2 | |
HOMME = 10 | |
POU = 11 | |
def papoupapa(separateurs, prefixe=u'les papous', objet=HOMME, mode=A_POUX, pluriel=False): | |
mode_suivant = mode + 1 | |
if mode_suivant == 3: | |
mode_suivant = 0 | |
separateurs = separateurs[0:-1] | |
suffixe = 's' if pluriel else '' | |
if len(separateurs) == 0: | |
return [] | |
elif objet == POU and mode == A_POUX: # Un pou n'a pas de poux | |
return papoupapa(separateurs, prefixe, objet, mode_suivant, pluriel) | |
else: | |
chaines = [] | |
if mode == A_POUX: | |
chaines.append(u' pas à poux') | |
chaines.extend([(u' à poux' + x) for x in | |
papoupapa(separateurs, prefixe + u' à poux', POU, mode_suivant, True)]) | |
elif mode == PAPA: | |
chaines.append(u' pas papa' + suffixe) | |
chaines.extend([u' papa' + suffixe + ' d\'un' + x for x in | |
papoupapa(separateurs, prefixe + u' papa', objet, mode_suivant, True)]) | |
elif mode == PAPOU: | |
chaines.append(u' pas papou' + suffixe) | |
chaines.append(u' papou' + suffixe) | |
resultats = [] | |
for chaine in chaines: | |
souschaines = papoupapa(separateurs[0:-1], prefixe=prefixe+chaine, | |
objet=objet, mode=mode_suivant, pluriel=pluriel) | |
if souschaines == []: | |
resultats.append(chaine) | |
else: | |
resultats.extend([chaine + x for x in souschaines]) | |
return resultats | |
if __name__ == '__main__': | |
print 'En Papouasie, il y a les papous' + (', les papous'.join(papoupapa('\n/.;,'))) + '.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment