Print factorial of given input -1 [closed] - python-3.x

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)

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

Finding elements with same lastletter in a list [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
I'm trying to figure out how I would use a closure which will find and groups of words that have the same ending letter.
For example: [United, Static, Rapid, Directed]
The return should be ["D":3, "c":1]
If you want to group use:
def list = ['United', 'Static', 'Rapid', 'Directed']
def groupped = list.groupBy{ it[ -1 ] }
assert groupped == [d:['United', 'Rapid', 'Directed'], c:['Static']]
For counting only you can use:
def counted = list.inject( [:].withDefault{ 0 } ){ res, curr ->
res[ curr[ -1 ] ]++
res
}
assert counted == [d:3, c:1]

Overload the necessary operator(s) so that instead of having to write [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 2 years ago.
Improve this question
Doing some exercises from Think like a CS with Python 3:
Have a task:
Overload the necessary operator(s) that instead of having to write
if t1.after(t2):...
we can use the more convenient
if t1 > t2: ...
How I can do it? Have no ideas.
You need to override the t1.__gt__(t2) method of your class. I would suggest overriding all of the following __gt__ __lt__ __le__ __ge__ special functions.
For example
class Point:
def __init__(self, x = 0, y = 0):
self.x = x
self.y = y
def __lt__(self,other):
self_mag = (self.x ** 2) + (self.y ** 2)
other_mag = (other.x ** 2) + (other.y ** 2)
return self_mag < other_mag
will allow you to write expressions like p1 < p2 but not p1 > p2. But it can be done trivially.
edit: turns out that simply overriding the __eq__ and __lt__ on top of using functools.#total_ordering gives the desired result.

Out of bounds error due to list assignment? [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
array1 = []
array2 = []
size = int(input("Enter the size of the array: "))
print("Enter the elements in the array: ")
for item in range(size):
element = int(input())
array1.append(element)
array2[0] = -1
for item in range(1, 8):
x = item-1
if array1[item] < array1[x]:
array2.append(-1)
elif array1[item] > array1[x]:
array2.append(array1[x])
elif array1[item] == array1[x]:
array2.append(array2[x])
print(array2)
Expected output: a proper execution of the code
Received output:
Traceback (most recent call last)
array2[0] = -1
IndexError: list assignment index out of range
First of all, note that your problem is in array2, so most of the code is superfluous to this issue. The minimal, complete example to reproduce your error is:
array2 = []
array2[0] = -1
When looking at this example, it's easier to see the problem - array2 is initialized with size 0, so its 0-th index is already out of bound.
Going back to your code, you can just initialize it as array2 = [-1] instead of how it's written - after all, array2[0] = -1 is the first time that array2 is accessed. Or you can change the array2[0]=-1 to an .append.
u set array2 as [],so when u define array2[0] = -1 the index is out of range

Answer by python language [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
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

Resources