need to make this into a recursive function [closed] - python

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 1 year ago.
Improve this question
i need to convert this over to a recursive function. the program basically prints out all variations of a string.
def comb(L):
for i in range(3):
for j in range(3):
for k in range(3):
# check if the indexes are not
# same
if (i!=j and j!=k and i!=k):
print(L[i], L[j], L[k])
# Driver Code
comb([1, 2, 3])
output:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

This might get you started:
Every permutation of a list can be made by picking a value from the list & putting it at the front of every permutation of what is left in the list after taking that value out. (Note that the "what is left" part is a smaller list than what you started with.)
When your list is small enough, it is the only permutation.

Related

How to code a parameter with 3 values for three conditions [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
I have a parameter that is called f.. I don't know how to code it, should I make it as a list or it needs an if loop
f = 1 if the indicator i exists in list 1
f = -1 if the indicator i exists in list 2
f = O otherwise
I will use the parameter f in a constraint with the right side equals to f
Any help?
Should any further clarification is required I am ready.
f
constraint
You can do this in one line in python:
f = 1 if i in list1 else -1 if i in list2 else 0

Sum in range that meet criteria [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
Trying to learn a new approach.It's the classical problem of finding the sum of all multiples of 3 and 5 below 'n'.
What I want is:
print ("The sum is: ", sum(range(1, 100)))
But with an if-statement for multiples of 3 / 5.My question:How can I include that if-statement for a one-line solution?
Thanks for your help. =)// Chris S.
This is the approach you may look into:
def thesum(the_numbers):
the_numbers = [i for i in the_numbers if i % 3 == 0 or i % 5 == 0]
return print(sum(the_numbers))
thesum(range(100))

How to get a list using the range 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
How to get expected answer [0, 1, 2, 3] using the range function?
I tried this Python code:
print(range(4)),
result:
range(0,4)
The result is different from online playgrounds:
result 01: range(0,4)
result 02: [0, 1, 2, 3]
The general syntax of range function:
range(start, stop, step)
start : Numerical index, at which position to start(optional)
stop : Index you go upto but not include the number(position to end)(required)
step: Incremental step size(optional)
Examples
x = range(0, 6)
for n in x:
print(n) //output: 0 1 2 3 4 5
In your case you need a list containing the integers so as #Green Cloak Guy said you should use list(range(4))
do list(range(4)).
The range() function returns a generator (only produces one value at a time). You have to convert that generator to a list.

Python compare rows and columns of a table [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 8 years ago.
Improve this question
I am looking for an efficient way to compare rows and columns of a table against each other (>= gets 1, otherwise 0) and store the result.
Example:
0.3642286 0.7945753 0.3527125
0.3642286 1 1 0
0.7945753 0 1 0
0.3527125 1 1 1
I have 21 tables with 480*480 rows and columns. What would be a proper way of generating and storing such a matrix?
All you really need is two loops.
def compare(first, second):
result = []
for x in first:
result.append([])
for y in second:
result[-1].append(1 if x >= y else 0)
result = [list(i) for i in zip(*result)]
return result
You might consider NumPy (1) if you are regularly handling large multi-dimensional arrays.

predict the outcome numbers using a list [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 8 years ago.
Improve this question
I have 3 lists with 3 sets of integers. I want to predict the outcome numbers using a list or by whatever other means is out there.
List1 = [0 0 2 0 1 2]
List2 = [3 0 1 0 1 0]
List3 = [0 0 2 1 1 1]
Im working in the first column, I was thinking of using a double if statement something like:
if 3 is in spot 0 of any list
and 0 is in spot 0 of any list
print 2
The other thing is I want to do this for all the other columns as well.
I know this is not complete but its on my mind like this and ive been
searching around and found nothing to solve this problem.
I would greatly appeciate any response to this question.
You cant define a list delimited with space; you have to use a comma',' instead.
l1=[0,0,2,0,1,2]
l2=[3,0,1,0,1,0,]
l3=[0,0,2,1,1,1]
spot0_list = zip(l1,l2,l3)[0]
if 3 in spot0_list and 0 in spot0_list:
print 2
And, if there are too many lists, you can use set:
spot0_list = set(zip(l1,l2,l3)[0])

Categories