MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / floyd

Function floyd

other/magicdiamondpattern.py:5–26  ·  view source on GitHub ↗

Print the upper half of a diamond pattern with '*' characters. Args: n (int): Size of the pattern. Examples: >>> floyd(3) ' * \\n * * \\n* * * \\n' >>> floyd(5) ' * \\n * * \\n * * * \\n * * * * \\n* * * * * \\n'

(n)

Source from the content-addressed store, hash-verified

3
4# Function to print upper half of diamond (pyramid)
5def floyd(n):
6 """
7 Print the upper half of a diamond pattern with '*' characters.
8
9 Args:
10 n (int): Size of the pattern.
11
12 Examples:
13 >>> floyd(3)
14 ' * \\n * * \\n* * * \\n'
15
16 >>> floyd(5)
17 ' * \\n * * \\n * * * \\n * * * * \\n* * * * * \\n'
18 """
19 result = ""
20 for i in range(n):
21 for _ in range(n - i - 1): # printing spaces
22 result += " "
23 for _ in range(i + 1): # printing stars
24 result += "* "
25 result += "\n"
26 return result
27
28
29# Function to print lower half of diamond (pyramid)

Callers 1

pretty_printFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected