For & Arrays

For Sprout Project Interview only

A for loop

  • 開始條件、繼續條件、迴圈條件
for (init-expr; cont-expr; loop-expr) {
	// code here
}

int N;
cin >> N;

for (int i = 0; i < N; i++) {
	cout << i * i << '\n';
}

while-loop with more

  • yes
int N = 5;
while (N > 0) {
    cout << N << '\n';
    N--;
}

for (int N = 5; N > 0; N--) {
    cout << N << '\n';
}

Not just ++

  • anything
for (int i = 3; i < 20; i += 2) {
    bool prime = 1;
    for (int j = 2; j * j <= i; j++) {
        if (i % j == 0) prime = 0;
    }

    if (prime) cout << i << " is prime.\n";
}

comma

  • who said only one command at a time?
for (int x = 1, y = 2; x * y < 10000; x++, y *= 2) {
    cout << x * y << '\n';
}
  • tip: you can put everything inside, but you don't have to

Array

  • A list of numbers, things, or whatever data you want.
int arr[5] = {3, 1, 4, 1, 5}; 

cout << arr[0] << '\n'; // 3

cout << arr[1] << ' ' << arr[3] << '\n'; // 1 1

cout << arr[4] << '\n'; // 5

cout << arr[5] << '\n'; // doesn't exist

Inputting an array

int arr[10];

for (int i = 0; i < 10; i++) {
    cin >> arr[i];
}

for (int i = 0; i < 10; i++) {
    cout << "i-th element: " << arr[i] << '\n';
}

Variable-sized

int N;
cin >> N;
int arr[N];

for (int i = 0; i < N; i++) {
    cin >> arr[i];
}

for (int i = 0; i < N; i++) {
    cout << "i-th element: " << arr[i] << '\n';
}

Modifying elements

for (int i = 0; i < N; i++) {
    arr[i] = 4 * i + 3;
}

for (int i = 1; i < N; i++) {
    arr[i] = arr[i] + arr[i - 1];
}

Black Magic

  • What is &?
for (int x: arr) {
    cout << x << '\n';
}

for (int& x: arr) {
    cin >> x;
}

Extra Cool Stuff

pair<int, char> pii = {3, 'c'};
cout << pii.first << ", " << pii.second << '\n';
// 3, c
auto pii_copy = pii;
cout << pii_copy.first << '\n';

auto [decomp_x, decomp_y] = pii;
cout << decomp_y << '\n';

pair<int, char> parr[N];
for (int i = 0; i < N; i++) {
    cin >> parr[i].first >> parr[i].second;
}

for (auto [x, y]: parr) {
    cout << x << ' ' << y << '\n';
}

Lambda (Py)

For Sprout Project Interview Only

Lambda Calculus

  • 沒有微分也沒有積分
  • \(f(x)=x^2\)
  • \(x \mapsto x^2\)
  • \((x \mapsto x^2)(3) = 9\)
  • 不用取名字的函數
  • 程式裡面也可以有不用取名字的函數
  • 圖靈完備,很帥

Some Examples

  • 這不是畫蛇添足嗎
N = 5

N2 = (lambda x: x ** 2)(N)

print(N2)
N = 5

square = lambda x: x ** 2

print(square(N))

Sorting

  • improve code simplicity
def sortKey(x):
    return (x ** 2, x)

arr = [4, 2, -2, -3, 3, 2, 1, 0]

print(sorted(arr, key=sortKey))

print(sorted(arr, key=lambda x: (x ** 2, x)))

Use \(\lambda\) to access attr

people = [
    {"name": "Alice", "age": 25},
    {"name": "Bob", "age": 30},
    {"name": "Charlie", "age": 20}
]
sorted_people = sorted(people, key=lambda person: person["age"])
print(sorted_people)

Composite functions

square = lambda x: x ** 2

def factorial(x):
    if x == 0: return 1

    return x * factorial(x - 1)

comp = lambda f, g: lambda x: f(g(x))

square_then_factorial = comp(factorial, square)
factorial_then_square = comp(square, factorial)

print(square_then_factorial(2)) # 24
print(factorial_then_square(3)) # 36
  • \(h(x) = (f \circ g)(x)=f(g(x))\)

Code

By Brine

Code

  • 9