For Sprout Project Interview only
for (init-expr; cont-expr; loop-expr) {
// code here
}
int N;
cin >> N;
for (int i = 0; i < N; i++) {
cout << i * i << '\n';
}
int N = 5;
while (N > 0) {
cout << N << '\n';
N--;
}
for (int N = 5; N > 0; N--) {
cout << N << '\n';
}
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";
}
for (int x = 1, y = 2; x * y < 10000; x++, y *= 2) {
cout << x * y << '\n';
}
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
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';
}
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';
}
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];
}
for (int x: arr) {
cout << x << '\n';
}
for (int& x: arr) {
cin >> x;
}
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';
}
For Sprout Project Interview Only
N = 5
N2 = (lambda x: x ** 2)(N)
print(N2)
N = 5
square = lambda x: x ** 2
print(square(N))
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)))
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)
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