od Santhos » 12. 2. 2009 19:53
Hey,
sup fellows? Lets rock:
4120, 4096, 4104,...?
4104
a) nezkompiluje se
b) 1,2
c) , (tedy pouze znak carka)
d) runtime error recpectively Exception
Kód: Vybrat vše
class Plus {
public static void AddPlus() {
Prg.AddOp("+",1);
}
}
class Divide {
public static void AddDivide() {
Prg.AddOp("/",2);
}
}
class Prg {
static Dictionary<string, int> d=new Dictionary<string, int>();
public static void AddOp(string s, int i) {
d.Add(s,i);
}
static void Main() {
Console.WriteLine(string.Format("{0},{1}",d["+"],d["-"]));
new Plus();
new Divide();
}
}
d)
4B, 8B, 32B, 64B, 2^32, 2^64B, platform dependent? // B = Byte
8B
cannot be compiled, 32, 25... dunno actually
Kód: Vybrat vše
delegate int F();
... a very nice code
?
System.Object, object, decimal, some class, System.Int64
decimal, System.Int64
What can interface I1 enforce?
Kód: Vybrat vše
a) operator *
b) public method
c) private method
d) internal method
e) interface I2
f) indexer []
g) static property
b), f) ... more?
Implement enque in priority queue made of doubly-linked list. Make it safe to use in any context (besides thread safe, exceptions).
Kód: Vybrat vše
class Queue<T, P>
{
_____Node
{
public Node prev, next;
public T item;
public P prio;
public Node(T i, P p) { item = t; prio = p; }
}
public void Enqueue(T item, P prio)
{
Node n = new Node(item, prio);
}
public T Dequeue()
{
... // count on correct implementation
}
}
Answer:
Kód: Vybrat vše
class Queue<T, P> where P:IComparable<P>
{
class Node
{
public Node prev, next;
public T item;
public P prio;
public Node(T i, P p) { item = t; prio = p; }
}
Node first = null;
public void Enqueue(T item, P prio)
{
Node n = new Node(item, prio);
lock (this)
{
if (first == null)
first = n;
else
{
Node cur = first;
Node prev = null;
while (cur != null && prio.CompareTo(cur.prio) <= 0)
{
prev = cur;
cur = cur.next;
}
prev.next = n;
if (cur != null)
cur.prev = n;
}
}
}
public T Dequeue()
{
...
}
!NO WARRANTY GUARANTEED!
Hey,
sup fellows? Lets rock:
[quote]
4120, 4096, 4104,...?
[code](24 | 4096) & (~(1 << 4))[/code]
4104
[/quote]
[quote]
a) nezkompiluje se
b) 1,2
c) , (tedy pouze znak carka)
d) runtime error recpectively Exception
[code]
class Plus {
public static void AddPlus() {
Prg.AddOp("+",1);
}
}
class Divide {
public static void AddDivide() {
Prg.AddOp("/",2);
}
}
class Prg {
static Dictionary<string, int> d=new Dictionary<string, int>();
public static void AddOp(string s, int i) {
d.Add(s,i);
}
static void Main() {
Console.WriteLine(string.Format("{0},{1}",d["+"],d["-"]));
new Plus();
new Divide();
}
}
[/code]
d)
[/quote]
[quote]
4B, 8B, 32B, 64B, 2^32, 2^64B, platform dependent? // B = Byte
[code]
size of long
[/code]
8B
[/quote]
[quote]
cannot be compiled, 32, 25... dunno actually
[code]
delegate int F();
... a very nice code
[/code]
?
[/quote]
[quote]
System.Object, object, decimal, some class, System.Int64
[code]
value types
[/code]
decimal, System.Int64
[/quote]
[quote]
What can interface I1 enforce?
[code]
a) operator *
b) public method
c) private method
d) internal method
e) interface I2
f) indexer []
g) static property
[/code]
b), f) ... more?
[/quote]
[quote]
Write 2 of the main reasons why to use immutable classes:
1) Immutable classes are thread safe
2) They can be used as hash values in hash tables such as Dictionary
http://codebetter.com/blogs/patricksmacchia/archive/2008/01/13/immutable-types-understand-them-and-use-them.aspx
[/quote]
[quote]
Implement enque in priority queue made of doubly-linked list. Make it safe to use in any context (besides thread safe, exceptions).
[code]
class Queue<T, P>
{
_____Node
{
public Node prev, next;
public T item;
public P prio;
public Node(T i, P p) { item = t; prio = p; }
}
public void Enqueue(T item, P prio)
{
Node n = new Node(item, prio);
}
public T Dequeue()
{
... // count on correct implementation
}
}
[/code]
Answer:
[code]
class Queue<T, P> where P:IComparable<P>
{
class Node
{
public Node prev, next;
public T item;
public P prio;
public Node(T i, P p) { item = t; prio = p; }
}
Node first = null;
public void Enqueue(T item, P prio)
{
Node n = new Node(item, prio);
lock (this)
{
if (first == null)
first = n;
else
{
Node cur = first;
Node prev = null;
while (cur != null && prio.CompareTo(cur.prio) <= 0)
{
prev = cur;
cur = cur.next;
}
prev.next = n;
if (cur != null)
cur.prev = n;
}
}
}
public T Dequeue()
{
...
}
[/code]
[/quote]
[line][/line]
!NO WARRANTY GUARANTEED!