This question already has answers here:
Python Sets vs Lists
(10 answers)
Complexity of *in* operator in Python
(3 answers)
Closed 1 year ago.
I was solving a hackerrank challenge.(in Python 3)
At one point I needed to sort a list and work with it.
(2 <= len(array) <= 10^5)
The list is containing unique integers.
I used arr.sort(), but I was getting "Time limit exceeded" message
Then I found if I use set(arr), it runs fast and passes all test cases.
here's is my function for more clarification
def pairs(k, ar):
ar.sort()
c=0
for i in ar:
if i+k in ar:
c+=1
return c
This gets me "Time limit exceeded"
def pairs(k, ar):
ar=set(ar)
c=0
for i in ar:
if i+k in ar:
c+=1
return c
This one with set() runs faster.
Why is this set() is faster than sort().
Don't they use same sorting algorithm?
This question already has answers here:
How to change for-loop iterator variable in the loop in Python?
(6 answers)
Updating Index Value in For Loop
(2 answers)
Closed 1 year ago.
I have a similar code that needs incrementing and while loop cant be used there,
m = range(10)
for i in range(len(m)):
print(i)
i+=2
Do it like this:
m = range(10)
nb = 0
for i in range(len(m)):
print(nb)
nb+=2
The problem is that you wanted to use i for two different tasks.
This question already has answers here:
Python, Make an iterative function into a recursive function
(4 answers)
Count to zero then Count up
(2 answers)
Closed 3 years ago.
I want to define a function as f(a,b) such that it generates a series as:
10,8,6,4,2,0,2,4,6,8,10 if a=10 and b=2 using Recursion.
def pattern(a,b):
if a-b < 0:
print(a)
return pattern(a+b,b)
print(a)
else:
print(a)
return pattern(a-b,b)
The result I get is
10
8
6
4
2
0
2
0
2
0
..... infinity
... but this is wrong.
You just need to use recursion
from __future__ import print_function
def psearch(a,b):
if a > 0:
print(a,end = ',')
psearch(a - b,b)
print(',',end="")
print(a,end = "")
else:
print(a,end="")
psearch(12,5)
print()
OUTPUT
12,7,2,-3,2,7,12
This question already has answers here:
Sum a list of numbers in Python
(26 answers)
Closed 4 years ago.
I'm starting to learn how to code and I've been trying to work out how to sum numbers from a list.
list1 = [1,2,3,4,5]
Using a for-loop, how would I set a variable to the sum of the list (15)?
Using a for loop:
acc = 0
for num in list1:
acc += num
Another approach:
acc = sum(list1)
This question already has answers here:
Python list comprehension expensive
(1 answer)
What is the advantage of a list comprehension over a for loop?
(1 answer)
Why is local variable access faster than class member access in Python?
(2 answers)
Closed 5 years ago.
I have tested four situations.
1.
testList = list()
for i in range(1000000):
testList.append(i)
2.
testList = list()
for i in range(1000000): testList.append(i)
3.
testList = list()
newAppend = testList.append
for i in range(1000000): newAppend(i)
4.
[i for i in range(1000000)]
Each time was
1 : 0.09166
2 : 0.08299
3 : 0.05003
4 : 0.04594
Why others are faster than 1?
Can I find related keywords?