Stuck in Pride Exercise - Stack - Page 2 - Programming - HWzone Forums
adplus-dvertising
Skip to content
  • Create an account
  • About Us

    Hello Guest!

     
    Please note - in order to participate in our community, comment and open new discussions, you must join as a registered member.

    Our members enjoy many advantages, including the ability to participate in discussions, enjoy raffles and promotions for members of the site, and receive our weekly content directly by email.

    Do not like being harassed by email? You can register for the site but do not submit your registration to the weekly email updates.

Stuck in a Pride Exercise - Stack


maor18
 Share

Recommended Posts

Quote by af db creid

There is no need for complicated stack tricks. Stack has a function called elementAt that does exactly what its name implies :)

And again, this is exactly what we are not allowed to touch.

The only ones we are allowed are push, pop, top and isEmpty. 3.

I wish our lives were easier

Link to content
Share on other sites

Quote by af db creid

Wait, did you not use three stacks? And forbidden?

We are allowed to use as many stacks as we want, but provided they meet exactly what we learned (which is what I wrote to you above) 

Quote by af db creid

And if you give meaningful names to variables it will be easier to understand what your code is trying to do.

temp1 claims to be the stack to which all digits equal to a number move. All unequal digits are passed to temp. This is basically the idea, but I can not implement it

Link to content
Share on other sites

Not a Java expert but familiar with data structures and in general it's a question of algorithmic (that's why there are a lot of "forbidden") so I will give my shenkel.


Suppose you have 3 cartridges (is this first in last out?) One given cartridge, and another 2 auxiliary StackIn, StackA, StackB
Choose an arbitrary direction from it for which you will look for a sequence (unity, tens of hundreds, etc. for example) and start checking the head and if there is no equation between the number of the unity and the head, pour from In to A. 
If you find an organ in In that matches the unity digit of the data, you will now start pouring organs into B instead of A. 
There are now 2 cases, 1 that you found a sequence and finished. 2 - There is no equation in one of the digits so you have to pour everything from B back to In, and the same digit from which you checked if a sequence starts pouring again to A and continue like this with the next organ.

Now cartridge A may fill up and you will not find a sequence because you checked in the wrong direction, but what good is it, cartridge A is exactly the opposite of In so you can do the same steps as before, just start checking and bending from A to In.

Hope that is understandable enough.
I recommend that you translate the text into as much algorithmic spelling as possible before the actual writing. 
 

Link to content
Share on other sites

Quote of exeaction

Not a Java expert but familiar with data structures and in general it's a question of algorithmic (that's why there are a lot of "forbidden") so I will give my shenkel.


Suppose you have 3 cartridges (is this first in last out?) One given cartridge, and another 2 auxiliary StackIn, StackA, StackB
Choose an arbitrary direction from it for which you will look for a sequence (unity, tens of hundreds, etc. for example) and start checking the head and if there is no equation between the number of the unity and the head, pour from In to A. 
If you find an organ in In that matches the unity digit of the data, you will now start pouring organs into B instead of A. 
There are now 2 cases, 1 that you found a sequence and finished. 2 - There is no equation in one of the digits so you have to pour everything from B back to In, and the same digit from which you checked if a sequence starts pouring again to A and continue like this with the next organ.

Now cartridge A may fill up and you will not find a sequence because you checked in the wrong direction, but what good is it, cartridge A is exactly the opposite of In so you can do the same steps as before, just start checking and bending from A to In.

Hope that is understandable enough.
I recommend that you translate the text into as much algorithmic spelling as possible before the actual writing. 
 

 

Link to content
Share on other sites

Quote of exeaction

Not a Java expert but familiar with data structures and in general it's a question of algorithmic (that's why there are a lot of "forbidden") so I will give my shenkel.


Suppose you have 3 cartridges (is this first in last out?) One given cartridge, and another 2 auxiliary StackIn, StackA, StackB
Choose an arbitrary direction from it for which you will look for a sequence (unity, tens of hundreds, etc. for example) and start checking the head and if there is no equation between the number of the unity and the head, pour from In to A. 
If you find an organ in In that matches the unity digit of the data, you will now start pouring organs into B instead of A. 
There are now 2 cases, 1 that you found a sequence and finished. 2 - There is no equation in one of the digits so you have to pour everything from B back to In, and the same digit from which you checked if a sequence starts pouring again to A and continue like this with the next organ.

Now cartridge A may fill up and you will not find a sequence because you checked in the wrong direction, but what good is it, cartridge A is exactly the opposite of In so you can do the same steps as before, just start checking and bending from A to In.

Hope that is understandable enough.
I recommend that you translate the text into as much algorithmic spelling as possible before the actual writing. 
 

After reading the code that works in part, I realized I was trying to do something very similar.
The problem with your code is that you are not handling well in cases where the organ at the top of the stack is not equal to the digit you are checking.
In this case you will want to re-scan the stack starting from the organ that came after the first organ where there was a match so as not to skip a proper sequence.
To do this you need to return them back to the cartridge as I described in my suggestion.
You can also do this using 2 cartridges instead of three as I described if you add variables that will count how many organs you may need to return to the cartridge.
Overall you were close.

Edited By exeaction
Link to content
Share on other sites

Quote of maor18

I gave up, a whole week to serve and just got stuck. 5 exercises, 4 I did and one just failed. I would love for you to help me.

The exercise is:
Write an external function that receives as a parameter a positive integer and a stack of digits.
The function must return true if digits of the number appear in sequence in the OK stack
Some and false otherwise.

How the hell do I do that? I was already mentally broken, I tried everything from everything.

 

My code looks something like this:










Stack temp = new Stack (); int x = num; boolean flag = false; while (! s.isEmpty ()) {if (s.top () == x% 10) {
				temp.push (s.pop()); x / = 10; if (x == 0) flag = true; } else {
				temp.push (s.pop()); x = num; }} x = num; while (! temp.isEmpty ()) {if (temp.top() == x% 10) {s.push (temp.pop()); x / = 10; if (x == 0) flag = true; } else {s.push (temp.pop()); x = num; }} return flag;

 

I do not know what you were offered, I have not read, but the simplest and fastest is to use a table / array reset in size ten, make a number of modulus ten And do one plus at a time instead of the digit you received. Divide the number by ten and continue.

 

Then just take a digit out of the stack and drop one in the appropriate place in the table. If you finished the number without trying to make one minus to zero then return true. You may need to copy the table several times and repeat the process until the stack empties and return false depending on the conditions of the problem and the size of the stack.

 

It's a code of some five or six lines.

 

Come on well, I have not written in Java for several years so with your permission I will write the code in C, I did not bother to compile but it does not really matter it is the algorithm and it is already your task:

bool is_digit_in_stack (stack digits, unsigned int number) {int digit_array [10] = {0}; int digit_array_copy [10] = {0}; int number_digits = 0; int stack_digits = 0; while (number) {++ digit_array [number% 10]; number / = 10; ++ number_digits; } while (! digits.is_empty ()) {memcopy (digit_array_copy, digit_array, 10); stack_digits = 0; while (digit_array_copy [digits.top ()]) {--digit_array_copy [digits.pop ()]; // I assume that pop returns the leading number ++ stack_digits; } if (digits.is_empty () && number_digits == stack_digits) {return true; } else if (digits.is_empty () {return false;}}}

Note that while (! Digits.is_empty ()) at first can also be while (True) but the practitioners who teach at the academy do not like this form of writing and download it for some reason so I preferred to write like that.

Edited By Buck
Link to content
Share on other sites

Quote of Buck

I do not know what you were offered, I have not read, but the simplest and fastest is to use a table / array reset in size ten, make a number of modulus ten And do one plus at a time instead of the digit you received. Divide the number by ten and continue.

 

Then just take a digit out of the stack and drop one in the appropriate place in the table. If you finished the number without trying to make one minus to zero then return true. You may need to copy the table several times and repeat the process until the stack empties and return false depending on the conditions of the problem and the size of the stack.

 

It's a code of some five or six lines.

 

Come on well, I have not written in Java for several years so with your permission I will write the code in C, I did not bother to compile but it does not really matter it is the algorithm and it is already your task:

bool is_digit_in_stack (stack digits, unsigned int number) {int digit_array [10] = {0}; int digit_array_copy [10] = {0}; int number_digits = 0; int stack_digits = 0; while (number) {++ digit_array [number% 10]; number / = 10; ++ number_digits; } while (! digits.is_empty ()) {memcopy (digit_array_copy, digit_array, 10); stack_digits = 0; while (digit_array_copy [digits.top ()]) {--digit_array_copy [digits.pop ()]; // I assume that pop returns the leading number ++ stack_digits; } if (digits.is_empty () && number_digits == stack_digits) {return true; } else if (digits.is_empty () {return false;}}}

 

Of course we offered similar things! forbidden :(

Quote of Buck

Note that while (! Digits.is_empty ()) at first can also be while (True) but the practitioners who teach at the academy do not like this form of writing and download it for some reason so I preferred to write like that.

For hours I argued with the practitioner and also with the lecturer, in the end they were convinced (by the way, the same thing about goto. And not that I used goto during school).

Link to content
Share on other sites

I think the ban is on using complex language tools that he has not learned about and not in a structure as simple as an array.

 

goto Bessie is great for clearing resources, unless practitioners / lecturers think they understand writing more code than Linus Tribalds and the writers of ...

 

Edited By Buck
Link to content
Share on other sites

Quote of Buck

I think the ban is on using complex language tools that he has not learned about and not in a structure as simple as an array.

I understand that the ban is on using anything that will not lead to a solution with the help of cartridges.

 

Quote of Buck

goto Bessie is great for clearing resources, unless practitioners / lecturers think they understand writing more code than Linus Tribalds and the writers of Linux...

Not only him, but also some of the big names ...

And that's it, that's exactly the problem, that it's what they think (even if they do not say it).

By the way, they say torvalds.

Edited By af db creid
Link to content
Share on other sites

Join the discussion

You can then join the discussion and then join our community. If you already have an account with us, please Log in now To comment under your username.
Note that: The comment will appear to the surfers after approval by the board management team.

guest
Add a comment

×   The content pasted is with formatting.   Remove formatting

  Only 75 emoji are allowed.

×   Your link has been automatically assimilated.   Show as regular link

×   Your previous content has been automatically restored.   Clear all

×   You can not paste images directly. Upload or insert images from URL.

 Share

  • Latest news

  • Buzz-Zone: Everything hot on the net

×
  • Create new ...

At the top of the news:

new on the site