Re: [Tutor] images

2018-07-25 Thread Beck, Caroline
Hi! I am a student working on a game in which I am trying to use a gif as my 
turtle. Currently, the turtle moves around the screen with the arrow keys, but 
the gif does not move with it. Below is how I have tried to tell the code that 
the gif is the turtle, but the code is not reading that the gif is my turtle. I 
was wondering if I could have some help with making the gif in my code to read 
as my turtle.
Thank you!

Here is my code so far:
import random
import turtle as t

screen = t.Screen()
screen.setup(1250,900)
screen.bgpic("newbackground.gif")

screen.addshape("shark.gif")
t.shape("shark.gif")
shark = t.Turtle()
shark.speed(0)
shark.penup()
shark.hideturtle()

leaf = t.Turtle()
leaf_shape = ((0,0), (14,2), (18,6), (20,20),(6,18), (2,14))
t.register_shape('leaf', leaf_shape)
leaf.shape('leaf')
leaf.color('orange')
leaf.penup()
leaf.hideturtle()
leaf.speed(0)


game_started = False
text_turtle = t.Turtle()
text_turtle.write('Press SPACE to start', align='center', 
font=('Arial',16,'bold'))
text_turtle.hideturtle()

score_turtle = t.Turtle()
score_turtle.hideturtle()
score_turtle.speed(0)

def outside_window():
left_wall = -t.window_width() / 2
right_wall = t.window_width() / 2
top_wall = t.window_height() / 2
bottom_wall = -t.window_height() / 2
(x,y) = t.pos()
outside = \
x< left_wall or \
x> right_wall or \
y< bottom_wall or \
y> top_wall
return outside

def game_over():
t.penup()
t.hideturtle()
t.write('GAME OVER!', align='center', font=('Arial', 30, 'normal'))

def display_score(current_score):
score_turtle.clear()
score_turtle.penup()
x = (t.window_width()/2) - 50
y = (t.window_height()/2) - 50
score_turtle.setpos(x, y)
score_turtle.write(str(current_score), align='right',font=('Arial', 40, 
'bold'))

def place_leaf():
leaf.ht()
leaf.setx(random.randint(-200,200))
leaf.sety(random.randint(-200,200))
leaf.st()

def start_game():
global game_started
if game_started:
return
game_started = True

score=0
text_turtle.clear

shark_speed = 2
shark_length = 3
shark.shapesize(1, shark_length,1)
shark.showturtle()
display_score(score)
place_leaf()

while True:
shark.forward(shark_speed)
if shark.distance(leaf) < 20:
place_leaf()
#shark_length=t_length + 1
#shark.shapesize(1, t_length, 1)
shark_speed=shark_speed + 1
score = score + 10
display_score(score)
if outside_window():
game_over()
break

def move_up():
if shark.heading() == 0 or shark.heading() == 180:
shark.setheading(90)

def move_down():
if shark.heading() == 0 or shark.heading() == 180:
shark.setheading(270)

def move_left():
if shark.heading() == 90 or shark.heading() == 270:
shark.setheading(180)

def move_right():
if shark.heading() == 90 or shark.heading() == 270:
shark.setheading(0)
t.onkey(start_game, 'space')
t.onkey(move_up, 'Up')
t.onkey(move_right, 'Right')
t.onkey(move_down, 'Down')
t.onkey(move_left, 'Left')
t.listen()
t.mainloop()

From: Crystal Frommert 
Sent: Tuesday, July 24, 2018 1:11 PM
To: Beck, Caroline 
Subject: images

Try a gif image instead of png
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] How can I find a group of characters in a list of strings?

2018-07-25 Thread Jim

Linux mint 18 and python 3.6

I have a list of strings that contains slightly more than a million 
items. Each item is a string of 8 capital letters like so:


['MIBMMCCO', 'YOWHHOY', ...]

I need to check and see if the letters 'OFHCMLIP' are one of the items 
in the list but there is no way to tell in what order the letters will 
appear. So I can't just search for the string 'OFHCMLIP'. I just need to 
locate any strings that are made up of those letters no matter their order.


I suppose I could loop over the list and loop over each item using a 
bunch of if statements exiting the inner loop as soon as I find a letter 
is not in the string, but there must be a better way.


I'd appreciate hearing about a better way to attack this.

thanks,  Jim

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How can I find a group of characters in a list of strings?

2018-07-25 Thread Mats Wichmann
On 07/25/2018 05:50 PM, Jim wrote:
> Linux mint 18 and python 3.6
> 
> I have a list of strings that contains slightly more than a million
> items. Each item is a string of 8 capital letters like so:
> 
> ['MIBMMCCO', 'YOWHHOY', ...]
> 
> I need to check and see if the letters 'OFHCMLIP' are one of the items
> in the list but there is no way to tell in what order the letters will
> appear. So I can't just search for the string 'OFHCMLIP'. I just need to
> locate any strings that are made up of those letters no matter their order.
> 
> I suppose I could loop over the list and loop over each item using a
> bunch of if statements exiting the inner loop as soon as I find a letter
> is not in the string, but there must be a better way.
> 
> I'd appreciate hearing about a better way to attack this.

It's possible that the size of the biglist and the length of the key has
enough performance impacts that a quicky (untested because I don't have
your data) solution is unworkable for performance reasons.  But a quicky
might be to take these two steps:

1. generate a list of the permutations of the target
2. check if any member of the target-permutation-list is in the biglist.

Python sets are a nice way to check membership.

from itertools import permutations
permlist = [''.join(p) for p in permutations('MIBMMCCO', 8)]

if not set(permlist).isdisjoint(biglist):
print("Found a permutation of MIBMMCCO")

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How can I find a group of characters in a list of strings?

2018-07-25 Thread Steven D'Aprano
On Wed, Jul 25, 2018 at 06:50:56PM -0500, Jim wrote:
[...]
> I need to check and see if the letters 'OFHCMLIP' are one of the items 
> in the list but there is no way to tell in what order the letters will 
> appear. So I can't just search for the string 'OFHCMLIP'. I just need to 
> locate any strings that are made up of those letters no matter their order.

data = ['MIBMMCCO', 'YOWHHOY', 'ABCDEFG', 'HCMLIPOF', 'TUVWXYZ']

target = sorted('OFHCMLIP')
for pos, item in enumerate(data):
if sorted(item) == target:
print("found", pos, item)
break


-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How can I find a group of characters in a list of strings?

2018-07-25 Thread Martin A. Brown


> I have a list of strings that contains slightly more than a 
> million items. Each item is a string of 8 capital letters like so:
>
> ['MIBMMCCO', 'YOWHHOY', ...]
>
> I need to check and see if the letters 'OFHCMLIP' are one of the items in the
> list but there is no way to tell in what order the letters will appear. So I
> can't just search for the string 'OFHCMLIP'. I just need to locate any strings
> that are made up of those letters no matter their order.
>
> I suppose I could loop over the list and loop over each item using a bunch of
> if statements exiting the inner loop as soon as I find a letter is not in the
> string, but there must be a better way.
>
> I'd appreciate hearing about a better way to attack this.
>
> thanks,  Jim

If I only had to do this once, over only a million items (given 
today's CPU power), so I'd probably do something like the below 
using sets.  I couldn't tell from your text whether you wanted to 
see all of the entries in 'OFHCMLIP' in each entry or if you wanted 
to see only that some subset were present.  So, here's a script that 
will produce a partial match and exact match.

Note, I made a 9-character string, too because you had a 7-character 
string as your second sample -- mostly to point out that the 
9-character string satisfies an exact match although it sports an 
extra character.

  farm = ['MIBMMCCO', 'YOWHHOY', 'OFHCMLIP', 'OFHCMLIPZ', 'FHCMLIP', 'NEGBQJKR']
  needle = set('OFHCMLIP')
  for haystack in farm:
  partial = needle.intersection(haystack)
  exact = needle.intersection(haystack) == needle
  print(haystack, exact, ''.join(sorted(partial)))

On the other hand, there are probably lots of papers on how to do 
this much more efficiently.

-Martin

MIBMMCCO False CIMO
YOWHHOY False HO
OFHCMLIP True CFHILMOP
OFHCMLIPZ True CFHILMOP
FHCMLIP False CFHILMP
NEGBQJKR False 


-- 
Martin A. Brown
http://linux-ip.net/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How can I find a group of characters in a list of strings?

2018-07-25 Thread Jim

On 07/25/2018 07:43 PM, Steven D'Aprano wrote:

On Wed, Jul 25, 2018 at 06:50:56PM -0500, Jim wrote:
[...]

I need to check and see if the letters 'OFHCMLIP' are one of the items
in the list but there is no way to tell in what order the letters will
appear. So I can't just search for the string 'OFHCMLIP'. I just need to
locate any strings that are made up of those letters no matter their order.


data = ['MIBMMCCO', 'YOWHHOY', 'ABCDEFG', 'HCMLIPOF', 'TUVWXYZ']

target = sorted('OFHCMLIP')
for pos, item in enumerate(data):
 if sorted(item) == target:
 print("found", pos, item)
 break


Stephen,

Perfect, thank you.  I did remove the break because I thought I would 
get more than one hit. I got 196, more that I hoped for but now I have a 
better idea of what I am working with.


Regards,  Jim


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How can I find a group of characters in a list of strings?

2018-07-25 Thread Jim

On 07/25/2018 07:29 PM, Martin A. Brown wrote:



I have a list of strings that contains slightly more than a
million items. Each item is a string of 8 capital letters like so:

['MIBMMCCO', 'YOWHHOY', ...]

I need to check and see if the letters 'OFHCMLIP' are one of the items in the
list but there is no way to tell in what order the letters will appear. So I
can't just search for the string 'OFHCMLIP'. I just need to locate any strings
that are made up of those letters no matter their order.

I suppose I could loop over the list and loop over each item using a bunch of
if statements exiting the inner loop as soon as I find a letter is not in the
string, but there must be a better way.

I'd appreciate hearing about a better way to attack this.

thanks,  Jim


If I only had to do this once, over only a million items (given
today's CPU power), so I'd probably do something like the below
using sets.  I couldn't tell from your text whether you wanted to
see all of the entries in 'OFHCMLIP' in each entry or if you wanted
to see only that some subset were present.  So, here's a script that
will produce a partial match and exact match.

Note, I made a 9-character string, too because you had a 7-character
string as your second sample -- mostly to point out that the
9-character string satisfies an exact match although it sports an
extra character.


Sorry, that was a typo, they are all 8 characters in length.


   farm = ['MIBMMCCO', 'YOWHHOY', 'OFHCMLIP', 'OFHCMLIPZ', 'FHCMLIP', 
'NEGBQJKR']
   needle = set('OFHCMLIP')
   for haystack in farm:
   partial = needle.intersection(haystack)
   exact = needle.intersection(haystack) == needle
   print(haystack, exact, ''.join(sorted(partial)))

On the other hand, there are probably lots of papers on how to do
this much more efficiently.

-Martin


Thanks for your help. Steven came up with a solution that works well for me.

Regards,  Jim


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How can I find a group of characters in a list of strings?

2018-07-25 Thread Steven D'Aprano
On Wed, Jul 25, 2018 at 05:29:27PM -0700, Martin A. Brown wrote:

> If I only had to do this once, over only a million items (given 
> today's CPU power), so I'd probably do something like the below 
> using sets. 

The problem with sets is that they collapse multiple instances of 
characters to a single one, so that 'ABC' will match 'ABBBCC'. There's 
no indication that is what is required.


-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor