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.
console.log(a); // maps to property "job" console.log("---") console.log(b); // property "none" doesn't exist so b is undefined console.log("---") console.log(rest_props); // row but without "job" property
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
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.
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.
// find the matched speed or continue go down to sub tree match time.cmp(&h) { // spend too much time to eat, speed up Ordering::Greater => Solution::min_eating_speed_inner(piles, h, mid + 1, max), // match, try to find a better (slower) speed _ => { letnew_speed = Solution::min_eating_speed_inner(piles, h, min, mid - 1);
if new_speed > 0 { return new_speed; } mid } } }
fncount_eating_time(piles: &Vec<i32>, speed: i32) ->i32 { // count the time to eat each pile and return sum letsum = piles .into_iter() .map(|pile| { if pile % speed > 0 { return pile / speed + 1 } return pile / speed }) .sum();
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 the mid b. target > mid: continue to find the target from the RIGHT side of the mid 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.
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.
num_i32 = num_i32.checked_sub(1).unwrap_or(100); // 100 // Since it overflows, it will return the default value // that we give to unwrap_or which is 100 for this example
$ spl-token mint 9J7Bmg8Yx4dPsSiiQAvdHuwLS5dCf9ET6jyamwpaJUKR 1
注意,這裏其實你也可以 mint 1 個以上,也可以成功,但這樣這個 token 總量就會有 1 個以上,就不是所謂的 NFT 了。
成功 mint 1 個代幣後,我們馬上關閉該代幣的鑄造功能。
這個關閉事不能再打開的,如此就達成意義上的 NFT 了,每個 token 都是獨一無二。
命令:
1
$ spl-token authorize <代幣 ID> mint --disable
執行結果:
1 2 3 4 5 6
$ spl-token authorize 9J7Bmg8Yx4dPsSiiQAvdHuwLS5dCf9ET6jyamwpaJUKR mint --disable Updating 9J7Bmg8Yx4dPsSiiQAvdHuwLS5dCf9ET6jyamwpaJUKR Current mint authority: 46hytJBhguswo6S8fCcVtR85HEnb9nd1hwMxFWYnSHXc New mint authority: disabled