Answer by python language [closed] - python-3.x

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
A positive integer n is said to be perfect if the sum of the factors of n, other than n itself, add up to n. For instance 6 is perfect since the factors of 6 are {1,2,3,6} and 1+2+3=6. Likewise, 28 is perfect because the factors of 28 are {1,2,4,7,14,28} and 1+2+4+7+14=28.
Write a Python function perfect(n) that takes a positive integer argument and returns True if the integer is perfect, and False otherwise.
Here are some examples to show how your function should work.
perfect(6)
True
perfect(12)
False
perfect(28)
True

def perfect(x):
factor_sum = 0
for i in range(1, x-1):
if x % i == 0:
factor_sum = factor_sum + i
if(factor_sum == x):
return True
return False
print perfect(6) #Prints True
print perfect(12) #Prints False
print perfect(28) #Prints True

Related

Recurcive or itrative method ,which is best best approch? [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.
Improve this question
I am wondering when use the recursion and iterative method please tell me which approch best and why ?
I don't know about the Data structure and Algorithm.
This is Recursive approach.
def recurse(n):
print(n)
if n==0:
return 0
return recurse(n-1)
recurse(10)
print(recurse(10))
This is Itrative approach.
def factorial(n):
if n < 0:
return 0
elif n == 0 or n == 1:
return 1
else:
fact = 1
while(n > 1):
fact *= n
n -= 1
return fact
print(factorial(5))
Like LinFelix in the comments said. "Which is better" is a broad question. I have here somesources you can read up on to better understand which is better in your case:
https://www.interviewkickstart.com/learn/difference-between-recursion-and-iteration#:~:text=Recursion%20is%20when%20a%20function,loops%20and%20%22while%22%20loops.
https://www.geeksforgeeks.org/difference-between-recursion-and-iteration/
recursion versus iteration

Is there a way to re-write math.ceil function [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 3 years ago.
Improve this question
For this function, I must find all the "Egyptian fractions" that add up to a fraction using a greedy algorithm. All Egyptian fractions have a numerator value of 1, they are distinct, and the sum = (numerator/denominator). I understand how to find one using division and math.ceil. However, the function never seems to resolve after I try with values for numerator and denominator. Is there a way to re-write my code using no division (i.e. no ceiling, division, or floor), just multiplication and subtraction? I can assume the numerator is always < denominator, and both are positive integers.
def egypt(numerator, denominator):
fracs = []
while numerator != 0:
n = int(numerator)
d = int(denominator)
c = math.ceil(d / n)
fracs.append(c)
n = (c*n) - d
d = c*d
return fracs
math.ceil is OK. The problem is that you reinitialize the cycle each time. Here is the fixed function:
def egypt(numerator, denominator):
fracs = []
n = int(numerator)
d = int(denominator)
while n != 0:
c = math.ceil(d / n)
fracs.append(c)
n = (c*n) - d
d = c*d
return fracs

Split a number per unit of token python 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 4 years ago.
Improve this question
I'm trying to create a function that receive as argument a number and return an array of 3 numbers max.
I have 3 tokens. 1 unit, 5 unit and 25 unit.
calculateUnit(4) should = [4,0,0]
calculateUnit(7) should = [2,1,0] (because 2 unit of 1 and 1 unit of 5 = 7)
calculateUnit(36) should = [1,2,1] (because 1 unit of 1, 2 unit of 5 and 1 unit of 25 = 36)
I have a basic code and I think I need to use modulo division, I already tried to search here and every other resources I have but I may not use the correct terms.
You can reduce your solution to:
def convertInToken(am):
return [am//25, (am%25)//5, am%5]
This leverages integer-division (3.x upwards, also named floor division) and modulo division.
Floor division returns the full integer that woud have been returned if you did a normal division and floored it.
Modulu division returns the "remainder" of a division.
I managed to do that, but thanks anyway :)
# your code goes here
import math
def convertInToken(am):
result = [];
#need to use 25
if am >= 25:
amount25 = math.floor((am/25))
amount5 = math.floor((am-(amount25*25))/5)
amount1 = math.floor(((am-(amount25*25)-(amount5*5))/1))
result = result+[amount1]
result = result+[amount5]
result = result+[amount25]
#need to use 5
elif am >= 5:
amount5 = math.floor((am/5))
amount1 = math.floor(((am-(amount5*5))/1))
result = result+[amount1]
result = result+[amount5]
result = result+[0]
#need to use 1
elif am < 5:
result = result+[am]
result = result+[0]
result = result+[0]
return result
print(convertInToken(4))
print(convertInToken(7))
print(convertInToken(12))
print(convertInToken(37))

if else to calculate from a string haskell [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
hi I am new to haskell and very confused about if condition of haskell.
I was trying to calculate a sum from a String of grades.
For example, grades "ABC" should have the result of "128" while A=56,B=40 and C=32.
I am wondering if I am heading to the right direction. I wanted to output the sum of the grades by having sum after then
Here is the code I got so far :
grades (x:xs) =
if x=="A" then sum+=56
else if x=="B" then sum+=40
else if x=="C" then sum+=32
else if x=="D" then sum+=24
else if x=="E" then sum+=8
else sum+=0
Here is something to get you started. I would recommend a good book, for example Hutton's Haskell Programming (2nd edition).
-- a function to convert grades to points
pts 'A' = 56
pts 'B' = 40
pts 'C' = 32
pts 'D' = 24
pts 'E' = 8
pts _ = 0
-- then your desired function
grades str = sum (map pts str)
-- or, point-free (no pun intended)
grades' = sum . map pts
After these definitions
λ> map pts "ABC"
[56,40,32]
λ> grades "ABC"
128
Happy Haskelling!

Print factorial of given input -1 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I try to print number of combinations of given count of elements. - Yes, there was such topic, but I'm very beginner of Python and I want to understand my errors. Count is true just for x=4. And one more question: why in the end it prints "None"?
x=int(input('Count of elements for combinations: '))
a=x
from math import factorial
def everywithevery(x):
y=x
print ('Graphical representation:')
print (x*'_ ')
while x>0:
print ((x-1)*'* ')
x=x-1
print('Stars count is equal combinations count. Total count is: ',factorial((y-1)));
print(everywithevery(a));
There you can try my script: http://goo.gl/EDFkYM
You print the following line but you didn't return any thing from everywithevery function. Thats why that line prints none
print(everywithevery(a));
Just use
everywithevery(a)

Resources