How to find combination in python? [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 5 days ago.
Improve this question
I'm trying to make combinations of two number I have facto defined before in my code now I want to use it to find combinations between n and k
I have this already
def facto(n):
res = 1
if n == 0:
return 1
for i in range(1, n+1):
res *= i
return res
now I need
def comb(n, k):

Related

Function that returns even numbers from a list and halves them [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 1 year ago.
The community is reviewing whether to reopen this question as of 1 year ago.
Improve this question
So I am having trouble concatenating this, I am not allowed to use .append(), and right now Im getting the error 'int' object not iterable.
def halveEvens(l):
num = []
for n in l:
if n % 2 == 0:
num += (n // 2)
return num
print(halveEvens([10,21,32,42,55]))```
sum(x//2 for x in numbers if x%2 == 0)
is probably how I would do it
if you just want to collect them (without summing them)
generator (x//2 for x in numbers if x%2 == 0) would be evaluated as you iterate it
or list comprehension that is evaluated immediatly
[x//2 for x in numbers if x%2 == 0]

New to programming, help in sum of matrix using for loop [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
Anyone pls help me in program to find out sum of elements in matrix using for loop.
This is my code.
a = [[1,2,3],[1,2,3],[1,2,3]]
total = 0
sha = np.shape(a)
for i in range(sha[0]):
for j in range(sha[1]):
total= total+a[i,j]
return total
Simply use sum
a = [[1,2,3],[1,2,3],[1,2,3]]
sum([sum(i) for i in a])
As it is nested list (list inside a list) you can refer to the following
a=[[1,2],[2,3],[4,5]]
total=0
for i in a:
total+=sum(i)
print(total)
a = [[1,2,3],[1,2,3],[1,2,3]]
Sum = 0
sha = np.shape(a)
for r in range(len(sha)):
for c in range(len(sha)):
sum = sum + a[r][c]
print(Sum)
Sum is looped and calculated for r->row and c-> column

recursive function that goes through a built in list of numbers [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
need help with this question. Much appreciated.
Write a recursive function big_numbers that takes a Python built-in list of numbers and returns True if each number in the list is greater than 100 and False otherwise.
def isGreater(myList, i=0):
if(i == len(myList)): # Termination True case
return True
if myList[i] > 100:
return isGreater(myList, i+1)
else:
return False # Termination False case
Demonstration:
test = [101, 129, 130]
print(isGreater(test))
True

How to form a given list of elements in python to create another set of lists from it? [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
List1 =['000095', '000094', '000092', '000101', '000099', '000096', '000095']
def makecycle(list, startElement):
A loop which forms the bottom list which is made from the upper one's elements!
if i pass that function the start element and the list it shoul print like this:
makecycle(list1, 000094) it should print:
['000094', '000092', '000101', '000099', '000096', '000095', '000094']
and if pass
makecycle(list1, 000101) it should print:
['000101', '000099', '000096', '000095', '000094', '000092', '000101']
and if pass
makecycle(list1, 000092) it should print:
['000092', '000101', '000099', '000096', '000095', '000094', '000092']
i know its kinda not clear enough but thats all i can point!
def makecycle(list1,startElement):
ind = list1.index(startElement)
l = len(list1)
i = ind
list2 = []
while ind < (l-1):
list2.append(list1[ind])
ind = ind + 1
for x in range(i):
if list1[x] not in list2:
list2.append(list1[x])
print(list2)

Python: how to display all possible cases to place brackets [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 6 years ago.
Improve this question
Given a number of brackets pairs. I want to display all correct combinations of these brackets. By correct I mean each bracket should be opened before closing in each combination. For example, if the number of brackets is 2 the output should be:
(())
()()
For 3:
((()))
()()()
(()())
(())()
()(())
The order of the output lines doesn't matter.
How can I do it with Python?
Try this code, please:
def __F(l, r, pref):
if r < l or l < 0 or r < 0:
return
if r == 0 and l == 0:
print(pref)
return
__F(l - 1, r, pref + "(")
__F(l, r - 1, pref + ")")
def F(n):
__F(n, n, "")
F(2)
F(3)

Categories