Answer the question
In order to leave comments, you need to log in
Why does the function return different numbers?
if in the function in range write for example 6010 and return the number with index 6000, and then write the number 10000 in range and again return the number with index 6000, then the numbers are different, why?
def dbl_linear(n):
integers = [1]
for k in range(10100):
f = integers[k] * 2 + 1
integers.append(f)
c = integers[k]*3+1
integers.append(c)
a = list(set(integers))
a.sort()
return a[6000]
Answer the question
In order to leave comments, you need to log in
1. Learn the basics of python. A set is always sorted if it is created from a collection (in your case, an array). And if you make an array out of it, it will also be sorted. There is no point in doing a.sort()
2. As k increases, the head of your array "a" MAY change, because it's far from certain that "f" and "c" will end up in the tail of the array (due to sorting). I can even promise you that this will not happen, because "f" grows more slowly than "c". Therefore, with a different number of steps, there will be different numbers at position 6000
3. If you want to remove duplicates from the array without changing positions, you cannot use set. The most obvious way (but very slow) is to transfer from one array to another:
a = [7, 5, 7, 7, 1, 2, 3, 2]
b = []
for i in a:
if i not in b:
b.append(i)
a = [7, 5, 7, 7, 1, 2, 3, 2]
b = []
s = set()
for i in a:
if i not in s:
b.append(i)
s.add(i)
a = list(set(integers))
this function takes a list(list), converts it to a set(set), only unique values from integers get there, roughly speaking removes duplicates and then list(integers) converts back to a list, however the list is an ordered array, but the set is not ordered, so after converting set(integers) inside everything will be shuffled differently each time, if you want to return an ordered one, try
a = [integer for i, integer in enumerate(integers) if integer not in integers [:i]]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question