The first academic year after entering the undergraduate program is not easy, and in the blink of an eye, it's almost time to find a job. To easily tackle the Blue Bridge Cup, I chose Python, which I have some foundation in, focusing on cost-effectiveness. I no longer want to spend a lot of time dealing with C++ environment setup and complex syntax.
My overall preparation plan is clear: first, complete Luogu's problem sets 1-6 (sequence, branch, loop, array, string, function) to build a solid foundation, then move on to practicing the 500 real past questions on the Blue Bridge Cloud platform for the final sprint.
Today, I spent 3 hours successfully clearing Luogu's [Beginner 1: Sequential Structure]. Although the problems are basic, when actually coding, I still encountered many Python-specific 'pitfalls' and data processing details. For future review, I've compiled today's troubleshooting log and summarized syntax templates into this note.
When solving P5708 [Triangle Area], the given Heron's formula is √[p(p-a)(p-b)(p-c)]. Following mathematical intuition, I casually wrote "p(p - a)".
Python immediately reported an error:
"TypeError: 'float' object is not callable".
It turns out Python doesn't recognize the omitted multiplication sign in algebra; it thinks "p" is a function and tries to call it. You must explicitly add the multiplication sign, writing "p * (p - a)".
While solving P1425 [Little Fish Swimming Time], calculating the time difference. I thought, just multiply hours by 60 to get minutes? So I wrote "a, b, c, d = input().split()", then directly used "c * 60" in the calculation.
The terminal instantly output a bunch of gibberish:
"1919191919..."
Then I remembered, the results from "split()" are all strings by default! In Python, multiplying a string by a number repeats it N times. Before calculation, you must first convert them to integers using "map(int, ...)".
Got stuck on B2029 [Elephant Drinking Water].
First, I completely blanked on the cylinder volume formula πr²h.
Second, the problem stated the elephant drinks 20 liters of water, but the bucket dimensions were in centimeters. I totally forgot the conversion 1 liter = 1000 cubic centimeters and pondered for a long time.
Encountered a problem asking to output an uppercase letter. My first instinct was to look up ASCII codes, thinking of using a convoluted method like print(chr(ord(input())+32)).
Actually, Python doesn't have a separate char type; all single characters are strings of length 1. So for case conversion, directly using string methods .upper() or .lower() is the correct solution, e.g., input().strip().upper(), which is concise and direct. For such problems in the future, don't keep thinking about converting Unicode.
Based on this problem-solving session, I reorganized the following core syntax points, which can be called a 'quick reference dictionary'.
f"{variable:format}". It avoids cumbersome string concatenation and rounding issues.// and modulo %: The golden combination for time conversion. E.g., 150 minutes = 150 // 60 hours, 150 % 60 minutes.math.ceil(): Essential for problems like 'at least how many buckets/cars are needed' (requires import math).a**0.5 is more concise than math.sqrt(a).s[::-1], don't write loops manually.s.upper() or s.lower()..strip() when reading strings to avoid format errors from trailing spaces/newlines judged by the evaluator.Today marks a good start for my preparation. Although it's just sequential structure, solidifying the basics and getting familiar with Python's 'temperament' is essential. Next up is challenging 'branch structure'. Keep going!