public static void showStrings(char[] alphabet, int length) {
show("", alphabet, length);
}
For example, the call showStrings(new char[]{'a', 'b', 'c'}, 2) displays:
aa
ab
ac
ba
bb
bc
ca
cb
cc
Write the helper method show recursively.
int x = 1;
for (int i = 0; i < n; i++) {
for (int j = 1; j <= x; j++) {
System.out.println("*");
}
x = x + x;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; i *= 2) {
System.out.println("*");
}
}
for (int i = 1; i * i <= n; i++) {
for (int j = 1; j <= n; j += j) {
System.out.println("*");
}
}
for (int i = 1; i <= n; i *= 2)
for (int j = 1; j <= n; j++)
System.out.println("*");
}
}
for (int i = 1; i <= n; i *= 2)
for (int j = 1; j <= i; j++)
System.out.println("*");
}
}
for (int i = 1; i <= n * n; i++) {
for (int j = 1; j <= n; j *= 2) {
System.out.println("*");
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i * i; j *= 2) {
System.out.println("*");
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j *= 2) {
System.out.println("*");
}
}
for (int i = n; n >= 1; n /= 4) {
for (int j = 0; j < i; j++) {
System.out.println("*");
}
}
// Assume some global variable t is defined out here
for (int i = n; n >= 1; n /= 2) {
if (new Date().getTime() > t) {
for (int j = 0; j < i; j++) {
System.out.print("*");
}
}
}
public static long power(int x, int n) {
return n == 0 ? 1
: n % 2 == 0 ? power(x * x, n / 2)
: x * power(x * x, n / 2)
}
ai1 & ai2 & & aik = bi1 & bi2 & & bik
For example, if A = ("able", "eci", "gek", "foo", "d", "un") and B = ("", "cidabl", "dab", "oo", "e", "und") then a solution is the sequence <6,5,2,5,1>. It turns out that any algorithm to solve this problem has complexity Ω(∞). What does this mean? Give a justification for this.
interface Function {
int evaluate(int x, int y);
}
and returns the value obtained by accumulating the result of applying
the function to each element of the array. For example, if you
had the array [5, 7, 6, 1] and the function
Function plus = new Function() {
int evaluate(int x, int y) {return x + y;}
};
your method should return the value 19.
interface Function {
int evaluate(int x, int y);
}
and returns the list obtained by applying f to x and each element of
the array in order. For example, if you had
int[] a = new int[]{5, 7, 6, 1};
Function subtract = new Function() {
int evaluate(int x, int y) {return x - y;}
};
and you called your method like this:
int[] b = whateverYouCallTheMethod(a, 2, subtract);then your method should return the array [-3, -5, -4, 1].
class Node {
public Object data;
public Node next;
}
The last node in the chain has a value of null for
its field called next. Without arguing the
brilliance or stupidity of the public
fields (gasp!) write a Java method that takes in a node and
removes all nodes containing Integer objects that appear
after the given node in the chain. You may not construct
any new nodes, just unhook the ones you are supposed to remove
from the chain.
Extra credit: It is possible for some smartalec to make one of the nodes' next field reference a node earlier in the chain, making an algorithm that stops when it sees a null actually loop forever. Make your method be tolerant of this and stop whenever it detects such a loop.
class Person {
private String name;
private Person mother;
private Person father;
...
public Set<Person> ancestors() {...}
public Set<Person> descendants() {...}
}
enqueue a
enqueue b
enqueue c
dequeue
enqueue d
dequeue
enqueue e
enqueue f
dequeue
dequeue
| 1 | Gamma | 0 | 0 |
| 2 | Beta | 1 | 3 |
| 3 | Epsilon | 6 | 5 |
| 4 | Omega | 2 | 7 |
| 5 | Sigma | 0 | 0 |
| 6 | Alpha | 0 | 0 |
| 7 | Tau | 8 | 0 |
| 8 | Delta | 0 | 0 |
(a = 3) >= m >= g . x [ 4 ] * ~ 6 || y %= 7 ^ 6 & p
A
/ \
B C
/ \ \
D E G
/
F
your method needs to return the collection [A, B, E, C].
R
/
N
/ \
K P
/ \ \
E L Q
/ \ \
D I M
/ \
G J
/ \
F H
public static boolean hasCycles(Graph g) {
return g.numberOfEdges() >= g.numberOfNodes();
}
Why or why not?A — B — C — D — E — F — G — H — I — J
f (f (... f ( x ) ...))
where there number of fs is k. Write a function to compute f k(x).
(x1 or x3 or not x2) and (x2 or not x3)
is satisfiable under the assignment x1 = True, x2 = False, x3 = False.
(x1 or x3 or x5) and (x1 or not x2 or x4) and (not x3 or x4 or x5) and (x2 or not x3 or x5)
C(n, k) = 1, if k = 0 or k = n
= C(n-1, k-1) + C(n-1,k) if 0 < k < n
= undefined, otherwise
It can be shown that C can also be defined non-recursively:
C(n,k) = n! / (k!(n-k)!)
Give the
worst-case complexity of (a) the naïve recursive, (b) the
non-recursive algorithms for computing C(n, k).