I'm supposed to create a recursive statement that if first calls triangle(n) it returns
'******\n *****\n ****\n ***\n **\n *'
This above is called for triangle(6) and if I print(triangle(6)) it returns below.
******
*****
****
***
**
*
Then I must create another code recursive_triangle(x, n) that returns a string with the LAST x lines of a right triangle of base and height n. For example if I did recursive_triangle(3, 6) it returns
' ***\n **\n *'
and if i print it should returns
***
**
*
So far my code is
#### DO NOT modify the triangle(n) function in any way!
def triangle(n):
return recursive_triangle(n, n)
###################
def recursive_triangle(k, n=0):
'''
Takes two integers k and n
>>> recursive_triangle(2,4)
' **\\n *'
>>> print(recursive_triangle(2,4))
**
*
>>> triangle(4)
'****\\n ***\\n **\\n *'
>>> print(triangle(4))
****
***
**
*
'''
# --- YOUR CODE STARTS HERE
if n == 1:
return "*"
else:
for i in range(1, n+1):
return ("*" *n) + "\n" + (' ' * i) + triangle (n - 1)
for print(triangle(4)) this is what i got
****
***
**
*
How do I modify the code to get the output above?
Your recursion case is ill-formed:
else:
for i in range(1, n+1):
return ("*" *n) + "\n" + (' ' * i) + triangle (n - 1)
First of all, this code returns after a single iteration: return ends your function instance, so i never gets to a value of 2. You need to do something simple, and then recur on a simpler case to handle the rest.
Next, triangle exists only to call recursive_triangle. Then, recursive_triangle needs to call itself, not loop back to triangle.
Finally, note that recursive_triangle utterly ignores the parameter k. This value is critical to determine where in the line to place the asterisks.
Each instance of recursive_triangle should produce a single line of the triangle -- you have that correct -- and then concatenate that line with the remainder of the triangle, returning that concatenated whole to the instance that called it. You'll want something roughly like:
else:
line = ... # build a line of k-n spaces and n asterisks
return line + recursive_triangle(k, n-1)
Can you take it from there? Among other things, remember to insert a few useful print commands to trace your execution flow and the values you generate.
First of all, there are better ways to achieve this.
However, if you really want to go this way, the following code can fix the spacing issues.
#### DO NOT modify the triangle(n) function in any way!
def triangle(n):
return recursive_triangle(n, n)
###################
def recursive_triangle(k, n=0):
'''
Takes two integers k and n
>>> recursive_triangle(2,4)
' **\\n *'
>>> print(recursive_triangle(2,4))
**
*
>>> triangle(4)
'****\\n ***\\n **\\n *'
>>> print(triangle(4))
****
***
**
*
'''
# --- YOUR CODE STARTS HERE
if n == 1:
return "*"
else:
for i in range(1, n+1):
return ("*" *n) + "\n" + (' ' * i) + triangle (n - 1).replace("\n", "\n ")
which gives you
****
***
**
*
in Python 3.6.5.
You can count the row with r and use a secondary parameter to count the spaces, s
def triangle (r = 0, s = 0):
if r is 0:
return ""
else:
return (" " * s) + ("*" * r) + "\n" + triangle (r - 1, s + 1)
print (triangle(5))
# *****
# ****
# ***
# **
# *
code.py:
#!/usr/bin/env python3
import sys
#### DO NOT modify the triangle(n) function in any way!
def triangle(n):
return recursive_triangle(n, n)
###################
def recursive_triangle(k, n=0):
'''
Takes two integers k and n
>>> recursive_triangle(2,4)
' **\\n *'
>>> print(recursive_triangle(2,4))
**
*
>>> triangle(4)
'****\\n ***\\n **\\n *'
>>> print(triangle(4))
****
***
**
*
'''
# --- YOUR CODE STARTS HERE
if k == 0:
return ""
else:
return "\n".join(["".join([" " * (n - k), "*" * k]), recursive_triangle(k - 1, n)])
#return " " * (n - k) + "*" * k + "\n" + recursive_triangle(k - 1, n)
def main():
print("triangle(6):\n{:s}".format(triangle(6)))
print("recursive_triangle(3, 6):\n{:s}".format(recursive_triangle(3, 6)))
print("repr recursive_triangle(2, 4): {:s}".format(repr(recursive_triangle(2, 4))))
print("repr triangle(4): {:s}".format(repr(triangle(4))))
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main()
Notes:
To make things simpler, you can look at recursive_triangle(k, n)'s argument meanings like:
k: Recursion step, and also the number of "*" characters. Decrements with every recursive function call
n: 1st (longest) triangle line length (the number of SPACEs + k). Remains constant inside recursion
Current line contains n - k SPACEs followed by k "*" s (n characters in total). Lines are joined together via [Python 3]: str.join(iterable) (or "manually", in the (next) commented line)
The function calls itself until there are no more "*" characters (k becomes 0)
Output:
(py35x64_test) e:\Work\Dev\StackOverflow\q052652407>"e:\Work\Dev\VEnvs\py35x64_test\Scripts\python.exe" code.py
Python 3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32
triangle(6):
******
*****
****
***
**
*
recursive_triangle(3, 6):
***
**
*
repr recursive_triangle(2, 4): ' **\n *\n'
repr triangle(4): '****\n ***\n **\n *\n'
Related
Write a recursive function called draw_triangle() that outputs lines of *'s to form a right side up isosceles triangle. Function draw_triangle() has one parameter, an integer representing the base length of the triangle. Assume the base length is always odd and less than 20. Output 9 spaces before the first '*' on the first line for correct formatting.
Hint: The number of '*' increases by 2 for every line drawn.
Ex: If the input of the program is: 3,
Then the function draw_triangle outputs:
*
***
If the input of the program is 19,
Then the function outputs
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
No space is output before the first asterisk on the last line when the base length is 19.
I was able to code this non-recursively as follows:
def draw_triangle(n):
lines_to_print = int(((n // 2) + 1))
spaces_to_print = 9
asts_to_print = 1
for i in range(lines_to_print):
print(spaces_to_print * ' ', end='')
print(asts_to_print * '*', end='')
print()
spaces_to_print -= 1
asts_to_print += 2
base_length = int(input())
draw_triangle(base_length)
But I cannot, for my life, figure out how to do it recursively, let alone with only one argument. What am I missing about recursion?
The simple trick here is to use the str.center() method.
def draw_triangle(n):
if n == 1:
print('*'.center(19))
else:
draw_triangle(n-2)
print((n*'*').center(19))
Test it:
draw_triangle(19)
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
One method is is to define functions to print the symbole you want to use and the spaces between them:
def print_ecart(ecart):
if (ecart == 0):
return;
print(" ", end = "")
print_ecart(ecart - 1)
def print_etoile(etoile):
if(etoile == 0):
return
print("* ", end = "")
print_etoile(etoile - 1)
def pattern(n, num):
if (n == 0):
return
print_ecart(n - 1)
print_etoile(num - n + 1)
print("");
pattern(n - 1, num)
In this case print_ecrat prints spaces and print_etoile prints the stars:
pattern(10,10)
returns:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
By printing after the recursion itself happens, you can print smaller lines over the bigger lines:
def draw_triangle(n):
spaces = 9 - n // 2 # Ensures correct spacing for each line
if n == 1:
print(spaces * ' ' + '*')
else:
draw_triangle(n-2)
print(spaces * ' ' + n * '*')
draw_triangle(19)
Well, it is a quite interesting question.
The main obstacle is that you have to preserve initial base length of triangle.
You can't save it via assigning input value to another parameter inside a recursive function because:
only one argument is allowed
assigning will work every function call.
Not sure if the following code is convenient with the conditions of the task but it may be useful to illustrate an idea of recursive functions for you:
base_length = 20
def draw_triangle(base=base_length):
if base % 2 == 0:
base = base - 1
draw_triangle(base)
else:
if base > 0:
# print(base)
# print(int(base/2 - 1))
print(' '*int((base - 2)/2+1) + '*'*(base_length - base) + ' '*int((base - 2)/2))
base = base - 2
draw_triangle(base)
elif base == 0:
return 0
draw_triangle(base=base_length)
Formally there is only one argument in this recursive function, but you have to define base_length paramater outside of the function and the function uses base_length inside.
I didn't know how to describe it in the question properly, so I'll try again here.
I have to solve this question where my code should print different types of squares. I have all of that figured out, however, I'm stuck at the printing part.
So, I start by inputting 4 as the size, and it results in different types of squares being made. For example, these two:
first = ""
for j in range(size-1):
first += "*" * size + "\n"
first += "*" * size
two = ""
for j in range(size-1):
if j == 0:
two += "*" * size + "\n"
else:
two += "*" + ((size - 2) * " ") + "*" + "\n"
two += "*" * size
Now, I have to print them like this:
**** ****
**** * *
**** * *
**** ****
separated by a '\t'.
Since these squares are 'stored' in different strings, this is unfamiliar territory for me as the cursor is at the end of the first square. I don't know what to do, help pls.
There are many ways for that, one is here:
>>> v = '\n'.join([i+'\t' + j for i,j in list(zip([i for i in first.split('\n') if i], [ i for i in two.split('\n') if i]))])
>>> print(v)
**** ****
**** * *
**** * *
**** ****
What i did:
Splitted both strings at newline character, then took corresponding parts and joined them by tab in between, then assembled the whole string.
You can change the data structure you use. Let the square will be not a string, but list of strings, where each string is line should be printed.
Then you can for each line write line_first + '\t' + line_two and print it.
This code worked as you expect.
first = []
for j in range(size - 1):
first.append("*" * size)
first.append("*" * size)
two = []
for j in range(size - 1):
if j == 0:
two.append("*" * size)
else:
two.append("*" + ((size - 2) * " ") + "*")
two.append("*" * size)
for f, t in zip(first, two):
print(f + '\t' + t)
How can I change this code that instead of printing the shape below, it prints it like a lozenge (rhomboid)?
def star (m):
for i in range (m + 1):
print ("*" * i)
for i in range ( m + 1 ):
print ("*" * (m - 1))
m -= 1
Output
*
**
***
****
***
**
*
A lozenge is equilateral, a rhomboid typically not. For illustration, I will do a rhomboid, but allow it to be equilateral.
To define a function, you should first specify the domain, in this case positive ints (or include 0 if you wish).
Functions should generally return an object that can be compared with the desired output for a given input. You can then write an automated test for example outputs. In this case, the function should produce an iterable of strings. The desired output can most easily be represented as a tuple or list of strings and the function output converted if necessary.
r43 = (
'****',
' ****',
' ****',
)
def rhom(width, height):
if not(isinstance(width, int) and width > 0 and
isinstance(height, int) and height > 0):
raise ValueError('width and height must be positive ints')
stars = width * '*'
for i in range(height):
yield i * ' ' + stars
out43 = tuple(rhom(4, 3))
print(r43 == out43)
for line in out43:
print(line)
prints
True
****
****
****
I like the following a bit better because it's more symmetrical:
def star(m):
def star_line(n):
print('{s:^{f:d}s}'.format(s='*'*n, f=m))
for i in range(1,m,2):
star_line(i)
for i in range(m,0,-2):
star_line(i)
star(5)
*
***
*****
***
*
This solution uses the nice feature of Python's new(er) formatting syntax which enables you to specify the width of a field dynamically as well as the contents of the field: e.g. '{s:^{f:d}}'.format(s='*'*3, f=5) becomes '{s:^5s}'.format(s='***') which becomes ' *** '.
def print_lozenge(num):
str = '*'
str_2 = str + (num - 1) * ' *'
size = 2 * num - 1
for n in range(num):
print str.center(size, ' ')
str += ' *'
for n in range(num):
str_2 = str_2[: -2]
print str_2.center(size, ' ')
print_lozenge(5)
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Is this what you wanted?
def star(m):
for i in range(1, m + 1):
print(("*" * i).center(m))
for i in reversed(range(1, m)):
print(("*" * i).center(m))
star(10)
Output:
*
**
***
****
*****
******
*******
********
*********
**********
*********
********
*******
******
*****
****
***
**
*
In Python the print function automatically takes new line after the statements, if we want to print the whole statement in a single line then what should be used?
For example:
>>> for number in range(1,6):
... for k in range (0,number):
... print ("*")
...
I got the following output:
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
While I need this one:
*
**
***
****
*****
Try the following in Python 2.x.x. You could use print "*"*i where i is a number which prints '*' for i number of times.
print "*" * 3
Output
***
All you have to do is to choose the value of i carefully.
for i in range(0,6): ## i increments through the range(0,6) for every iteration
print "*"*i ## "*" is printed 'i' times
Output:
*
**
***
****
*****
Set the end parameter of print to "" and put an extra print() just outside the inner loop:
for number in range(1,6):
for k in range (0,number):
print ("*", end="")
print() # This is needed to break up the lines.
Below is a demonstration:
>>> for number in range(1,6):
... for k in range (0,number):
... print ("*", end="")
... print()
...
*
**
***
****
*****
>>>
Use a comma after print:
>>> for number in range(1,6):
... for k in range (0,number):
... print ("*"),
... print()
I am trying to print out an arrow head using *s.
So far my code looks like this.
def head(n):
while n > 0:
print n * "*"
n = n - 1
print head(input())
and it works but if for example I enter 11, it prints this:
***********
**********
*********
********
*******
******
*****
****
***
**
*
But I want it to print like this:
*
***
*****
*******
*********
***********
Which has less arrows, but I can't figure out how to do it.
It makes the function a little simpler to think in terms of how many lines do you want:
def head(lines):
for n in range(1,lines*2,2): # count 1,3,5...
print(('*'*n).center(lines*2-1))
Output:
>>> head(5)
*
***
*****
*******
*********
Here's an alternate way to use a variable length format that is a little less obvious:
def head(lines):
for n in range(1,lines*2,2):
print('{:^{}}'.format('*'*n,lines*2-1))
use string formatting:
def head(size):
n=1
while n < size+1:
stars = n * "*"
print '{:^30}'.format(stars)
n += 2
it will center your asterisks on the field 30 chars wide.
def printHead(n):
for l in range(1,n):
print " "*(n-l)+"*"*(1 if l==1 else 2*l-1)
Each row has level-1 spaces. Then if it's the first level one start, otherwise it has 2*level-1.
>>> printHead(6)
*
***
*****
*******
*********
def head(n):
total = 2 * n - 1
s = ''
for i in xrange(1, n + 1):
k = 2 * i - 1
s += ' ' * ((total - k) / 2) + '*' * k + '\n'
return s
The number of stars on a line is equal to 2n - 1 where n is the line number.
I wouldn't call the parameter "n" in this function as it is not clear to me whether it refers to lines or stars.
You can use the center function to surround a string with whitespace, based on a specified width. You want the arrow to be centred around the longest line so you need 2 variables, one to keep track of the current line and another to remember the largest line.
You want to iterate in the opposite direction to what you have demonstrated, as you want the arrow to point up, not down.
I think this should work for you:
def head(total_lines):
for current_line in range(1, total_lines + 1):
print ((2 * current_line - 1) * "*").center(2 * total_lines - 1)