www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [your code here]

reply Jos van Uden <user domain.invalid> writes:
import std.string, std.traits, std.uni;

enum Alphabet : dstring {
     DE = "abcdefghijklmnopqrstuvwxyzßäöü",
     EN = "abcdefghijklmnopqrstuvwxyz",
     SV = "abcdefghijklmnopqrstuvwxyzåäö"
}

bool isPangram(S)(S s, dstring alpha = Alphabet.EN) if (isSomeString!S) {
     foreach (c; alpha)
         if (indexOf(s, c) == -1 && indexOf(s, toUpper(c)) == -1)
             return false;
     return true;
}

---

A pangram is a sentence that contains every letter of a given alphabet
at least once. A classic example is "The quick brown fox jumps over the 
lazy dog".
Feb 04 2012
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Jos van Uden:

See:
http://rosettacode.org/wiki/Pangram_checker#D

Bye,
bearophile
Feb 04 2012
parent Jos van Uden <user domain.invalid> writes:
On 4-2-2012 14:14, bearophile wrote:
 Jos van Uden:

 See:
 http://rosettacode.org/wiki/Pangram_checker#D
Yeah, it's mine. And I bet the other one is yours :-)
Feb 04 2012
prev sibling next sibling parent reply Manfred Nowak <svv1999 hotmail.com> writes:
Jos van Uden wrote:

 A pangram is a sentence that contains every letter of a given
 alphabet at least once.
1) Because such definitions do not contribute to D as a programming language, I believe that such threads do not belong into the D-forum. 2) Because solutions are presented, I believe that such threads do neither belong into the D.learn-forum. 3) Because no general usability is visible I doubt, that such threads belong into the D.announce-forum. 4) Because all other forums are for specific themes a D.misc-forum should be the right place for such threads---but it is missing. -manfred
Feb 04 2012
next sibling parent reply Tobias Pankrath <tobias pankrath.net> writes:
Manfred Nowak wrote:

 Jos van Uden wrote:
 
 A pangram is a sentence that contains every letter of a given
 alphabet at least once.
1) Because such definitions do not contribute to D as a programming language, I believe that such threads do not belong into the D-forum. 2) Because solutions are presented, I believe that such threads do neither belong into the D.learn-forum. 3) Because no general usability is visible I doubt, that such threads belong into the D.announce-forum. 4) Because all other forums are for specific themes a D.misc-forum should be the right place for such threads---but it is missing. -manfred
Quote from dlang.org
 Got a brief example illustrating D? Submit your code to the digitalmars.D 
forum, specifying "[your code here]" in the title. Upon approval, it will be showcased on a random schedule on D‘s homepage.
Feb 04 2012
parent reply Manfred Nowak <svv1999 hotmail.com> writes:
Tobias Pankrath wrote:

 Quote from dlang.org
 Got a brief example illustrating D? Submit your code to the
 digitalmars.D forum, specifying "[your code here]" in the title.
I doubt that the author of that text indeed wanted some hundred contributers and lurkers to see this title pop up some hundred times and stay amused. digitalmars.D.misc or digitalmars.D.propaganda would be good places to not distract interested people from the language. Furthermore I believe, that the costs for creating a new digitalmars.D.abc group are negligible. For example digitalmars.D.debugger has fewer than 500 messages and about 80 threads within a time span of five years. -manfred
Feb 04 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 02/04/2012 09:38 PM, Manfred Nowak wrote:
 Tobias Pankrath wrote:

 Quote from dlang.org
 Got a brief example illustrating D? Submit your code to the
 digitalmars.D forum, specifying "[your code here]" in the title.
I doubt that the author of that text indeed wanted some hundred contributers and lurkers to see this title pop up some hundred times and stay amused. digitalmars.D.misc or digitalmars.D.propaganda would be good places to not distract interested people from the language. Furthermore I believe, that the costs for creating a new digitalmars.D.abc group are negligible. For example digitalmars.D.debugger has fewer than 500 messages and about 80 threads within a time span of five years. -manfred
This post is not making any sense. I kindly request the author to reconsider.
Feb 04 2012
prev sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Manfred Nowak:

 Because such definitions do not contribute to D as a programming 
 language, I believe that such threads do not belong into the D-forum.
 etc.
Because we are gentle and tolerant people, we tolerate those posts, despite a better place for them is in D.learn. Bye, bearophile
Feb 04 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 02/04/2012 07:43 PM, bearophile wrote:
 Manfred Nowak:

 Because such definitions do not contribute to D as a programming
 language, I believe that such threads do not belong into the D-forum.
 etc.
Because we are gentle and tolerant people, we tolerate those posts, despite a better place for them is in D.learn. Bye, bearophile
'Got a brief example illustrating D? Submit your code to the ***digitalmars.D*** forum, specifying "[your code here]" in the title. Upon approval, it will be showcased on a random schedule on D‘s homepage.' I completely agree with the rest of your post though.
Feb 04 2012
prev sibling parent reply Manfred Nowak <svv1999 hotmail.com> writes:
Jos van Uden wrote:

 bool isPangram
The presented code is not an acceptable example for the usage of the D programming language. 1) `indexOf( s, c)' has a worst case running time of O( `s.length'). `indexOf' is called once for each `c' in the used member `alpha' of `Alphabet'. Therefore the runtime of the presented code is O( `s.length' * `alpha.length') whereas O( `s.length' + `alpha.length') is possible. 2) The optional third parameter of `indexOf' can be called with `CaseSensitive.no'. But that parameter is left untouched. Instead a check with `toUpper( c)' is used, thereby risking a further visitation of the whole string . 3) The use of the literal value `-1' stands out of the code and hints to a maldesign in phobos. This is because `-1' is not mnemonic. -manfred
Feb 04 2012
next sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 2/4/12 10:05 PM, Manfred Nowak wrote:
 Jos van Uden wrote:

 bool isPangram
The presented code is not an acceptable example for the usage of the D programming language. 1) `indexOf( s, c)' has a worst case running time of O( `s.length'). `indexOf' is called once for each `c' in the used member `alpha' of `Alphabet'. Therefore the runtime of the presented code is O( `s.length' * `alpha.length') whereas O( `s.length' + `alpha.length') is possible. 2) The optional third parameter of `indexOf' can be called with `CaseSensitive.no'. But that parameter is left untouched. Instead a check with `toUpper( c)' is used, thereby risking a further visitation of the whole string . 3) The use of the literal value `-1' stands out of the code and hints to a maldesign in phobos. This is because `-1' is not mnemonic.
Sensible arguments. Would you want to redo the example in better style? Andrei
Feb 04 2012
parent Manfred Nowak <svv1999 hotmail.com> writes:
Andrei Alexandrescu wrote:

 Would you want to redo the example
No. I do not see the usefullness of a random ten liner shown on the frontpage of a programming language. If the goal is to attract the attention of a visitor to some feature of the language, that feature should be stated prominently and the example code should be shown on request. At least such a goal is not obvious for this example. -manfred
Feb 06 2012
prev sibling parent reply Jos van Uden <user domain.invalid> writes:
On 5-2-2012 5:05, Manfred Nowak wrote:

 2)
 The optional third parameter of `indexOf' can be called with
 `CaseSensitive.no'. But that parameter is left untouched. Instead a
 check with `toUpper( c)' is used, thereby risking a further visitation
 of the whole string .
Using CaseSensitive.no is a lot slower
Feb 04 2012
parent reply "Era Scarecrow" <rtcvb32 yahoo.com> writes:
 The optional third parameter of `indexOf' can be called with
 `CaseSensitive.no'. But that parameter is left untouched. 
 Instead a check with `toUpper( c)' is used, thereby risking a 
 further visitation of the whole string .
Using CaseSensitive.no is a lot slower
Hmmm then perhaps a whole re-write. I've gotten this done, although I'll need someone to tell me how well it does. Far as I can tell it's O(n). It's based loosely on the ascii version, except templatized. Hope it's not too ugly... import std.stdio, std.string, std.traits, std.uni, std.conv; string T_isPangram(string name, string set, string uset = "") { string x = "bool " ~ name ~ "(S)(S s) if (isSomeString!S) {\n"; if(set.length > 32) { x ~= "\tulong bitset;\n"; } else { x ~= "\tuint bitset;\n"; } x ~= "\tforeach(dchar c; s) {\n\t\tswitch(toLower(c)) {\n"; foreach(i, c; set) { x ~= "\t\t\tcase '" ~ c ~ "':"; x ~= "\tbitset |= 1 << " ~ to!string(i) ~ "; break;\n"; } int i = set.length; while(uset.length >= 6) { x ~= "\t\t\tcase '" ~ uset[0 .. 6] ~ "':"; x ~= "\tbitset |= 1 << " ~ to!string(i) ~ "; break;\n"; uset = uset[6 .. $]; i++; } x ~= "\t\t\tdefault:\n\t\t}\n\t}\n\treturn bitset == (1 << " ~ to!string(i) ~ ") - 1;\n}"; return x; } mixin(T_isPangram("EN_isPangram", "abcdefghijklmnopqrstuvwxyz")); mixin(T_isPangram("DE_isPangram", "abcdefghijklmnopqrstuvwxyz", "\\u00DF\\u00e4\\u00f6\\u00dc")); mixin(T_isPangram("SV_isPangram", "abcdefghijklmnopqrstuvwxyz", "\\u00e5\\u00e4\\u00f6")); unittest { writeln(T_isPangram("TEST_isPangram", "ab", "\\u00DF\\u00e4")); //example output below assert(!EN_isPangram("this doesn't cover everything")); assert(EN_isPangram("the quick brown fox jumps over the LAZY dog")); //to-lower check along with english assert(DE_isPangram("Falsches Üben von Xylophonmusik quält jeden größeren Zwerg")); assert(SV_isPangram("Yxskaftbud, ge vår wczonmö iqhjälp"w)); } /* bool TEST_isPangram(S)(S s) if (isSomeString!S) { uint bitset; foreach(dchar c; s) { switch(toLower(c)) { case 'a': bitset |= 1 << 0; break; case 'b': bitset |= 1 << 1; break; case '\u00DF': bitset |= 1 << 2; break; case '\u00e4': bitset |= 1 << 3; break; default: } } return bitset == (1 << 4) - 1; } */
Feb 05 2012
parent reply "Era Scarecrow" <rtcvb32 yahoo.com> writes:
 mixin(T_isPangram("DE_isPangram", "abcdefghijklmnopqrstuvwxyz", 
 "\\u00DF\\u00e4\\u00f6\\u00dc"));
somehow this got mixed up with the earlier messed up one.. this is the correction for this line. mixin(T_isPangram("DE_isPangram", "abcdefghijklmnopqrstuvwxyz", "\\u00DF\\u00e4\\u00f6\\u00fc"));
Feb 05 2012
parent "Marco Leise" <Marco.Leise gmx.de> writes:
Am 06.02.2012, 07:38 Uhr, schrieb Era Scarecrow <rtcvb32 yahoo.com>:

 mixin(T_isPangram("DE_isPangram", "abcdefghijklmnopqrstuvwxyz",  
 "\\u00DF\\u00e4\\u00f6\\u00dc"));
somehow this got mixed up with the earlier messed up one.. this is the correction for this line. mixin(T_isPangram("DE_isPangram", "abcdefghijklmnopqrstuvwxyz", "\\u00DF\\u00e4\\u00f6\\u00fc"));
Actually, yeah a bit Scarecrow like. For dlang or rosettacode I prefer short and easy to read code. The one example where the result is checked against 0b11_1111111_11111111_11111111 was really nice, since it expresses the bit set nature in a concise and readable fashion. But it also shows that you can put integer literals in dual notation and place _ to segment the number.
Feb 06 2012