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.
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
In Python you can iterate over the list itself:
for item in my_list:
#do something with item
or to use indices you can use xrange():
for i in xrange(1,len(my_list)): #as indexes start at zero so you
#may have to use xrange(len(my_list))
#do something here my_list[i]
There's another built-in function called enumerate(), which returns both item and index:
for index,item in enumerate(my_list):
# do something here
examples:
In [117]: my_lis=list('foobar')
In [118]: my_lis
Out[118]: ['f', 'o', 'o', 'b', 'a', 'r']
In [119]: for item in my_lis:
print item
.....:
f
o
o
b
a
r
In [120]: for i in xrange(len(my_lis)):
print my_lis[i]
.....:
f
o
o
b
a
r
In [122]: for index,item in enumerate(my_lis):
print index,'-->',item
.....:
0 --> f
1 --> o
2 --> o
3 --> b
4 --> a
5 --> r
Yes you can, with range [docs]:
for i in range(1, len(l)):
# i is an integer, you can access the list element with l[i]
but if you are accessing the list elements anyway, it's more natural to iterate over them directly:
for element in l:
# element refers to the element in the list, i.e. it is the same as l[i]
If you want to skip the the first element, you can slice the list [tutorial]:
for element in l[1:]:
# ...
can you do another for loop inside this for loop
Sure you can.
How do I loop though the length of an array inside of another array in Python? - Stack Overflow
Loop over array elements without knowing length
Why iterate over an array using the index?
What's the python version of (i=0;i<array.length;i++)? for x in list doesn't work as expected.
I think this is an XY problem. Explain exactly what you're trying to accomplish and why and how it's not working. for x in y: is the way it ought to be expressed in 99.9% of cases.
Videos
The length of an list can be get by len. Lists in a list can be traversed by:
for i in range(len(keys)):
for j in range(len(keys[i])):
Another option is to use enumerate:
for i, keylist in enumerate(keys):
for j, letter in enumerate(keylist):
To your question as posted:
for j in len(keys[i]):
print(keys[i][j])
# print individual characters
I suspect that you want the elements, not the indices, but enumerate gives you both:
def keyDraw(self):
for i, char_list in enumerate(keys):
for j, char in enumerate(char_list):
pygame.draw.rect(screen,(255,0,255),(60*i + 10,60*j, 50,50))
I can't adjust your usage of the list elements, since you didn't provide any.
i is your index into keys; char_list will iterate through the character lists, being equivalent to keys[i].