python function project, histo function that return o -


histogram(xs): given list of integers named xs, return string which, when printed, shows histogram corresponding integers. use small "o" display symbol.

  • assume: xs list of non-negative integers; xs empty.
  • restrictions: none stated.
  • histogram([3,10,0,5]) → 'ooo\noooooooooo\n\nooooo'
  • histogram([]) → ''

this tried:

def histogram(xs):     last = len(xs)     histo = ''      number in xs:          while number > 0:              histo += 'o'             number -= 1              last -= 1             if last == 0:                 if histo == none:                     return ''                  return histo             histo += '\n' 

i may have had indentation wrong when edited question. had guess because pulling out of comment. also, might have had indentation problem. anyways, seems work (it's original code proper indentation):

def histogram(xs):     last = len(xs)     histo = ''      number in xs:         while number > 0:              histo += 'o'             number -= 1          last -= 1         if last == 0:             if histo == none:                 return ''              return histo         histo += '\n' 

here's more compact solution:

def histogram(xs):     output = ""     number in xs:         output += "o" * number + "\n"     return output 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -