Stack and Queue (reading code) - java

I want to solve this problem WITHOUT putting it into an Java IDE. (If it came up in an exam I would not be able to solve it.)
So what I know so far.
I know that stack is referred to as LIFO
and queue is referred to as FIFO. But I do not understand what this code would pop out (Or if I'm correct).
public static void main (String[] args) {
Queue<String> q = new Queue<String> ();
q.enqueue ("one");
q.enqueue ("two");
q.enqueue ("four");
q.enqueue ("six");
String s = "";
int i = 0;
while (!q.isEmpty()) {
s = s + q.dequeue().substring(i);
i++;
}
StdOut.print (s);
}
Since it is a queue it would be FIFO
and the substring would cause the output to be: onewour? Since s.substring() for six would be (3) so no value would be present.
And lastly I found two problems in a Princeton CS class but do not know how the answer came to be
Suppose that a client performs an intermixed sequence of (queue) enqueue and dequeue operations. The enqueue operations put the integers 0 through 9 in order onto the queue; the dequeue operations print out the return value. Which of the following sequence(s) could not occur?
(a) 0 1 2 3 4 5 6 7 8 9
(b) 4 6 8 7 5 3 2 9 0 1
(c) 2 5 6 7 4 8 9 3 1 0
(d) 4 3 2 1 0 5 6 7 8 9
Answer: (b), (c), and (d).
Suppose that an intermixed sequence of (stack) push and pop operations are performed. The pushes push the integers 0 through 9 in order; the pops print out the return value. Which of the following sequence(s) could not occur?
(a) 4 3 2 1 0 9 8 7 6 5
(b) 4 6 8 7 5 3 2 9 0 1
(c) 2 5 6 7 4 8 9 3 1 0
(d) 4 3 2 1 0 5 6 7 8 9
(e) 1 2 3 4 5 6 9 8 7 0
(f) 0 4 6 5 3 8 1 7 2 9
(g) 1 4 7 9 8 6 5 3 0 2
(h) 2 1 4 3 6 5 8 7 9 0
Answer: (b), (f), and (g).

question 3)
Look at b)
4 6 8 7 5 3 2 9 0 1
When it popped and printed 9, it means all push operations were finished at that point
(due to the ordering of pushes 0,1,...,9). OK, now it is reading 9. Then it is reading 0,
this means it is reading the very first number which was ever pushed to the stack.
Then there's no way it could pop and print 1 because it already popped the last
possible number (there's no way 1 was pushed after 0 was popped because as said
all push operations were done at the time 9 was popped).
You need to use similar logical observations to see why f) and g) are not possible too.
When reading all these sequences of popped numbers just try to imagine the sequence
of pushes which happens in between the pops (i.e. what was pushed since the last pop).
Whichever sequence of pops leads to a hiccup: that one is not possible i.e. could not occur.
question 2)
This one is trivial, anything but a) is not possible. Right?
Because as with a queue in a shop, the customers are served in the
order in which they came to the cash desk. Otherwise it would not be a queue.
question 1)
You will learn more if you actually type this one, compile it and run it.

Ques 3) "Intermixed push and pop operations of integers 0 through 9 in order"
I think this means the order is maintained starting anywhere from 0 and 9. Rotation is allowed. Ie: for example: pushing 8,9,0,1,2 in the same order, is valid.
Otherwise the option 'e' is not possible.
The option 'g' here is also possible.
Here are the operations you have to perform to get the sequence given in option 'g'
Stack r = new Stack();
//Not providing the implementation details of stack here.
r.push(1);
System.out.print(r.pop());
r.push(2);
r.push(3);
r.push(4);
System.out.print(r.pop());
r.push(5);
r.push(6);
r.push(7);
System.out.print(r.pop());
r.push(8);
r.push(9);
System.out.print(r.pop());
System.out.print(r.pop());
System.out.print(r.pop());
System.out.print(r.pop());
System.out.print(r.pop());
r.push(0);
System.out.print(r.pop());
System.out.print(r.pop());
Only 'b' and 'f' cannot occur. Correct me if I am wrong.

Related

Backtracking chopsticks in java

I have an exercise that is killing my brain; which is:
I have x sticks, and I break them into x chunks which I measure, they are a vector of numbers, the solution to the problem is to find the minimum number to create sticks of the same size:
Sample input
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0 (this is the end of the input it have to be there)
Sample output
6
5

How to calculate time needed through multiple routes?

For example, with input like:
4 6 7
1 2 6 2
......//more routes statement same format as second line
1 4
It means there are 4 places in total, and 6 routes in total, with 7cm convex hull of the ship(From the first line). The next 6 lines states where does the route connect, time needed and how many cm of the hull were worn down in the process. (In the second line, the route connects 1 and , takes 6 minutes, and wore down the hull by 2. The last line states where the starting point and destination are (1 and 4 in this case). The goal for this program is to get the minimum time used with the hull not completely worn down. I had tried using a
map(starting point of a route, end point);
but I can't really assign time used or the number of hull worn down this way, since multiple route can have the same starting point, meaning the two locations can't be used as a key like
map(starting point, time);
Also
Matrix[boolean][boolean]
doesn't seem to be an effective way to do this question. What technique should I use for this program and how do I do it?
Complete example with input:
10 4 7
1 2 4 4
1 3 7 2
3 1 8 1
3 2 2 2
4 2 1 6
3 4 1 1
1 4 6 12
1 4
output:
7
The routes can be used in two directions.

How to create guitar chord variation?

I'm trying to create guitar chord application but the chord chart for a single chord is too many. Listing all would take time and not efficient. For example, C major chord has variation such as the chart below
x 3 2 0 1 0
x 3 2 0 1 3
x 3 5 5 5 0
8 10 10 9 8 8
Is there any way that the chart can be generated by with/without knowing the keys to create the chord? ie, CEG for chord C major.
Contrary to the comments I think this is possible. If you got all of the open chord shapes such as..
0 2 2 1 0 0 (open E)
0 0 2 2 2 0 (open A)
0 3 2 0 1 0 (open C)
X 0 0 2 3 2 (open D)
Then you can shift them up using bar chords and you should be able to write code to calculate all the permutations for a particular chord. For example if you wanted all the C major triads (as in your question) you know one is
0 3 2 0 1 0 (open C)
But you also know one of them is the open A shape above but shifted UP 3 frets...
3 3 5 5 5 3 (open A shifted up as a bar chord)
This is your third chord in your question. You can also calculate where to shift the open E shape, which would be...
8 10 10 7 8 8
Again, this is one of the chords in your question. This way you can calculate all the positions for any chord using code - providing you have a sufficient set of open chords that can be comfortably shifted up like this.
So your set of C chords using these initial open shapes would be...
C Chords...
8 10 10 7 8 8
3 3 5 5 5 3
0 3 2 0 1 0
X 10 10 12 14 12
Now if you wanted to know the set of D chords you can actually calculate these by adding 2 to all the numbers above (two of them wrap around an octave but you can work this into a calculation)
D Chords...
10 12 12 11 10 10
5 5 7 7 7 5
10 13 12 10 11 10
X 0 0 2 3 2
Have you ever seen the book called "Fret Logic"? It shows the mathematical logic of the guitar fretboard.
There are also shortcut versions of most chords, such as the nice 3 string version used a lot in cross picking:
x
x
x
x
5
5
3
This is a shortened version the C chord in the A position, but it is used a lot.

Misclassification in Binary SVM

Given a dataset
0 0 1
3 4 1
5 9 1
12 1 1
8 7 1
9 8 -1
6 12 -1
10 8 -1
8 5 -1
14 8 -1
When applied SVM on these train data,
3 4 1,8 7 1 and 6 12 -1 are missclassified.
What does it mean--
whether the datapoint 3 4 doesnot fall in class label 1 and but falls in -1.
Is it like that?
Yes, it means that the model you have calibrated yields :
3 4 => -1
8 7 => -1
6 12=> +1
You have an error rate of 30%.
Using linear discriminant analysis approach, you can have a 20% error rate.
But keep in mind that you do not have much elements in your population (only 10). This is relatively low for 2 descriptors.
Indeed, if you estimate the error rate with a bootstrap method, we find out :
A 60% error rate for discriminant analysis approach
A 55% error rate for SVM approach : SVM is often more robust to overfitting.
Cheers

Algorithm for reaching target sum using given array of integers such that certain numbers are avoided? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
The exercise is a simple challenging Java program. the input contains:
size of an array "n",
the inputs for the array A and
the inputs for another array B of size "n-1"
"finalsum" is sum of all the elements in Array A
What is the most correct algorithm for printing the correct order of adding all the elements in array A to reach the final sum, i.e. "finalsum", such that we avoid summing up to any value in array B.
Inputs: (split to three lines for clarity)
1.
3 //n, the size of the array
2 4 6 //array a of size n
4 8 //array b of size n-1
//finalsum = 2 + 4 + 6 = 12. Similarly for the 2nd input
Output: 0 1 2 (or) 2 1 0 is also correct
but 1 0 2 is wrong because it cannot add up to 4, since 4 is present in the array b
2.
20
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
209 208 207 206 205 204 203 202 201 200 199 198 197 196 195 194 193 192 191
Output: 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 (or) many other ways too
Your algorithm could look something like this:
Take an element from A
Add element to current sum. Check if current sum is contained in B
If current sum is in B, skip to next element in A
If current sum NOT in B, recurse to select next element from A (with current element removed).
When all elements from A have been selected, return indexes back up the stack as solution.
If all elements of A have been tested without solution, AND we're currently at the top-most level of the recursion stack, then conclude there is no solution.
Most of these steps should be relatively straightforward to implement.
why can't we use the modified form of all permutation algorithm, where check for the sum in each step. if the current sum is equal to any element in B (for this we can use hash map) then don't proceed else proceed.
Hope i am clear.
Thanks.

Categories

Resources