Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to build a function that will return the sum of the first item in the data list and every tenth item after, so I write the script below, however, the result is always the first data in my list. How should I modify my code and fix it?
def Sum10th(data):
sum=0
for i,d in enumerate(data):
if (i % 10 == 0): sum = sum+d
return sum
p = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
print(Sum10th(p))
The desired result should be 31, however the computer only returns the value of 1.
You can try
p = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
def Sum10th(data):
return sum([v for i, v in enumerate(p) if (i + 1) % 10 == 0 or i == 0])
print(Sum10th(p))
Output
31
By using the list comprehension with an if, at the end of it, you can get all the first item with the tenth item following it from a list.
Try this way:
def sum10thdata(data):
sum = 0
l = len(data)
for i in range(0,l,10):
sum+=data[i]
return sum
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Can anyone help me out with this question. I did it in the right way but it is showing me an error. I dont know whats the issue.
Find duplicates in an array
Given an array a[] of size N which contains elements from 0 to N-1, you need to find all the elements occurring more than once in the given array.
Error while compiling
Here is my code
def duplicates(self, arr, n):
result = []
a = []
arr.sort()
for i in arr:
if i not in a:
a.append(i)
else:
if i not in result:
result.append(i)
if len(a) == n:
result.append(-1)
return result
I think the error the error is due to the worst time and space complexity. You have used the brute force method to program the code which is not the right way. Make your code more efficient for a good time and space complexity. Here is my code.
def duplicates(self, arr, n):
# Make an array of n size with a default value of 0
result = [0] * n
ans = []
#Update the index with value in an array
for i in range(n):
result[arr[i]] +=1
# Iterate through each element in an array to find the element which has a value of more than 1. return the I value and break.
for i in range(len(result)):
if result[i] >1:
ans.append(i)
if not ans:
ans.append(-1)
return ans
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am writing a recursion function that simply returns x^n:
def myPow(x,n) -> float:
def recurs_n(x,n):
if n==1:
return x
if n>1:
result = myPow(x,n-1)
return result
return recurs_n(x,n)
print(myPow(2,5))
The output that I am getting for the above is 2, which isn't correct obviously. Any idea where I am going wrong?
Your inner function calls the outer function, that should not be allowed. Furthermore, there is no logic applied anywhere for the multiplication when going to the n-1 step. So when n>1 you need to return x*myPow(x,n-1)
I should also mention, that in your case there is no real need to have an inner and outer function, one function should be enough. Like so;
def myPow(x,n) -> float:
if n == 1:
return x
if n > 1:
return x*myPow(x,n-1)
print(myPow(2,5))
Your code runs until power n=5 reaches 1, where it activates the first if condition where x is returned (here x=2) without continuing onwards.
An alternative is presented below:
def power(N, P):
# if power is 0 then return 1
if P == 0:
return 1
# if power is 1 then number is
# returned
elif P == 1:
return N
else:
return (N*power(N, P-1))
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am having a problem with the output my code is giving me and I am stumped. The output should look like this:
1,2,3,4 = 10
But instead, I am getting this:
[1,2,3,4] = 10
Can anyone review my code and tell me why its outputting that?
Here it is:
def sumList(NumList):
NumList = [int(input('Enter the number: ')) for i in range(4)]
print(NumList, "=" ,sum(NumList))
sumList(4)
NumList is a list object, which is denoted by the squared brackets, that's why when you print it shows [1,2,3,4].
If you want to print it like 1,2,3,4 = 10 you will need to treat it first with something like
NewList = [str(i) for i in NumList]
NewList = ','.join(NewList)
then your final function should be
def sumList(NumList):
NumList = [int(input('Enter the number: ')) for i in range(4)]
NewList = [str(i) for i in NumList]
NewList = ','.join(NewList)
print(NewList, "=" ,sum(NumList))
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I tried to find the relevant question but couldn't find so creating a new one.
My program creates a new list using list comprehension in python as per a simple if condition.
Newone = [ temp for temp in Oldone if temp % 2 != 0 ]
It works fine but when in some situation it doesn't work. For example this one
Oldone = [1]
Newone = [ temp for temp in Oldone if temp % 2 != 0 ]
This returns [1] but i am expecting Newone to be []
1%2 == 1
So your your condition: temp % 2 != 0 is True, therefore it is included in the list. If you want an empty list, you should change it temp % 2 == 0.
If you are not sure what's happening. Your list comprehension:
Newone = [ temp for temp in Oldone if temp % 2 != 0 ]
Means; put in my new list Newone all temp values from my existing list Oldone, which satisfy the condition temp % 2 != 0 (Essentially keep only odd numbers, since the remainder is 1, whenever an odd number is divided by 2)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
First and foremost, I am new to python. As of such, I only know for loops, range, lens, and sum to do this problem. I am having difficulty trying to make a function that can average list of numbers.
This is my code so far:
def ave(L):
L = list(range(len(L))
for a in range(len(L)):
if len(L) == 0: return 0
else: return float((sum(L))/len(L))
So far, I am getting a syntax error on my third line with range(L).
All you need to do is return the sum of L divided by the length of L:
def ave(L):
if not L:
return 0
return sum(L) / len(L)
No range() or float() or for required.
In Python 3, / always produces a floating point number. sum() will do all the looping for you. The only thing you need to take care of, is returning 0 if the list is empty.
Following line is missing a ):
L = list(range(len(L)))
^
Because average of empty list is undefined, you should rather return None instead of '0'.
And instead checking for length, it is better to catch potential error, according to EAFP principle. It makes also more clear what are you doing, as error is self-descriptive.
def ave(L):
try:
return sum(L) / len(L)
except ZeroDivisionError:
return None