## Category Archives: Programming

#### Pizza price calculation | 计算披萨价钱🍕

Many of us are still in coronavirus lockdown. We are very proud of all of you for staying healthy and safe.😀

In today’s google classroom, we ordered some pizza for lunch. 🍕 We made a Python program to calculate 👩💻 which pizza size gives us the best value.

Our goal: to find the lowest price per bite. To get that, we divide cost by size.

```
def pizzaBitePrice(radius,price):
area=radius**2*3.14
unit_price = price/area
return(unit_price)
# small pizza
pizzaBitePrice(7,12)
0.07799298063174313
# medium pizza
pizzaBitePrice(8,14.75)
0.06593351910828026
# large pizza
pizzaBitePrice(9,14.75)
0.057993237398757565
```

👍The conclusion is: the large size one gives us the best value with price of 5.7 cents (plus tax) per bite.

#### Welcome to our New Google Classroom!!😃

Hello everybody!😃 Welcome back to Magic Math Mandarin. Since we are staying home because of covid-19, we brought our classes to google classroom so our students can keep on learning even during this pandemic👩🏫. Now we can all communicate with each other online💻. In this classroom we will be learning Chinese🈷, math➗, and programming👩💻. Our teachers will put new assignments everyday about each topic. If you would also like to join our wonderful classrooms then here is the class code **mtxl6j4**

Remember to stay home and don’t get sick!😷 Please join our classroom today!👍💖

#### Loops in Swift Code | 循环 Xúnhuán

Hello everybody! Today in class we have learned 👩🏫 how to write loops 循环 (Xúnhuán) in Swift code 👩💻. We learned two kinds of loops ➰.

The first type of loop that we learned was a loop ➿ that repeats itself for a certain amount of times⏲. We learned 🏫 how to write this kind of loop in Swift code. Here’s an example.

for i in 1 … 4 {

moveForward()

turnRight()

moveForward()

turnLeft()

}

for i in 1 ... 4 { moveForward() turnRight() moveForward() turnLeft() }

Next we learned another loop ⭕ that repeats itself until the condition is false🙅♀️. This loop is called a while loop. Here’s an example.

while thereIsACoin {

moveForward()

collectCoin()

}

while thereIsACoin { moveForward() collectCoin() }

In a while loop the loop will go forward ⏩ and collect the coin 💰 nonstop until the is no more coins ☹ in front of the coin collecter to collect. This is what we have accoplished today in class 👩🎓👨🎓.

We will learn much more very soon. Goodbye!!😃😃

In the meantime, please do your homework, and use the Swift documentation (aka “instructions”).

#### Learning Swift | 学习Swift

This weekend we started our first lesson on Swift. Swift is the language used by Apple across its products.

The class first tried using XCode, which is Apple’s IDE for Swift. But not every child is familiar with the Mac. So we switched to a learn Swift phone app, which worked well. But the class really love Swift Playground. It is like Scratch to them. The difference is that: we are going to make an app in two months using Swift.

The kids enjoyed programming a lot! They didn’t want to stop.

The only minor issue is that it only works on iPads.

#### Ipython Shell Keyboard shortcuts 键盘快捷键 （Kuàijié jiàn)

In this past weekend’s coding class we practice keyboard ⌨ shortcuts in the interactive Python shell (Ipython).

Since most of the students are learning typing ⌨, practicing these keyboard shortcuts actually help them learn typing by reinforcing memory of where the keys are.

The keyboard shortcuts allows you to minimize finger movements on the keyboard, which means you don’t even have to lift your hand ✋ for the “Backspace”, ‘Home’, ‘End’ or arrow keys (most of the time). Spend 15 minutes per day practicing these. Pretty soon, you will be a fast coder.

🚕**Navigation**

keystroke | Action |
---|---|

`Ctrl-a` |
Move cursor to the beginning of the line |

`Ctrl-e` |
Move cursor to the end of the line |

`Ctrl-b` or the left arrow key |
Move cursor back one character |

`Ctrl-f` or the right arrow key |
Move cursor forward one character |

👦**Text Entry**

Keystroke | Action |
---|---|

Backspace key | Delete previous character in line |

`Ctrl-d` |
Delete next character in line |

`Ctrl-k` |
Cut text from cursor to end of line |

`Ctrl-u` |
Cut text from beginning of line to cursor |

`Ctrl-y` |
Yank (i.e. paste) text that was previously cut |

`Ctrl-t` |
Transpose (i.e., switch) previous two characters |

🐞**Command History**

Keystroke | Action |
---|---|

`Ctrl-p` (or the up arrow key) |
Access previous command in history |

`Ctrl-n` (or the down arrow key) |
Access next command in history |

`Ctrl-r` |
Reverse-search through command history |

🐳**Miscellaneous**

Keystroke | Action |
---|---|

`Ctrl-l` |
Clear terminal screen |

`Ctrl-c` |
Interrupt current Python command |

`Ctrl-d` |
Exit IPython session |

The shortcuts are referenced from Python Data Science Handbook. As its author Jake Vanderplas says, these shortcuts are not inherent in Ipython shell itself, but are based on GNU Readline library.

#### Use snippets to code faster | snippet 加速编程

Today’s weekend programming lesson we covered how to save code snippets in a code editor, such as Sublime Test 3 (ST) and Visual Studio Code (code).

🙂 You can save these chunks of code so next time you wouldn’t have to type it again.

Until NeuralLink or other developments that can help us bypass typing all together, we want to find ways to save ourselves time from typing code. Using snippet is a must for children, who may not be great at typing. Here is how to do it in Sublime Test 3 (ST) and Visual Studio Code (code).

**😎Sublime Test**:

Tools – > Developer -> New Snippet.

**😎VS Code**:

`Shift`

+ `Control`

+ `p`

**😎 **Shift + Control + F: to search for a string in your folder (perhaps the name of the snippet you saved).

**💡Write the snippet that can be used by the code editor:**

Snippets are json files. Use this ➡ web app, or some packages, or write json if you prefer to put the code into the json format.

**Explaining .json snippet in VS Code**:

- The first set of “” encloses the name of the snippet (call it anything you like).
- Prefix defines a prefix used in the IntelliSense drop down. For example, if you have a snippet for plotting, you may want to prefix it with “plot”.
- body is the snippet content.
- Note that: $1, $2 for tab stops
- Description is the description. When you start typing the prefix of a snippet, its description will come up.

By the way, to quickly see what are the keyboard shortcuts are available in VS Code:

Ctrl + Shift + p

#### Logit transform | 分数对数转换

After we discussed logarithm (‘log’) last week, we explored a bit on some commonly used methods that have log embedded in them. For example, the logit function, or logit transform (using the “natural” logarithm). We explained its definition by the following Python code.

>>> epsilon=0.001

>>> def logit(c):

>>> d = np.log((c+epsilon)/(1+ epsilon-c))

>>> return d

The following is the inverse, which is to bring what was transformed back to what it was before.

>>> def inverse_logit(a):

>>> b = ((1+ epsilon)*np.exp(a) – epsilon)/(np.exp(a)+1)

>>> return b

>> print(logit(0.1)) #-2.1883847407670785

>>> print(inverse_logit(logit(0.1))) #0.09999999999999999

It is much more revealing on what the logit transform is doing by looking at some pictures of how this works. See how fast when it is transformed! Why it is stretched instead of being shrunk? We know that taking log is to do division multiple times (recall log10 of a number is how many times it needs to divide by 10 in order to become 1). But when it applies to numbers between 0 and 1, it gives us the opposite effect. A positive small number less than one has to divide by 10 negative times to become 1. For example, 0.01 needs to be divided by 10 negative two times to be restored to 1. That’s why you see that y axis we have negatives.

On the other hand, we also have positives in the y-axis. That’s because about half of the numbers `(c+epsilon)/(1+ epsilon-c)`

(the odds) are large positive numbers. Play around with it and you will surely get it.

>> x = np.linspace(0,1,1000)

>>> y = logit(x)

>>> plt.scatter(x=x, y=y, alpha=0.3)

>>> title =”logit transform”

>>> plt.title(“%s”%title)

>>> plt.xlabel(“numbers between 0 and 1 (inclusive”)

>>> plt.ylabel(“after logit transform”)

>>> plt.xlim(-6, 6)

>>> plt.ylim(-6, 6)

>>> plt.gca().set_aspect(‘equal’, adjustable=’box’)

>>> plt.draw()

Look at the same plot with the axis scaled differently:

#### zero, one and two | 零，一，二

It is not easy for a young child to comprehend multiplication by 1, as how they are taught in school is often the robotic multiplication table. She or he can very quickly answer mutiplications by 2, or 3. Because of this, questions like “what is the product of 1,2, 3, 4” (i.e. 4 factorial) can get a wide range of answers because the number “1” confuses the young mind.

Pychologist says that an infant learns the number 2 before the number 1. And we can see why: with 2, there is something to compare against, like two fingers. If there is only one finger, there is no variation, it is confusing.

When we teach multiplication, don’t forget to show that math is an integral part of the real world around us. It is invented to simplify addition. Multiply by 1 means just the thing itself. Multiply by 2 means adding two of this thing together. Multiply by 3 means adding three of the thing together. The thing can be a bag of candies or the footage of a home.

Finally, we should show children how to use computers (not calculators) to do computations. While a question like “give me the sum from 1 to 199” can be solved within seconds with math tricks, a slightly different question “give me the product from 1 to 199” won’t work with the same trick. But if you know how to make the computer do the job, you can still answer it within seconds.

#### argmax, argmin argsort and quick sort | 快速排序

This Saturday class we went over indexing and ordering a group of items by their sorted indices. For those who are more advanced, please go over the section on quick sort.

For example,

>> import numpy as np

>>> packpack =np.array([‘snack’,’book’,’pen’,’eraser’,’apple’])

# Position of the biggest word (alphabetically)

`>>> np.argmax(packpack)`

[out]: 0

# Position of the smallest word (alphabetically)

`>>> np.argmin(packpack)`

[out]: 4

# Position of the words if we are to sort them alphabetically

`>>> np.argsort(packpack)`

[out]: array([4, 1, 3, 2, 0], dtype=int64)

Now let us sort them:

`>>> packpack[np.argsort(packpack)]`

[out]: array([‘apple’, ‘book’, ‘eraser’, ‘pen’, ‘snack’], dtype='<U6′)

Then we tried sorting numbers:

numbers = np.array([2,3,5,7,1,4,6,15,5,2,7,9,10,15,9,17,12])

`>>> numbers[np.argsort(numbers)]`

[out]: array([ 1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 9, 9, 10, 12, 15, 15, 17])

Finally we dig deeper: how do you really sort things fast systematically? Using quick sort!

def quick_sort(data): """快速排序""" if len(data) >= 2: # 递归入口及出口 mid = data[len(data)//2] # 选取基准值，也可以选取第一个或最后一个元素 left, right = [], [] # 定义基准值左右两侧的列表 data.remove(mid) # 从原始数组中移除基准值 for num in data: if num >= mid: right.append(num) else: left.append(num) return quick_sort(left) + [mid] + quick_sort(right) else: return data numbers= [2,3,5,7,1,4,6,15,5,2,7,9,10,15,9,17,12] Backpack = ['snack','book','pen','eraser','apple'] print(quick_sort(Backpack)) print(quick_sort(numbers))