working with recursion in python 3.x - python-3.x

here is my code
def printTriangleRecursive(width):
# Recursive implementation
if width > 1:
printTriangleRecursive(width - 1)
print ("*" * width, end=" ")
Am suppose to get this using recursion
*
* *
* * *
* * * *
* * * * *
but am getting a different staff

By default, print will issue a newline after printing the text. But with
end=" "
you are overriding that behavior and telling it to print a space instead of a newline.
To fix, just delete that
def printTriangleRecursive(width):
# Recursive implementation
if width > 1:
printTriangleRecursive(width - 1)
print ("*" * width)

Related

Python: a pattern of two signs (* and $) that creates a pyramid

I want to create a pattern that looks like this:
*
* $
* $ *
* $ * $
* $ * $ *
* $ * $
* $ *
* $
*
I know how to use nested loops to print pattern like this with one sign but I have difficulties to create it wth two signs.
Any idea how to do that?
I am using python3 here
for i in range(5):
for j in range(i+1):
print('*' if j%2==0 else '$' , end=" ")
print()
for i in range(4):
for j in range(4-i):
print('*' if j%2==0 else '$' , end=" ")
print()

Creating a large letter 'A' in python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I started printing a pyramid to start...
Here's what I made so far:`
num = int(input("Enter the Number: "))
for i in range(1, num+1):
for j in range(0, i):
print(" ", end="")
for j in range(1, (num*2 - (2*i - 1))+1):
if i == 1 or j == 1 or j ==(num*2 -(2*i-1)):
print("*", end="")
else:
print(" ", end="")
print()
This is what the output should look like...
*
* *
* *
* *
*********
* *
* *
* *
Here a simple solution.
def large_a(height):
rows = ["*"] + ["*" + " " * (2 * i + 1) + "*" for i in range(height - 1)]
middle = len(rows) // 2
rows[middle] = rows[middle].replace(" ", "*")
return "\n".join(f"{r:^{height * 2}}" for r in rows)
print(large_a(8))
*
* *
* *
* *
*********
* *
* *
* *
print(large_a(15))
*
* *
* *
* *
* *
* *
* *
***************
* *
* *
* *
* *
* *
* *
* *

How can we solve dynamic pattern program for odd no of values?

what is the solution of the pattern program given below.
like if we have, n=3 then pattern should be
*
* *
*
for n=5
*
* *
* * *
* *
*
and so on for n= odd values
here is answer :
Enter any odd no of your choice
n= int(input("Enter the number:"))
floor division to divide the pattern into two parts
z= n//2
for i in range(z+1):
print(" " *(z-i) + "* " *(i+1))
for i in range(z):
print(" " *(i+1) + "* " *(z-i))

How can I correctly implement this example of using classes?

In the following example, one can choose constants to depend upon the context of a future situtation.
class Constants:
SPEEDLIGHT = 3 * 10**8
GRAVITY = 9.81
C = Constants()
print(C.GRAVITY)
>> 9.81
That was not too difficult because each quantity is a fixed constant. But suppose I want to do something similar for functions. In this first block of code below, I specify two distributions of integrable variable x and fixed parameters a and b.
class IntegrableDistribution:
def Gaussian(x,a,b):
cnorm = 1 / ( b * (2 * pi)**(1/2) )
return cnorm * np.exp( (-1) * (x-a)**2 / (2 * b**2) )
# Gaussian = Gaussian(x,a,b)
def Lognormal(x,a,b):
cnorm = 1 / ( b * (2 * pi)**(1/2) )
return cnorm * exp( (-1) * (np.log(x)-a)**2 / (2 * b**2) ) / x
# Lognormal = Lognormal(x,a,b)
I was trying to name the distributions so that they could be callable. That resulted in an error message, hence the commented out code above. In this next block of code, I am trying to use an input to select a distribution for integration (though I feel it is extremely inefficient).
Integrable = IntegrableDistribution()
class CallIntegrableDistribution:
def Model():
def Pick():
"""
1 : Gaussian Distribution
2 : Lognormal Distribution
"""
self.cmnd = cmnd
cmnd = int(input("Pick a Distribution Model: "))
return cmnd
self.cmnd = cmnd
if cmnd == 1:
Distribution = Integrable.Gaussian
if cmnd == 2:
Distribution = Integrable.Lognormal
return Distribution
OR ALTERNATIVELY
cmnd = {
1: Gaussian,
2: Lognormal,
}
I'm not really concerned with the problem of distributions; I'm only applying it to showcase my knowns and unknowns. What are some ways to properly do this or something similar/simpler using classes or dictionaries?
Use static methods:
class IntegrableDistribution:
#staticmethod
def Gaussian(x,a,b):
cnorm = 1 / ( b * (2 * pi)**(1/2) )
return cnorm * np.exp( (-1) * (x-a)**2 / (2 * b**2) )
#staticmethod
def Lognormal(x,a,b):
cnorm = 1 / ( b * (2 * pi)**(1/2) )
return cnorm * exp( (-1) * (np.log(x)-a)**2 / (2 * b**2) ) / x
And usage:
some_result = IntegrableDistribution.Gaussian(1, 2, 3)
another_result = IntegrableDistribution.Lognormal(1, 2, 3)

how to print FlippedTriangle pattern using python

Here is my code but it is not working as expected
def printFlippedTriangle(width):
for i in range(0, width):
for J in range(0, width-i):
print(" ", end=" ") # single line
for j in range(0,i):
print(" "+"* ", end=" ") # single line
j=j-1
print("*")
Am getting this:
*
* * * * *
* * * * * * *
* * * * * * *
* * * * *
am suppose to get:
*
* *
* * *
* * * *
* * * * *
Any idea and or suggestion will be appreciated
This will get the job done, and in a single loop too!
def triangle(w):
for i in range(0, w):
print(' ' * ((w - i - 1) * 2), end='') # spaces for each row
print('* ' * (i + 1), end='') # * for each row
print() # new line
>>> triangle(5)
*
* *
* * *
* * * *
* * * * *
Each row needs width - rowNumber - 1 spaces and rowNumber + 1 asterisks when starting from 0

Resources