File tree Expand file tree Collapse file tree 7 files changed +168
-15
lines changed
elements-of-programming-interviews Expand file tree Collapse file tree 7 files changed +168
-15
lines changed Original file line number Diff line number Diff line change 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"
Original file line number Diff line number Diff line change 22
33#include " test_framework/generic_test.h"
44using 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+
518void 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+
942vector<int > ApplyPermutationWrapper (const vector<int >& perm, vector<int > A) {
1043 ApplyPermutation (perm, &A);
1144 return A;
Original file line number Diff line number Diff line change 44#include " test_framework/test_failure.h"
55using std::string;
66
7+ #define ASCII_ZERO 48
8+
79string 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+
1127int 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+
1540void Wrapper (int x, const string& s) {
1641 if (stoi (IntToString (x)) != x) {
1742 throw TestFailure (" Int to string conversion failed" );
Original file line number Diff line number Diff 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 } ,
Original file line number Diff line number Diff line change 44
55
66def 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
Original file line number Diff line number Diff line change 33
44
55def 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
1017def 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
1528def wrapper (x , s ):
@@ -20,7 +33,11 @@ def wrapper(x, s):
2033
2134
2235if __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+ # """
You can’t perform that action at this time.
0 commit comments