ClustrMaps

The roadrunner is back as never before! See also: My homepage or my very obsolete site
If by any means entries in my blog are considered to be harmful or damaging, please let me know (add comment) or just mail me. In this (unhopely) case I will remove or change the contents.

Friday, July 04, 2008

Daisy chain


Zie: http://en.wikipedia.org/wiki/Daisy_chain_(electrical_engineering)
In electrical and electronic engineering a daisy chain is a wiring scheme in which, for example, device A is wired to device B, device B is wired to device C, device C is wired to device D, et cetera. [1] Connections do not form webs (in the preceding example, device C cannot be directly connected to device A), nor do they loop back from the last device to the first. Daisy chains may be used for power, analog signals, digital data, or a combination thereof.

De roadrunner twijfelt soms aan zichzelf (en aan alles ...).
Op het werk (we noemen geen name) is er een puzzelprijs.
Moeilijk te kraken.

Denken in analogieën kan soms helpen.
En jawel, de effort (bloed, zweet en tranen) heeft uiteindelijk geloond.
Zoals bijna elke voldoening zal ook deze weer van korte duur zijn.

Voor de Java fanatici, toch even de code, onder voorbehoud want elk probleem kent talloze oplossingen.
Dit is er slechts één en niet al te hoogdravend:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;

public class ReadFile {

public static void main(String[] args) throws Exception {
ReadFile readFile = new ReadFile();
readFile.execute();
System.exit(0);
}

private void execute() throws Exception {
FileOutputStream fos = new FileOutputStream("out.txt");
FileInputStream fis = new FileInputStream("names.csv");
String line;
InputStreamReader converter = new InputStreamReader(fis);
BufferedReader in = new BufferedReader(converter);
//save in list
ArrayList list = new ArrayList();
while (true) {
line = in.readLine();
if (line == null) {
// end of file
break;
}
// - >> blank, blank >> { so after Z
line = line.replace('-', ' ').replace(' ', '{');
list.add(new Person(line));
}
fis.close();

Person person;
String name;
int length;
String value;
String nextValue;
Person nextPerson;
Iterator iterator;
// write first name
iterator = list.iterator();
person = (Person) iterator.next();
name = person.getName().replace('{', ' ');
for (int j = 0; j < name.length(); j++) {
fos.write(name.charAt(j));
}
fos.write('\n');
iterator.remove();

while (!list.isEmpty()) {
iterator = list.iterator();
length = person.getFirstName().length();
nextPerson = null;
nextValue = null;
while (iterator.hasNext()) {
person = (Person) iterator.next();
if (person.getName().length() < length) {
// name too short
continue;
}
value = person.getName().substring(length - 1, length);
// compare upper values
if (nextPerson == null || value.toUpperCase().compareTo(nextValue.toUpperCase()) < 0) {
nextPerson = person;
nextValue = value;
}
}
name = nextPerson.getName().replace('{', ' ');
// write next name
for (int j = 0; j < name.length(); j++) {
fos.write(name.charAt(j));
}
fos.write('\n');
person = nextPerson;
list.remove(person);
}

fos.close();
}

private class Person {

String firstName;
String lastName;

private Person(String line) {
int i = line.indexOf(';');
firstName = line.substring(0, i);
lastName = line.substring(i + 1);
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public String getName() {
return firstName + '{' + lastName;
}
}
}

De blogger sloopt de tabs, maar een code-beautify doet wonderen.

No comments: