**News Alert:**Experts Exchange Confirmed as Safe in Cloudbleed Leak Read More

Solved

Posted on 2006-11-20

Hi,

i have the following piece of code but cannot find out what

Pos

Ret

Temp

are asking for me to return can anyone give me any hints?

function LASTAT(s,num)

{ /* XImp */

var pos in Int

var ret in Int

var temp in Stack of Int

temp <-- s

for (pos <-- 0 to 4)

{

if (PEEK(temp) == num) then

{ ret <-- pos }

temp <-- POP(temp)

}

return ret

}

Thanks

max

i have the following piece of code but cannot find out what

Pos

Ret

Temp

are asking for me to return can anyone give me any hints?

function LASTAT(s,num)

{ /* XImp */

var pos in Int

var ret in Int

var temp in Stack of Int

temp <-- s

for (pos <-- 0 to 4)

{

if (PEEK(temp) == num) then

{ ret <-- pos }

temp <-- POP(temp)

}

return ret

}

Thanks

max

1 Comment

Let's assume we denote a stack with the following notation:

[5, 3, 7, 1] where 5 is the top element.

Let's assume we look at the state at 3 different points namely:

function LASTAT(s,num)

{

var pos in Int

var ret in Int

var temp in Stack of Int

temp <-- s

>1>>>>>>>>>>>>>>>>>>>>>>>>

for (pos <-- 0 to 4)

{

if (PEEK(temp) == num) then { ret <-- pos }

temp <-- POP(temp)

>2>>>>>>>>>>>>>>>>>>>>>>>>

}

>3>>>>>>>>>>>>>>>>>>>>>>>>

return ret

}

Then in an example:

LASTAT([5, 3, 7, 1], 7);

[1] pos=0, ret=0, temp=[5,3,7,1], num=7

[2] pos=0, ret=0, temp=[3,7,1], num=7 (if statement compares PEEK(temp)=5 with num=7)

[2] pos=1, ret=0, temp=[7,1], num=7 (if statement compares PEEK(temp)=3 with num=7)

[2] pos=2, ret=2, temp=[1], num=7 (if statement compares PEEK(temp)=7 with num=7)

[2] pos=3, ret=2, temp=[], num=7 (if statement compares PEEK(temp)=1 with num=7)

[3] pos=3, ret=2, temp=[], num=7 (if statement compares PEEK(temp)=1 with num=7)

I just noticed that the name is LASTAT (=> LastAt). That's what it does, it finds the last index of num in a stack of 4 elements.

pos is used as counter through the stack

ret is used as return value

temp is used as stack copy (since there is a POP, the stack changes so a temporary value is necessary)

I made several assumptions about the language, but i guess this will be sufficient.

Mark

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Title | # Comments | Views | Activity |
---|---|---|---|

oracle 11g | 23 | 84 | |

java stored proc example | 9 | 32 | |

Java string replace | 11 | 48 | |

Is Applet the way to go for my drag and drop system? | 8 | 17 |

Join the community of 500,000 technology professionals and ask your questions.