Maya Python API: Hello world!
Write a custom Maya command that print “Hello world!” in Maya console.
There are Python API 1.0 and 2.0. The syntax would be different.
We will integrate with API 2.0 in this example.
1 | import sys |
Open Maya Plug-in Manager, and hit “Browse” to load our command.

Then we can run our command
1 | helloworld; |
We can also call it in Python:
1 | from maya import cmds |
GO: Proto Buffer
Doc:
https://developers.google.com/protocol-buffers/docs/gotutorial
Proto Buffer
Download Proto Buffer:
https://developers.google.com/protocol-buffers/docs/downloads
Check if protoc
installed
1 | $ protoc --version |
Install protoc-gen-go
1 | $ go install google.golang.org/protobuf/cmd/protoc-gen-go@latest |
Add these text to ~/.bashrc
or ~/.zshrc
(depends on what shell you use)
1 | export GOPATH=$HOME/go |
This step is important to avoid Error "protoc-gen-go: program not found or is not executable"
.proto
File
.proto
example:
1 | syntax = "proto3"; |
Scaffolding:
1 | project/ |
Compile
1 | $ cd proto |
plugins=grpc
is needed or the output file will lack some functions
Install grpc
1 | $ go get -u google.golang.org/grpc |
Reference
- How to Solve Error: unable to determine go import path: https://developpaper.com/protocol-gen-go-unable-to-determine-go-import-path-when-exporting-protocol/
Rust: Borsh Serialize
Study how to use Borsh Serialize. When we write vanila Solana program, we need to serialize and deserialize accounts’ data by ourselves.
1 | // Cargo.toml |
1 | // main.rs |
Run the code:
1 | $ cargo run |
Javascript: Rest Parameter
1 | // rest.js |
run the code:
1 | $ node rest.js |
More
Solve Leetcode in Rust: Implement strStr()
Problem
Problem: Implement strStr()
Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. If needle is empty, return 0.
Thoughts
strategies:
- two pointers to compare each characters of needle and haystack (2 while loops)
- iterate through haystack to get slices to compare with needle
- iterate through haystack to get slices. Hash each slice and compare the hashed with the hashed needle
- use
find
method
Code
Two While Loops
1 | impl Solution { |
Compare String Slice
1 | impl Solution { |
Use Manual Hash
1 | const MAGIC_NUM: u64 = 31; // can be any number but should not too big |
Use Find
1 | impl Solution { |
If An Unicode String Given
1 | let str = String::from("好"); |
Since the len()
returns the length of bytes not characters, we need to change our code if unicode strings given.
1 | impl Solution { |
Conculsion
- utilize string slice or
find
looks easier. - in some situations, hash may be useful. However, for this case, it may not be the best practise.
Solve Leetcode in Rust: Rebuild A Tree From An Inorder and A Preorder Traversal
Problem
Given a inorder-traversal result and a preorder-traversal result of a tree, Figure out the tree structure.
For example:
- inorder: 1,2,3,4,5,6,7
- preorder: 5,2,1,4,3,6,7
What is the structure of this tree? Can we build a tree based on these two clues?
Thoughts
The first element of preorder-traversal must be the Root of a tree. On the other hand, we can determine left and right from inorder-traversal after we get the root. As the result, we can again get inorder and preorder result of left-sub-tree and right-sub-tree, then both left and right redo above process until they get no or only one element.
Overview the tree for now
So we can expect we can get the whole tree if we repeat the process.
Code
1 | pub struct Node { |
Solve Leetcode in Rust: Koko Eating Bananas
Problem
Problem: Koko Eating Bananas
Let’s draw some pictures to understand this quesiton. If there are 4 piles of bananas like below, the given array would be [3, 2, 4, 1]
.
Then if Koko can eat 3 bananas per hour, it will takes her 5 hours to eat all the bananas like blow.
Now, if the limit hours h
is 5 then 3 bananas per hour satisfies. However, if h
, for example, is 3, then Koko need to SPEEDS UP to finish all the bananas. On the other hand, if h
is 10 which means we still have time right? So Koko should SLOW DOWN to certain speed to make herself chill but still can finish all the bananas in time.
So what is the best eating speed is the question!
Thoughts
- The maximum of speed should equals to the number of the bananas of the tallest pile
- The minimun of speed should equals to 1
Which means we are trying to find the best from 1
~ piles.max()
. Searching a value from a sorted array… maybe binary search is a soslution.
Code
Try #1
1 | use std::cmp::Ordering; |
Solve Leetcode in Rust: Find The First And The Last Position of Element In A Sorted Array
Thoughts
The core concept is to find the target from a sorted array which is quite suitable to use binary serach I think.
Here are my instructions:
- Get the middle element,
mid
- Compare the
mid
with the target. there will be 3 branches:
a.target
<mid
: continue to find the target from the LEFT side of themid
b.target
>mid
: continue to find the target from the RIGHT side of themid
c.target
=mid
: In this case,[index_of_mid, index_of_mid]
will be our base answer so keep it. Then based on this answer, we need to go further to the both left and right to seek if there is other elements match to the search target.
For example, if the given array is [1, 2, 3, 4, 4, 4, 4, 6, 9]
, and the search target is 4
.
Then we are trying to get this green range.
When we go through the instructions, we will get the middle element, and hit the c case.
Then base on this position, we seek towar left and right to get the final range.
So the answer should be [3, 6]
Code
Try #1
1 | impl Solution { |
Try #2
1 | use std::cmp::Ordering; // Don't forget to import this |
Conclusions
Improvements
- avoid overflows in some edge cases
- move the recursion function into
Solution
struct - more rusty coding style
Learned
- Combo of
cmp
&match
- How to use the function itself in a implementation
->Solution::bst_search_index_inner
Rust: checked_sub & unwrap_or
Just a quick note for these two functions I learned today.
checked_sub
is used to check if the abstraction of an integer overflows. For example:
1 | let mut num_u32: u32 = 0; |
To prevent it, we can use the combo of checked_sub
and unwrap_or
. checked_sub
will return Option<self>
which means if there is no overflow occurs, we get Some(substraction_result)
, else we will get None
. unwarp_or
can then give the result a default value if we get None
or just unwrap Some(substraction_result)
to return substraction_result
. Let’s look at the code.
1 | let mut num_i32 = i32::MIN + 1; // -2147483648 + 1 |
That’s it for today!