Related
A=[2,3,4,1] B=[1,2,3,4]
I need to find how many elements of list A appear before than the same element of list B. In this case values 2,3,4 and the expected return would be 3.
def count(a, b):
muuttuja = 0
for i in range(0, len(a)-1):
if a[i] != b[i] and a[i] not in b[:i]:
muuttuja += 1
return muuttuja
I have tried this kind of solution but it is very slow to process lists that have great number of values. I would appreciate some suggestions for alternative methods of doing the same thing but more efficiently. Thank you!
If both the lists have unique elements you can make a map of element (as key) and index (as value). This can be achieved using dictionary in python. Since, dictionary uses only O(1) time for lookup. This code will give a time complexity of O(n)
A=[2,3,4,1]
B=[1,2,3,4]
d = {}
count = 0
for i,ele in enumerate(A) :
d[ele] = i
for i,ele in enumerate(B) :
if i > d[ele] :
count+=1
Use a set of already seen B-values.
def count(A, B):
result = 0
seen = set()
for a, b in zip(A, B):
seen.add(b)
if a not in seen:
result += 1
return result
This only works if the values in your lists are immutable.
Your method is slow because it has a time complexity of O(N²): checking if an element exists in a list of length N is O(N), and you do this N times. We can do better by using up some more memory instead of time.
First, iterate over b and create a dictionary mapping the values to the first index that value occurs at:
b_map = {}
for index, value in enumerate(b):
if value not in b_map:
b_map[value] = index
b_map is now {1: 0, 2: 1, 3: 2, 4: 3}
Next, iterate over a, counting how many elements have an index less than that element's value in the dictionary we just created:
result = 0
for index, value in enumerate(a):
if index < b_map.get(value, -1):
result += 1
Which gives the expected result of 3.
b_map.get(value, -1) is used to protect against the situation when a value in a doesn't occur in b, and you don't want to count it towards the total: .get returns the default value of -1, which is guaranteed to be less than any index. If you do want to count it, you can replace the -1 with len(a).
The second snippet can be replaced by a single call to sum:
result = sum(index < b_map.get(value, -1)
for index, value in enumerate(a))
You can make a prefix-count of A, which is an array where for each index you keep track of the number of occurrences of each element before the index.
You can use this to efficiently look-up the prefix-counts when looping over B:
import collections
A=[2,3,4,1]
B=[1,2,3,4]
prefix_count = [collections.defaultdict(int) for _ in range(len(A))]
prefix_count[0][A[0]] += 1
for i, n in enumerate(A[1:], start=1):
prefix_count[i] = collections.defaultdict(int, prefix_count[i-1])
prefix_count[i][n] += 1
prefix_count_b = sum(prefix_count[i][n] for i, n in enumerate(B))
print(prefix_count_b)
This outputs 3.
This still could be O(NN) because of the copy from the previous index when initializing the prefix_count array, if someone knows a better way to do this, please let me know*
I'm trying to figure out how to loop through an array or list in variable steps.
So for example, if I have the following list...
a = [0,0,1,0,0,1,0]
...and want to use the following logic:
Start at index 0
If index two spots away is a 0, move two spots
If index two spots away is a 1, move one spot
However, I'm not quite clear how I can implement this logic as it seems like I can't change my index value I iterate through.
Why does this snippet still return values from 0-6 instead of 0,3,6?
for idx,val in enumerate(a):
print(idx)
idx+=3
Don't use a for loop.
for loops in python are different than in C or Java. In those languages, a for loop has an initial condition, a termination condition, and an increment for each time the loop runs. Whereas in python, a for loop is more of a for each loop - you give it an iterable object, and it runs the code for every item in that iterable object.
Modifying the iterable object while you're running through it is a bad idea that can have difficult-to-predict repercussions and will usually break your code.
However, you can always use a while loop:
a = [0,0,1,0,0,1,0]
idx = 0
while(idx < len(a) - 2):
print(idx)
if a[idx + 2] == 0:
idx += 2
elif a[idx + 2] == 1:
idx += 1
print(idx)
which produces the expected output
0 1 3 4 6
Or, if you change the increments to 3 and 2 respectively, rather than 2 and 1,
0 2 5
Your reasoning is pretty confusing, and I don't see ANY application for this, but here is how I understand your problem...
The reason is because you aren't actually returning the values, you're simply returning the index + 3, which is wrong to begin with. What you're trying to do is point to a new index of the array based on its value and return the index if it contains a value greater than 0.
You need to reference the index you want, check its value, then return the index which contains a value.
a = [0, 0, 1, 0, 0, 1, 0]
for i, v in enumerate(a):
if i == 0:
print(i)
next
if v == 0:
next
else
print(i)
But let's be honest, this is extremely ugly and un-pythonic. Let's simply check for whether a[i] contains a value, and if so, return the index...
for i, v in enumerate(a):
if v or i == 0:
print(i)
The purpose of if v or i == 0 is to check if v has a value, if so, print the index. OR if we are looking at the first element of i.
If you want to EXPLICITLY move the index by two, you must set your index at the start and use a while loop, since enumerate can't help you here and doesn't actually let you move the indicies...
a = [0, 0, 1, 0, 0, 1, 0]
i = 0
while i < len(a) - 1: # -1 to avoid out of bounds error
print(i)
if a[i + 2] == 0:
i += 2
elif a[i + 2] == 1:
i += 1
print(i) # Final print statement for the last index at the end of the while loop
I want to impress upon you the fact that this solution does NOT scale with different or larger lists, which is why it isn't recommended. By simply checking for whether a value does or doesn't exist, you guarantee your accuracy.
You should simply return the index based upon whether or not it contains a value. Even for very large lists, this will be extremely fast, and will always scale, even for values greater than 1.
The only other reason I would see you would want to do this differently is if you're doing string-search algorithms.
I believe that python refreshes the the variables each iteration. So at the end of the first run, idx would be 3 but then at the start of the second run, it gets reset to 1.
If you want to go through a loop by 3s, you can still use a for loop
for i in range(0,len(a),3):
print(i)
If you want a dynamic iteration value, then you need to use a while loop.
i=0
while i<len(a):
print(i)
if(a[i]==0):
i+=2
else:
i+=1
I am playing a code challenge. Simply speaking, the problem is:
Given a list L (max length is of the order of 1000) containing positive integers.
Find the number of "Lucky Triples", which is L[i] divides L[j], and L[j] divides L[k].
for example, [1,2,3,4,5,6] should give the answer 3 because [1,2,4], [1,2,6],[1,3,6]
My attempt:
Sort the list. (let say there are n elements)
3 For loops: i, j, k (i from 1 to n-2), (j from i+1 to n-1), (k from j+1 to n)
only if L[j] % L[i] == 0, the k for loop will be executed
The algorithm seems to give the correct answer. But the challenge said that my code exceeded the time limit. I tried on my computer for the list [1,2,3,...,2000], count = 40888(I guess it is correct). The time is around 5 second.
Is there any faster way to do that?
This is the code I have written in python.
def answer(l):
l.sort()
cnt = 0
if len(l) == 2:
return cnt
for i in range(len(l)-2):
for j in range(1,len(l)-1-i):
if (l[i+j]%l[i] == 0):
for k in range(1,len(l)-j-i):
if (l[i+j+k]%l[i+j] == 0):
cnt += 1
return cnt
You can use additional space to help yourself. After you sort the input list you should make a map/dict where the key is each element in the list and value is a list of elements which are divisible by that in the list so you would have something like this
assume sorted list is list = [1,2,3,4,5,6] your map would be
1 -> [2,3,4,5,6]
2-> [4,6]
3->[6]
4->[]
5->[]
6->[]
now for every key in the map you find what it can divide and then you find what that divides, for example you know that
1 divides 2 and 2 divides 4 and 6, similarly 1 divides 3 and 3 divides 6
the complexity of sorting should be O(nlogn) and that of constructing the list should be better than O(n^2) (but I am not sure about this part) and then I am not sure about the complexity of when you are actually checking for multiples but I think this should be much much faster than a brute force O(n^3)
If someone could help me figure out the time complexity of this I would really appreciate it
EDIT :
You can make the map creation part faster by incrementing by X (and not 1) where X is the number in the list you are currently on since it is sorted.
Thank you guys for all your suggestions. They are brilliant. But it seems that I still can't pass the speed test or I cannot handle with duplicated elements.
After discussing with my friend, I have just come up with another solution. It should be O(n^2) and I passed the speed test. Thanks all!!
def answer(lst):
lst.sort()
count = 0
if len(lst) == 2:
return count
#for each middle element, count the divisors at the front and the multiples at the back. Then multiply them.
for i, middle in enumerate(lst[1:len(lst)-1], start = 1):
countfirst = 0
countthird = 0
for first in (lst[0:i]):
if middle % first == 0:
countfirst += 1
for third in (lst[i+1:]):
if third % middle == 0:
countthird += 1
count += countfirst*countthird
return count
I guess sorting the list is pretty inefficient. I would rather try to iteratively reduce the number of candidates. You could do that in two steps.
At first filter all numbers that do not have a divisor.
from itertools import combinations
candidates = [max(pair) for pair in combinations(l, 2) if max(pair)%min(pair) == 0]
After that, count the number of remaining candidates, that do have a divisor.
result = sum(max(pair)%min(pair) == 0 for pair in combinations(candidates, 2))
Your original code, for reference.
def answer(l):
l.sort()
cnt = 0
if len(l) == 2:
return cnt
for i in range(len(l)-2):
for j in range(1,len(l)-1-i):
if (l[i+j]%l[i] == 0):
for k in range(1,len(l)-j-i):
if (l[i+j+k]%l[i+j] == 0):
cnt += 1
return cnt
There are a number of misimplementations here, and with just a few tweaks we can probably get this running much faster. Let's start:
def answer(lst): # I prefer not to use `l` because it looks like `1`
lst.sort()
count = 0 # use whole words here. No reason not to.
if len(lst) == 2:
return count
for i, first in enumerate(lst):
# using `enumerate` here means you can avoid ugly ranges and
# saves you from a look up on the list afterwards. Not really a
# performance hit, but definitely looks and feels nicer.
for j, second in enumerate(lst[i+1:], start=i+1):
# this is the big savings. You know since you sorted the list that
# lst[1] can't divide lst[n] if n>1, but your code still starts
# searching from lst[1] every time! Enumerating over `l[i+1:]`
# cuts out a lot of unnecessary burden.
if second % first == 0:
# see how using enumerate makes that look nicer?
for third in lst[j+1:]:
if third % second == 0:
count += 1
return count
I bet that on its own will pass your speed test, but if not, you can check for membership instead. In fact, using a set here is probably a great idea!
def answer2(lst):
s = set(lst)
limit = max(s) # we'll never have a valid product higher than this
multiples = {} # accumulator for our mapping
for n in sorted(s):
max_prod = limit // n # n * (max_prod+1) > limit
multiples[n] = [n*k for k in range(2, max_prod+1) if n*k in s]
# in [1,2,3,4,5,6]:
# multiples = {1: [2, 3, 4, 5, 6],
# 2: [4, 6],
# 3: [6],
# 4: [],
# 5: [],
# 6: []}
# multiples is now a mapping you can use a Depth- or Breadth-first-search on
triples = sum(1 for j in multiples
for k in multiples.get(j, [])
for l in multiples.get(k, []))
# This basically just looks up each starting value as j, then grabs
# each valid multiple and assigns it to k, then grabs each valid
# multiple of k and assigns it to l. For every possible combination there,
# it adds 1 more to the result of `triples`
return triples
I'll give you just an idea, the implementation should be up to you:
Initialize the global counter to zero.
Sort the list, starting with smallest number.
Create a list of integers (one entry per number with same index).
Iterate through each number (index i), and do the following:
Check for dividers at positions 0 to i-1.
Store the number of dividers in the list at the position i.
Fetch the number of dividers from the list for each divider, and add each number to the global counter.
Unless you finished, go to 3rd.
Your result should be in the global counter.
Context:
This code is really simple, I´m just new to python. I have an incremental list of numbers, all I need to do is check if there is any missing value, and if I do, insert -1 in that position, example:
If I have a list with values [1,2,4,5], I want it to become [1,2,-1,4,5].
If I have a list with values [1,4,5], I want it to become [1,-1,-1,4,5].
Simple, yet I can´t do it properly on python.
My code:
id: The list I want to modify.
i, j, and z: Counters.
MyRange: I can´t show the real name of the variable (I don´t own the code), but the range is correct.
z=0
for i in MyRange:
value = id[i]
value2 = id[i+1]
j=z
//This while is here because I try not to compare a value with -1
//(I think this is the problem)
while value == -1:
j=j-1
value = id[j]
if(int(value)+1 == int(value2)):
if(value2 != -1):
id.insert(i,-1)
z=z+1
This code identifies any missing value, but then fills the rest of the list (From the missing value to the last value with -1).
Any help would be apprecciated. Thank you and sorry for any english mistakes.
One somewhat easy way to do this is to make a set of the numbers. Then you can count from the lowest to the biggest and look for the number in the set. If it's there, you're all good. If it's not there, then yield a -1.
def fill_range(initial_range, fill_vallue):
smallest = initial_range[0]
biggest = initial_range[-1]
items = set(initial_range)
for i in range(smallest, biggest+1): # use xrange on python2.x
if i in items:
yield i
else:
yield fill_value
You might use this generator function like this:
print(list(fill_range([1,2,4,5], -1)))
If you haven't seen a generator function before, they're worth learning about but the answer above might be slightly confusing. Here's a version which accumulates a list and then returns it at the end:
def fill_range(initial_range, fill_vallue):
result = []
smallest = initial_range[0]
biggest = initial_range[-1]
items = set(initial_range)
for i in range(smallest, biggest+1):
if i in items:
result.append(i)
else:
result.append(fill_value)
return result
You might also notice that the if else suite could be replaced here pretty easily by a conditional expression...
You need only one additional variable to keep track of missing elements of the sequence.
def insert_minus_ones(lst):
new_lst = []
last = lst[0] - 1
for e in lst:
while (last + 1) != e:
new_lst.append(-1)
last += 1
new_lst.append(e)
last += 1
return new_lst
The code above works for any sequences of numbers:
>>> insert_minus_ones([1,2,4,6,10])
[1, 2, -1, 4, -1, 6, -1, -1, -1, 10]
>>> insert_minus_ones([-5,-4,-3,2])
[-5, -4, -3, -1, -1, -1, -1, 2]
I wrote a recursive function to find the summation of a list, here is my code:
def rsum (eleList):
if len(eleList) == 1:
return eleList[0]
else:
return eleList[0] + rsum(eleList[1:])
However, right now I want to write a recursive function to find the summation of the max and min of a list, and I have no clue where I should start. Could anyone give me some hint?
If you want to write a recursive function, you have to figure out how to solve this problem based on having solved a smaller problem. You know that the sum of this list is the first element plus the sum of the rest of the list. If you have a list:
[1,2,3,4,5]
and you know that the max of the last four elements is 5 and the first element is 1, and you want to find the total maximum, how do you do that in a constant number of operations?
I would search for maximum and minimum value in each iteration. And if that is the case, I will return the sum.
Something like:
def rsum(eleList, ans, index):
if index == len(eleList) - 1:
if max(eleList) == eleList[index] or min(eleList) == eleList[index]:
return ans+eleList[index]
else:
return ans
else:
if max(eleList) == eleList[index] or min(eleList) == eleList[index]:
return ans + eleList[index] + rsum(eleList, ans, index + 1)
else:
return rsum(eleList, ans, index + 1)
print rsum([9, 2, 3, 4], 0, 0)
Output:
11
Might not be the smartest one or most pythonic but it gets things done.
For finding the sum of all the elements of the list use sum function:
temp = [1,2,3,4]
sum(temp)
#output = 10
If you want to find sum of min and max elements, get the list sorted and add first and last element:
temp = [1,2,3,4]
sorted_list = sorted(temp)
total = sorted_list[0] + sorted_list[-1]
Avoid making your own functions whenever there is a possibility of built-in function being present.