22
Last seen 5 years ago
Member for 8 years, 4 months
Difficulty Normal
Would like to receive comments/critics of my published solutions.
`sum` is a Python keyword, you shouldn't use keywords for naming your variables. The correct usage of `sum` (as a function) would be if you replaced lines 20-23 with:
return sum(users[node] for node in all_nodes.difference(next_nodes))
Lines 7-9: you can use tuple unpacking directly in the for
More
This is a random review so I don't know if anyone commented the same thing already, but:
You are creating way too long list of triangle numbers. There is a condition that input number is less than 1000, so there's no point of triangular numbers higher than 1000 in that list. (As a matter of fact, t
More
Interesting approach - usually people create an empty list and then append to it values that appear more than once.
Also, you might want to know about [list comprehensions](https://docs.python.org/3.5/tutorial/datastructures.html#list-comprehensions)
More
Couple of things here:
1. Is your line 16:
data= newdata
really necessary?
Why you don't just:
return newdata
---
2. Lines 11, 12, etc:
for i in range(len(data)):
cnt = data.count(data[i])
can be replaced with more 'pythonic':
for item in data:
More
Your first line could be written as:
stripped = [c.lower() for c in text if c.isalpha()]
More
You could slightly simplify your lines 5, 6, 7 with:
if 0 <= k < len(grid) and 0 <= n < len(grid[0]):
if (grid[k][n] and (k!=row or n!=col)):
s += 1
More
'sum' is a name of the Python's built-in function - avoid naming your variables the same.
But if you want to use 'sum' (the built-in one :)), there's a way to simplify your last 5 lines of code:
return sum(grid[row][col] for row,col in coords)
More
There's no need for 3 separate for-loops:
def checkio(data):
length_check = len(data) >= 10
digit_check = False
upper_check = False
lower_check = False
for char in data:
if char.isdigit():
digit_check = True
More
Since you use 'amount/2' in three places, isn't it better to define it as 'len(data)/2' and then just use 'amount'? And your if-clause can be then done as 'if not len(data) % 2:'
More
Changed the line 8 to:
neighbours = sum(sum(hood) for hood in neighbourhood)
since @veky said in a recent comment that sum is "clearer, faster and more consistent with that subtraction later" than count.
More
Line 4 - there's no need for subtracting 1 from the length of data, so you'll just have 'len(data)//2' - notice the // - that's integer division, so you don't need 'int' in front of your expression.
And now you'll have that expression in three places in your code - you could introduce new variable
More
Why you first define x = 0, then change it both in 'if' and 'else'? Just make it x = n/2 at line 4 and that's it.
Also, there's no need for the second 'if' at line 7 - just use the expression from line 10.
Here it is what it looks like with those changes:
def checkio(data):
data.sort(
More
You don't have to use 'int' and 'length - 1' if you use // - that's integer division (or to be more precise - floor division). So 'mid' can be defined only once for all cases.
It would look like something like this (lines 2 and 5 are also slightly changed):
def checkio(data):
data.sort
More
By using // you'll lose all those 'ints' and your cryptic list-comprehension becomes a bit more readable:
def checkio(data):
data.sort()
return (data[len(data)//2]+data[(len(data)//2)-1])/2 if len(data)%2==0 else data[len(data)//2]
More
[NO GOD PLEASE GOD NO](https://i.giphy.com/12XMGIWtrHBl5e.gif) :D :D
(I've seen your second solution, so I guess there's no need to explain anything here :D)
More
Line 23: rather than use string concatenation, using format is preferred:
def __repr__(self):
return 'Building({}, {}, {}, {}, {})'.format(
self.south, self.west, self.width_WE, self.width_NS, self.height)
More
Line 27: rather than use string concatenation, using format is preferred:
def __repr__(self):
return 'Building({}, {}, {}, {}, {})'.format(
self.south, self.west, self.width_WE, self.width_NS, self.height)
More
Line 30: rather than use string concatenation, using format is preferred:
def __repr__(self):
return 'Building({}, {}, {}, {}, {})'.format(
self.south, self.west, self.width_WE, self.width_NS, self.height)
More
'word' and 'text' are already strings, no need for converting them to string (line 4)
More