:3
Everything is barely weeks. Everything is days. We have minutes to live.
- about me
- Github: @r1bb1t-h0l3
Everything is barely weeks. Everything is days. We have minutes to live.
Posted
Over the past month, I’ve been focusing on improving my fundamental programming skills, not by learning more Python tricks, but by strengthening my core understanding of programming concepts. A lot of this has involved working on DMOJ problems and reading Learn to Code by Solving Problems by Daniel Zingaro.
Recently, while solving a problem from the Timus judge, the book introduced the top-down approach. This approach has been incredibly helpful for me, especially when it comes to structuring code and using functions effectively. The top-down method encourages you to start by breaking down a task into broad steps and then narrowing down what each function needs to accomplish.
I’ve often struggled with organizing tasks in code, particularly when it comes to breaking down a complex problem into manageable steps. The top-down approach provides a clear way to do this. By writing commentary for each function, you not only clarify what the function does but also ensure your code is easier to understand and maintain.
Below is an example from the Timus judge Action figures problem, using the top-down approach with helpful comments. This method helped me write clearer, more concise programs, and I found it valuable in organizing my thoughts around function-based programming.
#Read input
#Check whether all boxes are OK
#Obtain a new lit of boxees with only left and right heights
#Sort boxes
#Determine whether boxes are organised
def read_boxes(n):
"""
n is the number of boxes to read.
Read the boxes from the input, and return them as a list of boxes;
Each box is a list of action figure heights.
"""
boxes = []
for i in range(n):
box = input().split()
box.pop(0)
for i in range(len(box)):
box[i] = int(box[i])
boxes.append(box)
return boxes
def all_boxes_ok(boxes):
"""
boxes is a list of boxes; each box is a list of action figure heights.
Return True if each box in boxes has its action figures in
nondecreasing order of height, False otherwise.
"""
for box in boxes:
if not box_ok(box):
return False
return True
def box_ok(box):
"""
box is the list of action figure heights in a given box.
Return True if the heights in box are in nondecreasing order,
False otherwise.
"""
for i in range(len(box) - 1):
if box[i] > box[i + 1]:
return False
return True
def boxes_endpoints(boxes):
"""
boxes is a list of boxes; each box is a lit of action figure heights.
return a list, where each value is a list of two values:
the heights of the leftmost and rightmost figures in a box.
"""
endpoints = []
for box in boxes:
endpoints.append(box[0], box[-1])
return endpoints
def all_endpoints_ok(endpoints):
"""
endpoints is a list, where each value is a list of two values:
the heights of the leftmost and rightmost action figures in a box.
Requires: endponts is sorted by action figure heights.
Return True if the endpoints came from boxes that can be put in order,
False otherwise.
"""
maximum = endpoints[0][1]
for i in range(1, len(endpoints)):
if endpoints[i][0] < maximum:
return False
maximum = endpoints[i][1]
return True
#Main Program
#Read input
n = int(input())
boxes = read_boxes(n)
#Check whether all boxes are OK
if not all_boxes_ok(boxes):
print('NO')
else:
#Obtain a new list of boxes with only left and ight heights
endpoints = boxes_endpoints(boxes)
#Sort boxes
endpoints.sort()
#Determine whether boxes are organised
if all_endpoints_ok(endpoints):
print('YES')
else:
print('NO')
Author
ribbit
Categories
coding challenge