how to do draw pattern with python?

Discussion in 'Python' started by echo.hping@gmail.com, Sep 21, 2012.

  1. Guest

    may i know how to shift the bits using only looping and branching??

    x....x
    ..x..x.
    ...xx..
    ...xx..
    ..x..x.
    x....x

    xx....
    ...x..x
    ....xx.
    ....xx.
    ...x..x
    xx....

    ..xx...
    x..x..
    .....xx
    .....xx
    x..x..
    ..xx...

    etc..
    , Sep 21, 2012
    #1
    1. Advertising

  2. Laszlo Nagy Guest

    On 2012-09-21 15:36, wrote:
    > may i know how to shift the bits using only looping and branching??


    >
    > x....x
    > .x..x.
    > ..xx..
    > ..xx..
    > .x..x.
    > x....x


    What kinds of bits? What are these points and x-es anyway? Are they
    strings? Or binary data?

    I recommend this for reading:

    http://www.catb.org/esr/faqs/smart-questions.html
    Laszlo Nagy, Sep 21, 2012
    #2
    1. Advertising

  3. On 21/09/2012 14:36, wrote:
    > may i know how to shift the bits using only looping and branching??
    >
    > x....x
    > .x..x.
    > ..xx..
    > ..xx..
    > .x..x.
    > x....x
    >
    > xx....
    > ..x..x
    > ...xx.
    > ...xx.
    > ..x..x
    > xx....
    >
    > .xx...
    > x..x..
    > ....xx
    > ....xx
    > x..x..
    > .xx...
    >
    > etc..
    >


    You write some code and test it. If it doesn't work you cut and paste
    the smallest sample of code that can reproduce the problem, together
    with the full traceback if applicable, and you're likely to get plenty
    of answers.

    --
    Cheers.

    Mark Lawrence.
    Mark Lawrence, Sep 21, 2012
    #3
  4. Dave Angel Guest

    On 09/21/2012 09:36 AM, wrote:
    > may i know how to shift the bits using only looping and branching??


    Yes, show us your code, and what isn't working, and we'll try to help
    you complete the assignment. It'd probably also be good to specify the
    rest of the homework, like what version of what language it has to be
    implemented in.

    I don't see any bits, only strings of characters. And it seems to me
    that using slices is the most obvious mechanism for rotating
    fixed-length strings.


    > x....x
    > .x..x.
    > ..xx..
    > ..xx..
    > .x..x.
    > x....x
    >
    > xx....
    > ..x..x
    > ...xx.
    > ...xx.
    > ..x..x
    > xx....
    >
    > .xx...
    > x..x..
    > ....xx
    > ....xx
    > x..x..
    > .xx...
    >
    > etc..


    --

    DaveA
    Dave Angel, Sep 21, 2012
    #4
  5. Peter Otten Guest

    wrote:

    > may i know how to shift the bits using only looping and branching??


    import time

    data = """\
    x....x
    ..x..x.
    ...xx..
    ...xx..
    ..x..x.
    x....x

    """.splitlines()

    data = [line * 12 for line in data] # optional

    while True:
    print "\x1b[2J\x1b[0;0H" # optional
    for i, line in enumerate(data):
    print line
    data = line[1:] + line[:1]
    time.sleep(.1)

    Doing your homework since 2001 ;)
    Peter Otten, Sep 21, 2012
    #5
  6. 2012/9/21 Peter Otten <>:
    > wrote:
    >
    > print "\x1b[2J\x1b[0;0H" # optional


    Nice code : )

    Could you dissect that weird string for us?

    It isn't returning the cursor to (0,0), it's just like executing
    clear(1), and looks like those line coloring scape sequences for bash.

    Ismael


    --
    Do not let me induce you to satisfy my curiosity, from an expectation,
    that I shall gratify yours. What I may judge proper to conceal, does
    not concern myself alone.
    Ismael Farfán, Sep 21, 2012
    #6
  7. Peter Otten Guest

    Ismael Farfán wrote:

    > 2012/9/21 Peter Otten <>:
    >> wrote:
    >>
    >> print "\x1b[2J\x1b[0;0H" # optional

    >
    > Nice code : )
    >
    > Could you dissect that weird string for us?
    >
    > It isn't returning the cursor to (0,0), it's just like executing
    > clear(1), and looks like those line coloring scape sequences for bash.


    "\x1b[2J" or ESC [2J should clear the screen and

    "\x1b[1;1H" or ESC [1;1H should move the cursor to the origin (I got that
    wrong in the previous post)

    There may be other problems -- I stopped reading

    http://en.wikipedia.org/wiki/ANSI_escape_code

    as soon as I got the desired effect (scrolling) in konsole.
    Peter Otten, Sep 21, 2012
    #7
  8. On Sat, Sep 22, 2012 at 2:50 AM, Ismael Farfán <> wrote:
    > 2012/9/21 Peter Otten <>:
    >> wrote:
    >>
    >> print "\x1b[2J\x1b[0;0H" # optional

    >
    > Nice code : )
    >
    > Could you dissect that weird string for us?
    >
    > It isn't returning the cursor to (0,0), it's just like executing
    > clear(1), and looks like those line coloring scape sequences for bash.


    It's an ANSI escape sequence, or rather two of them. The first one
    clears the screen, the second returns you to 0,0. (Isn't that implicit
    in the 2J code? Maybe I'm misremembering.) But it depends on the
    terminal responding to them, and not all terminals do. For instance,
    most MUD clients parse only a very small subset of ANSI codes, eg
    color codes only.

    ChrisA
    Chris Angelico, Sep 21, 2012
    #8
  9. Ian Kelly Guest

    On Fri, Sep 21, 2012 at 10:50 AM, Ismael Farfán <> wrote:
    > 2012/9/21 Peter Otten <>:
    >> wrote:
    >>
    >> print "\x1b[2J\x1b[0;0H" # optional

    >
    > Nice code : )
    >
    > Could you dissect that weird string for us?
    >
    > It isn't returning the cursor to (0,0), it's just like executing
    > clear(1), and looks like those line coloring scape sequences for bash.


    They're called "ANSI escape codes". :)

    CSI 2J clears the screen.
    CSI 0;0H means "move the cursor to row 0, column 0". However, I don't
    think that's valid ANSI, as the coordinates are 1-based. Probably it
    should have been "\x1b[2J\x1b[1;1H".
    Ian Kelly, Sep 21, 2012
    #9
  10. On Sat, Sep 22, 2012 at 3:31 AM, Chris Angelico <> wrote:
    > It's an ANSI escape sequence, or rather two of them. The first one
    > clears the screen, the second returns you to 0,0. (Isn't that implicit
    > in the 2J code? Maybe I'm misremembering.)


    Ah. From Wikipedia:
    "If n is two, clear entire screen (and moves cursor to upper left on
    MS-DOS ANSI.SYS)."

    So adding \e[H is necessary.

    ChrisA
    Chris Angelico, Sep 21, 2012
    #10
  11. On 21/09/2012 15:29, Peter Otten wrote:
    > wrote:
    >
    >> may i know how to shift the bits using only looping and branching??

    >
    > import time
    >
    > data = """\
    > x....x
    > .x..x.
    > ..xx..
    > ..xx..
    > .x..x.
    > x....x
    >
    > """.splitlines()
    >
    > data = [line * 12 for line in data] # optional
    >
    > while True:
    > print "\x1b[2J\x1b[0;0H" # optional
    > for i, line in enumerate(data):
    > print line
    > data = line[1:] + line[:1]
    > time.sleep(.1)
    >
    > Doing your homework since 2001 ;)
    >


    I tried running your code but got this:-

    c:\Users\Mark>pattern.py
    File "C:\Users\Mark\pattern.py", line 22
    Doing your homework since 2001
    ^
    SyntaxError: invalid syntax

    What am I doing wrong?

    --
    Cheers.

    Mark Lawrence.
    Mark Lawrence, Sep 21, 2012
    #11
  12. On Fri, 21 Sep 2012 11:50:35 -0500, Ismael Farfán <>
    declaimed the following in gmane.comp.python.general:

    > 2012/9/21 Peter Otten <>:
    > > wrote:
    > >
    > > print "\x1b[2J\x1b[0;0H" # optional

    >
    > Nice code : )
    >
    > Could you dissect that weird string for us?


    http://www.termsys.demon.co.uk/vtansi.htm
    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
    Dennis Lee Bieber, Sep 21, 2012
    #12
  13. Hans Mulder Guest

    On 21/09/12 19:32:20, Ian Kelly wrote:
    > On Fri, Sep 21, 2012 at 10:50 AM, Ismael Farfán <> wrote:
    >> 2012/9/21 Peter Otten <>:
    >>> wrote:
    >>>
    >>> print "\x1b[2J\x1b[0;0H" # optional

    >>
    >> Nice code : )
    >>
    >> Could you dissect that weird string for us?
    >>
    >> It isn't returning the cursor to (0,0), it's just like executing
    >> clear(1), and looks like those line coloring scape sequences for bash.

    >
    > They're called "ANSI escape codes". :)
    >
    > CSI 2J clears the screen.
    > CSI 0;0H means "move the cursor to row 0, column 0". However, I don't
    > think that's valid ANSI, as the coordinates are 1-based. Probably it
    > should have been "\x1b[2J\x1b[1;1H".


    Yes, the coordinates are 1-base, so it should have been
    "\x1b[2J\x1b[1;1H". Or, since 1;1 is the default, "\x1b[2J\x1b[H".

    On my machine, clear(1) uses "\x1b[H\x1b[2J".

    Using clear(1) appears to be the most portable way to do it:

    import os, time

    data = """\
    x....x
    ..x..x.
    ...xx..
    ...xx..
    ..x..x.
    x....x

    """.splitlines()

    data = [line * 12 for line in data] # optional

    try:
    while True:
    os.system("clear") # optional
    for i, line in enumerate(data):
    print line
    data = line[1:] + line[:1]
    time.sleep(.1)
    except KeyboardInterrupt:
    pass



    Hope this helps,

    -- HansM
    Hans Mulder, Sep 22, 2012
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    17
    Views:
    1,837
    Chris Uppal
    Nov 16, 2005
  2. sunny
    Replies:
    1
    Views:
    451
    Salt_Peter
    Dec 7, 2006
  3. Pallav singh
    Replies:
    0
    Views:
    343
    Pallav singh
    Jan 22, 2012
  4. Pallav singh
    Replies:
    0
    Views:
    389
    Pallav singh
    Jan 22, 2012
  5. Pallav singh
    Replies:
    1
    Views:
    442
    Peter Remmers
    Jan 22, 2012
Loading...

Share This Page