digitalmars.D - [your code here]
- Jos van Uden (13/13) Jan 28 2012 import std.stdio, std.stream, std.string, std.range;
- Mantis (6/19) Jan 28 2012 The same may be done without memory duplication by changing comparison
- Jos van Uden (18/39) Jan 28 2012 Good idea.
- Andrei Alexandrescu (5/46) Jan 28 2012 Could you please also compare with code that uses foreach with
- Jos van Uden (18/34) Jan 28 2012 After having added the upTo param, which is an improvement on both
- Joshua Niehus (3/17) Jan 28 2012 I ran this code on Mac OSX Lion using the "/usr/share/dict/words"
- Joshua Niehus (3/6) Jan 28 2012 found the problem: PEBKAC (problem exists between keyboard and
- Manfred Nowak (3/4) Jan 28 2012 Me got an unicode-error on that file on line 100.
- Jos van Uden (7/11) Jan 29 2012 The original file is encoded ISO 8859-1. You'll have to convert
- Andrei Alexandrescu (3/4) Jan 28 2012 if (line.walkLength(2) > 1) {
- Andrei Alexandrescu (5/18) Jan 28 2012 Thanks, Jos.
import std.stdio, std.stream, std.string, std.range; void main() { int countPalindromes; auto infile = new BufferedFile("unixdict.txt"); foreach (char[] line; infile) { if (line.walkLength > 1) { line.toLowerInPlace; if (line == line.dup.reverse) countPalindromes++; } } writeln("palindromes found: ", countPalindromes); }
Jan 28 2012
28.01.2012 15:31, Jos van Uden пишет:import std.stdio, std.stream, std.string, std.range; void main() { int countPalindromes; auto infile = new BufferedFile("unixdict.txt"); foreach (char[] line; infile) { if (line.walkLength > 1) { line.toLowerInPlace; if (line == line.dup.reverse) countPalindromes++; } } writeln("palindromes found: ", countPalindromes); }The same may be done without memory duplication by changing comparison to this (equal function is in std.algorithm): if (equal(line, retro(line))) Hard to say without profiling if it will have actual impact on performance, but at least it should be more GC-friendly.
Jan 28 2012
On 28-1-2012 14:57, Mantis wrote:28.01.2012 15:31, Jos van Uden пишет:Good idea. It's also faster. I tried with a larger file (ukacd17.txt) which has 240,000 entries. --- import std.stdio, std.stream, std.string, std.range, std.algorithm; void main() { int countPalindromes; auto infile = new BufferedFile("unixdict.txt"); foreach (char[] line; infile) { if (line.walkLength > 1) { line.toLowerInPlace; if (equal(line, retro(line))) countPalindromes++; } } writeln("palindromes found: ", countPalindromes); }import std.stdio, std.stream, std.string, std.range; void main() { int countPalindromes; auto infile = new BufferedFile("unixdict.txt"); foreach (char[] line; infile) { if (line.walkLength > 1) { line.toLowerInPlace; if (line == line.dup.reverse) countPalindromes++; } } writeln("palindromes found: ", countPalindromes); }The same may be done without memory duplication by changing comparison to this (equal function is in std.algorithm): if (equal(line, retro(line))) Hard to say without profiling if it will have actual impact on performance, but at least it should be more GC-friendly.
Jan 28 2012
On 1/28/12 8:21 AM, Jos van Uden wrote:On 28-1-2012 14:57, Mantis wrote:Could you please also compare with code that uses foreach with stdin.byLine()? Thanks, Andrei28.01.2012 15:31, Jos van Uden пишет:Good idea. It's also faster. I tried with a larger file (ukacd17.txt) which has 240,000 entries. --- import std.stdio, std.stream, std.string, std.range, std.algorithm; void main() { int countPalindromes; auto infile = new BufferedFile("unixdict.txt"); foreach (char[] line; infile) { if (line.walkLength > 1) { line.toLowerInPlace; if (equal(line, retro(line))) countPalindromes++; } } writeln("palindromes found: ", countPalindromes); }import std.stdio, std.stream, std.string, std.range; void main() { int countPalindromes; auto infile = new BufferedFile("unixdict.txt"); foreach (char[] line; infile) { if (line.walkLength > 1) { line.toLowerInPlace; if (line == line.dup.reverse) countPalindromes++; } } writeln("palindromes found: ", countPalindromes); }The same may be done without memory duplication by changing comparison to this (equal function is in std.algorithm): if (equal(line, retro(line))) Hard to say without profiling if it will have actual impact on performance, but at least it should be more GC-friendly.
Jan 28 2012
On 28-1-2012 16:07, Andrei Alexandrescu wrote:After having added the upTo param, which is an improvement on both methods, the infile is (marginally) faster on my system than the byLine. win7 64-bits i7 2600, dmd 2.057, optimizations enabled: -O -inline -release -- import std.stdio, std.stream, std.string, std.range, std.algorithm; void main() { int countPalindromes; auto infile = new BufferedFile("ukacd17.txt"); foreach (char[] line; infile) { if (line.walkLength(2) > 1) { line.toLowerInPlace; if (equal(line, retro(line))) countPalindromes++; } } writeln("palindromes found: ", countPalindromes); }import std.stdio, std.stream, std.string, std.range, std.algorithm; void main() { int countPalindromes; auto infile = new BufferedFile("unixdict.txt"); foreach (char[] line; infile) { if (line.walkLength > 1) { line.toLowerInPlace; if (equal(line, retro(line))) countPalindromes++; } } writeln("palindromes found: ", countPalindromes); }Could you please also compare with code that uses foreach with stdin.byLine()?
Jan 28 2012
import std.stdio, std.stream, std.string, std.range, std.algorithm; void main() { int countPalindromes; auto infile = new BufferedFile("ukacd17.txt"); foreach (char[] line; infile) { if (line.walkLength(2) > 1) { line.toLowerInPlace; if (equal(line, retro(line))) countPalindromes++; } } writeln("palindromes found: ", countPalindromes); }I ran this code on Mac OSX Lion using the "/usr/share/dict/words" file and got 235834 words out of 235886. I think something is wrong.
Jan 28 2012
I ran this code on Mac OSX Lion using the "/usr/share/dict/words" file and got 235834 words out of 235886. I think something is wrong.found the problem: PEBKAC (problem exists between keyboard and chair) sorry:)
Jan 28 2012
Jos van Uden wrote:BufferedFile("ukacd17.txt");Me got an unicode-error on that file on line 100. -manfred
Jan 28 2012
On 29-1-2012 7:55, Manfred Nowak wrote:Jos van Uden wrote:The original file is encoded ISO 8859-1. You'll have to convert it to utf-8. My earlier code sample used the unixdict.txt that I found here http://www.puzzlers.org/pub/wordlists/unixdict.txt I only changed to ukacd17.txt to test performance. JosBufferedFile("ukacd17.txt");Me got an unicode-error on that file on line 100. -manfred
Jan 29 2012
On 1/28/12 8:21 AM, Jos van Uden wrote:if (line.walkLength > 1) {if (line.walkLength(2) > 1) { Andrei
Jan 28 2012
On 1/28/12 7:31 AM, Jos van Uden wrote:import std.stdio, std.stream, std.string, std.range; void main() { int countPalindromes; auto infile = new BufferedFile("unixdict.txt"); foreach (char[] line; infile) { if (line.walkLength > 1) { line.toLowerInPlace; if (line == line.dup.reverse) countPalindromes++; } } writeln("palindromes found: ", countPalindromes); }Thanks, Jos. We still don't have the code that randomly rotates the homepage samples. Any volunteers? If not, I'll implement it in Javascript. Andrei
Jan 28 2012