2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,6 +1,10 @@
{{omit from|Lilypond}}
Write a function or method to check a sentence
to see if it is a [[wp:Pangram|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''.
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''.
;Task:
Write a function or method to check a sentence to see if it is a   [[wp:Pangram|pangram]]   (or not)   and show its use.
<br><br>

View file

@ -0,0 +1,91 @@
use framework "Foundation" -- ( for case conversion function )
-- isPangram :: String -> Bool
on isPangram(s)
script charUnUsed
property lowerCaseString : my toLowerCase(s)
on lambda(c)
lowerCaseString does not contain c
end lambda
end script
length of filter(charUnUsed, "abcdefghijklmnopqrstuvwxyz") = 0
end isPangram
-- TEST
on run
map(isPangram, {¬
"is this a pangram", ¬
"The quick brown fox jumps over the lazy dog"})
--> {false, true}
end run
-- GENERIC HIGHER ORDER FUNCTIONS (FILTER AND MAP)
-- filter :: (a -> Bool) -> [a] -> [a]
on filter(f, xs)
tell mReturn(f)
set lst to {}
set lng to length of xs
repeat with i from 1 to lng
set v to item i of xs
if lambda(v, i, xs) then set end of lst to v
end repeat
return lst
end tell
end filter
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property lambda : f
end script
end if
end mReturn
-- OBJC function: lowercaseStringWithLocale
-- toLowerCase :: String -> String
on toLowerCase(str)
set ca to current application
unwrap(wrap(str)'s ¬
lowercaseStringWithLocale:(ca's NSLocale's currentLocale))
end toLowerCase
-- wrap :: AS value -> NSObject
on wrap(v)
set ca to current application
ca's (NSArray's arrayWithObject:v)'s objectAtIndex:0
end wrap
-- unwrap :: NSObject -> AS value
on unwrap(objCValue)
if objCValue is missing value then
return missing value
else
set ca to current application
item 1 of ((ca's NSArray's arrayWithObject:objCValue) as list)
end if
end unwrap

View file

@ -0,0 +1 @@
{false, true}

View file

@ -1,16 +1,24 @@
#include <algorithm>
#include <cctype>
#include <string>
using namespace std;
#include <iostream>
const string alphabet("abcdefghijklmnopqrstuvwxyz"); // sorted, no duplicates
const std::string alphabet("abcdefghijklmnopqrstuvwxyz");
bool is_pangram(string s) {
// Convert to lower case.
transform(s.begin(), s.end(), s.begin(), ::tolower);
// Convert to a sorted sequence of (not necessarily unique) characters.
sort(s.begin(), s.end());
// Is the second sequence a subset of the first sequence?
// Repeated letters in "s" are okay, since it still "includes" the single letter
return includes(s.begin(), s.end(), alphabet.begin(), alphabet.end());
bool is_pangram(std::string s)
{
std::transform(s.begin(), s.end(), s.begin(), ::tolower);
std::sort(s.begin(), s.end());
return std::includes(s.begin(), s.end(), alphabet.begin(), alphabet.end());
}
int main()
{
const auto examples = {"The quick brown fox jumps over the lazy dog",
"The quick white cat jumps over the lazy dog"};
std::cout.setf(std::ios::boolalpha);
for (auto& text : examples) {
std::cout << "Is \"" << text << "\" a pangram? - " << is_pangram(text) << std::endl;
}
}

View file

@ -1,27 +1,32 @@
#include <stdio.h>
int isPangram(const char *string)
int is_pangram(const char *s)
{
const char *alpha = ""
"abcdefghjiklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char ch, wasused[26] = {0};
int total = 0;
while ((ch = *string++)) {
int index;
while ((ch = *s++) != '\0') {
const char *p;
int idx;
if('A'<=ch&&ch<='Z')
index = ch-'A';
else if('a'<=ch&&ch<='z')
index = ch-'a';
else
if ((p = strchr(alpha, ch)) == NULL)
continue;
total += !wasused[index];
wasused[index] = 1;
idx = (p - alpha) % 26;
total += !wasused[idx];
wasused[idx] = 1;
if (total == 26)
return 1;
}
return (total==26);
return 0;
}
int main()
int main(void)
{
int i;
const char *tests[] = {
@ -31,6 +36,6 @@ int main()
for (i = 0; i < 2; i++)
printf("\"%s\" is %sa pangram\n",
tests[i], isPangram(tests[i])?"":"not ");
tests[i], is_pangram(tests[i])?"":"not ");
return 0;
}

View file

@ -0,0 +1,51 @@
identification division.
program-id. pan-test.
data division.
working-storage section.
1 text-string pic x(80).
1 len binary pic 9(4).
1 trailing-spaces binary pic 9(4).
1 pangram-flag pic x value "n".
88 is-not-pangram value "n".
88 is-pangram value "y".
procedure division.
begin.
display "Enter text string:"
accept text-string
set is-not-pangram to true
initialize trailing-spaces len
inspect function reverse (text-string)
tallying trailing-spaces for leading space
len for characters after space
call "pangram" using pangram-flag len text-string
cancel "pangram"
if is-pangram
display "is a pangram"
else
display "is not a pangram"
end-if
stop run
.
end program pan-test.
identification division.
program-id. pangram.
data division.
1 lc-alphabet pic x(26) value "abcdefghijklmnopqrstuvwxyz".
linkage section.
1 pangram-flag pic x.
88 is-not-pangram value "n".
88 is-pangram value "y".
1 len binary pic 9(4).
1 text-string pic x(80).
procedure division using pangram-flag len text-string.
begin.
inspect lc-alphabet converting
function lower-case (text-string (1:len))
to space
if lc-alphabet = space
set is-pangram to true
end-if
exit program
.
end program pangram.

View file

@ -17,27 +17,21 @@ func main() {
}
func pangram(s string) bool {
var rep [26]bool
var count int
for _, c := range s {
if c >= 'a' {
if c > 'z' {
continue
}
c -= 'a'
} else {
if c < 'A' || c > 'Z' {
continue
}
c -= 'A'
}
if !rep[c] {
if count == 25 {
return true
}
rep[c] = true
count++
}
}
return false
var missing uint32 = (1 << 26) - 1
for _, c := range s {
var index uint32
if 'a' <= c && c <= 'z' {
index = uint32(c - 'a')
} else if 'A' <= c && c <= 'Z' {
index = uint32(c - 'A')
} else {
continue
}
missing &^= 1 << index
if missing == 0 {
return true
}
}
return false
}

View file

@ -0,0 +1,14 @@
Sequence isPangram := method(
letters := " " repeated(26)
ia := "a" at(0)
foreach(ichar,
if(ichar isLetter,
letters atPut((ichar asLowercase) - ia, ichar)
)
)
letters contains(" " at(0)) not // true only if no " " in letters
)
"The quick brown fox jumps over the lazy dog." isPangram println // --> true
"The quick brown fox jumped over the lazy dog." isPangram println // --> false
"ABC.D.E.FGHI*J/KL-M+NO*PQ R\nSTUVWXYZ" isPangram println // --> true

View file

@ -1,12 +1,11 @@
function is_pangram(str) {
var s = str.toLowerCase();
function isPangram(s) {
var letters = "zqxjkvbpygfwmucldrhsnioate"
// sorted by frequency ascending (http://en.wikipedia.org/wiki/Letter_frequency)
var letters = "zqxjkvbpygfwmucldrhsnioate";
s = s.toLowerCase().replace(/[^a-z]/g,'')
for (var i = 0; i < 26; i++)
if (s.indexOf(letters.charAt(i)) == -1)
return false;
return true;
if (s.indexOf(letters[i]) < 0) 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
console.log(isPangram("is this a pangram")) // false
console.log(isPangram("The quick brown fox jumps over the lazy dog")) // true

View file

@ -1,36 +1,20 @@
var _ = require("underscore");
(() => {
'use strict';
// Curried mixin function
// Utility Methods
_.mixin({
checkAToZ: function(s) {
return function(letter) {
if (s.indexOf(letter) != -1) { return true};
}
}
});
// isPangram :: String -> Bool
let isPangram = s => {
let lc = s.toLowerCase();
_.mixin({
toLower: function(str) {
return str.toLowerCase();
}
});
return 'abcdefghijklmnopqrstuvwxyz'
.split('')
.filter(c => lc.indexOf(c) === -1)
.length === 0;
};
_.mixin({
isPangram: function(lstr) {
var letters = "zqxjkvbpygfwmucldrhsnioate".split('');
return _.every(letters, _.checkAToZ(lstr));
}
});
// TEST
return [
'is this a pangram',
'The quick brown fox jumps over the lazy dog'
].map(isPangram);
var panGramStr = "The quick brown fox jumps over the lazy dog";
var IsPanGram = function(panGramStr) {
return _.chain(panGramStr).toLower().isPangram().value();
};
console.log("Result IsPanGram - \"", panGramStr,"\" - " , IsPanGram.call(this,panGramStr));
console.log("Result IsPanGram - \"", "the World","\" - ", IsPanGram.call(this, "the World"));
// Result IsPanGram - " The quick brown fox jumps over the lazy dog " - true
// Result IsPanGram - " the World " - false
})();

View file

@ -0,0 +1,18 @@
(context 'PGR) ;; Switch to context (say namespace) PGR
(define (is-pangram? str)
(setf chars (explode (upper-case str))) ;; Uppercase + convert string into a list of chars
(setf is-pangram-status true) ;; Default return value of function
(for (c (char "A") (char "Z") 1 (nil? is-pangram-status)) ;; For loop with break condition
(if (not (find (char c) chars)) ;; If char not found in list, "is-pangram-status" becomes "nil"
(setf is-pangram-status nil)
)
)
is-pangram-status ;; Return current value of symbol "is-pangram-status"
)
(context 'MAIN) ;; Back to MAIN context
;; - - - - - - - - - -
(println (PGR:is-pangram? "abcdefghijklmnopqrstuvwxyz")) ;; Print true
(println (PGR:is-pangram? "abcdef")) ;; Print nil
(exit)

View file

@ -0,0 +1,13 @@
function Test-Pangram ( [string]$Text, [string]$Alphabet = 'abcdefghijklmnopqrstuvwxyz' )
{
$Text = $Text.ToLower()
$Alphabet = $Alphabet.ToLower()
$IsPangram = $Alphabet.ToCharArray().Where{ $Text.Contains( $_ ) }.Count -eq $Alphabet.Length
return $IsPangram
}
Test-Pangram 'The quick brown fox jumped over the lazy dog.'
Test-Pangram 'The quick brown fox jumps over the lazy dog.'
Test-Pangram 'Съешь же ещё этих мягких французских булок, да выпей чаю' 'абвгдежзийклмнопрстуфхцчшщъыьэюяё'

View file

@ -0,0 +1,13 @@
function Test-Pangram ( [string]$Text, [string]$Alphabet = 'abcdefghijklmnopqrstuvwxyz' )
{
$Text = $Text.ToLower()
$Alphabet = $Alphabet.ToLower()
$IsPangram = ( $Alphabet.ToCharArray() | Where { $Text.Contains( $_ ) } ).Count -eq $Alphabet.Length
return $IsPangram
}
Test-Pangram 'The quick brown fox jumped over the lazy dog.'
Test-Pangram 'The quick brown fox jumps over the lazy dog.'
Test-Pangram 'Съешь же ещё этих мягких французских булок, да выпей чаю' 'абвгдежзийклмнопрстуфхцчшщъыьэюяё'

View file

@ -1,16 +1,14 @@
/*REXX program checks to see if an entered string (sentence) is a pangram. */
@abc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' /*a list of all (Latin) capital letters*/
/*REXX program verifies if an entered/supplied string (sentence) is a pangram. */
@abc= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' /*a list of all (Latin) capital letters*/
do forever; say /*keep promoting 'til null (or blanks).*/
say ' Please enter a pangramic sentence:'; say
pull y /*this also uppercases the Y variable.*/
if y='' then leave /*if nothing entered, then we're done.*/
?=verify(@abc,y) /*Are all the (Latin) letters present? */
do forever; say /*keep promoting 'til null (or blanks).*/
say ' Please enter a pangramic sentence (or a blank to quit):'; say
pull y /*this also uppercases the Y variable.*/
if y='' then leave /*if nothing entered, then we're done.*/
?=verify(@abc, y) /*Are all the (Latin) letters present? */
if ?==0 then say ' Sentence is a pangram.'
else say "──────── Sentence isn't a pangram, missing:" substr(@abc, ?, 1)
say
end /*forever*/
if ?==0 then say 'Sentence is a pangram.'
else say "Sentence isn't a pangram, missing:" substr(@abc,?,1)
say
end /*forever*/
say ' PANGRAM program ended. '
/*stick a fork in it, we're all done. */
say ' PANGRAM program ended. ' /*stick a fork in it, we're all done. */

View file

@ -0,0 +1,69 @@
#![feature(test)]
extern crate test;
use std::collections::HashSet;
pub fn is_pangram_via_bitmask(s: &str) -> bool {
// Create a mask of set bits and convert to false as we find characters.
let mut mask = (1 << 26) - 1;
for chr in s.chars() {
let val = chr as u32 & !0x20; /* 0x20 converts lowercase to upper */
if val <= 'Z' as u32 && val >= 'A' as u32 {
mask = mask & !(1 << (val - 'A' as u32));
}
}
mask == 0
}
pub fn is_pangram_via_hashset(s: &str) -> bool {
// Insert lowercase letters into a HashSet, then check if we have at least 26.
let letters = s.chars()
.flat_map(|chr| chr.to_lowercase())
.filter(|&chr| chr >= 'a' && chr <= 'z')
.fold(HashSet::new(), |mut letters, chr| {
letters.insert(chr);
letters
});
letters.len() == 26
}
pub fn is_pangram_via_sort(s: &str) -> bool {
// Copy chars into a vector, convert to lowercase, sort, and remove duplicates.
let mut chars: Vec<char> = s.chars()
.flat_map(|chr| chr.to_lowercase())
.filter(|&chr| chr >= 'a' && chr <= 'z')
.collect();
chars.sort();
chars.dedup();
chars.len() == 26
}
fn main() {
let examples = ["The quick brown fox jumps over the lazy dog",
"The quick white cat jumps over the lazy dog"];
for &text in examples.iter() {
let is_pangram_sort = is_pangram_via_sort(text);
println!("Is \"{}\" a pangram via sort? - {}", text, is_pangram_sort);
let is_pangram_bitmask = is_pangram_via_bitmask(text);
println!("Is \"{}\" a pangram via bitmask? - {}",
text,
is_pangram_bitmask);
let is_pangram_hashset = is_pangram_via_hashset(text);
println!("Is \"{}\" a pangram via bitmask? - {}",
text,
is_pangram_hashset);
}
}