import math


def wrap_always(text, width):
    """A simple word-wrap function that wraps text on exactly width characters.
       It doesn't split the text in words."""
    return '\n'.join([ text[width*i:width*(i+1)] \
                       for i in range(int(math.ceil(1.*len(text)/width))) ])


# TEST OF DISPLAY CODE

if __name__ == '__main__':
    import gridworld, util

    grid = gridworld.getCliffGrid3()
    print(grid.getStates())

    policy = dict([(state, 'east') for state in grid.getStates()])
    values = util.Counter(
        dict([(state, 1000.23) for state in grid.getStates()]))
    prettyPrintValues(grid, values, policy, currentState=(0, 0))

    stateCrossActions = [[(state, action)
                          for action in grid.getPossibleActions(state)]
                         for state in grid.getStates()]
    qStates = reduce(lambda x, y: x + y, stateCrossActions, [])
    qValues = util.Counter(
        dict([((state, action), 10.5) for state, action in qStates]))
    qValues = util.Counter(
        dict([((state, action), 10.5) for state, action in reduce(
                + delim.join([justify(str(item),width) for (item,width) in zip(row,maxWidths)]) \
                + postfix
        if separateRows or hasHeader: print >> output, rowSeparator; hasHeader=False
    return output.getvalue()
    
import math
def wrap_always(text, width):
    """A simple word-wrap function that wraps text on exactly width characters.
       It doesn't split the text in words."""
    return '\n'.join([ text[width*i:width*(i+1)] \
                       for i in xrange(int(math.ceil(1.*len(text)/width))) ])
    
    
# TEST OF DISPLAY CODE
                                
if __name__ == '__main__':
  import gridworld, util

  grid = gridworld.getCliffGrid3()
  print grid.getStates()
  
  policy = dict([(state,'east') for state in grid.getStates()])
  values = util.Counter(dict([(state,1000.23) for state in grid.getStates()]))
  prettyPrintValues(grid, values, policy, currentState = (0,0))

  stateCrossActions = [[(state, action) for action in grid.getPossibleActions(state)] for state in grid.getStates()]
  qStates = reduce(lambda x,y: x+y, stateCrossActions, [])
  qValues = util.Counter(dict([((state, action), 10.5) for state, action in qStates]))
  qValues = util.Counter(dict([((state, action), 10.5) for state, action in reduce(lambda x,y: x+y, stateCrossActions, [])]))
  prettyPrintQValues(grid, qValues, currentState = (0,0))