sumDivs could you help me with my code please [closed] - python

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 8 years ago.
Improve this question
Write a function called sumDivs that accepts a single, positive integer and calculates the
sum of all of its proper divisors. Use a for loop to find and sum the proper divisors of the
given integer.
For example,
>>> sumDivs(8)
7
this is what i have:
def sums(n):
i=0
for i in range (1, n-1):
if n % i == 0:
return n-1
i+=1

You shouldn't return the first divisor you find, you should be adding them as you go; i takes on the values from 1 to n-2 in your loop change to for i in range(1,n); and there is no need to define an initial value for i. Try this instead:
def sums(n):
sum=0
for i in range (1, n):
if n % i == 0:
sum+=i
return sum

Related

Find duplicates in an array using Python DSA [closed]

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

How do these simple Python functions work? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have run the the following functions and studied them line-by-line. I understand how the outer for-loop in f(n) works and how the while-loop works in g(n) but I understand the role of the inner for-loop in f(n). Also, how do these loops work with t = t+1? Thanks in advance!
def f(n):
t=0
for i in range(n):
for j in range(2*i):
t=t+1
return t
f(5)
def g(n):
t=0
j=n
while j>1:
t = t+1
j = j/2
return t
g(32)
The inner loop keeps adding 1 to t until it adds 2 times each item from the outer loop. So it adds 0 + 2 + 4 + 6 + 8. The range(5) is similar to a list equivalent to [0,1,2,3,4].
t=t+1 simply adds 1 to the value of t every time the line is run.

How Values are assigned in Anonymous function? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I don't understand whether 15 is sent to x or n
def func_compute(n):
return lambda x : x * n
result = func_compute(2)
print("Double the number of 15 =", result(15))
Assume a function f(x, n) = xn. Now you want to have this function for a fixed n, i.e., f_n(x) = xn. This is what func_compute(n) does, it returns f_n(x), a function, for the given n.
result(15) is then f_2(15) = f(15, 2) = 30.

Python - can someone explain this code please? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
def divisible_by(numbers, divisor):
return [x for x in numbers if x % divisor == 0]
i know it returns the divisible numbers from the array of numbers, but can someone explain how it gets there?
I am learning python as my first language, but i have not got to arrays yet.
i am mostly confused by this part "x for x in numbers if x"
Thanks
This is what is called "list comprehension". In one line, it creates a new list of all the numbers in "numbers" that are divisible by divisor. That's what the modulo (%) is checking. It checks that the remainder of the division is equal to 0.
The list comprehension is equivalent to saying:
divisible_numbers = []
for x in numbers:
if x % divisor == 0:
divisible_numbers.append(x)
return divisible_numbers

append all Integers of an integer in a list in python using recursive function [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I need an example of a recursive function that will append all integer elements of a number into a list using python and doing this with the most basic algorithm.
For example if n = 1234, list will be [1,2,3,4].
Try this?
def numberToList(number):
# base case
if number == 0:
return []
# recurse
return numberToList(number / 10) + [ number % 10 ]
Run it like this:
>>> numberToList(1234)
[4, 3, 2, 1]

Categories

Resources