update
This commit is contained in:
parent
1f1ad49427
commit
6f050a029e
2496 changed files with 37609 additions and 3031 deletions
27
Task/String-comparison/Ada/string-comparison.ada
Normal file
27
Task/String-comparison/Ada/string-comparison.ada
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
with Ada.Text_IO, Ada.Characters.Handling;
|
||||
|
||||
procedure String_Compare is
|
||||
|
||||
procedure Print_Comparison (A, B: String) is
|
||||
use Ada.Text_IO, Ada.Characters.Handling;
|
||||
begin
|
||||
Put_Line
|
||||
( """" & A & """ and """ & B & """: " &
|
||||
(if A = B then "equal, " elsif To_Lower(A) = To_Lower(B)
|
||||
then "case-insensitive-equal, " else "not equal at all, ") &
|
||||
(if A /= B then "/=, " else "") &
|
||||
(if A < B then "before, " else "") &
|
||||
(if A > B then "after, " else "") &
|
||||
(if A <= B then "<=, " else "(not <=), ") & "and " &
|
||||
(if A >= B then ">=. " else "(not >=).") );
|
||||
end Print_Comparison;
|
||||
|
||||
begin
|
||||
Print_Comparison("this", "that");
|
||||
Print_Comparison("that", "this");
|
||||
Print_Comparison("THAT", "That");
|
||||
Print_Comparison("this", "This");
|
||||
Print_Comparison("this", "this");
|
||||
Print_Comparison("the", "there");
|
||||
Print_Comparison("there", "the");
|
||||
end String_Compare;
|
||||
102
Task/String-comparison/Bracmat/string-comparison.bracmat
Normal file
102
Task/String-comparison/Bracmat/string-comparison.bracmat
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
( {Comparing two strings for exact equality}
|
||||
& ( ( @(abc:abc)
|
||||
& @(123:%123)
|
||||
{Previous pairs of strings are exactly equal}
|
||||
)
|
||||
& ( @(abc:Abc)
|
||||
| @(123:%246/2)
|
||||
| @(abc:ab)
|
||||
| @(123:%12)
|
||||
| {Previous pairs of strings are not exactly equal}
|
||||
)
|
||||
)
|
||||
{Comparing two strings for inequality (i.e., the inverse of exact equality)}
|
||||
& ( ( @(abc:~<>abc)
|
||||
& @(abc:~<>Abc)
|
||||
{Previous pairs of strings are more or less equal}
|
||||
)
|
||||
& ( @(abc:~<>ab)
|
||||
| {Previous pairs of strings are not more or less equal}
|
||||
)
|
||||
)
|
||||
{Comparing two strings to see if one is lexically ordered before than the other}
|
||||
& ( ( @(Abc:<abc)
|
||||
& @(Abc:<a)
|
||||
& @(123:<%246/2)
|
||||
& @(123:<%2)
|
||||
& @(12:<%123)
|
||||
& @(ab:<abc)
|
||||
{Previous pairs of strings are lexically ordered one before the other}
|
||||
)
|
||||
& ( @(abc:<abc)
|
||||
| @(abc:<Abc)
|
||||
| @(246/2:<%123)
|
||||
| @(abc:<ab)
|
||||
| @(123:<%12)
|
||||
| @(123:<%123)
|
||||
| {Previous pairs of strings are not lexically ordered one before the other}
|
||||
)
|
||||
)
|
||||
{Comparing two strings to see if one is lexically ordered after than the other}
|
||||
& ( ( @(abc:>Abc)
|
||||
& @(a:>Abc)
|
||||
& @(246/2:>%123)
|
||||
& @(2:>%123)
|
||||
& @(123:>%12)
|
||||
& @(abc:>ab)
|
||||
{Previous pairs of strings are lexically ordered one after the other}
|
||||
)
|
||||
& ( @(abc:>abc)
|
||||
| @(Abc:>abc)
|
||||
| @(123:>%246/2)
|
||||
| @(ab:>abc)
|
||||
| @(12:>%123)
|
||||
| @(123:>%123)
|
||||
| {Previous pairs of strings are not lexically ordered one after the other}
|
||||
)
|
||||
)
|
||||
{How to achieve both case sensitive comparisons and case insensitive comparisons within
|
||||
the language}
|
||||
& ( ( @(abc:~<>abc)
|
||||
& @(abc:~<>Abc)
|
||||
& @(БЪЛГАРСКИ:~<>български)
|
||||
{Previous pairs of strings are more or less equal}
|
||||
)
|
||||
& ( @(abc:~<>ab)
|
||||
| {Previous pairs of strings are not more or less equal}
|
||||
)
|
||||
)
|
||||
{How the language handles comparison of numeric strings if these are not treated lexically}
|
||||
& ( ( @(246/2:123)
|
||||
& @(2:<123)
|
||||
& @(123:>12)
|
||||
& @(123:246/2)
|
||||
& @(12:<123)
|
||||
{Previous numeric string comparisons succeed}
|
||||
)
|
||||
& ( @(123:<246/2)
|
||||
| @(12:>123)
|
||||
| @(123:>123)
|
||||
| @(123:~123)
|
||||
| {Previous numeric string comparisons fail}
|
||||
)
|
||||
)
|
||||
{Demonstrate any other kinds of string comparisons that the language provides, particularly
|
||||
as it relates to your type system. For example, you might demonstrate the difference between
|
||||
generic/polymorphic comparison and coercive/allomorphic comparison if your language supports
|
||||
such a distinction.}
|
||||
& ( ( @(246/2:>12--3)
|
||||
& @(2:>123kg)
|
||||
& @(123:<12d)
|
||||
& @(123:~24/6/2)
|
||||
& @(12a:>123)
|
||||
{Previous coercive string comparisons succeed}
|
||||
)
|
||||
& ( @(2013-05-01:20130501)
|
||||
| @(246/2a:123a)
|
||||
| @(1239:<123-)
|
||||
| {Previous coercive string comparisons fail}
|
||||
)
|
||||
)
|
||||
& done
|
||||
);
|
||||
2
Task/String-comparison/C/string-comparison-1.c
Normal file
2
Task/String-comparison/C/string-comparison-1.c
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
/* WRONG! */
|
||||
if (strcmp(a,b)) action_on_equality();
|
||||
61
Task/String-comparison/C/string-comparison-2.c
Normal file
61
Task/String-comparison/C/string-comparison-2.c
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
compilation and test in bash
|
||||
$ a=./c && make $a && $a ball bell ball ball YUP YEP ball BELL ball BALL YUP yep
|
||||
cc -Wall -c -o c.o c.c
|
||||
eq , ne , gt , lt , ge , le
|
||||
ball 0 1 0 1 0 1 bell
|
||||
ball 0 1 0 1 0 1 bell ignoring case
|
||||
ball 1 0 0 0 1 1 ball
|
||||
ball 1 0 0 0 1 1 ball ignoring case
|
||||
YUP 0 1 1 0 1 0 YEP
|
||||
YUP 0 1 1 0 1 0 YEP ignoring case
|
||||
ball 0 1 1 0 1 0 BELL
|
||||
ball 0 1 0 1 0 1 BELL ignoring case
|
||||
ball 0 1 1 0 1 0 BALL
|
||||
ball 1 0 0 0 1 1 BALL ignoring case
|
||||
YUP 0 1 0 1 0 1 yep
|
||||
YUP 0 1 1 0 1 0 yep ignoring case
|
||||
*/
|
||||
|
||||
#include<string.h>
|
||||
|
||||
#define STREQ(A,B) (0==strcmp((A),(B)))
|
||||
#define STRNE(A,B) (!STREQ(A,B))
|
||||
#define STRLT(A,B) (strcmp((A),(B))<0)
|
||||
#define STRLE(A,B) (strcmp((A),(B))<=0)
|
||||
#define STRGT(A,B) STRLT(B,A)
|
||||
#define STRGE(A,B) STRLE(B,A)
|
||||
|
||||
#define STRCEQ(A,B) (0==strcasecmp((A),(B)))
|
||||
#define STRCNE(A,B) (!STRCEQ(A,B))
|
||||
#define STRCLT(A,B) (strcasecmp((A),(B))<0)
|
||||
#define STRCLE(A,B) (strcasecmp((A),(B))<=0)
|
||||
#define STRCGT(A,B) STRCLT(B,A)
|
||||
#define STRCGE(A,B) STRCLE(B,A)
|
||||
|
||||
#include<stdio.h>
|
||||
|
||||
void compare(const char*a, const char*b) {
|
||||
printf("%s%2d%2d%2d%2d%2d%2d %s\n",
|
||||
a,
|
||||
STREQ(a,b), STRNE(a,b), STRGT(a,b), STRLT(a,b), STRGE(a,b), STRLE(a,b),
|
||||
b
|
||||
);
|
||||
}
|
||||
void comparecase(const char*a, const char*b) {
|
||||
printf("%s%2d%2d%2d%2d%2d%2d %s ignoring case\n",
|
||||
a,
|
||||
STRCEQ(a,b), STRCNE(a,b), STRCGT(a,b), STRCLT(a,b), STRCGE(a,b), STRCLE(a,b),
|
||||
b
|
||||
);
|
||||
}
|
||||
int main(int ac, char*av[]) {
|
||||
char*a,*b;
|
||||
puts("\teq , ne , gt , lt , ge , le");
|
||||
while (0 < (ac -= 2)) {
|
||||
a = *++av, b = *++av;
|
||||
compare(a, b);
|
||||
comparecase(a, b);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
12
Task/String-comparison/Erlang/string-comparison.erl
Normal file
12
Task/String-comparison/Erlang/string-comparison.erl
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
10> V = "abcd".
|
||||
"abcd"
|
||||
11> V =:= "abcd".
|
||||
true
|
||||
12> V =/= "abcd".
|
||||
false
|
||||
13> V < "b".
|
||||
true
|
||||
15> V > "aa".
|
||||
true
|
||||
16> string:to_lower(V) =:= string:to_lower("ABCD").
|
||||
true
|
||||
6
Task/String-comparison/Forth/string-comparison.fth
Normal file
6
Task/String-comparison/Forth/string-comparison.fth
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
: str-eq ( str len str len -- ? ) compare 0= ;
|
||||
: str-neq ( str len str len -- ? ) compare 0<> ;
|
||||
: str-lt ( str len str len -- ? ) compare 0< ;
|
||||
: str-gt ( str len str len -- ? ) compare 0> ;
|
||||
: str-le ( str len str len -- ? ) compare 0<= ;
|
||||
: str-ge ( str len str len -- ? ) compare 0>= ;
|
||||
19
Task/String-comparison/Lua/string-comparison.lua
Normal file
19
Task/String-comparison/Lua/string-comparison.lua
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
function compare(a, b)
|
||||
print(("%s is of type %s and %s is of type %s"):format(
|
||||
a, type(a),
|
||||
b, type(b)
|
||||
))
|
||||
if a < b then print(('%s is strictly less than %s'):format(a, b)) end
|
||||
if a <= b then print(('%s is less than or equal to %s'):format(a, b)) end
|
||||
if a > b then print(('%s is strictly greater than %s'):format(a, b)) end
|
||||
if a >= b then print(('%s is greater than or equal to %s'):format(a, b)) end
|
||||
if a == b then print(('%s is equal to %s'):format(a, b)) end
|
||||
if a ~= b then print(('%s is not equal to %s'):format(a, b)) end
|
||||
print ""
|
||||
end
|
||||
|
||||
compare('YUP', 'YUP')
|
||||
compare('BALL', 'BELL')
|
||||
compare('24', '123')
|
||||
compare(24, 123)
|
||||
compare(5.0, 5)
|
||||
17
Task/String-comparison/REXX/string-comparison-1.rexx
Normal file
17
Task/String-comparison/REXX/string-comparison-1.rexx
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
animal = 'dog'
|
||||
if animal = 'cat' then say animal "is lexically equal to cat"
|
||||
if animal \= 'cat' then say animal "is not lexically equal cat"
|
||||
if animal > 'cat' then say animal "is lexically higher than cat"
|
||||
if animal < 'cat' then say animal "is lexically lower than cat"
|
||||
if animal >= 'cat' then say animal "is not lexically lower than cat"
|
||||
if animal <= 'cat' then say animal "is not lexically higher than cat"
|
||||
|
||||
/*The above comparative operators do not consider */
|
||||
/*leading and trailing whitespace when making comparisons.*/
|
||||
|
||||
if ' cat ' = 'cat' then say "this will print because whitespace is stripped"
|
||||
|
||||
/*To consider any whitespace in a comparison, */
|
||||
/*we need to use strict comparative operators.*/
|
||||
|
||||
if ' cat ' == 'cat' then say "this will not print because comparison is strict"
|
||||
23
Task/String-comparison/REXX/string-comparison-2.rexx
Normal file
23
Task/String-comparison/REXX/string-comparison-2.rexx
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
/* REXX ***************************************************************
|
||||
* 16.05.2013 Walter Pachl
|
||||
**********************************************************************/
|
||||
Call test 'A','<','a'
|
||||
Call test 'A','=',' a'
|
||||
Call test 'A','==',' a'
|
||||
Call test 'Walter','<',' Wolter'
|
||||
Exit
|
||||
|
||||
test: Procedure
|
||||
Parse Arg o1,op,o2
|
||||
Say q(o1) op q(o2) '->' clcompare(o1,op,o2)
|
||||
Return
|
||||
|
||||
clcompare: Procedure
|
||||
/* caseless comparison of the operands */
|
||||
Parse Arg opd1,op,opd2
|
||||
opd1u=translate(opd1)
|
||||
opd2u=translate(opd2)
|
||||
Interpret 'res=opd1u' op 'opd2u'
|
||||
Return res
|
||||
|
||||
q: Return '"'arg(1)'"'
|
||||
25
Task/String-comparison/Seed7/string-comparison-1.seed7
Normal file
25
Task/String-comparison/Seed7/string-comparison-1.seed7
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
$ include "seed7_05.s7i";
|
||||
|
||||
const proc: showComparisons (in string: a, in string: b) is func
|
||||
begin
|
||||
writeln("compare " <& literal(a) <& " with " <& literal(b) <&":");
|
||||
writeln("a = b returns: " <& a = b);
|
||||
writeln("a <> b returns: " <& a <> b);
|
||||
writeln("a < b returns: " <& a < b);
|
||||
writeln("a > b returns: " <& a > b);
|
||||
writeln("a <= b returns: " <& a <= b);
|
||||
writeln("a >= b returns: " <& a >= b);
|
||||
writeln("compare(a, b) returns: " <& compare(a, b));
|
||||
writeln("compare(lower(a), lower(b)) returns: " <& compare(a, b));
|
||||
end func;
|
||||
|
||||
const proc: main is func
|
||||
begin
|
||||
showComparisons("this", "that");
|
||||
showComparisons("that", "this");
|
||||
showComparisons("THAT", "That");
|
||||
showComparisons("this", "This");
|
||||
showComparisons("this", "this");
|
||||
showComparisons("the", "there");
|
||||
showComparisons("there", "the");
|
||||
end func;
|
||||
35
Task/String-comparison/Seed7/string-comparison-2.seed7
Normal file
35
Task/String-comparison/Seed7/string-comparison-2.seed7
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
include "scanstri.s7i";
|
||||
|
||||
const func integer: cmpNumeric (in var string: stri1, in var string: stri2) is func
|
||||
result
|
||||
var integer: signumValue is 0;
|
||||
local
|
||||
var string: part1 is "";
|
||||
var string: part2 is "";
|
||||
begin
|
||||
while signumValue = 0 and (stri1 <> "" or stri2 <> "") do
|
||||
part1 := getDigits(stri1);
|
||||
part2 := getDigits(stri2);
|
||||
if part1 <> "" and part2 <> "" then
|
||||
signumValue := compare(part1 lpad0 length(part2), part2 lpad0 length(part1));
|
||||
if signumValue = 0 then
|
||||
signumValue := compare(length(part1), length(part2));
|
||||
end if;
|
||||
elsif part1 <> "" then
|
||||
signumValue := compare(part1, stri2);
|
||||
elsif part2 <> "" then
|
||||
signumValue := compare(stri1, part2);
|
||||
end if;
|
||||
if signumValue = 0 then
|
||||
part1 := getNonDigits(stri1);
|
||||
part2 := getNonDigits(stri2);
|
||||
if part1 <> "" and part2 <> "" then
|
||||
signumValue := compare(part1, part2);
|
||||
elsif part1 <> "" then
|
||||
signumValue := compare(part1, stri2);
|
||||
elsif part2 <> "" then
|
||||
signumValue := compare(stri1, part2);
|
||||
end if;
|
||||
end if;
|
||||
end while;
|
||||
end func;
|
||||
18
Task/String-comparison/UNIX-Shell/string-comparison-1.sh
Normal file
18
Task/String-comparison/UNIX-Shell/string-comparison-1.sh
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#!/bin/sh
|
||||
|
||||
A=Bell
|
||||
B=Ball
|
||||
|
||||
# Traditional test command implementations test for equality and inequality
|
||||
# but do not have a lexical comparison facility
|
||||
if [ $A = $B ] ; then
|
||||
echo 'The strings are equal'
|
||||
fi
|
||||
if [ $A != $B ] ; then
|
||||
echo 'The strings are not equal'
|
||||
fi
|
||||
|
||||
# All variables in the shell are strings, so numeric content cause no lexical problems
|
||||
# 0 , -0 , 0.0 and 00 are all lexically different if tested using the above methods.
|
||||
|
||||
# However this may not be the case if other tools, such as awk are the slave instead of test.
|
||||
39
Task/String-comparison/UNIX-Shell/string-comparison-2.sh
Normal file
39
Task/String-comparison/UNIX-Shell/string-comparison-2.sh
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#!/bin/bash
|
||||
|
||||
isint() {
|
||||
printf "%d" $1 >/dev/null 2>&1
|
||||
}
|
||||
|
||||
compare() {
|
||||
local a=$1
|
||||
local b=$2
|
||||
|
||||
[[ $a = $b ]] && echo "'$a' and '$b' are lexically equal"
|
||||
[[ $a != $b ]] && echo "'$a' and '$b' are not lexically equal"
|
||||
|
||||
[[ $a > $b ]] && echo "'$a' is lexically after '$b'"
|
||||
[[ $a < $b ]] && echo "'$a' is lexically before '$b'"
|
||||
|
||||
shopt -s nocasematch # Turn on case insensitivity
|
||||
|
||||
[[ $a = $b ]] && echo "'$a' and '$b' are equal with case insensitivity"
|
||||
|
||||
shopt -u nocasematch # Turn off case insensitivity
|
||||
|
||||
# If args are numeric, perform some numeric comparisions
|
||||
if isint $a && isint $b
|
||||
then
|
||||
[[ $a -eq $b ]] && echo "$a is numerically equal to $b"
|
||||
[[ $a -gt $b ]] && echo "$a is numerically greater than $b"
|
||||
[[ $a -lt $b ]] && echo "$a is numerically less than $b"
|
||||
fi
|
||||
|
||||
echo
|
||||
}
|
||||
|
||||
|
||||
compare foo foo
|
||||
compare foo bar
|
||||
compare FOO foo
|
||||
compare 24 123
|
||||
compare 50 20
|
||||
Loading…
Add table
Add a link
Reference in a new issue