Videos
Appending elements while looping using append() is correct and it's a built-in method within Python lists.
However you can have the same result:
Using list comprehension:
result_t = [k for k in range(1,6)]
print(result_t)
>>> [1, 2, 3, 4, 5]
Using + operator:
result_t = []
for k in range(1,6):
result_t += [k]
print(result_t)
>>> [1, 2, 3, 4, 5]
Using special method __iadd__:
result_t = []
for k in range(1,6):
result_t.__iadd__([k])
print(result_t)
>>> [1, 2, 3, 4, 5]
The range function returns an iterator in modern Python. The list function converts an iterator to a list. So the following will fill your list with the values 1 to 5:
result_t = list(range(1,6)) # yields [1, 2, 3, 4, 5]
Note that in order to include 5 in the list, the range argument has to be 6.
Your last example doesn't parse unless you assign t a value before the loop. Assuming you do that, what you're doing in that case is modifying t each time through the loop, not just producing a linear range. You can get this effect using the map function:
t = 0
b = 2
def f(i):
global t
t = i + b*t
return t
result_b = list(map(f, range(1, 5))) # Yields [1, 4, 11, 26]
The map function applies the f function to each element of the range and returns an iterator, which is converted into a list using the list function. Of course, this version is more verbose than the loop, for this small example, but the technique itself is useful.
Your solution seems correct - but it seems u have a couple of syntax errors if you're using python
You also have to re-initialize sum1 & sum2 between test cases. Right now you get the total sum of all arrs rather than arr[x]-hopefully that makes sense. You also have to remove duplicate numbers from the sum. For instance, if you're getting the sum of all numbers up to 20 - you'll end up adding 15 twice as it is divisible by 3 and 5. So the inner for loops will add it twice to the sum. So you'll need to remove 15 to get rid of duplicates.
for x in range(len(arr)): #by default range starts at o. therefore, range(len(arr)) = range(0, len(arr))
sum1 = 0 # you forgot to initialize sum1
sum2 = 0 # you forgot to initialize sum2
duplicates = 0 #you have to remove duplicates from the answer
for b5 in range (0, arr[x], 5): #you have to add the colons here
sum1 = sum1 + b5
for b3 in range (0, arr[x], 3): #you have to add the colons here
sum2 = sum2 + b3
for dup in range(0, arr[x], 3*5): # removes duplicates from the final sum
duplicates = duplicates + dup
sum = sum1 + sum2 - duplicates
print(sum)
This is an O(n^2) solution - you can drop it down to O(n) using a little bit of math.
You'll notice that the inner for loops can be represented using the formula sum(n)=Σd*i=d*Σi-where the summation starts at i = 0, end at ⌊(n-1)/d⌋ and d is the divisor (in the case of your question d=3 or 5).
for b5 in range (0, arr[x], 5):
sum1 = sum1 + b5
(https://en.wikipedia.org/wiki/Summation)
There is a very common summation formula that is commonly used to convert summations into a closed-form expression (something with finite steps - which is O(1))
Σi=n*(n+1)/2
In the case of the inner loop - it would be sum(n) = d*(⌊(n-1)/d⌋)*(⌊(n-1)/d⌋+1)/2.
let,
f(n,d) = (⌊(n-1)/d⌋+1)/2
Therefore, the solution to your problem would be f(n,3)+f(n,5)-f(n,3*5)
Which would convert the inner for loops from O(n) to O(1). Which, means your entire solution would be O(n).
I'll let you figure out the code on your own. However, theoretically, there is a better solution; such that as arr grows indefinitely the work scales linearly rather than quadratically.
- Create Array with input number (Eg.
arr = [12, 15, 4]) - Iterate over the created Array
- In
forloop check if number are divisible by 3 and 5
Sample code:
arr = [12, 15, 4]
total = 0
for num in arr:
if num % 3 == 0 and num % 5 == 0:
total = total + num
print(total) # 15