Pangram checker

From Rosetta Code
Revision as of 22:14, 25 January 2010 by Tikkanz (talk | contribs) (Add J)
Task
Pangram checker
You are encouraged to solve this task according to the task description, using any language you may know.

Write a function or method to check a sentence to see if it is a pangram or not and show its use.

A pangram is a sentence that contains all the letters of the alphabet at least once, for example: The quick brown fox jumps over the lazy dog.

E

<lang e>def isPangram(sentence :String) {

   return ("abcdefghijklmnopqrstuvwxyz".asSet() &! sentence.toLowerCase().asSet()).size() == 0

}</lang>

&! is the “but-not” or set difference operator.

Factor

Translation of: E

<lang factor>: pangram? ( str -- ? )

   [ "abcdefghijklmnopqrstuvwxyz" ] dip >lower diff length 0 = ;

"How razorback-jumping frogs can level six piqued gymnasts!" pangram? .</lang>

Haskell

<lang haskell>import Data.Char (toLower) import Data.List ((\\))

pangram :: String -> Bool pangram = null . (['a' .. 'z'] \\) . map toLower

main = print $ pangram "How razorback-jumping frogs can level six piqued gymnasts!"</lang>

J

Solution: <lang j>require 'strings' isPangram=: (a. {~ 97+i.26) */@e. tolower</lang>

Example use: <lang j> isPangram 'The quick brown fox jumps over the lazy dog.' 1

  isPangram 'The quick brown fox falls over the lazy dog.'

0</lang>

OCaml

<lang ocaml>let pangram str =

 let ar = Array.make 26 false in
 String.iter (function
 | 'a'..'z' as c -> ar.(Char.code c - Char.code 'a') <- true
 | _ -> ()
 ) (String.lowercase str);
 Array.fold_left ( && ) true ar</lang>

<lang ocaml>let check str =

 Printf.printf " %b -- %s\n" (pangram str) str

let () =

 check "this is a sentence";
 check "The quick brown fox jumps over the lazy dog.";
</lang>

outputs:

false -- this is a sentence
true -- The quick brown fox jumps over the lazy dog.

Perl

<lang perl>use List::MoreUtils 'all';

sub pangram {all {$_[0] =~ /$_/i} 'a' .. 'z';}

print "Yes.\n" if pangram 'Cozy lummox gives smart squid who asks for job pen.';</lang>

Python

Using set arithmetic: <lang python>import string, sys if sys.version_info[0] < 3:

   input = raw_input

def ispangram(sentence, alphabet=string.ascii_lowercase):

   alphaset = set(alphabet)
   return not alphaset - set(sentence.lower())

print ( ispangram(input('Sentence: ')) )</lang>

Sample output:

Sentence: The quick brown fox jumps over the lazy dog
True

Ruby

<lang ruby>def pangram?(sentence)

 unused_letters = ('a'..'z').to_a - str.downcase.chars.to_a
 unused_letters.empty?

end

p pangram?('this is a sentence') # ==> false p pangram?('The quick brown fox jumps over the lazy dog.') # ==> true</lang>