22
Last seen 5 years ago
Member for 8 years, 4 months
Difficulty Normal
Would like to receive comments/critics of my published solutions.
Wouldn't it be easier to just create a list of non-unique elements, rather than:
(1) looping over original data to find unique elements,
(2) creating a variable count just to check if it equal to 1,
(3) looping over unique elements to remove them from the original data?
Something like this:
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 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
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
Have you read the comments which came with this task? (and can be seen in your solution) ;)
Rather than having this double loop and condition (lines 12-16), nicer thing to do would have been:
for element in data:
if data.count(element) > 1:
result.append(element)
More
Line 4 is not needed nor used.
Also, there's no need for creating a list (by using "[" and "]")
More
'word' and 'text' are already strings, no need for converting them to string (line 4)
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
Lines 7-9 could be done like:
data[i], data[i+1] = data[i+1], data[i]
But even better thing would be to replace lines 4-9 with short and simple:
data.sort()
Also, there exists integer division so you don't need to convert floating number to integer (your line 10)
l = len(data) // 2
More
Rather than using int(num/2) three times, you could have created a new variable, and use integer division, so no need to convert a float to int:
mid = num // 2
More
((You don't need) all those parentheses) (;))
if len(data) % 2 == 1:
return data[index]
More
There's no need for your line 4, since you define med for each case. But - I would rather use line 4, if it was well formulated:
med = l // 2
(Notice // - it is integer division, so you don't need to convert a float to an int)
Then your lines 5-12 become:
if l % 2 == 1:
return da
More
Iterating three times trough data is not efficient at all and should be avoided.
Your lines 12-27 could be replaced with:
for x in data:
if x.isupper():
upper = True
if x.islower():
lower = True
if x.isdigit():
digit = True
As a bon
More
There's no need for ; at the end of the lines.
int(len(data)/2) could be replaced by using // (integer division):
len(data) // 2
More
You might want to learn about [bitwise operators](https://wiki.python.org/moin/BitwiseOperators), they would make your life easier here. (And there are some other task where you'll find them useful)
More
There's no need for all those if-clauses when you're using bitwise operators.
For example, this is all you need per operation:
if operation == OPERATION_NAMES[0]:
return x & y
More
You might want to learn about [bitwise operators](https://wiki.python.org/moin/BitwiseOperators), they would make your life easier here. (And there are some other task where you'll find them useful)
More
Lines 6 and 7 are not needed. Also, line 4 can be written just like:
if znak.isupper():
More