Triangle using a recursion function [duplicate] - python

This question already has answers here:
Pascal's Triangle for Python
(12 answers)
Closed 2 years ago.
How do I make a triangle using a recursive function like this:
def triangle(3):
And the triangle should look like this:
1
1 1
1 2 1
And so on.

You can do something like this.
n=4
def triangle(n):
if n==0:
return
num=11**(triangle.n-n)
print "{}{}".format(" "*n, " ".join(list(str(num))))
triangle(n-1)
triangle.n = n
triangle(n)
Output:
1
1 1
1 2 1
1 3 3 1

Related

Best way to do range(N) * 2 in python 3 [duplicate]

This question already has answers here:
Is there an elegant way to cycle through a list N times via iteration (like itertools.cycle but limit the cycles)?
(6 answers)
Closed 3 years ago.
I know that python 3 range no longer produces a list, but a range object.
Is there a best way to do the following in python 3?
for i in range(3) * 2:
print(i)
# 0
# 1
# 2
# 0
# 1
# 2
One way to do it without making a list is to use chain.from_iterable and repeat from the itertools module. This uses O(1) extra space.
>>> from itertools import chain, repeat
>>> for i in chain.from_iterable(repeat(range(3), 2)):
... print(i)
...
0
1
2
0
1
2
Just make a list from the range:
for i in list(range(3)) * 2:
print(i)
Result:
0
1
2
0
1
2

How to print i till its less than k while k is decreasing every iteration? [duplicate]

This question already has answers here:
Scope of python variable in for loop
(10 answers)
Closed 3 years ago.
I want to print i and k till i is less than or equal to k.
in C++ the code can be given as:
for(i=0;i<k;i++){
cout<<i<<k;
k--;
}
I am not getting the correct output.
this is my code
k=5
for i in range(k):
print(i,k)
k-=1
the output i get is:
0 5
1 4
2 3
3 2
4 1
but i want to get:
0 5
1 4
2 3
is there someway to use the range() function for this?
For loops in Python are really for-each and suboptimal for your needs. Use while instead:
i = 0; k = 5
while i < k:
print(i,k)
i += 1
k -= 1
k=5
for i in range(k):
print(i,k)
if k<=i:
break
k-=1

Convert a list of string into a vector [duplicate]

This question already has answers here:
String to list in Python
(5 answers)
Closed 5 years ago.
Given:
['1 -1 1 1 1 1 1 1 1']
How can I convert it (efficiently) to be a vector of integers something like:
[1 -1 1 1 1 1 1 1 1]
Thank you.
return [int(n)
for n in s.split()]

How can I count elements in matrix ( if elements >1)? in python [duplicate]

This question already has answers here:
Count all values in a matrix less than a value
(6 answers)
Closed 5 years ago.
Data = matrix R
First I wanted to count elements of each row
countR = np.count_nonzero(R, axis=1)
Then, I could get matrix countR.
[25 2 1 2 2 55 1 2 1 2 1 1 2 2 1 1 1 1 2 2 1 2 14 1 3 ..
Second, I want to count elements in matrix
"if element>1 "
So what I did is here
countR1 = pd.value_counts(countR.values, sort>1)
But there was an error.
How can i count elements?
you can do it easily like this:
y=np.array(countR)
len(y[y>1])
if I understand correctly you want to count all the elements that are larger than 1 in the matrix R.
you can filter the data-frame by doing so(to dispose of the elements that are larger than 1):
biggerThanOne = R[R<1]
you can then get the size of the array and get the number of elements:
biggerThanOne.size
if you mean you want to count the elements of countR you can practically do the same thing

print all loops once/together in python [duplicate]

This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 8 years ago.
if I have a simple for loop like:
for i in range(10):
total = 0
total = total + i
print(total)
How can I print the loops once/together like:
1 2 3 4 5 6 7 8 9
Instead of every loop by itself:
1
2
3 etc
for i in range(10):
total = 0
total = total + i
print(total,end=" ")
The ability of print function, end of the each line changing from \n to " "

Categories

Resources