#Author: Bo Lee #Last Modified: January 1, 2014 #This is the Markov generator. It takes in a lists of #strings and then creates a single dictionary. The #dictionary takes in the current move as a key and #outputs a list of possible next moves import random def createMovesDict (choreoL): """Input: chore0L is list of different choreographies for a style of dance. Each choreo is represented by a list of moves. Each move is represented by a string. output: movesDict is a dictionary. Keys are moves and the output is a list of possible next moves.""" movesDict = {} #The first move of a routine will be marked by # the '$' key. movesDict['$']=[] for i in range(len(choreoL)): movesDict['$'].append(choreoL[i][0]) #The jth move of the choreo will be the # current move for j in range(len(choreoL[i])-1): if choreoL[i][j] in movesDict: #Add the following move from the # choreo to the dictionary under # the current move movesDict[choreoL[i][j]].append( choreoL[i][j+1]) #This is a new current move else: #We need a new key movesDict[choreoL[i][j]]=[choreoL[i][j+1]] return movesDict def generateChoreo(movesDict, minNum, maxNum, loop=True): """Randomly generates choregraphy based on the existing choregraphy represented in the movesDict. The choreography will be at least minNum moves long, and at most maxNum moves long. It will try to end with the same move that it started with in order to allow for looping. Choreography will be returned as a list of strings Note: If loop is set to false, then the choreo will be exactly as long as maxNum and minNum is irrelevant.""" #Start with the first move choreography = [random.choice(movesDict['$'])] #Add at least minNum moves to the choreo for i in xrange(minNum-1): choreography.append(random.choice(movesDict[choreography[-1]])) while len(choreography)<=maxNum: if loop and choreography[0]==choreography[-1]: break choreography.append(random.choice(movesDict[choreography[-1]])) return choreography testMode=True if testMode: L=[['a','b','c','a','b','a'],['a','c','d','b','a','c','a']] d = createMovesDict(L) print d print generateChoreo(d,5,10)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question