Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to do draw pattern with python?

52 views
Skip to first unread message

echo....@gmail.com

unread,
Sep 21, 2012, 9:36:58 AM9/21/12
to
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..

Laszlo Nagy

unread,
Sep 21, 2012, 9:54:29 AM9/21/12
to pytho...@python.org
On 2012-09-21 15:36, echo....@gmail.com 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

Mark Lawrence

unread,
Sep 21, 2012, 9:55:30 AM9/21/12
to pytho...@python.org
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.

Dave Angel

unread,
Sep 21, 2012, 10:00:33 AM9/21/12
to echo....@gmail.com, pytho...@python.org
On 09/21/2012 09:36 AM, echo....@gmail.com 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

Peter Otten

unread,
Sep 21, 2012, 10:29:31 AM9/21/12
to pytho...@python.org
echo....@gmail.com 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[i] = line[1:] + line[:1]
time.sleep(.1)

Doing your homework since 2001 ;)

Ismael Farfán

unread,
Sep 21, 2012, 12:50:35 PM9/21/12
to Peter Otten, pytho...@python.org
2012/9/21 Peter Otten <__pet...@web.de>:
> echo....@gmail.com 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.

Peter Otten

unread,
Sep 21, 2012, 1:29:02 PM9/21/12
to pytho...@python.org
Ismael Farfán wrote:

> 2012/9/21 Peter Otten <__pet...@web.de>:
>> echo....@gmail.com 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.

Chris Angelico

unread,
Sep 21, 2012, 1:31:54 PM9/21/12
to pytho...@python.org
On Sat, Sep 22, 2012 at 2:50 AM, Ismael Farfán <sulf...@gmail.com> wrote:
> 2012/9/21 Peter Otten <__pet...@web.de>:
>> echo....@gmail.com 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

Ian Kelly

unread,
Sep 21, 2012, 1:32:20 PM9/21/12
to pytho...@python.org
On Fri, Sep 21, 2012 at 10:50 AM, Ismael Farfán <sulf...@gmail.com> wrote:
> 2012/9/21 Peter Otten <__pet...@web.de>:
>> echo....@gmail.com 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".

Chris Angelico

unread,
Sep 21, 2012, 1:35:24 PM9/21/12
to pytho...@python.org
On Sat, Sep 22, 2012 at 3:31 AM, Chris Angelico <ros...@gmail.com> 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

Mark Lawrence

unread,
Sep 21, 2012, 3:55:02 PM9/21/12
to pytho...@python.org
On 21/09/2012 15:29, Peter Otten wrote:
> echo....@gmail.com 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[i] = 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.

Message has been deleted

Chris Angelico

unread,
Sep 21, 2012, 11:08:43 PM9/21/12
to pytho...@python.org
On Sat, Sep 22, 2012 at 5:55 AM, Mark Lawrence <bream...@yahoo.co.uk> wrote:
> 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?

The problem is the first non-blank line after "I tried running your
code". Note the path designators and the way they appear ready to fall
over backwards at a moment's notice, just like their host operating
system. Switch to my new HomeworkOS, freely downloadable from my
personal server running my own protocols and completely incompatible
with TCP/IP, and this will work perfectly!

*searches the Yellow Pages for cheek-tongue-removal services*

ChrisA

Hans Mulder

unread,
Sep 22, 2012, 3:34:58 PM9/22/12
to
On 21/09/12 19:32:20, Ian Kelly wrote:
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[i] = line[1:] + line[:1]
time.sleep(.1)
except KeyboardInterrupt:
pass



Hope this helps,

-- HansM
0 new messages