You are in an interview, and the question is:
Write a program to print the prime factors of 18
if you don’t write:
System.out.println("2, 3, 3");
but instead write a prime factoriser for N, then -1 productivity point.
You are in an interview, and the question is:
Write a program to print the prime factors of 18
if you don’t write:
System.out.println("2, 3, 3");
but instead write a prime factoriser for N, then -1 productivity point.
public class LazyFactory { private Object oUnsafe; private Object oSync; public Object get() { Object o = this.oUnsafe; if (o == null) { Object notNull = crateNewIfNeeded(); oUnsafe = notNull; return notNull; } else { return o; } } synchronized Object crateNewIfNeeded() { if (oSync == null) { oSync = getExpensiveNewInstance(); } return oSync; } /** Don't call this too often! */ Object getExpensiveNewInstance() { try { Thread.sleep(500); } catch (InterruptedException ignored) {} return new Object(); } }
Unless I’ve done something stupid:
Worst offender: Barclays iBank online banking. Using the back button logs you out. Bastards.
I thank you.
Dearest colleagues,
(Further to Pietro’s blog post)
I do hope you are getting on well with your C++ assignment, which from the sounds of it is going down like a lubed anvil. Couple of things id like to get off my chest, if I may be so bold.
1. Hash Maps
A hash map is none of the following:
If we store our records in a linked list, we can access them by scanning along and comparing the key until we find the right one. If the list is unsorted and of an unknown length this will take at worse N comparisons (and increments) where N is the length of the list. If the list is sorted and of a known length then it will take Log(N) comparisons (binary cut), but to get to each node one must still traverse the list in that direction, so it in fact takes (I believe… john?) xLog(x) (i.e the order of the integral of log(x)). So sorting a linked list and binary chopping is therefore futile unless you can access elements without scanning. Don’t iterate over list.at(i)!
Enter: the hash map. Let us say we are storing students with a cid and a name. The key will be the id. We know that these are evenly distributed between odd/even so we could construct two linked lists, one with the evens and one the odds. This would cut the average search time in half. Thus the hash function (which decides which list to enter) would be cid%2. Obviously this generalises quite nicely, so if we have 10,000,000 students we could have 100 lists from our table and use cid%100 as the hash function.
However, when running on a computer that has even a marginally faster clock rate than a wall clock, there is no fucking difference – just search the damn list and get on with something more useful.
Here is the only code you need to look for records:
class Record {
int cid;
string name;
};
class Node {
Record* record;
Node* next;
};
class JamesList
{
Node *head;
string lookupNameByCid(int cid) {
Node* runner = head;
while (head != null) {
if (runner->record->cid == cid) return runner->record->name;
runner = runner->next;
}
return null;
}
};
I thank you.
2. Simulating Circuits
Here is a circuit:
(circuit)
The aim is simulate its behaviour using an event queue. There are two different types of entities here, nodes (blob) and gates. Both share common functionality, they have an output value (Vo) and a set (see above) of outward connections. This makes traversing the network in the same direction as the input changes propagate easy.
So:
class Device {
int Vo;
int delay;
JamesList drivenDevices;
};
class Node : public Device {};
class Gate : public Device {};
Good. Some good stuff there. So when the user presses the button for node a to change its voltage to 1, we create an event in the event queue (an ordered list of some kind). Then start the simulation by making the first event happen. The output of a device only changes when the event is actually executed, not when we realise that a change must happen. The time difference between these is the propagation delay of the device (for a node this = 0).
I think the event execution should go something like this:
void simulate(int until)
{
EventListNode *runner;
runner = events->head;
/* FOR EACH EVENT */
for (int i=0; i
{
//Check we arent about to overstep the mark
if (runner->event->time > until && until > 0)
{
break;
}
//This event's node
Device *changedDevice;
changedDevice = runner->event->node;
//Actually change the output voltage
changedDevice->setVo(runner->event->Vnew);
//The gate immediately to the right of this node
Set *affectedGates;
affectedGates = changedDevice->drivenDevices;
if (affectedGates != NULL)
{
set
itr = affectedGates->begin();
Device *thisDevice; //Gate for which to update output
//Foreach Device which this Device affects, recalculate its output
int vnew;
while( itr != affectedGates->end() )
{
thisDevice = ((*itr)->device);
vnew = thisDevice->Vnew;
//If the voltage has changed make an event
if (thisDevice->Vo != vnew)
{
events->addEvent(thisDevice,
thisDevice->Vo,
vnew,
runner->event->time + thisDevice->delay);
}
itr++;
}
}
runner = runner->next;
}
//We're done, so delete the events.
events->clear();
}
I do hope that makes some sense, since I have personally given up caring.
Its also important that the ordered event list puts new events with the same time as existing events after the current ones, else they will be missed.
Love and cuddles,
James
:: james at jgubby.com
Hi, my name’s James and this is my weblog, LOL. I’m a liberal ‘wannabe’ computer scientist. In my spare time I write the most appalling Java, much to John’s distaste. Often I can be found playing the pretent-o-phone or ‘piano’ as it is known to less liberal comrades.
My Amazon wish-list:
Marxism for dummies (softback)
Mastering Visual Basic 6 Linear Alegbra Edition (hardback)
Frontlight for beginners (only lightback)
I’m selling:
Flannel (unwanted and unused gift)
Me and my piano (just completed)
Inflatable Jeremy Beadle (used)
Seems there’s a wordpress plugin that uses enscript to highlight the syntax of code available, from this site.
Download the states-highlight.phps file from http://www.nextthing.org/code/wp-states-highlight/. Rename it states-highlight.php and put it in wordpress/plugins.
Also, get a copy of enscript.st from http://www.nextthing.org/code/enscript/ and put it in the same folder. Then enable the plugin in the admin thingy and it should work.
There is a readme in the folder of the first thing too, that tells you to put some CSS classes in various files. The admin one exists, but I couldnt find it in the layout one, so put it in the file of the theme.
Check there is enscript, too:
whereis enscript
on my fanny liberal mac it is in /usr/bin/enscript
Look at how fabulous it is:
Java:
import fixture.Fixture;
import junit.framework.TestCase;
public class Main {
public static void main(String[] args) {
Fixture f1 = new Fixture("Mac_500_Stage_Left_1");
Fixture f2 = new Fixture("Mac_500_Stage_Right_1");
}
}
C++:
#include
using namespace std;
int main(int argc, char*[] argv) {
while(true) {
cout << "John is a TERRBIBLE PINCHER" << endl;
}
cout << "Take that you most terrible pincher!" << endl;
return 0;
}