Cracking The Coding Interview: Chapter 1 Solutions in PeopleCode
(Some functions can be improved using smarter features, comments welcome)
# 1
Function isUnique(&checkLetters As string) Returns boolean
Local number &I = 0;
Local array of string &holder;
&holder = Split(&checkLetters, "");
&holder.Sort("A");
If &holder.Len > 128 Then
Return false;
End-If;
While &holder.Next(&I)
If &holder.Len > &I Then
If &holder [&I] = &holder [&I + 1] Then
Return False;
End-If;
End-If;
End-While;
Return True;
End-Function;
# 2
Function checkPermute(&A As string, &B As string) Returns boolean
Local array of string &hold1, &hold2;
Local string &C, &D;
If Not Len(&A) = Len(&B) Then
Return False;
End-If;
&hold1 = Split(&A, "");
&hold2 = Split(&B, "");
&hold1.Sort("A");
&hold2.Sort("A");
&C = &hold1.Join("");
&D = &hold2.Join("");
Return Exact(&C, &D);
End-Function;
# 3
Function urlify(&url As string) Returns string
Local string &url20;
&url20 = LTrim(RTrim(&url));
&url20 = Substitute(&url20, " ", "%20");
Return &url20;
End-Function;
# 4
Function palinPerm(&pp As string) Returns boolean
Local array of string &hold;
Local string &temp;
Local number &I, &K, &E, &cnt;
Local array of number &counterArr;
&K = 0;
&temp = Substitute(&pp, " ", "");
&hold = Split(&temp, "");
&hold.Sort("A");
&counterArr = CreateArrayRept(1, &hold.Len);
&cnt = 1;
For &I = 1 To &hold.Len
&K = &I;
While &hold.Next(&K)
If &hold [&I] = &hold [&K] Then
&counterArr [&cnt] = &counterArr [&cnt] + 1;
Else
&I = &K - 1;
&cnt = &cnt + 1;
Break;
End-If;
End-While;
End-For;
For &E = 1 To &cnt
If &counterArr [&E] > 1 Then
If Mod(&counterArr [&E], 2) = 1 Then
Return False;
End-If;
Else
If &counterArr [&E] <> 1 Then
Return False;
End-If;
End-If;
End-For;
Return True;
End-Function;
# 5
Function OneAway(&str1 As string, &str2 As string) Returns boolean
Local array of string &hold1, &hold2;
Local number &I, &width, &exception;
&hold1 = Split(&str1, "");
&hold2 = Split(&str2, "");
&hold1.Sort("A");
&hold2.Sort("A");
If Abs(&hold1.Len - &hold2.Len) > 1 Then
Return False;
End-If;
If &hold1.Len > &hold2.Len Then
&width = &hold2.Len
Else
&width = &hold1.Len
End-If;
&exception = 0;
For &I = 1 To &width Step 1
If &hold1 [&I] <> &hold2 [&I] Then
&exception = &exception + 1;
End-If;
End-For;
If &exception > 1 Then
Return False;
End-If;
Return True;
End-Function;
# 6
Function compString(&str As string) Returns string
Local array of string &hold, &sorted, &temp;
Local number &I, &S, &cnt;
Local string &item, &result;
&hold = Split(&str, "");
&sorted = &hold.Sort("A");
&cnt = 0;
&I = 0;
While &hold.Next(&I)
&item = &hold [&I];
&temp.Push(&item);
&S = 0;
While &sorted.Next(&S);
If &hold [&I] = &item Then
&cnt = &cnt + 1;
End-If;
End-While;
&temp.Push(&cnt);
&cnt=0;
End-While;
&result = &temp.Join();
If &hold.Len <= &temp.Len Then
Return &str;
Else
Return &result;
End-If;
End-Function;
# 7
Function rotateMatrixClockWise90(&origM As array of array of number, &dim As number) Returns array of array of number
Local array of array of number &rotatedM;
Local number &y, &x, &first, &last, &i, &offset, ⊤
&rotatedM = &origM.Clone();
&y = &dim;
For &x = 1 To &dim / 2 Step 1
&first = &x;
&last = &y;
For &i = &first To &last Step 1
&offset = &i - &first;
&top = &rotatedM [&first][&i];
&rotatedM [&first][&i] = &rotatedM [&last - &offset][&first];
&rotatedM [&last - &offset][&first] = &rotatedM [&last][&last - &offset];
&rotatedM [&last][&last - &offset] = &rotatedM [&i][&last];
&rotatedM [&i][&last] = ⊤
End-For;
&y = &y - 1;
End-For;
Return &rotatedM;
End-Function;
# 8
Function zeroMatrix(&origM As array of array of number, &row As number, &col As number) Returns array of array of number
Local array of array of number &zeroMat;
Local number &i, &j, &r, &c;
&zeroMat = &origM.Clone();
For &i = 1 To &row
For &j = 1 To &col
If &zeroMat [&i][&j] = 0 Then
For &c = 1 To &col
&origMat [&i][&c] = 0;
End-For;
For &r = 1 To &row
&origMat [&r][&j] = 0;
End-For;
End-If;
End-For;
End-For;
Return &origMat;
End-Function;
# 9
Function isRotation(&orig As string, &test As string) Returns boolean
Local string &dup;
&dup = &orig | &orig;
return isSubstring(&orig, &test);
Return False;
End-Function;
# 1
Function isUnique(&checkLetters As string) Returns boolean
Local number &I = 0;
Local array of string &holder;
&holder = Split(&checkLetters, "");
&holder.Sort("A");
If &holder.Len > 128 Then
Return false;
End-If;
While &holder.Next(&I)
If &holder.Len > &I Then
If &holder [&I] = &holder [&I + 1] Then
Return False;
End-If;
End-If;
End-While;
Return True;
End-Function;
# 2
Function checkPermute(&A As string, &B As string) Returns boolean
Local array of string &hold1, &hold2;
Local string &C, &D;
If Not Len(&A) = Len(&B) Then
Return False;
End-If;
&hold1 = Split(&A, "");
&hold2 = Split(&B, "");
&hold1.Sort("A");
&hold2.Sort("A");
&C = &hold1.Join("");
&D = &hold2.Join("");
Return Exact(&C, &D);
End-Function;
# 3
Function urlify(&url As string) Returns string
Local string &url20;
&url20 = LTrim(RTrim(&url));
&url20 = Substitute(&url20, " ", "%20");
Return &url20;
End-Function;
# 4
Function palinPerm(&pp As string) Returns boolean
Local array of string &hold;
Local string &temp;
Local number &I, &K, &E, &cnt;
Local array of number &counterArr;
&K = 0;
&temp = Substitute(&pp, " ", "");
&hold = Split(&temp, "");
&hold.Sort("A");
&counterArr = CreateArrayRept(1, &hold.Len);
&cnt = 1;
For &I = 1 To &hold.Len
&K = &I;
While &hold.Next(&K)
If &hold [&I] = &hold [&K] Then
&counterArr [&cnt] = &counterArr [&cnt] + 1;
Else
&I = &K - 1;
&cnt = &cnt + 1;
Break;
End-If;
End-While;
End-For;
For &E = 1 To &cnt
If &counterArr [&E] > 1 Then
If Mod(&counterArr [&E], 2) = 1 Then
Return False;
End-If;
Else
If &counterArr [&E] <> 1 Then
Return False;
End-If;
End-If;
End-For;
Return True;
End-Function;
# 5
Function OneAway(&str1 As string, &str2 As string) Returns boolean
Local array of string &hold1, &hold2;
Local number &I, &width, &exception;
&hold1 = Split(&str1, "");
&hold2 = Split(&str2, "");
&hold1.Sort("A");
&hold2.Sort("A");
If Abs(&hold1.Len - &hold2.Len) > 1 Then
Return False;
End-If;
If &hold1.Len > &hold2.Len Then
&width = &hold2.Len
Else
&width = &hold1.Len
End-If;
&exception = 0;
For &I = 1 To &width Step 1
If &hold1 [&I] <> &hold2 [&I] Then
&exception = &exception + 1;
End-If;
End-For;
If &exception > 1 Then
Return False;
End-If;
Return True;
End-Function;
# 6
Function compString(&str As string) Returns string
Local array of string &hold, &sorted, &temp;
Local number &I, &S, &cnt;
Local string &item, &result;
&hold = Split(&str, "");
&sorted = &hold.Sort("A");
&cnt = 0;
&I = 0;
While &hold.Next(&I)
&item = &hold [&I];
&temp.Push(&item);
&S = 0;
While &sorted.Next(&S);
If &hold [&I] = &item Then
&cnt = &cnt + 1;
End-If;
End-While;
&temp.Push(&cnt);
&cnt=0;
End-While;
&result = &temp.Join();
If &hold.Len <= &temp.Len Then
Return &str;
Else
Return &result;
End-If;
End-Function;
# 7
Function rotateMatrixClockWise90(&origM As array of array of number, &dim As number) Returns array of array of number
Local array of array of number &rotatedM;
Local number &y, &x, &first, &last, &i, &offset, ⊤
&rotatedM = &origM.Clone();
&y = &dim;
For &x = 1 To &dim / 2 Step 1
&first = &x;
&last = &y;
For &i = &first To &last Step 1
&offset = &i - &first;
&top = &rotatedM [&first][&i];
&rotatedM [&first][&i] = &rotatedM [&last - &offset][&first];
&rotatedM [&last - &offset][&first] = &rotatedM [&last][&last - &offset];
&rotatedM [&last][&last - &offset] = &rotatedM [&i][&last];
&rotatedM [&i][&last] = ⊤
End-For;
&y = &y - 1;
End-For;
Return &rotatedM;
End-Function;
# 8
Function zeroMatrix(&origM As array of array of number, &row As number, &col As number) Returns array of array of number
Local array of array of number &zeroMat;
Local number &i, &j, &r, &c;
&zeroMat = &origM.Clone();
For &i = 1 To &row
For &j = 1 To &col
If &zeroMat [&i][&j] = 0 Then
For &c = 1 To &col
&origMat [&i][&c] = 0;
End-For;
For &r = 1 To &row
&origMat [&r][&j] = 0;
End-For;
End-If;
End-For;
End-For;
Return &origMat;
End-Function;
# 9
Function isRotation(&orig As string, &test As string) Returns boolean
Local string &dup;
&dup = &orig | &orig;
return isSubstring(&orig, &test);
Return False;
End-Function;
Comments
Post a Comment