Cracking the Coding Interview Chapter 3 Solutions in PeopleCode
#1
Function createMultiStack Returns array of array of string
Local array of array of string &multiStack;
Local array of string &lineData;
Local File &dataFile;
Local string &holder;
&triple = CreateArrayRept(CreateArrayRept("", 0), 0);
&lineData = CreateArrayRept("", 0);
&dataFile = GetFile("names_phones_emails.csv", "R");
While &dataFile.ReadLine(&holder);
&lineData = Split(&holder, ",");
&triple [1].push(&lineData [1]);
&triple [2].push(&lineData [2]);
&triple [3].push(&lineData [3]);
End-While;
For &I = 0 To &triple.Len
&holder = &triple [1][&I] | " " | &triple [2][&I] | " " | &triple [3][&I];
MessageBox(0, "", 0, 0, &holder);
End-For;
Return &triple;
End-Function;
#2
/* For every pop or push the min part of the array needs to be
checked and reinitialized when needed */
Function stackWithMin Returns array of array of number
Local array of array of number &stackWithMin;
Local array of number &findMin;
Local File &dataFile;
Local string &holder;
Local number &index;
&stackWithMin = CreateArrayRept(CreateArrayRept(0, 0), 0);
&findMin = CreateArrayRept(0, 0);
&dataFile = GetFile("numbers.txt", "R");
While &dataFile.ReadLine(&holder)
&stackWithMin.Push(Value(&holder));
&findMin.Push(Value(&holder));
End-While;
&findMin.Sort("A");
&index = 0;
While &stackWithMin.Next(&index);
&stackWithMin [&index].Push(&findMin [1]);
End-While;
Return &stackWithMin;
End-Function;
#3
class stackSet
method stackSet();
method sspush(&pushItem As number);
method sspop() Returns number;
method sspeek() Returns number;
method popAt(&targetStack As number) Returns number;
property number threshhold get set;
private
instance array of array of number &ss;
instance number &itemcount;
instance number &stackcount;
end-class;
method stackSet
%This.threshhold = 10;
%This.ss = CreateArrayRept(CreateArrayRept(0, 10), 0);
%This.itemcount = 0;
%This.stackcount = 0;
end-method;
method sspush
/+ &pushItem as Number +/
If %This.itemcount = 0 And
%This.stackcount = 0 Then
%This.itemcount = 1;
%This.stackcount = 1;
Else
If %This.itemcount < %This.threshhold Then
%This.itemcount = %This.itemcount + 1;
Else
%This.stackcount = %This.stackcount + 1;
%This.itemcount = 1;
End-If;
End-If;
%This.ss [%This.stackcount].Push(&pushItem);
end-method;
method sspop
/+ Returns Number +/
Local number &prevCount = %This.itemcount;
If %This.itemcount = 1 Then
If %This.stackcount > 1 Then
%This.itemcount = %This.threshhold;
%This.stackcount = %This.stackcount - 1;
Else
%This.itemcount = 0;
%This.stackcount = 0;
End-If
Else
If %This.itemcount > 1 Then
%This.itemcount = %This.itemcount - 1;
End-If;
End-If;
If &prevCount > 0 Then
Return %This.ss [%This.stackcount].Pop();
End-If;
end-method;
method sspeek
/+ Returns Number +/
Local number &peekval;
&peekval = %This.ss [%This.stackcount][%This.itemcount];
Return &peekval;
end-method;
method popAt
/+ &targetStack as Number +/
/+ Returns Number +/
Local number &targetItem, &holdItem, &I;
If &targetStack > 0 Then
If &targetStack = %This.stackcount Then
Return %This.sspop();
End-If;
If &targetStack < %This.stackcount Then
&targetItem = %This.ss [&targetStack].Pop();
For &I = &targetStack + 1 To %This.stackcount Step 1
&holdItem = %This.ss [&I].Shift();
%This.ss [&I - 1].Push(&holdItem);
End-For;
If %This.itemcount = 1 Then
%This.itemcount = %This.threshhold;
%This.stackcount = %This.stackcount - 1;
Else
%This.itemcount = &itemcount - 1;
End-If;
Return &targetItem;
End-If;
End-If;
end-method;
get threshhold
/+ Returns Number +/
Return %This.threshhold;
end-get;
set threshhold
/+ &NewValue as Number +/
%This.threshhold = &NewValue;
%This.ss = CreateArrayRept(CreateArrayRept(0, &NewValue), 0);
end-set;
#4
class qW2stack
method qW2stack();
method firstIn(&inItem As number);
method firstOut() Returns number;
method firstPeek() Returns number;
private
instance array of number &putStack;
instance array of number &viewStack;
end-class;
method qW2stack
%This.putStack = CreateArrayRept(0, 0);
%This.viewStack = CreateArrayRept(0, 0);
end-method;
method firstIn
/+ &inItem as Number +/
%This.putStack.Push(&inItem);
end-method;
method firstOut
/+ Returns Number +/
Local number &item, &I, &J;
If %This.putStack.Len > 0 Then
For &I = 1 To %This.putStack.Len
%This.viewStack.Push(%This.putStack.Pop());
End-For;
&item = %This.viewStack.Pop();
For &J = 1 To %This.viewStack.Len
%This.putStack.Push(%This.viewStack.Pop());
End-For;
Return &item;
End-If;
end-method;
method firstPeek
/+ Returns Number +/
Local number &item, &I, &J;
If %This.putStack.Len > 0 Then
For &I = 1 To %This.putStack.Len
%This.viewStack.Push(%This.putStack.Pop());
End-For;
&item = %This.viewStack.Pop();
%This.putStack.Push(&item);
For &J = 1 To %This.viewStack.Len
%This.putStack.Push(%This.viewStack.Pop());
End-For;
Return &item;
End-If;
end-method;
#5
Function sortDescending(&stackArray As array of number)
&stackArray.Sort("D");
End-Function;
#6
Function animalShelter(&choice As string, &dogQ As array of datetime, &catQ As array of datetime) Returns datetime
&dogQ.Sort("A");
&catQ.Sort("A");
Evaluate &choice
When "O"
If &dogQ [1] < &catQ [1] Then
Return &dogQ.Shift();
Else
Return &catQ.Shift();
End-If;
Break;
When "D"
Return &dogQ.Shift();
Break;
When "C"
Return &catQ.Shift();
Break;
End-Evaluate;
End-Function;
Function createMultiStack Returns array of array of string
Local array of array of string &multiStack;
Local array of string &lineData;
Local File &dataFile;
Local string &holder;
&triple = CreateArrayRept(CreateArrayRept("", 0), 0);
&lineData = CreateArrayRept("", 0);
&dataFile = GetFile("names_phones_emails.csv", "R");
While &dataFile.ReadLine(&holder);
&lineData = Split(&holder, ",");
&triple [1].push(&lineData [1]);
&triple [2].push(&lineData [2]);
&triple [3].push(&lineData [3]);
End-While;
For &I = 0 To &triple.Len
&holder = &triple [1][&I] | " " | &triple [2][&I] | " " | &triple [3][&I];
MessageBox(0, "", 0, 0, &holder);
End-For;
Return &triple;
End-Function;
#2
/* For every pop or push the min part of the array needs to be
checked and reinitialized when needed */
Function stackWithMin Returns array of array of number
Local array of array of number &stackWithMin;
Local array of number &findMin;
Local File &dataFile;
Local string &holder;
Local number &index;
&stackWithMin = CreateArrayRept(CreateArrayRept(0, 0), 0);
&findMin = CreateArrayRept(0, 0);
&dataFile = GetFile("numbers.txt", "R");
While &dataFile.ReadLine(&holder)
&stackWithMin.Push(Value(&holder));
&findMin.Push(Value(&holder));
End-While;
&findMin.Sort("A");
&index = 0;
While &stackWithMin.Next(&index);
&stackWithMin [&index].Push(&findMin [1]);
End-While;
Return &stackWithMin;
End-Function;
#3
class stackSet
method stackSet();
method sspush(&pushItem As number);
method sspop() Returns number;
method sspeek() Returns number;
method popAt(&targetStack As number) Returns number;
property number threshhold get set;
private
instance array of array of number &ss;
instance number &itemcount;
instance number &stackcount;
end-class;
method stackSet
%This.threshhold = 10;
%This.ss = CreateArrayRept(CreateArrayRept(0, 10), 0);
%This.itemcount = 0;
%This.stackcount = 0;
end-method;
method sspush
/+ &pushItem as Number +/
If %This.itemcount = 0 And
%This.stackcount = 0 Then
%This.itemcount = 1;
%This.stackcount = 1;
Else
If %This.itemcount < %This.threshhold Then
%This.itemcount = %This.itemcount + 1;
Else
%This.stackcount = %This.stackcount + 1;
%This.itemcount = 1;
End-If;
End-If;
%This.ss [%This.stackcount].Push(&pushItem);
end-method;
method sspop
/+ Returns Number +/
Local number &prevCount = %This.itemcount;
If %This.itemcount = 1 Then
If %This.stackcount > 1 Then
%This.itemcount = %This.threshhold;
%This.stackcount = %This.stackcount - 1;
Else
%This.itemcount = 0;
%This.stackcount = 0;
End-If
Else
If %This.itemcount > 1 Then
%This.itemcount = %This.itemcount - 1;
End-If;
End-If;
If &prevCount > 0 Then
Return %This.ss [%This.stackcount].Pop();
End-If;
end-method;
method sspeek
/+ Returns Number +/
Local number &peekval;
&peekval = %This.ss [%This.stackcount][%This.itemcount];
Return &peekval;
end-method;
method popAt
/+ &targetStack as Number +/
/+ Returns Number +/
Local number &targetItem, &holdItem, &I;
If &targetStack > 0 Then
If &targetStack = %This.stackcount Then
Return %This.sspop();
End-If;
If &targetStack < %This.stackcount Then
&targetItem = %This.ss [&targetStack].Pop();
For &I = &targetStack + 1 To %This.stackcount Step 1
&holdItem = %This.ss [&I].Shift();
%This.ss [&I - 1].Push(&holdItem);
End-For;
If %This.itemcount = 1 Then
%This.itemcount = %This.threshhold;
%This.stackcount = %This.stackcount - 1;
Else
%This.itemcount = &itemcount - 1;
End-If;
Return &targetItem;
End-If;
End-If;
end-method;
get threshhold
/+ Returns Number +/
Return %This.threshhold;
end-get;
set threshhold
/+ &NewValue as Number +/
%This.threshhold = &NewValue;
%This.ss = CreateArrayRept(CreateArrayRept(0, &NewValue), 0);
end-set;
#4
class qW2stack
method qW2stack();
method firstIn(&inItem As number);
method firstOut() Returns number;
method firstPeek() Returns number;
private
instance array of number &putStack;
instance array of number &viewStack;
end-class;
method qW2stack
%This.putStack = CreateArrayRept(0, 0);
%This.viewStack = CreateArrayRept(0, 0);
end-method;
method firstIn
/+ &inItem as Number +/
%This.putStack.Push(&inItem);
end-method;
method firstOut
/+ Returns Number +/
Local number &item, &I, &J;
If %This.putStack.Len > 0 Then
For &I = 1 To %This.putStack.Len
%This.viewStack.Push(%This.putStack.Pop());
End-For;
&item = %This.viewStack.Pop();
For &J = 1 To %This.viewStack.Len
%This.putStack.Push(%This.viewStack.Pop());
End-For;
Return &item;
End-If;
end-method;
method firstPeek
/+ Returns Number +/
Local number &item, &I, &J;
If %This.putStack.Len > 0 Then
For &I = 1 To %This.putStack.Len
%This.viewStack.Push(%This.putStack.Pop());
End-For;
&item = %This.viewStack.Pop();
%This.putStack.Push(&item);
For &J = 1 To %This.viewStack.Len
%This.putStack.Push(%This.viewStack.Pop());
End-For;
Return &item;
End-If;
end-method;
#5
Function sortDescending(&stackArray As array of number)
&stackArray.Sort("D");
End-Function;
#6
Function animalShelter(&choice As string, &dogQ As array of datetime, &catQ As array of datetime) Returns datetime
&dogQ.Sort("A");
&catQ.Sort("A");
Evaluate &choice
When "O"
If &dogQ [1] < &catQ [1] Then
Return &dogQ.Shift();
Else
Return &catQ.Shift();
End-If;
Break;
When "D"
Return &dogQ.Shift();
Break;
When "C"
Return &catQ.Shift();
Break;
End-Evaluate;
End-Function;
Comments
Post a Comment