2
def muchbetter(x):
    count_list = []
    for char in "abcdefghijklmnopqrstuvwxyz":
        count_list.append(x.lower().count(char))
    return tuple(count_list)

this function makes a list of when each letter appears in a sample text (x), what i want, is to turn the result of this function into a sortof "tower" of "*", so for example if (x)='AAAA' the list would show (4,0,0,0,0,0.. zeroes for letters tat don't appear) i want it to turn that result into a tower that looks like this

*
*
*
*

if (x)=AABBBB i want it to show something like this

 *
 *
**
**

so a tower of 2 for A and 4 for B, and so on if it were say x=(python is difficult) it would make towers with stars equal to the value of each letter in the list.

Anton Protopopov
  • 30,354
  • 12
  • 88
  • 93
A.raven
  • 105
  • 3
  • 9
  • Possible duplicate of [Pascal's triangle](http://stackoverflow.com/questions/1740499/pascals-triangle) –  Mar 02 '16 at 01:35
  • It looks like you have never accepted any answer for your questions. Please consider using the feature when you get an answer that solves your problem. – timgeb Mar 04 '16 at 09:59

4 Answers4

3

Something like that should work using list comprehension, zip, join:

def muchbetter(x):
    count_list = []
    for char in "abcdefghijklmnopqrstuvwxyz":
        count_list.append(x.lower().count(char))
    return tuple(count_list)

def print_stars(x):
    tup = muchbetter(x)
    stars = [' '*(max(tup) - s) + '*'*s for s in tup if s != 0] 
    print('\n'.join([''.join(a) for a in list(zip(*stars))]))


in_str = 'AABBBBCCCCC'
print_stars(in_str)
  *
 **
 **
***
***

in_str = 'AABBBB'
print_stars(in_str)
 *
 *
**
**

EDIT

In case you want to print horizontal towers you could use that:

def print_hor_stars(x):
    tup = muchbetter(x)
    stars = [' '*(max(tup) - s) + '*'*s for s in tup if s != 0] 
    print('\n'.join([''.join(a) for a in stars]))

in_str = 'AABBBB'
print_hor_stars(in_str)
  **
****

EDIT2

If you want your towers to be over the particular letter you could use function:

def print_stars_order(x):
    tup = muchbetter(x)
    stars = [' '*(max(tup) - s) + '*'*s for s in tup]
    print('\n'.join([''.join(a) for a in list(zip(*stars))]))
    print("abcdefghijklmnopqrstuvwxyz")

in_str='python is difficult'
print_stars_order(in_str)

        *                 
     *  *          *      
  ** * **  * ***  ***   * 
abcdefghijklmnopqrstuvwxyz
Anton Protopopov
  • 30,354
  • 12
  • 88
  • 93
0
def muchbetter(x):
    count_list = []
    for char in "abcdefghijklmnopqrstuvwxyz":
        count_list.append(x.lower().count(char))
    return tuple(count_list)

s = 'This is a sample sentence'

z = muchbetter(s)
print(s)
for i in range(max(z), 0, -1):
    for j in range(0, 26):
        if z[j] >= i:
            print('*', end='')
        else:
            print(' ', end='')
    print('')
print("abcdefghijklmnopqrstuvwxyz")

Or something like that ...

jfsturtz
  • 397
  • 4
  • 13
0

Here's one way.

from string import ascii_lowercase
from itertools import groupby

def make_row(group_lists):
    start_index = ord('a')
    row = [' '] * 26
    nonempties = filter(None, group_lists)
    for group_list in nonempties:
        char = group_list.pop()
        row[ord(char) - start_index] = '*'
    return "".join(row), nonempties

def histogram(chars):
    base_str = "==========================\n" + ascii_lowercase + "\n"
    grouped_chars = [list(y) for x, y in groupby(sorted(chars.lower()))]
    while grouped_chars:
        row, grouped_chars = make_row(grouped_chars)
        base_str = row + "\n" + base_str
    return base_str

This is a Python modification of a Haskell code golf problem for a similar histogram over the integers 0-9. Here's my Haskell code.

In [13]: print histogram("BBAAddZzyX")

** *                     *
** *                   ***
==========================
abcdefghijklmnopqrstuvwxyz


In [14]: print histogram("AABBBB")

 *                        
 *                        
**                        
**                        
==========================
abcdefghijklmnopqrstuvwxyz
ely
  • 74,674
  • 34
  • 147
  • 228
0
from collections import Counter
import string

cnt = Counter([x for x in 'thequicaaaaaaakbrownfoxjumpsoverthelzydogdddd'])
max_height = max(cnt.values())

display = []

for step in range(max_height,0,-1):
    line = []
    for letter in string.ascii_lowercase:
        if cnt[letter] == step:
            line.append('.')
        elif cnt[letter] > step:
            line.append('|')
        else:
            line.append(' ')
    display.append(line)

for x in display:
    print(''.join(x))
print(string.ascii_lowercase)

Tweaked the display a bit for fun-- output looks like:

.                         
|                         
|  .                      
|  |          .           
|  |.         |           
|  ||  .      |  . ..     
|..||..|......|..|.||.....
abcdefghijklmnopqrstuvwxyz
Doobeh
  • 9,280
  • 39
  • 32