In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to achieve fancy printing in Python", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "how to achieve fancy printing in Python" this article.
1. Introduction
There are a lot of fun fancy printing in Python, which may be a piece of cake for a good master, but a retrogression for an entry-level rookie. Let's challenge the following three common fancy printing today.
two。 Print Christmas Tree 2.1 problem description
Code the function christmas_tree (height), which inputs an integer to represent the height of the Christmas tree and outputs a printed Christmas tree pattern with a height of height, in which the default trunk height is always 2. 5%.
Examples are as follows:
# height = 3 * # height = 4 * # height = 5 * 2.2 problem Analysis
Looking carefully at the above output pattern, we can divide the Christmas tree into two parts-the top triangle and the trunk.
# case: height = 4 * 3 space + 1 * 2 space + 3 * * 1 space + 5 * 0 space + 7 * * 3 space + 1 * * 3 space + 1 *
If we use range (height), then at this time we need to map the value of I 0Magne1Magol 2Magne3 to the space number 3meme2Magol 0 and * character number 1mem3Magol 5re7. This is as follows:
Num_space num_stari=0 3 1i=1 2 3i=2 1 5i=3 0 7i height-i-1 iTunes 2
We abstract it, using the formula height-i-1 to map I to num_space, and using formula iSum 2 to 1 to map I to num_star. In turn, we can derive our code implementation:
Def christmas_tree (height): for i in range (height): print ("* (height-i-1) + *" (iTun2x1)) for i in range (2): print ("* (height-1) +" * ") 3. Print alphabet version of Christmas Tree 3.1 problem description
The coding implementation function string_christmas_tree (string) takes a string as input and prints a Christmas tree pattern based on the string. Similarly, the height of the trunk is always 2.
Examples are as follows:
# string = "abc" a babcbabc an a # string = "abcd" a bab cbabcdcbabcd an a # string = "abcde" a babcbabc dcbabcdedcbabcde an a3.2
Similar to the solution to problem 1, we can split the above output into two parts, the first for the top triangle and the second for the trunk.
# case where string = "abcd" a 3 space + string [0: 0 bab 1] + string [: 1] bab 2 space + string [1: 0 dcbabcd 1] + string [: 2] cbabc 1 space + string [2: 0 dcbabcd 1] + string [: 3] dcbabcd 0 space + string [3: 0 space 1] + string [: 4] a 3 space + string [0] a 3 space + string [0]
Here, if we use range (len (string)), we need to map the I-value 0 minus 1 and 2 to the space number 3, the left slice 0, the left slice 0, and the right slice 1, 2 and 4. This is as follows:
Space left righti=0 3 0 1i=1 2 1 2i=2 1 2 3i=3 0 3 4i len (string)-iMusi 1 iiqure 1
More generally, we can use the formula len (string)-iMel 1 to map I to num_space. I is exactly the same as left, so there is no need for mapping here, we can simply add I by 1 to get right.
In turn, we can derive our code implementation:
Def string_christmas_tree (string): for i in range (len (string)): print ("* (len (string)-iMurray 1) + string [I: 0 len [1]] + string [: iTun1]) for i in range (2): print (" * (len (string)-1) + string [0]) 4. Print alphabetical diamond 4.1 problem description
The coding implementation function string_diamond (string) takes a string as input and prints the following diamond pattern. Suppose the string has an odd number of characters.
Examples are as follows:
# string = "abcde" an abcabcde cde e # string = "abcdefg" an abc abcdeabcdefg cdefg efg g # string = "abcdefghi" an abcabcde abcdefgabcdefghi cdefghi efghi ghi i4.2 problem Analysis
According to our observation, we still divide the above output pattern into two parts, the top half is the top triangle and the other half is the bottom inverted triangle.
# case where string = "abcdefg" a 3 space + string [: 1] abc 2 space + string [: 3] abcde 1 space + string [: 5] abcdefg 0 space + string [: 7] cdefg 1 space + string [2:] efg 2 space + string [4:] g 3 space + string [6:]
First, we need to find the height of the triangle at the top of the upper half. We can use the formula len (string) / / 2cm 1 to get the height.
Then let's analyze the triangle output of the upper half:
If we use range (height), we need to map I to the number of spaces 3, 2, and then the end of the slice, 1, 3, 5, and 7.
Spaces slicei=0 3 1i=1 2 3i=2 1 5i=3 0 7i height-i-1 iTunes 2
Finally, let's analyze the triangle output of the lower half:
We can use range (1, height), where we need to map I to the number of spaces 1, 2, and the beginning of the slice, 2, 4, and 6.
Spaces slicei=1 1 2i=2 2 4i=3 3 6i iTunes 2
With the above analysis, we can happily code, the code implementation is as follows:
Def string_diamond (string): height = len (string) / / 2x1for i in range (height): print ("* (height-i-1) + string [: iTun2y1]) for i in range (1, height): print (" * I + string [iTunes 2:]) these are all the contents of this article entitled "how to achieve fancy printing in Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.