illustrative abstractions

0%

Discrete Random Variable

Given , a discrete random variable,

Population Mean (Expectancy)

Population Variance

If two discrete random variables $X$ and $Y$ are independent,

Bernoulli Distribution

If success, . If failure, .

Binomial Distribution

$X = $ number of successes in independent repetitions of the experiment.

Define as the th repetition’s success or not. follows a Bernoulli Distribution.

Geometric Distribution

$X = $ number of tries up to and including first success

Let

Given

Such that

Using the same method as above,

Poisson Distribution (To be continued…)

Poisson Distribution is essentially a binomial distribution with very large $n$ (very small time interval).

Description

I am developing a mobile application lately with Swift on iOS, with a provided and working back-end written in Python, with the Flask framework. The back-end project utilises a set of plugins named flask-security to handle login and user-related work.

This is an awesome plugin that works right out of the box. Almost little to no configuration is needed and it works like a charm on the webpage. It utilises a session-based authentication method that stores the key to sensitive information in browsers as cookies, which expires after a certain amount of time for security purposes.

Although session-based authentication is a viable way to set up a web application, this poses certain issues to a mobile client where we cannot store cookies like browsers and shall not record user password and send them out over and over again in every HTTP request.

Initial Solution

Therefore, we will need a token-based authentication method, where a JSON Web Token (JWT) is issued the first time we log in. For all following requests, we package this JWT in the header as a way of verifying legit requests. For our particular plugin flask-security, this is done by sending an application-json request instead of x-www-form-urlencoded to the endpoint login.

BUT…

But, the problem is not that easy. This is where Cross-Site Request Forgery (CSRF) comes into play. If we allow clients to simply authenticate by sending a JSON request with username and password, any other websites can fake that and make malicious requests. How to deal with this and have token-based authentication with CSRF protection at the same time?

More

For more information, checkout

  • Flask-Security 3.0.0 Features

Description

Alter/Nate

Two friends, Alter and Nate, have a conversation:

Alter: Nate, let’s play a game. I’ll pick an integer between 1 and 10 (inclusive), then you’ll pick an integer between 1 and 10 (inclusive), and then I’ll go again, then you’ll go again, and so on and so forth. We’ll keep adding our numbers together to make a running total. And whoever makes the running total be greater than or equal to 100 loses. You go first.

Nate: That’s not fair! Whenever I pick a number X, you’ll just pick 11-X, and then I’ll always get stuck with 99 and I’ll make the total go greater than 100.

Alter: OK fine. New rule then, no one can pick a number that would make the sum of that number and the previous number equal to 11. You still go first. Now can we play?

Nate: Um… sure.

Who wins, and what is their strategy?

Solution

The first player, Nate, is guaranteed to win in this case.

Our goal is to let Nate get the sum of all , which would eventually let him get , a guaranteed win.

Therefore, our strategy for Nate is to let first pick number , which contributes to the offset. Later, for him to increment by each round, if Alter picks any number , Nate can pick the number . Otherwise if Alter picks , Nate then picks . Given the rule that two consecutive number cannot add up to the sum of , Alter can only pick in this case. Such that in these three rounds (Alter, Nate, Alter). Nate will then pick to reach .

If Nate can guarantee he reaches a sum of , he wins the game by reaching .

More

For more puzzles, checkout

  • Jane Street Puzzles

Description

It is quite painful where you have a decent video API, but doesn’t provide you with a thumbnail. This makes it very hard to demonstrate to users what the video looks like a first glance.

Therefore, we have came up with a technique that buffers the first minute of a video and generate a thumbnail on the fly.

Code

Using AVAsset, we can write an extension that dispatches a thread dedicated to generate a thumbnail (also duration of the video in the case below).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
extension AVAsset {
func generateThumbnail(completion: @escaping (UIImage?, Int?) -> Void) {
DispatchQueue.global().async {
let imageGenerator = AVAssetImageGenerator(asset: self)
let time = CMTime(seconds: 60.0, preferredTimescale: 600)
let times = [NSValue(time: time)]
let duration = Int(self.duration.seconds)
imageGenerator.generateCGImagesAsynchronously(forTimes: times, completionHandler: { _, image, _, _, _ in
if let image = image {
completion(UIImage(cgImage: image), duration)
} else {
completion(nil, nil)
}
})
}
}
}

More

For more information, checkout

  • AVAsset - AVFoundation | Apple Developer Documentation
  • Generate images from AVAsset with AVAssetImageGenerator

welcome to my new blog.

for old material, please visit an alternative site.

title

below is a test on code snippets.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<bits/stdc++.h>
#pramga GCC optimize ("Ofast")

using namespace std;

static int fast_io = [] () {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
return 0;
} ();

int main () {
return 0;
}