www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [your code here]

reply Jos van Uden <user domain.invalid> writes:
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
next sibling parent reply Mantis <mail.mantis.88 gmail.com> writes:
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
parent reply Jos van Uden <user domain.invalid> writes:
On 28-1-2012 14:57, Mantis wrote:
 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.
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); }
Jan 28 2012
next sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 1/28/12 8:21 AM, Jos van Uden wrote:
 On 28-1-2012 14:57, Mantis wrote:
 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.
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); }
Could you please also compare with code that uses foreach with stdin.byLine()? Thanks, Andrei
Jan 28 2012
parent reply Jos van Uden <user domain.invalid> writes:
On 28-1-2012 16:07, Andrei Alexandrescu wrote:

 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()?
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); }
Jan 28 2012
next sibling parent reply "Joshua Niehus" <jm.niehus gmail.com> writes:
 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
parent "Joshua Niehus" <jm.niehus gmail.com> writes:
 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
prev sibling parent reply Manfred Nowak <svv1999 hotmail.com> writes:
Jos van Uden wrote:

 BufferedFile("ukacd17.txt");
Me got an unicode-error on that file on line 100. -manfred
Jan 28 2012
parent Jos van Uden <user domain.invalid> writes:
On 29-1-2012 7:55, Manfred Nowak wrote:
 Jos van Uden wrote:

 BufferedFile("ukacd17.txt");
Me got an unicode-error on that file on line 100. -manfred
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. Jos
Jan 29 2012
prev sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 1/28/12 8:21 AM, Jos van Uden wrote:
 if (line.walkLength > 1) {
if (line.walkLength(2) > 1) { Andrei
Jan 28 2012
prev sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
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