Tuesday, 15 June 2010

How to create the built-in function count, without using it. python -



How to create the built-in function count, without using it. python -

i exact same result code:

def histogram(s): d = {} w in s: d[w] = s.count(w) k in sorted(d): print (k + ': ' + str(d[k]))

but without using built-in functions. want utilize len() , range() , chr() , ord().

the result of programme when typing in mississippi is:

m: 1 i: 4 p: 2 s: 4

to clear upp!

write function histogram(s) takes string parameter , returns list histogram on numbers of characters.

and function histprint should implemented, takes list function histogram(s) have returned , writes table on screen, containing characters in string. result should like:

>>> h=histogram('mississippi') >>> histprint(h) m: 1 i: 4 p: 2 s: 4

no built-in functions!

count , counter ways go next should work fine too.

my = "stackoverflow" d={} l in my: d[l] = d.get(l,0) + 1 print d {'a': 1, 'c': 1, 'e': 1, 'f': 1, 'k': 1, 'l': 1, 'o': 2, 's': 1, 'r': 1, 't': 1, 'w': 1, 'v': 1}

python function count built-in

No comments:

Post a Comment