I made an attempt to solve Uncle Bobs bowling game kata (http://www.butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata) but didn't really find a solution that felt pythonic enough.
This solution is more or less an adaptation of Martins C++ solution and uses array indexes to calculate scores for strikes and spares. It works but doesn't feel quite as pythonic as I would like it to be.
class Game():
def __init__(self):
self.rolls = []
def roll(self, pins):
self.rolls.append(pins)
def score_c(self):
total_score = 0
frame_index = 0
for frame in range(10):
if self.rolls[frame_index] == 10:
total_score += 10 + self.rolls[frame_index + 1] + self.rolls[frame_index + 2]
frame_index +=1
elif self.rolls[frame_index] + self.rolls[frame_index + 1] == 10:
total_score += 10 + self.rolls[frame_index + 1]
frame_index += 2
else:
total_score += self.rolls[frame_index] + self.rolls[frame_index + 1]
frame_index += 2
return total_score
I could have used convenience functions for strike and spare conditions, but you get the picture.
But I thought there must be a way to do it without accessing the rolls array directly though indexes. That feels like a very c-like way of doing it and incrementing frame_index directly definitely doesn't feel right in python. So I think there must be a neater way to do it. I made an attempt below which didn't really work for perfect games.
This one use a generator to provide frames which felt pretty neat but it also meant that 0 had to be added for strikes to make complete 2 roll frames.
class Game():
def __init__(self):
self.rolls = []
def _frame_iterator(self):
for i in range(0, 20, 2):
yield (self.rolls[i], self.rolls[i+1])
def roll(self, pins):
self.rolls.append(pins)
if pins == 10:
self.rolls.append(0)
def score(self):
total_score = 0
spare = False
strike = False
for frame in self._frame_iterator():
if spare:
total_score += frame[0]
spare = False
if strike:
total_score += frame[1]
strike = False
if frame[0] + frame[1] == 10:
spare = True
if frame[0] == 10:
strike = True
total_score += frame[0] + frame[1]
return total_score
My questions are basically, has anyone solved the bowling kata in Python in a different and more pythonic way than uncle bobs C++ solution? And suggestions how to improve on my attempt?
This is definitely a different approach (implementing most of the rules in roll(), instead of score()), and I think it's pretty pythonic too.
class Game(object):
def __init__(self):
self._score = [[]]
def roll(self, pins):
# start new frame if needed
if len(self._score[-1]) > 1 or 10 in self._score[-1]:
self._score.append([])
# add bonus points to the previous frames
for frame in self._score[-3:-1]:
if sum(frame[:2]) >= 10 and len(frame) < 3:
frame.append(pins)
# add normal points to current frame
for frame in self._score[-1:10]:
frame.append(pins)
def score(self):
return sum(sum(x) for x in self._score)
The main idea here is instead of storing all rolls in a single list, to make a list of frames that contains a list of rolls (and bonus points) for each frame.
What makes it pythonic is for example the generator expression in the score method.
Another pythonic example is the use of list slices. A previous version of middle part of roll() I did looked like:
for i in [2, 3]:
if len(self._score) >= i:
sum(self._score[-i][:2]) >= 10 and len(self._score[-i]) < 3:
self._score[-i].append(pins)
The elegant thing of the current version using a list slice is that you don't have to check whether the list is long enough to look 1 or 2 frames back. Moreover, you get a nice local variable (frame = self._score[-i]) for free, without having to dedicate a separate line for it.
Related
I made a while loop for Maya python study. It works well, but it is redundant and I think there must be a way to shorten them better or make it looks good. Can you guys give me a suggestion about what I should do? Do you think using another def function would be better than this?
def addWalls(self, length, width, floorNum, bboxScale):
# count variables
count = 1
floorCount = 1
# length loop
while count < length:
# Adding floors on wall
while floorCount < floorNum:
cmds.duplicate(instanceLeaf=True)
cmds.xform(relative=True, translation=[0, 0, bboxScale[2]])
floorCount += 1
floorCount = 1
# Adding next wall
cmds.duplicate(instanceLeaf=True)
cmds.xform(relative=True, translation=[0, -bboxScale[1], -bboxScale[2] * (floorNum - 1)])
count += 1
# Final adding floors
if count == length:
while floorCount < floorNum:
cmds.duplicate(instanceLeaf=True)
cmds.xform(relative=True, translation=[0, 0, bboxScale[2]])
floorCount += 1
floorCount = 1
When I run your script it creates a grid of objects like this:
So if all it needs to do is make a grid of objects then your assumption is right, it makes no sense using a while loop. In fact it's really easy to do it with 2 for loops that represent the "wall's" width and height:
import maya.cmds as cmds
spacing = 5
width_count = 15
height_count = 15
for z in range(width_count):
for y in range(height_count):
cmds.duplicate(instanceLeaf=True)
cmds.xform(ws=True, t=[0, y * spacing, z * spacing])
It will yield the same result with a much shorter and readable script. If you want more flexibility in there it would only take simple tweaks.
I'm programming game pong in python and I wan't the ball to be faster everytime it bounces of a bat. So I tried to add
global SPEED
SPEED + 25
into a function higher_speed which will trigger everytime when the ball bounces of the bat.
short version of game code here:
...
BALL_SIZE = 20
BAT_WIDTH = 10
BAT_HEIGHT = 100
SPEED = 250 # (in pixels per second)
BAT_SPEED = SPEED * 1.5 # (in pixels per second)
...
def higher_speed(SPEED, x):
x = 25
global SPEED
SPEED + x
return SPEED
# bounce left
if ball_position[0] < BAT_WIDTH + BALL_SIZE / 2:
if bat_min < bat_position[0] < bat_max:
# bat bounces ball
BALL_SPEED[0] = abs(BALL_SPEED[0])
global SPEED
higher_speed()
else:
# bat hadn't bounced the ball, player loses
score[1] += 1
reset()
# bounce right
if ball_position[0] > WIDTH7777 - (BAT_WIDTH + BALL_SIZE / 2):
if bat_min < bat_position[1] < bat_max:
BALL_SPEED[0] = -abs(BALL_SPEED[0])
higher_speed()
else:
score[0] += 1
reset()
...
Please help. I appreciate your time :).
Several things here:
First of all, the value is not being changed because it is not assigned in the first place, your function should look like this:
def higher_speed(SPEED, x):
x=25
global SPEED
SPEED += x
Second, if you're overwriting x at the beginning of the function and using SPEED as global, why pass it?:
def higher_speed():
global SPEED
SPEED += 25
Third, according to Python's PEP8 standards, capitalized words are only for constants, it would be a good use to the speed increase, so it should look like this:
SPEED_INCREASE = 25
def higher_speed():
global speed
speed += SPEED_INCREASE
And last, in general is a bad idea using global variables you can check this article or google it, so try to avoid it, then it should look like this:
def higher_speed(speed):
return speed+SPEED_INCREASE
speed = higher_speed(speed)
or you could set this inline:
speed += SPEED_INCREASE
I hope it helped!
Here are 2 functions that do exactly the same thing, but does anyone know why the one using the count() method is much faster than the other? (I mean how does it work? How is it built?)
If possible, I'd like a more understandable answer than what's found here : Algorithm used to implement the Python str.count function
or what's in the source code : https://hg.python.org/cpython/file/tip/Objects/stringlib/fastsearch.h
def scoring1(seq):
score = 0
for i in range(len(seq)):
if seq[i] == '0':
score += 1
return score
def scoring2(seq):
score = 0
score = seq.count('0')
return score
seq = 'AATTGGCCGGGGAG0CTTC0CTCC000TTTCCCCGGAAA'
# takes 1min15 when applied to 100 sequences larger than 100 000 characters
score1 = scoring1(seq)
# takes 10 sec when applied to 100 sequences larger than 100 000 characters
score2 = scoring2(seq)
Thanks a lot for your reply
#CodeMonkey has already given the answer, but it is potentially interesting to note that your first function can be improved so that it runs about 20% faster:
import time, random
def scoring1(seq):
score=0
for i in range(len(seq)):
if seq[i]=='0':
score+=1
return score
def scoring2(seq):
score=0
for x in seq:
score += (x =='0')
return score
def scoring3(seq):
score = 0
score = seq.count('0')
return score
def test(n):
seq = ''.join(random.choice(['0','1']) for i in range(n))
functions = [scoring1,scoring2,scoring3]
for i,f in enumerate(functions):
start = time.clock()
s = f(seq)
elapsed = time.clock() - start
print('scoring' + str(i+1) + ': ' + str(s) + ' computed in ' + str(elapsed) + ' seconds')
test(10**7)
Typical output:
scoring1: 5000742 computed in 0.9651326495293333 seconds
scoring2: 5000742 computed in 0.7998054195159483 seconds
scoring3: 5000742 computed in 0.03732172598339578 seconds
Both of the first two approaches are blown away by the built-in count().
Moral of the story: when you are not using an already optimized built-in method, you need to optimize your own code.
Because count is executed in the underlying native implementation. The for-loop is executed in slower interpreted code.
For a project I'm working on I'm trying to write some code to detect collisions between non-point particles in a 2D space. My goal is to try to detect collision for a few thousand particles at least a few times per time step which I know is a tall order for python. I've followed this blog post which implements a quadtree to significantly reduce the number pairwise checks I need to make. So where I believe I'm running into issues is this function:
def get_index(self, particle):
index = -1
bounds = particle.aabb
v_midpoint = self.bounds.x + self.bounds.width/2
h_midpoint = self.bounds.y + self.bounds.height/2
top_quad = bounds.y < h_midpoint and bounds.y + bounds.height < h_midpoint
bot_quad = bounds.y > h_midpoint
if bounds.x < v_midpoint and bounds.x + bounds.width < v_midpoint:
if top_quad:
index = 1
elif bot_quad:
index = 2
elif bounds.x > v_midpoint:
if top_quad:
index = 0
elif bot_quad:
index = 3
return index
This function from my initial profiling is the bottleneck and I need it to be blistering fast, because of its high call count. Originally I was just supplying an object axis-aligned bounding box which was working almost at the speed I needed, then realized I had no way of determining which particles may actually be colliding. So now I'm passing in a list of particles to my quadtree constructor and just using the class attribute aabb to get my bounds.
Is there someway I could pass something analogues to a object pointer instead of the whole object? Additionally are there other recommendation to optimize this above code?
Don't know if they'll help, but here are a few ideas:
v_midpoint and h_midpoint are re-calculated for every particle added to the quadtree. Instead, calculate them once when a Quad is initialized, then access them as attributes.
I don't think the and is needed in calculating top_quad. bounds.x + bounds.width < v_midpoint is sufficient. Same for left_quad.
Do the simpler checks first and only do the longer one if necessary: bounds.x > v_midpoint vs. bounds.x + bounds.width < v_midpoint
bounds.x + bounds.width is calculated multiple times for most particles. Maybe bounds.left and bounds.right can be calculated once as attributes of each particle.
No need to calculate bot_quad if top_quad is True. Or visa-versa.
Maybe like this:
def get_index(self, particle):
bounds = particle.aabb
# right
if bounds.x > self.v_midpoint:
# bottom
if bounds.y > self.h_midpoint:
return 3
# top
elif bounds.y + bounds.height < self.h_midpoint:
return 0
# left
elif bounds.x + bounds.width < self.v_midpoint:
# bottom
if bounds.y > self.h_midpoint:
return 2
# top
elif bounds.y + bounds.height < self.h_midpoint:
return 1
return -1
I am trying to figure out how to take in a list of numbers and sort them into certain categories such as 0-10, 10-20, 20-30 and up to 90-100 but I have the code started, but the code isn't reading in all the inputs, but only the last one and repeating it. I am stumped, anyone help please?
def eScores(Scores):
count0 = 0
count10 = 0
count20 = 0
count30 = 0
count40 = 0
count50 = 0
count60 = 0
count70 = 0
count80 = 0
count90 = 0
if Scores > 90:
count90 = count90 + 1
if Scores > 80:
count80 = count80 + 1
if Scores > 70:
count70 = count70 + 1
if Scores > 60:
count60 = count60 + 1
if Scores > 50:
count50 = count50 + 1
if Scores > 40:
count40 = count40 + 1
if Scores > 30:
count30 = count30 + 1
if Scores > 20:
count20 = count20 + 1
if Scores > 10:
count10 = count10 + 1
if Scores <= 10:
count0 = count0 + 1
print count90,'had a score of (90 - 100]'
print count80,'had a score of (80 - 90]'
print count70,'had a score of (70 - 80]'
print count60,'had a score of (60 - 70]'
print count50,'had a score of (50 - 60]'
print count40,'had a score of (40 - 50]'
print count30,'had a score of (30 - 40]'
print count20,'had a score of (20 - 30]'
print count10,'had a score of (10 - 20]'
print count0,'had a score of (0 - 10]'
return eScores(Scores)
Each time eScores is called is sets all the counters (count10, count20) back to zero. So only the final call has any effect.
You should either declare the counters as global variables, or put the function into a class and make the counters member variables of the class.
Another problem is that the function calls itself in the return statement:
return eScores(Scores)
Since this function is (as I understand it) supposed to update the counter variables only, it does not need to return anything, let alone call itself recursively. You'd better remove the return statement.
One thing you're making a mistake on is that you're not breaking out of the whole set of if's when you go through. For example, if you're number is 93 it is going to set count90 to 1, then go on to count80 and set that to one as well, and so on until it gets to count10.
Your code is repeating because the function is infintely recursive (it has no stop condition). Here are the relevant bits:
def eScores(Scores):
# ...
return eScores(Scores)
I think what you'd want is more like:
def eScores(Scores):
# same as before, but change the last line:
return
Since you're printing the results, I assume you don't want to return the values of score10, score20, etc.
Also, the function won't accumulate results since you're creating new local counts each time the function is called.
Why don't you just use each number as a key (after processing) and return a dictionary of values?
def eScores(Scores):
return_dict = {}
for score in Scores:
keyval = int(score/10)*10 # py3k automatically does float division
if keyval not in return_dict:
return_dict[keyval] = 1
else:
return_dict[keyval] += 1
return return_dict