ABC words: Difference between revisions
Content added Content deleted
(Added Swift solution) |
(Added Java solution) |
||
Line 435: | Line 435: | ||
54: tabernacle |
54: tabernacle |
||
55: tablecloth |
55: tablecloth |
||
</pre> |
|||
=={{header|Java}}== |
|||
<lang java>import java.io.BufferedReader; |
|||
import java.io.FileReader; |
|||
public class AbcWords { |
|||
public static void main(String[] args) { |
|||
String fileName = "unixdict.txt"; |
|||
String chars = "abc"; |
|||
for (int i = 0; i + 1 < args.length |
|||
&& args[i].length() > 1 |
|||
&& args[i].charAt(0) == '-'; ++i) { |
|||
switch (args[i].charAt(1)) { |
|||
case 'f': |
|||
fileName = args[++i]; |
|||
break; |
|||
case 'c': |
|||
chars = args[++i]; |
|||
break; |
|||
} |
|||
} |
|||
try { |
|||
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) { |
|||
String line; |
|||
int n = 0; |
|||
while ((line = reader.readLine()) != null) { |
|||
if (match(line, chars)) { |
|||
++n; |
|||
System.out.print(String.format("%3d: %-20s", n, line)); |
|||
if (n % 3 == 0) |
|||
System.out.println(); |
|||
} |
|||
} |
|||
if (n > 0 && n % 3 != 0) |
|||
System.out.println(); |
|||
} |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
// Returns true if word contains every character in chars in the |
|||
// same order. chars may contain the same character more than once. |
|||
private static boolean match(String word, String chars) { |
|||
int length = chars.length(); |
|||
boolean[] seen = new boolean[length]; |
|||
int wordLength = word.length(); |
|||
for (int w = 0; w < wordLength; ++w) { |
|||
char ch = word.charAt(w); |
|||
int index = -1; |
|||
for (int c = 0; c < length; ++c) { |
|||
if (ch == chars.charAt(c) && !seen[c]) { |
|||
index = c; |
|||
break; |
|||
} |
|||
} |
|||
if (index == -1) |
|||
continue; |
|||
if (index + 1 == length) |
|||
return index == 0 ? true : seen[index - 1]; |
|||
if (index > 0 && !seen[index - 1]) |
|||
return false; |
|||
seen[index] = true; |
|||
} |
|||
return false; |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
With no command line arguments: |
|||
<pre> |
|||
1: aback 2: abacus 3: abc |
|||
4: abdicate 5: abduct 6: abeyance |
|||
7: abject 8: abreact 9: abscess |
|||
10: abscissa 11: abscissae 12: absence |
|||
13: abstract 14: abstracter 15: abstractor |
|||
16: adiabatic 17: aerobacter 18: aerobic |
|||
19: albacore 20: alberich 21: albrecht |
|||
22: algebraic 23: alphabetic 24: ambiance |
|||
25: ambuscade 26: aminobenzoic 27: anaerobic |
|||
28: arabic 29: athabascan 30: auerbach |
|||
31: diabetic 32: diabolic 33: drawback |
|||
34: fabric 35: fabricate 36: flashback |
|||
37: halfback 38: iambic 39: lampblack |
|||
40: leatherback 41: metabolic 42: nabisco |
|||
43: paperback 44: parabolic 45: playback |
|||
46: prefabricate 47: quarterback 48: razorback |
|||
49: roadblock 50: sabbatical 51: snapback |
|||
52: strabismic 53: syllabic 54: tabernacle |
|||
55: tablecloth |
|||
</pre> |
|||
With command line arguments "-c pmm": |
|||
<pre> |
|||
1: anthropomorphism 2: epigrammatic 3: epimorphism |
|||
4: euphemism 5: optimism 6: optimum |
|||
7: pandemonium 8: pantomime 9: pantomimic |
|||
10: pemmican 11: persimmon 12: pessimism |
|||
13: pessimum 14: plummet 15: postmortem |
|||
16: pragmatism 17: praseodymium 18: premium |
|||
19: primitivism 20: programmable 21: programmed |
|||
22: programmer 23: programming 24: promethium |
|||
25: pummel 26: supremum |
|||
</pre> |
</pre> |
||