Pangram checker: Difference between revisions

From Rosetta Code
Content added Content deleted
(Forth)
(Logo)
Line 167: Line 167:
print(is_pangram("is this a pangram")); // false
print(is_pangram("is this a pangram")); // false
print(is_pangram("The quick brown fox jumps over the lazy dog")); // true</lang>
print(is_pangram("The quick brown fox jumps over the lazy dog")); // true</lang>

=={{header|Logo}}==
<lang logo>to remove.all :s :set
if empty? :s [output :set]
if word? :s [output remove.all butfirst :s remove first :s :set]
output remove.all butfirst :s remove.all first :s :set
end
to pangram? :s
output empty? remove.all :s "abcdefghijklmnopqrstuvwxyz
end

show pangram? [The five boxing wizards jump quickly.] ; true</lang>


=={{header|OCaml}}==
=={{header|OCaml}}==

Revision as of 19:35, 27 January 2010

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 English 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>

Forth

<lang forth>: pangram? ( addr len -- ? )

 0 -rot bounds do
   i c@ 32 or [char] a -
   dup 0 26 within if
     1 swap lshift or
   else drop then
 loop
 1 26 lshift 1- = ;

s" The five boxing wizards jump quickly." pangram? . \ -1</lang>

Fortran

Works with: Fortran version 90 and later

<lang fortran>module pangram

 implicit none
 private
 public :: is_pangram
 character (*), parameter :: lower_case = 'abcdefghijklmnopqrstuvwxyz'
 character (*), parameter :: upper_case = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

contains

 function to_lower_case (input) result (output)
   implicit none
   character (*), intent (in) :: input
   character (len (input)) :: output
   integer :: i
   integer :: j
   output = input
   do i = 1, len (output)
     j = index (upper_case, output (i : i))
     if (j /= 0) then
       output (i : i) = lower_case (j : j)
     end if
   end do
 end function to_lower_case
 function is_pangram (input) result (output)
   implicit none
   character (*), intent (in) :: input
   character (len (input)) :: lower_case_input
   logical :: output
   integer :: i
   lower_case_input = to_lower_case (input)
   output = .true.
   do i = 1, len (lower_case)
     if (index (lower_case_input, lower_case (i : i)) == 0) then
       output = .false.
       exit
     end if
   end do
 end function is_pangram

end module pangram</lang> Example: <lang fortran>program test

 use pangram, only: is_pangram
 implicit none
 character (256) :: string
 string = 'This is a sentence.'
 write (*, '(a)') trim (string)
 write (*, '(l1)') is_pangram (string)
 string = 'The five boxing wizards jumped quickly.'
 write (*, '(a)') trim (string)
 write (*, '(l1)') is_pangram (string)

end program test</lang> Output: <lang>This is a sentence. F The five boxing wizards jumped quickly. T</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>

Java

<lang java>public class Pangram {

   public static boolean isPangram(String test){
       boolean pangram = true;
       String lcTest = test.toLowerCase();
       for(char a = 'a'; a <= 'z'; a++){
           pangram &= lcTest.contains("" + a);
       }
       return pangram;
   }
   public static void main(String[] args){
       System.out.println(isPangram("the quick brown fox jumps over the lazy dog"));//true
       System.out.println(isPangram("the quick brown fox jumped over the lazy dog"));//false, no s
       System.out.println(isPangram("ABCDEFGHIJKLMNOPQRSTUVWXYZ"));//true
       System.out.println(isPangram("ABCDEFGHIJKLMNOPQSTUVWXYZ"));//false, no r
       System.out.println(isPangram("ABCDEFGHIJKL.NOPQRSTUVWXYZ"));//false, no m
       System.out.println(isPangram("ABC.D.E.FGHI*J/KL-M+NO*PQ R\nSTUVWXYZ"));//true
       System.out.println(isPangram(""));//false
   }

}</lang> Output:

true
false
true
false
false
true
false

JavaScript

Translation of: Java

<lang javascript>function is_pangram(str) {

   var s = str.toLowerCase();
   // sorted by frequency ascending (http://en.wikipedia.org/wiki/Letter_frequency)
   var letters = "zqxjkvbpygfwmucldrhsnioate";
   for (var i = 0; i < 26; i++)
       if (s.indexOf(letters.charAt(i)) == -1)
           return false;
   return true;

}

print(is_pangram("is this a pangram")); // false print(is_pangram("The quick brown fox jumps over the lazy dog")); // true</lang>

<lang logo>to remove.all :s :set

 if empty? :s [output :set]
 if word? :s [output remove.all butfirst :s remove first :s :set]
 output remove.all butfirst :s remove.all first :s :set

end to pangram? :s

 output empty? remove.all :s "abcdefghijklmnopqrstuvwxyz

end

show pangram? [The five boxing wizards jump quickly.]  ; true</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.

Oz

<lang oz>declare

 fun {IsPangram Xs}
    {List.sub
     {List.number &a &z 1}
     {Sort {Map Xs Char.toLower} Value.'<'}}
 end

in

 {Show {IsPangram "The quick brown fox jumps over the lazy dog."}}</lang>

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>

Tcl

<lang tcl>proc pangram? {sentence} {

   set letters [regexp -all -inline {[a-z]} [string tolower $sentence]]
   expr {
       [llength [lsort -unique $letters]] == 26
   }

}

puts [pangram? "This is a sentence"]; # ==> false puts [pangram? "The quick brown fox jumps over the lazy dog."]; # ==> true</lang>