View mode: basic / threaded / horizontal-split · Log in · Help
January 28, 2012
[your code here]
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);
}
January 28, 2012
Re: [your code here]
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.
January 28, 2012
Re: [your code here]
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
January 28, 2012
Re: [your code here]
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);
}
January 28, 2012
Re: [your code here]
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
January 28, 2012
Re: [your code here]
On 1/28/12 8:21 AM, Jos van Uden wrote:
> if (line.walkLength > 1) {

if (line.walkLength(2) > 1) {


Andrei
January 28, 2012
Re: [your code here]
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);
}
January 29, 2012
Re: [your code here]
> 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.
January 29, 2012
Re: [your code here]
> 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:)
January 29, 2012
Re: [your code here]
Jos van Uden wrote:

> BufferedFile("ukacd17.txt");

Me got an unicode-error on that file on line 100.

-manfred
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home