Improve Your Using of Asterisk, Slash, Underscore and Operator Overloading
Did you know, that at your profile, after clicking on big percent number of your progress, you may see module and methods you have already used in your shared solutions. If you want to discover all CheckiO features, visit our tutorial. It's a longread, but it's worth it!
Today's missions demand different skills from you! You need to cut rectangle into squares, broke an iteger into cubes and count a number of 🛣 to Rome! We hope you would like them all! 🐍
Cut Into Squares by freeman_lex - You are given a rectangle, which is defined by its width w and height h, both positive integers. We allow a rectangle to be cut into two smaller rectangles with either a straight horizontal or a straight vertical cut at any integer position. For example, one of the possible ways to cut the rectangle (5, 8) in two pieces would be to cut it into (2, 8) and (3, 8). Another way would be to cut it into two pieces is (5, 4) and (5, 4) etc. The resulting smaller pieces can then be further cut into smaller pieces, as long as the length of the side being cut is at least two to allow a cut. You are NOT allowed to cut multiple pieces in one motion of the blade by stacking pieces on top of each other. Your task is to keep cutting the given rectangle into smaller pieces until each piece is a square, that is, the width of each piece equals its own height. This is always possible, since you could just keep cutting until the size of each piece has become 1-by-1. However, this function should return the smallest number of cuts that makes each piece a square.
cut_into_squares(4, 4) == 0 cut_into_squares(4, 2) == 1 cut_into_squares(7, 6) == 4
Sum of Distinct Cubes by freeman_lex - Positive integers can be broken down into sums of distinct cubes of positive integers, sometimes in multiple different ways. Your function should find and return the descending list of distinct cubes whose sum equals the given positive integer n. If it is impossible to express n as a sum of distinct cubes, return None. If n allows several breakdowns into sums of distinct cubes, the function must return the lexicographically highest solution that starts with the largest possible first number, followed by the lexicographically highest representation of the rest of the number n-a*a*a. For example, called with n = 1729, this function should return [12, 1] instead of [10, 9].
sum_of_cubes(1729) == [12, 1] sum_of_cubes(8) ==  sum_of_cubes(11) == None
All Roads Lead to Rome! by freeman_lex - You are standing at the point (x, y) in the lattice grid of pairs of non-negative numbers, and wish to make your way to the origin point (0, 0). At any point, you are allowed to move either one step left or one step down. Furthermore, you are never allowed to step into any of the points in the tabu list (origin is never in tabu). This function should add up the number of different paths that lead from the point (x,y) to the origin (0,0) under these constraints.
lattice_paths((3, 3), ) == 20 lattice_paths((3, 4), [(2, 2)]) == 17 lattice_paths((10, 5), [(6, 1), (2, 3)]) == 2063
All articles are united by a common theme: better undertanding of using some operators in Python. It's asterisk (*), slash (/), undrescore (_) and math operators overloading. Bet you will find something 🆕 for you!
What Are Python Asterisk and Slash Special Parameters For? - Whenever you think of Python’s asterisk operator (*), you most likely think of multiplication or exponentiation. Similarly, you probably associate the forward slash operator (/) with division. But you can also use the bare asterisk and slash as special parameters in function headers. These do something completely unrelated to mathematics.
Usages of Underscore - We will take a look at all the use cases there are for _ in Python. There are a couple of places where _ has a very special role syntactically, and we will talk about those places. We will also talk about the uses of _ that are just conventions people follow, and that allow one to write more idiomatic code.
Operator Overloading in Python - Python is an object-oriented programming language and one of its features is that it supports operator overloading, that is, it allows us to redefine the behavior of native operators (+,-, *, /, +=, -=, **, etc.).1 This means we can create code with greater readability, since we use native operators to define new representations, comparisons, and operations between objects that we have created. To illustrate how operator overloading works, I’ll walk you through how to redefine the behavior of the + and - operators using the special __add__ and __sub__ methods of Python classes.
I believe, we all of the second type! 😎
🙌 Thanks for your attention! Hope to meet you at CheckiO. We are really interested in your thoughts! Please, leave a comment below! ⤵
Welcome to CheckiO - games for coders where you can improve your codings skills.
The main idea behind these games is to give you the opportunity to learn by exchanging experience with the rest of the community. Every day we are trying to find interesting solutions for you to help you become a better coder.Join the Game