-3

Can anyone tell me why num_chars and num_rows have to be the same?

from ctypes import *

num_chars = 8
num_rows = 8
num_cols = 6

buffer = create_string_buffer (num_chars*num_rows*num_cols+num_chars)

for char in range(num_chars):
        for row in range(num_rows):
                for col in range(num_cols):
                        if char == num_chars-1 and col == num_cols-1:
                                buffer[row*num_rows*num_cols+char*num_cols+col+row] = '|'
                                buffer[row*num_rows*num_cols+char*num_cols+col+row+1] = '\n'
                        elif col == num_cols-1:
                                buffer[row*num_rows*num_cols+char*num_cols+col+row] = '|'
                        else:
                                buffer[row*num_rows*num_cols+char*num_cols+col+row] = ('.', '*')[char>row]

print buffer.value

The output

.....|*****|*****|*****|*****|*****|*****|*****|
.....|.....|*****|*****|*****|*****|*****|*****|
.....|.....|.....|*****|*****|*****|*****|*****|
.....|.....|.....|.....|*****|*****|*****|*****|
.....|.....|.....|.....|.....|*****|*****|*****|
.....|.....|.....|.....|.....|.....|*****|*****|
.....|.....|.....|.....|.....|.....|.....|*****|
.....|.....|.....|.....|.....|.....|.....|.....|

And now changing num_chars to 15.

.....|*****|*****|*****|*****|*****|*****|*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
.....|*****|*****|*****|*****|*****|*****|*****|
8
  • Can you please explain what you are trying to do? May 16, 2009 at 15:03
  • Well, I thought the output would be enough. You have to play with this and plug in different numbers to see what it is doing. I'll do that and plug in the output.
    – Scott
    May 16, 2009 at 15:12
  • Why are you using ctypes for this? May 16, 2009 at 15:18
  • For a mutable character buffer.
    – Scott
    May 16, 2009 at 15:23
  • 1
    The source and the output is not sufficient as You have stated that it's not working right. You want us to fix it but You don't say what exacly it should do, only that it does for the first example and doesn't for the second. PLEEEEASE be more descriptive. Help us help You. May 16, 2009 at 15:28

2 Answers 2

5

You said you are using ctypes because you want mutable char buffer for this. But you can get the output you want from list comprehension

num_chars = 5
num_rows = 8
empty = ['.' * num_chars]
full = ['*' * num_chars]
print '\n'.join(
    '|'.join(empty * (i + 1) + (num_rows - i - 1) * full)
    for i in xrange(num_rows)
)

.....|*****|*****|*****|*****|*****|*****|*****
.....|.....|*****|*****|*****|*****|*****|*****
.....|.....|.....|*****|*****|*****|*****|*****
.....|.....|.....|.....|*****|*****|*****|*****
.....|.....|.....|.....|.....|*****|*****|*****
.....|.....|.....|.....|.....|.....|*****|*****
.....|.....|.....|.....|.....|.....|.....|*****
.....|.....|.....|.....|.....|.....|.....|.....

EDIT

I'll show you how you can use list comprehensions to draw whatever char bitmap you want to draw. The idea is simple. Build a boolean array with True in the places you want to print the character and False otherwise. And just use the 'or' trick to print the right character. This example will build a chess like board. You can use the same concept to draw any shape you want.

rows = 5
cols = 6
char = '#'
empty = '.'
bitmap = [[ (i + j)%2 == 0 for i in xrange(cols)] for j in xrange(rows)]
print '\n'.join(
    '|'.join(bitmap[j][i] * char or empty for i in xrange(cols))
    for j in xrange(rows)
)
14
  • I'm a little confused by this. I don't get the same output. Why isn't num_cols used? Yet you have that many cols in your characters.
    – Scott
    May 16, 2009 at 15:35
  • If you copy and paste my code it'll produce the exact output in the answer May 16, 2009 at 15:37
  • What do you mean by num_cols? you need to be more descriptive. I'm assuming that num_chars means the number of chars in every |.....| May 16, 2009 at 15:39
  • WelI get this: ........|********|********|********|********|********|********|******** ........|........|********|********|********|********|********|******** ........|........|........|********|********|********|********|******** ........|........|........|........|********|********|********|******** ........|........|........|........|........|********|********|******** ........|........|........|........|........|........|********|******** ........|........|........|........|........|........|........|******** ........|........|........|........|........|........|........|........
    – Scott
    May 16, 2009 at 15:39
  • Bah. Well the characters are 8 spaces wide.
    – Scott
    May 16, 2009 at 15:39
1

There we go. I had rownum_rows instead of rownum_chars I must need a Dr Pepper. And by the way, this wasn't homework. It's for an LCD project.

num_chars = 10
num_rows = 8
num_cols = 6

buffer = create_string_buffer (num_chars*num_rows*num_cols+num_chars)

for char in range(num_chars):
        for row in range(num_rows):
                for col in range(num_cols):
                        if char == num_chars-1 and col == num_cols-1:
                                buffer[row*num_chars*num_cols+char*num_cols+col+row] = '|'
                                buffer[row*num_chars*num_cols+char*num_cols+col+row+1] = '\n'
                        elif col == num_cols-1:
                                buffer[row*num_chars*num_cols+char*num_cols+col+row] = '|'
                        else:
                                buffer[row*num_chars*num_cols+char*num_cols+col+row] = ('.', '*')[char>row]

print repr(buffer.raw)
print buffer.value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.