Skip to content

Commit e5f267c

Browse files
committed
8/7/23
- EPI: Apply permuation (Python/CPP) - EPI: Convert int to string and vice versa (Python/CPP) - Anki update
1 parent 976c003 commit e5f267c

File tree

7 files changed

+168
-15
lines changed

7 files changed

+168
-15
lines changed

.vscode/settings.json

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,74 @@
5959
"format": "cpp",
6060
"stacktrace": "cpp",
6161
"__locale": "cpp",
62-
"locale": "cpp"
62+
"locale": "cpp",
63+
"__bits": "cpp",
64+
"__config": "cpp",
65+
"__debug": "cpp",
66+
"__errc": "cpp",
67+
"__mutex_base": "cpp",
68+
"__node_handle": "cpp",
69+
"__nullptr": "cpp",
70+
"__string": "cpp",
71+
"__threading_support": "cpp",
72+
"__tuple": "cpp",
73+
"atomic": "cpp",
74+
"barrier": "cpp",
75+
"bit": "cpp",
76+
"bitset": "cpp",
77+
"cctype": "cpp",
78+
"cfenv": "cpp",
79+
"cinttypes": "cpp",
80+
"clocale": "cpp",
81+
"codecvt": "cpp",
82+
"concepts": "cpp",
83+
"condition_variable": "cpp",
84+
"csetjmp": "cpp",
85+
"csignal": "cpp",
86+
"cstdarg": "cpp",
87+
"cstddef": "cpp",
88+
"cstdint": "cpp",
89+
"cstdio": "cpp",
90+
"cstdlib": "cpp",
91+
"cstring": "cpp",
92+
"ctime": "cpp",
93+
"cwchar": "cpp",
94+
"cwctype": "cpp",
95+
"exception": "cpp",
96+
"coroutine": "cpp",
97+
"fstream": "cpp",
98+
"future": "cpp",
99+
"iomanip": "cpp",
100+
"ios": "cpp",
101+
"istream": "cpp",
102+
"latch": "cpp",
103+
"limits": "cpp",
104+
"memory": "cpp",
105+
"mutex": "cpp",
106+
"new": "cpp",
107+
"numbers": "cpp",
108+
"numeric": "cpp",
109+
"optional": "cpp",
110+
"ostream": "cpp",
111+
"queue": "cpp",
112+
"semaphore": "cpp",
113+
"sstream": "cpp",
114+
"stack": "cpp",
115+
"stdexcept": "cpp",
116+
"streambuf": "cpp",
117+
"typeindex": "cpp",
118+
"typeinfo": "cpp",
119+
"algorithm": "cpp",
120+
"*.tcc": "cpp",
121+
"cuchar": "cpp",
122+
"expected": "cpp",
123+
"iterator": "cpp",
124+
"shared_mutex": "cpp",
125+
"spanstream": "cpp",
126+
"stdfloat": "cpp",
127+
"stop_token": "cpp",
128+
"syncstream": "cpp",
129+
"thread": "cpp"
63130
},
64131
"[python]": {
65132
"editor.defaultFormatter": "ms-python.autopep8"

anki/Competitive Programming.apkg

3.83 KB
Binary file not shown.

elements-of-programming-interviews/cpp/apply_permutation.cc

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,43 @@
22

33
#include "test_framework/generic_test.h"
44
using std::vector;
5+
void ApplyPermutationLinearSpace(vector<int> perm, vector<int>* A_ptr) {
6+
vector<int>& A = *A_ptr;
7+
vector<int> result(A.size(), 0);
8+
for (int i = 0; i < perm.size(); i++) {
9+
int idx = perm[i];
10+
result[idx] = A[i];
11+
}
12+
for (int i = 0; i < result.size(); i++) {
13+
A[i] = result[i];
14+
}
15+
return;
16+
}
17+
518
void ApplyPermutation(vector<int> perm, vector<int>* A_ptr) {
6-
// TODO - you fill in here.
19+
vector<int>& A = *A_ptr;
20+
if (A.size() == 0) {
21+
return;
22+
}
23+
int idx = 0;
24+
int curr = A[idx];
25+
for (int i = 0; i < perm.size(); i++) {
26+
int newIdx = perm[i];
27+
if (newIdx == -1) {
28+
continue;
29+
}
30+
while (newIdx != -1) {
31+
int next = A[newIdx];
32+
A[newIdx] = curr;
33+
curr = next;
34+
perm[idx] = -1;
35+
idx = newIdx;
36+
newIdx = perm[newIdx];
37+
}
38+
}
739
return;
840
}
41+
942
vector<int> ApplyPermutationWrapper(const vector<int>& perm, vector<int> A) {
1043
ApplyPermutation(perm, &A);
1144
return A;

elements-of-programming-interviews/cpp/string_integer_interconversion.cc

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,39 @@
44
#include "test_framework/test_failure.h"
55
using std::string;
66

7+
#define ASCII_ZERO 48
8+
79
string IntToString(int x) {
8-
// TODO - you fill in here.
9-
return "0";
10+
if (!x) {
11+
return "0";
12+
}
13+
bool neg = (x < 0);
14+
string val;
15+
while (x) {
16+
int digit = abs(x % 10) + ASCII_ZERO;
17+
val += digit;
18+
x /= 10;
19+
}
20+
if (neg) {
21+
val += "-";
22+
}
23+
reverse(val.begin(), val.end());
24+
return val;
1025
}
26+
1127
int StringToInt(const string& s) {
12-
// TODO - you fill in here.
13-
return 0;
28+
bool neg = (s[0] == '-');
29+
bool skipFirstChar = neg || (s[0] == '+');
30+
unsigned place = 1;
31+
int total = 0;
32+
for (int i = s.length() - 1; i >= (skipFirstChar ? 1 : 0); i--) {
33+
char digit = s[i] - ASCII_ZERO;
34+
total += (digit * place);
35+
place *= 10;
36+
}
37+
return total * (neg ? -1 : 1);
1438
}
39+
1540
void Wrapper(int x, const string& s) {
1641
if (stoi(IntToString(x)) != x) {
1742
throw TestFailure("Int to string conversion failed");

elements-of-programming-interviews/problem_mapping.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,15 +312,15 @@ problem_mapping = {
312312
},
313313
"5.10 Permute the elements of an array": {
314314
"C++: apply_permutation.cc": {
315-
"passed": 0,
315+
"passed": 101,
316316
"total": 101
317317
},
318318
"Java: ApplyPermutation.java": {
319319
"passed": 0,
320320
"total": 101
321321
},
322322
"Python: apply_permutation.py": {
323-
"passed": 0,
323+
"passed": 101,
324324
"total": 101
325325
}
326326
},
@@ -482,15 +482,15 @@ problem_mapping = {
482482
},
483483
"6.01 Interconvert strings and integers": {
484484
"C++: string_integer_interconversion.cc": {
485-
"passed": 0,
485+
"passed": 15002,
486486
"total": 15002
487487
},
488488
"Java: StringIntegerInterconversion.java": {
489489
"passed": 0,
490490
"total": 15002
491491
},
492492
"Python: string_integer_interconversion.py": {
493-
"passed": 0,
493+
"passed": 15002,
494494
"total": 15002
495495
}
496496
},

elements-of-programming-interviews/python/apply_permutation.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,18 @@
44

55

66
def apply_permutation(perm: List[int], A: List[int]) -> None:
7-
# TODO - you fill in here.
7+
for i, idx in enumerate(perm):
8+
if idx == -1:
9+
continue
10+
val = A[idx]
11+
nextIdx = perm[idx]
12+
while nextIdx != -1:
13+
nextVal = A[nextIdx]
14+
A[nextIdx] = val
15+
val = nextVal
16+
perm[idx] = -1
17+
idx = nextIdx
18+
nextIdx = perm[idx]
819
return
920

1021

elements-of-programming-interviews/python/string_integer_interconversion.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,26 @@
33

44

55
def int_to_string(x: int) -> str:
6-
# TODO - you fill in here.
7-
return '0'
6+
if not x:
7+
return "0"
8+
neg = "-" if x < 0 else ""
9+
x = x*-1 if neg else x
10+
string = []
11+
while x:
12+
string.append(str(x % 10))
13+
x //= 10
14+
return neg + "".join(string[::-1])
815

916

1017
def string_to_int(s: str) -> int:
11-
# TODO - you fill in here.
12-
return 0
18+
place = 1
19+
val = 0
20+
neg = s[0] == "-"
21+
s = s[1:] if neg or s[0] == "+" else s
22+
for char in s[::-1]:
23+
val += int(char) * place
24+
place *= 10
25+
return val * -1 if neg else val
1326

1427

1528
def wrapper(x, s):
@@ -20,7 +33,11 @@ def wrapper(x, s):
2033

2134

2235
if __name__ == '__main__':
36+
# print(int_to_string(0))
37+
# print(string_to_int("0"))
38+
# """
2339
exit(
2440
generic_test.generic_test_main('string_integer_interconversion.py',
2541
'string_integer_interconversion.tsv',
2642
wrapper))
43+
# """

0 commit comments

Comments
 (0)