The f-strings in Python3.6
Updates:
Walrus Operator (Python 3.8)
Intro
Today, I would like to share a neat introduced in Python3.8 - the walrus operator. It sparked quite a bit of discussion before ultimately being accepted. 🤔️
The power of the walrus operator lies in turning an assignment statement into an assignment expression. You may be wondering: what exactly is the difference between a statement and an expression?
Expression vs Statement
In programming languages, you may encounter many different constructs. Broadly, they can be divided into two categories - expressions and statements. An expression is evaluated and produces a value, whereas a statement performs an action and does not return anything.
Pattern Matching in Python 3.10
Intro
Today I want to talk about the new feature bring in Python 3.10 – Pattern matching 🎉
Those who have learned C language must be familiar with the following switch statement:
System programming: stack and heap
Intro
If you’ve been using dynamic languages like Python, JavaScript, etc., you probably won’t notice the difference between stack and heap. Because these languages have garbage collectors (GCs) that will automatically manage memory for you, you just need to program at a high level without considering the details. The bad news is that GC is not a cost-free design. No matter how well-designed a GC algorithm is, the performance of the code will be degraded to some extent. If you have been in programming for a long time, you may have heard something like “the recursion explodes the stack”. You may or may not click on the search engine to understand the difference between stack and heap. Chances are you just clicked into this article :)
Vim Macro: Recording, Editing, and Execution
Intro
In the process of learning Vim, the most enlightening sentence for me is :
Vim is a kind of programming language
How to use the semantic actions to generate the symbol tables in ANTLR4
What are the semantic actions
When the parser processes the input code, it not only determines whether the syntax is correct but also performs some useful actions. These actions are called semantic actions. In fact, it is a piece of code, which is generally embedded in the rules of the grammar file. Then when the parser applies this specific rule, the code you set will be executed. From another perspective, semantic actions are actually “triggers”, and the trigger condition is that the parser applies the corresponding rules.
Boyer-Moore Majority Voting Algorithm Explained
Intro
Today I coded the Leetcode 169. Majority Element again. I vaguely remember what the optimal solution is called Boyer-Moore Majority Voting Algorithm. However, I have no idea what is except for its name. So I plan to systematically learn the principle of this algorithm and summarize it to write this blog. I once heard that:
If you want to master something, teach it :)
So, I’m here today to share this algorithm with you, and try to teach you this method in plain language, so let’s get started :)
Solution of Proj3.Ants vs SomeBees of CS61A (2021-Fall)
Intro
I have finished the first two projects - Hog and Cats. The first two projects are relatively simple and uncomplicated. But today, the difficulty of the third project has indeed increased (you can see how complicated this is by looking at the rules of the game). It feels like Plants vs. Zombies
So I’m going to write a blog to sort out the ideas when writing code. 🤗
Phase 1: Basic gameplay
Problem 1 (1 pt)
Part A: Currently, there is no cost for placing any type of
Ant, and so there is no challenge to the game. The base classAnthas afood_costof zero. Override this class attribute forHarvesterAntandThrowerAntaccording to the “Food Cost” column in the table below.
Lab14 solutions (UCB CS61A@2021-Fall)
Trees
Q1: Prune Min
Write a function that prunes a
Treetmutatively.tand its branches always have zero or two branches. For the trees with two branches, reduce the number of branches from two to one by keeping the branch that has the smaller label value. Do nothing with trees with zero branches.Prune the tree in a direction of your choosing (top down or bottom up). The result should be a linear tree.
Hw10 solutions (UCB CS61A@2021 Fall)
BNF
Q1: Grouping and Pipes
In this question, you will add support for grouping and piping.
Recall that grouping allows for an entire regular expression to be treated as a single unit, and piping allows for a pattern to match an expression on either side. Combined, these will let us create patterns which match multiple strings!
Define the
groupandpipeexpressions in your grammar.
- A
groupconsists of anyregexexpression surrounded by parentheses (()).- A
pipeoperator consists of aregexexpression, followed by a pipe (|) character, and lastly followed by anotherregexexpression.For example,
r"apples"would match exactly the phrase “apples” in an input. If we wanted our pattern from before to match “oranges” as well, we could expand ourrstringto do so using groupings and pipes:r"(apples)|(oranges)".
Lab12 solutions (UCB CS61A@2021 Fall)
Regular Expressions
Q1: Calculator Ops
Write a regular expression that parses strings written in the 61A Calculator language and returns any expressions which have two numeric operands, leaving out the parentheses around them.
We need to write a regular expression to match a pattern - (operand operator1 operator2). The operands consist of +, -, *, /. We can use [] here. Don’t forget to put a \ in front of - to escape it.
Hw09 solutions (UCB CS61A@2021 Fall)
Q2: Roman Numerals
Write a regular expression that finds any string of letters that resemble a Roman numeral and aren’t part of another word. A Roman numeral is made up of the letters I, V, X, L, C, D, M and is at least one letter long.
For the purposes of this problem, don’t worry about whether or not a Roman numeral is valid. For example, “VIIIII” is not a Roman numeral, but it is fine if your regex matches it.