5

Design a function called TwoStacks which implements a queue by using two stacks....

Question

Design a function called TwoStacks which implements a queue by using two stacks.

Design a function called TwoStacks which implements a queue by using two stacks.



Answers

Create a function machine that produces 3 more than twice every input as an output.

Already. So we are trying to do an algorithm that will determine whether a function on a finite set is 1 to 1 such recall. What it means to be 1 to 1 and we want toe. One means that for any two inputs of function. Soon since we're working with a finite seven, maybe a I and possibly a J right, So that a I does not equal a J should stipulate that if we have two different inputs than a function is one toe one if the outputs are not the same, right? So if they were the same, that's gonna look like, you know, if this is a all I and this is A J, but we have the same value that's gonna be sort of your, uh, hyperbole is if you can draw a horizontal line and get to values that's the Intersect. We are not 1 to 1. Something that is 1 to 1 might look more like this, right, So you can draw your horizontal line, and no matter what value you choose, you were going to get a different output from the function. So that's what we're trying to determine and so weaken traitor algorithm. Call it 1 to 1. And the input okay, is gonna be our into your list their input into juice that we put into our functions. It one day, and and and to be greater than equal to one, it it could just be one element. Right X is equal to four is a function is perfectly well, they find we'll have our output list. And this is sort of what, uh, effort a one or a two is gonna be equal to something in this list. Okay. Also, uh, integers And with I'm also being greater than ankle toe one and f is our function license through the space were working in just the input. Depending on what your your function is, you may not actually need to be in putting this other list. That's a little dependent. Well, I want to start with a sort of a checking variable. We'll just set it equal to true. All right, so then we'll say, Well, four I from one to end. So this is the entire space on for J Also running from the entire input space. Okay, We want to say Well, if if we have an a I that does not equal A J. Says how we're making sure that our to impose are different. However, if these two things aren't the same but the interview that it maps to write so effort, I will be equal to one of these be into jurors and f of a J will also be equal to something on there. If this hold and this council review return is equaling saw something in the bees but overcomplicated then we can reset our check. So initially we were sort of assuming that it is 1 to 1. But I'm gonna say, you know what? It's not so to say, our check instead, let's set that equal to false okay, and then we just want to return our check. So if it turns out it goes to this whole list and never has a problem with the outputs, the return true that it is 1 to 1. And if at some point it finds out that it's not wonder one in return of false for us

So here we're gonna use the bubble sort algorithm to sort this list given of 31754 Okay. And bubble sort compares to adjacent elements and then sorts them if they are in decreasing order. Right. So the opposite of what we want. So our first generation through this, it will compare one in three. Right, And it will see that they are currently in decreasing order. Settle, swap. So then we will get one, 375 and four. All right, then we compare the second and third element and see that three years less than seven. So we'll leave that alone. That is fine the way it is. And then the next it's gonna compare these seven and five. But, uh, since seven is greater than five, they need to be swap. So so then we'll get 1357 four. And lastly, we'll compare the last element. Okay, so we compare 7 to 4 and see that seven is greater than four. So we swapped them. Now the seven is guaranteed to be in the crack spot and and no longer needs to be checked. So the second pass through this algorithm really really need to check the's 1st 4 elements, right? And so now for a second house, we're working with the list. 1354 and seven. Let me keep that seven and blue to remind myself not to check it. All right, so now we compare the first and third elements, but one is less than three. So so that's fine. Okay, it's left alone. We would get one, 3547 No compare three and five and determined. That's fine. So those good left alone will end up with 1354 AM seven. Next. We'll compare five and four. But this time they do need to switch. Right? Some issues read here. Okay, so get 134 five. And then, of course, there. Seven. Okay. We don't need to check the last two. We know that they're correct on Additionally, by this augur than we also know that five is correct. Okay? No. We with our human brains, can see this list is sorted, but the bubble algorithm will continue to go. It only knows that the last two are correct s So we're working within the list. 13 four and our other than only knows that fight and seven is correct. So it's going to check one and three and see that it's fine. Leave them alone. And that is gonna check three and four. See that? Those air. Fine. Leave them alone. So 13 457 Except now it knows that the fours in the right spot is gonna do one more. Check our final and forth past comparing one in three sees that it's fine. And now the bubble algorithm knows that all of them are in the right spot in the order is 1345

Already. So if you're gonna go through another bubble sort sorting algorithm, But instead, it'll numbers. We have letters. D que him a b. And the thing is, you can think about letters the same way you would numbers, right? So you could think of a is one and B is two Indias four. And so these further in the alphabet, letters like A and M are gonna be greater than, uh, A and B. And so we'll start with our story, and we'll ask ourselves, Is D less than f thinking the alphabet? It does come before F S O. The answer is yes. Slowly that alone unless the same thing. What, uh, is f so less than cable? F comes before Kenny up of it Since the yes, the move on the last one is K last n m me any off about the answers will be yes comes before am Let me ask yourselves, Is m less than a No? Okay, so we're gonna go and switch those two. So give us D f k A and be Where's the real? Ask yourselves. A similar question here at the second to last in next is M less than B and the answer's no becomes a four missile. Switch him and I'll give us d f k a be. And that would be considered our first past. When did the same thing And we saw above that this d f and care in the right order already. Okay, But so then once we get to comparing the K with a, then we will need to switch them. They will come before. Okay, so get DEA. Hey, K b m And then we'll compare the okay with the B and C. Those would be switched as well. Okay, so that we have d f a be okay. And then kay and m r in the crack order. That's the move. Move on to our third pass here. I'm gonna rewrite what's at the bottom here at the top D f A. B okay. And D is comes before. Abso that's good. But F does not come before a so I want to switch that you can sort of see then how this bubble sorting algorithm is just shuffling these letters towards the front. The ones that come earlier in the alphabets, just sort of funneling him forward. So we'll be comparing the F to the B again here, and it's switched. And then one final pass should do. It's the F and the K, and then we're all in the correct order already. Okay, so we will switch our d n A. To get A D. B s K and and then one final switch with the D and the be here. You know that F cam isn't, though. Create order to get a be d f k um, and that will be the end result of our bubble sorting algorithm.

All right, We're gonna improve the bubble sorts alerting improved bubble sort. I be, Ah, because even when our list is in the correct order, it continues to try to sort them right. We want to fix that. So given a list here of introduce with and greater than or equal to two, and that's greater than two. Because to make a comparison, you need more than one, right? But they're only input is this list and we're gonna initialized variable. So first we'll set I equal to two. And that's because we will be making comparisons to the pre seeding numbers and we'll set this variable change 20 Okay, so what'll happen is when we make a change or swap when we swapped two days in the list will change that to be greater than zero. And so when it runs to the list, if no, if no swaps are made, it will stay zero. And that's how we will verify when everything's in the right order. We'll start in a while, loop here, and if that's confusing it, I hope once this while it was conclude here complete, it'll be a little bit clearer. So So while I is less than or equal to end, and that's to exhaust the whole list or dealer condition. Here is that change is equal zero, right? So either either we've gone through the whole list and changes continued to be made or when we were able to stop because, uh, it's already in the orders was well, and this algorithm okay, we'll start by within the loop, setting change equal to one right. So initially zero to get into loop here and then started right away, setting equal to one. They won't go into the four loop said J equal to one two in minus I. Okay, so we are just making changes, essentially up to all the unverifiable points. If you call in the bubble sword, it sorts sort of guarantees that the last values if you have a list of some numbers the first swipe through guarantees the last one is correct. The second round through again she's the second to last one is correct. That's what's happening here is that we're just going up to, uh, that point. Okay, so for that will make our comparisons in our list, right? So, for if a eyes and the less than the preceding value are minus one. Then we'll switch the variables around. And to do that will set a temporary variable equal to ay ay animals that a I equal to it's proceeding values there. And then we'll set the aye, aye minus one equal to temp. So those values have switched. So these these three lines just swap swap two values around, right? So do you have and then four. At the end of it, you will have four followed by eight. Okay. But we also need to add something else. So a little bit of space here to the right. I'm gonna carry my algorithm over there. So just pretend this is right below that, because we want to set change equal to zero. Yes, we made a swap and want to note that a swap made by setting change equals zero. So it'll continue to put us into this loop if we go through this whole thing here and don't make any changes that keeps change at one and ends the algorithm, you can. And so finally, either the other than ends, because it's in the right order ordinance that's gone through the whole thing. It's in the right order. Uh, either way, we want to return our list, and it will be in a nice sorted order one through a en.


Similar Solved Questions

5 answers
T& Jh2 cerivatives of tte followtng furctions: I) {(z) = Ez sc"(e) tan"(uJdu{(2) = ; Inflr(n(y)))dym) f(=} = SV ain(e)jtiv} {() = Gninle' + _Jde{c} = (&" s-tosls:}Sic; ei ((5" &sss} {€" ralsy}}
T& Jh2 cerivatives of tte followtng furctions: I) {(z) = Ez sc"(e) tan"(uJdu {(2) = ; Inflr(n(y)))dy m) f(=} = SV ain(e)jt iv} {() = Gninle' + _Jde {c} = (&" s-tosls:} Sic; ei ((5" &sss} {€" ralsy}}...
5 answers
1. Let A; B C be three events with P(A) = P(B) = P(C) = 4 We assume that A and B are mutually exclusive and is independent of both and B2] Find P(AU BUC). [3] b) Let X be the indicator random variable of the event (AU B) and Y the indicator random variable of the event (BUC) (that is_ X=lif AU B occurred and 0 otherwise, Y = 1 if BUC occurred and 0 otherwise). Find the joint probability function of X and Y. [2] Are X and Y independent? Justify your answer _
1. Let A; B C be three events with P(A) = P(B) = P(C) = 4 We assume that A and B are mutually exclusive and is independent of both and B 2] Find P(AU BUC). [3] b) Let X be the indicator random variable of the event (AU B) and Y the indicator random variable of the event (BUC) (that is_ X=lif AU B oc...
5 answers
Use the Binomial Theorem t0 find the coeficient of x* in the expansion of (x ~2)12
Use the Binomial Theorem t0 find the coeficient of x* in the expansion of (x ~2)12...
5 answers
Find the EMF of a galvanic cell at 259C based on the following standard reduction potentials and the concentrations_Fe*2 + 2e 7FeEred = -0.44VClz + 2e 72c/"Ered = +1.36VIn this cell, the concentrations are [Fe"?] = 0.01M and [CI ] =0.2M
Find the EMF of a galvanic cell at 259C based on the following standard reduction potentials and the concentrations_ Fe*2 + 2e 7Fe Ered = -0.44V Clz + 2e 72c/" Ered = +1.36V In this cell, the concentrations are [Fe"?] = 0.01M and [CI ] =0.2M...
5 answers
Points possible:Total attempts:Find ((52 2r*)dr19thiix 442 Points possible- 10. Total attempts: Evaluate the definite integral L#3 Points possible: 10. Total attempts:Find+4V#4 Points possible: 10. Total attempts: Find Ka +4 + 3 )dz#5 Points possible:Total attempts:Find Ikz + 6)(= 3Jdr76 Points possible Total attempts: Evuluule lnc integral Fv by making te substitutionNOTE: Your answer should bein tcrms of andinoiALenalnu phr
Points possible: Total attempts: Find ((52 2r*)dr 19thiix 4 42 Points possible- 10. Total attempts: Evaluate the definite integral L #3 Points possible: 10. Total attempts: Find +4V #4 Points possible: 10. Total attempts: Find Ka +4 + 3 )dz #5 Points possible: Total attempts: Find Ikz + 6)(= 3Jdr ...
5 answers
For the function f(z) = 32 calculate the following function values:f(4) f(5)
For the function f(z) = 32 calculate the following function values: f(4) f(5)...
5 answers
Use the balanced equation for the combustion of ethane to complete the table 2C,Ho(g) 702(g) 4COz(g) 6HzO(g)Initially mixed0.232 g1.514 g0.00 g0.00 gHow much reacts1o.232Composition of final mixture
Use the balanced equation for the combustion of ethane to complete the table 2C,Ho(g) 702(g) 4COz(g) 6HzO(g) Initially mixed 0.232 g 1.514 g 0.00 g 0.00 g How much reacts 1o.232 Composition of final mixture...
5 answers
In the figure shown; the charge (in HC) on the 4 "VF capacitor is2 HF4HF6 HF 8 HF LAF H V
In the figure shown; the charge (in HC) on the 4 "VF capacitor is 2 HF 4HF 6 HF 8 HF LAF H V...
5 answers
Many cigarette lighters contain liquid butane, $mathrm{C}_{4} mathrm{H}_{10}(l)$. Using standard enthalpies of formation, calculate the quantity of heat produced when $5.00 mathrm{~g}$ of butane is completely combusted in air under standard conditions.
Many cigarette lighters contain liquid butane, $mathrm{C}_{4} mathrm{H}_{10}(l)$. Using standard enthalpies of formation, calculate the quantity of heat produced when $5.00 mathrm{~g}$ of butane is completely combusted in air under standard conditions....
1 answers
Three equal positive charges $q$ are at the corners of an equilateral triangle of side $a$ as shown in Figure P19.26. (a) Assume that the three charges together create an electric field. Sketch the field lines in the plane of the charges. Find the location of a point (other than $\infty$ ) where the electric field is zero. (b) What are the magnitude and direction of the electric field at $P$ due to the two charges at the base?
Three equal positive charges $q$ are at the corners of an equilateral triangle of side $a$ as shown in Figure P19.26. (a) Assume that the three charges together create an electric field. Sketch the field lines in the plane of the charges. Find the location of a point (other than $\infty$ ) where the...
5 answers
When we study linear equations in two variables, it will be necessary to change equations from the form $A x+B y=C$ to $y=m x+b .$ For Exercises, express each equation in the form $y=m x+b$ by solving for $y .$ (see Example 5.1).$$5 x-7 y=15$$
When we study linear equations in two variables, it will be necessary to change equations from the form $A x+B y=C$ to $y=m x+b .$ For Exercises, express each equation in the form $y=m x+b$ by solving for $y .$ (see Example 5.1). $$5 x-7 y=15$$...
5 answers
A generator supplies electric power at a voltage of 600 Volts toa transformer’s primary coil. The primary coil has 500 turns andthe secondary coil has 85 turns. Determine the output voltage ofthe secondary coil of the transformer in Volts.
A generator supplies electric power at a voltage of 600 Volts to a transformer’s primary coil. The primary coil has 500 turns and the secondary coil has 85 turns. Determine the output voltage of the secondary coil of the transformer in Volts....
5 answers
ResetNelpCHyNHzPO4HSOaNOzCHzStrong baseWeak baseSpecies with negligible basicity
Reset Nelp CHyNHz PO4 HSOa NOz CHz Strong base Weak base Species with negligible basicity...
5 answers
Question 9: The data set below is sample of the speeds of I0 cars passing a construction site on particular day: 59,53, 50, 50, 57, 54, 51 , 48, 33, 58. Is this data discrete or continuous? Explain your answerFind the mean and the median_Is there mode? Why or why not?
Question 9: The data set below is sample of the speeds of I0 cars passing a construction site on particular day: 59,53, 50, 50, 57, 54, 51 , 48, 33, 58. Is this data discrete or continuous? Explain your answer Find the mean and the median_ Is there mode? Why or why not?...
5 answers
What IS the value of tan € if the terminal side of angle 0 intersects the unit circle In the first quadrant at €Provide your answer below:
What IS the value of tan € if the terminal side of angle 0 intersects the unit circle In the first quadrant at € Provide your answer below:...
5 answers
Consider the innization of nitrous acid at 25 *€;HNO_(aq) + HzOW) Hjot(aq) NOz (aq)4G" =KJ mol-1Whal is Ihe value of K for this equilibrium?Periodlc Jable_and_ Dalasheel0.220 22+10]045*10010 10
Consider the innization of nitrous acid at 25 *€; HNO_(aq) + HzOW) Hjot(aq) NOz (aq) 4G" = KJ mol-1 Whal is Ihe value of K for this equilibrium? Periodlc Jable_and_ Dalasheel 0.22 0 22+10] 045*10 010 10...
4 answers
If C is the boundary of the rectan 5 4 <x < 0. = 1 <y < 1 in the clockwise direction, then 2-4 Sc 7+2-3dz =3ti3 2 Ti3ZiiZwi4
If C is the boundary of the rectan 5 4 <x < 0. = 1 <y < 1 in the clockwise direction, then 2-4 Sc 7+2-3dz = 3ti 3 2 Ti 3 Zii Zwi 4...

-- 0.029454--