Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,5 +1,7 @@
|
|||
{{basic data operation}}[[Category:Basic language learning]]The task is to demonstrate how to compare two strings from within the language and how to achieve a lexical comparison. The task should demonstrate:
|
||||
{{basic data operation}} [[Category:Basic language learning]] [[Category:Simple]]
|
||||
The task is to demonstrate how to compare two strings from within the language and how to achieve a lexical comparison.
|
||||
|
||||
The task should demonstrate:
|
||||
* Comparing two strings for exact equality
|
||||
* Comparing two strings for inequality (i.e., the inverse of exact equality)
|
||||
* Comparing two strings to see if one is lexically ordered before than the other
|
||||
|
|
@ -12,4 +14,5 @@ Here "generic/polymorphic" comparison means that the function or operator you're
|
|||
|
||||
'''See also:'''<!-- Note that this part might go away, as it is handled by the “basic data operation” template -->
|
||||
* [[Integer comparison]]
|
||||
* [[Character matching]]
|
||||
* [[String matching]]
|
||||
* [[Compare a list of strings]]
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ BEGIN {
|
|||
|
||||
if (a == b) { print "The strings are equal" }
|
||||
if (a != b) { print "The strings are not equal" }
|
||||
if (a > b) { print "The first string is lexically after than the second" }
|
||||
if (a < b) { print "The first string is lexically before than the second" }
|
||||
if (a > b) { print "The first string is lexically after than the second" }
|
||||
if (a < b) { print "The first string is lexically before than the second" }
|
||||
if (a >= b) { print "The first string is not lexically before than the second" }
|
||||
if (a <= b) { print "The first string is not lexically after than the second" }
|
||||
|
||||
|
|
|
|||
|
|
@ -1,13 +1,15 @@
|
|||
with Ada.Text_IO, Ada.Characters.Handling;
|
||||
with Ada.Text_IO, Ada.Strings.Equal_Case_Insensitive;
|
||||
|
||||
procedure String_Compare is
|
||||
|
||||
procedure Print_Comparison (A, B: String) is
|
||||
use Ada.Text_IO, Ada.Characters.Handling;
|
||||
use Ada.Text_IO;
|
||||
Function eq(Left, Right : String) return Boolean
|
||||
renames Ada.Strings.Equal_Case_Insensitive;
|
||||
begin
|
||||
Put_Line
|
||||
( """" & A & """ and """ & B & """: " &
|
||||
(if A = B then "equal, " elsif To_Lower(A) = To_Lower(B)
|
||||
(if A = B then "equal, " elsif eq(A,B)
|
||||
then "case-insensitive-equal, " else "not equal at all, ") &
|
||||
(if A /= B then "/=, " else "") &
|
||||
(if A < B then "before, " else "") &
|
||||
|
|
|
|||
16
Task/String-comparison/Aime/string-comparison.aime
Normal file
16
Task/String-comparison/Aime/string-comparison.aime
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
text s, t;
|
||||
|
||||
s = "occidental";
|
||||
t = "oriental";
|
||||
|
||||
# operator case sensitive comparison
|
||||
o_form("~ vs ~ (==, !=, <, <=, >=, >): ~ ~ ~ ~ ~ ~\n", s, t, s == t, s != t, s < t, s <= t, s >= t, s > t);
|
||||
|
||||
s = "Oriental";
|
||||
t = "oriental";
|
||||
|
||||
# case sensitive comparison
|
||||
o_form("~ vs ~ (==, !=, <, >): ~ ~ ~ ~\n", s, t, !compare(s, t), compare(s, t), compare(s, t) < 0, 0 < compare(s, t));
|
||||
|
||||
# case insensitive comparison
|
||||
o_form("~ vs ~ (==, !=, <, >): ~ ~ ~ ~\n", s, t, !icompare(s, t), icompare(s, t), icompare(s, t) < 0, 0 < icompare(s, t));
|
||||
18
Task/String-comparison/Common-Lisp/string-comparison-1.lisp
Normal file
18
Task/String-comparison/Common-Lisp/string-comparison-1.lisp
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
>(string= "foo" "foo")
|
||||
T
|
||||
> (string= "foo" "FOO")
|
||||
NIL
|
||||
> (string/= "foo" "bar")
|
||||
0
|
||||
> (string/= "bar" "baz")
|
||||
2
|
||||
> (string/= "foo" "foo")
|
||||
NIL
|
||||
> (string> "foo" "Foo")
|
||||
0
|
||||
> (string< "foo" "Foo")
|
||||
NIL
|
||||
> (string>= "FOo" "Foo")
|
||||
NIL
|
||||
> (string<= "FOo" "Foo")
|
||||
1
|
||||
12
Task/String-comparison/Common-Lisp/string-comparison-2.lisp
Normal file
12
Task/String-comparison/Common-Lisp/string-comparison-2.lisp
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
> (string-equal "foo" "FOo")
|
||||
T
|
||||
> (string-not-equal "foo" "FOO")
|
||||
NIL
|
||||
> (string-greaterp "foo" "Foo")
|
||||
NIL
|
||||
> (string-lessp "BAR" "foo")
|
||||
0
|
||||
> (string-not-greaterp "foo" "Foo")
|
||||
3
|
||||
> (string-not-lessp "baz" "bAr")
|
||||
2
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
> (string> "45" "12345")
|
||||
0
|
||||
> (string> "45" "9")
|
||||
NIL
|
||||
|
|
@ -44,7 +44,7 @@ simple case folding rules.`
|
|||
// case folding functions interpret UTF-8, you might then ask about
|
||||
// other equality and inequality tests that interpret UTF-8.
|
||||
// Functions for this are not in the Go standard library but are in
|
||||
// the Go "sub repository" at code.google.com/p/go. There is support
|
||||
// the Go "sub repository" at golang.org/x/text. There is support
|
||||
// for Unicode normalization, collation tables, and locale sensitive
|
||||
// comparisons.
|
||||
}
|
||||
|
|
|
|||
15
Task/String-comparison/Haskell/string-comparison.hs
Normal file
15
Task/String-comparison/Haskell/string-comparison.hs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
> "abc" == "abc"
|
||||
True
|
||||
> "abc" /= "abc"
|
||||
False
|
||||
> "abc" <= "abcd"
|
||||
True
|
||||
> "abc" <= "abC"
|
||||
False
|
||||
> "HELLOWORLD" == "HelloWorld"
|
||||
False
|
||||
> :m +Data.Char
|
||||
> map toLower "ABC"
|
||||
"abc"
|
||||
> map toLower "HELLOWORLD" == map toLower "HelloWorld"
|
||||
True
|
||||
38
Task/String-comparison/Java/string-comparison.java
Normal file
38
Task/String-comparison/Java/string-comparison.java
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
public class Compare
|
||||
{
|
||||
public static void main (String[] args)
|
||||
{
|
||||
compare("Hello", "Hello");
|
||||
compare("5", "5.0");
|
||||
compare("java", "Java");
|
||||
compare("ĴÃVÁ", "ĴÃVÁ");
|
||||
compare("ĴÃVÁ", "ĵãvá");
|
||||
}
|
||||
public static void compare (String A, String B)
|
||||
{
|
||||
if (A.equals(B))
|
||||
System.out.printf("'%s' and '%s' are lexically equal.", A, B);
|
||||
else
|
||||
System.out.printf("'%s' and '%s' are not lexically equal.", A, B);
|
||||
System.out.println();
|
||||
|
||||
if (A.equalsIgnoreCase(B))
|
||||
System.out.printf("'%s' and '%s' are case-insensitive lexically equal.", A, B);
|
||||
else
|
||||
System.out.printf("'%s' and '%s' are not case-insensitive lexically equal.", A, B);
|
||||
System.out.println();
|
||||
|
||||
if (A.compareTo(B) < 0)
|
||||
System.out.printf("'%s' is lexically before '%s'.\n", A, B);
|
||||
else if (A.compareTo(B) > 0)
|
||||
System.out.printf("'%s' is lexically after '%s'.\n", A, B);
|
||||
|
||||
if (A.compareTo(B) >= 0)
|
||||
System.out.printf("'%s' is not lexically before '%s'.\n", A, B);
|
||||
if (A.compareTo(B) <= 0)
|
||||
System.out.printf("'%s' is not lexically after '%s'.\n", A, B);
|
||||
|
||||
System.out.printf("The lexical relationship is: %d\n", A.compareTo(B));
|
||||
System.out.printf("The case-insensitive lexical relationship is: %d\n\n", A.compareToIgnoreCase(B));
|
||||
}
|
||||
}
|
||||
19
Task/String-comparison/Mathematica/string-comparison.math
Normal file
19
Task/String-comparison/Mathematica/string-comparison.math
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
compare[x_, y_] := Module[{},
|
||||
If[x == y,
|
||||
Print["Comparing for equality (case sensitive): " <> x <> " and " <> y <> " ARE equal"],
|
||||
Print["Comparing for equality (case sensitive): " <> x <> " and " <> y <> " are NOT equal" ]] ;
|
||||
If[x != y,
|
||||
Print["Comparing for inequality (case sensitive): " <> x <> " and " <> y <> " are NOT equal"],
|
||||
Print["Comparing for inequality (case sensitive): " <> x <> " and " <> y <> " ARE equal" ]] ;
|
||||
Switch[Order[x, y],
|
||||
1, Print["Comparing for order (case sensitive): " <> x <> " comes before " <> y],
|
||||
-1, Print["Comparing for order (case sensitive): " <> x <> " comes after " <> y],
|
||||
0, Print["Comparing for order (case sensitive): " <> x <> " comes in the same spot as " <> y]];
|
||||
If[ToLowerCase[x] == ToLowerCase[y],
|
||||
Print["Comparing for equality (case insensitive): " <> x <> " and " <> y <> " ARE equal"],
|
||||
Print["Comparing for equality (case insensitive): " <> x <> " and " <> y <> " are NOT equal" ]] ;
|
||||
Print[];
|
||||
]
|
||||
compare["Hello", "Hello"]
|
||||
compare["3.1", "3.14159"]
|
||||
compare["mathematica", "Mathematica"]
|
||||
18
Task/String-comparison/PicoLisp/string-comparison.l
Normal file
18
Task/String-comparison/PicoLisp/string-comparison.l
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
(setq
|
||||
str= =
|
||||
str< <
|
||||
str> > )
|
||||
|
||||
(println
|
||||
(str= (lowc "Foo") (lowc "foo") (lowc "fOO"))
|
||||
(str= "f" "foo")
|
||||
(str= "foo" "foo" "foo")
|
||||
(str= "" "") )
|
||||
|
||||
(println
|
||||
(str< "abc" "def")
|
||||
(str> "abc" "def")
|
||||
(str< "" "")
|
||||
(str< "12" "45") )
|
||||
|
||||
(bye)
|
||||
19
Task/String-comparison/R/string-comparison-1.r
Normal file
19
Task/String-comparison/R/string-comparison-1.r
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
compare <- function(a, b)
|
||||
{
|
||||
cat(paste(a, "is of type", class(a), "and", b, "is of type", class(b), "\n"))
|
||||
|
||||
if (a < b) cat(paste(a, "is strictly less than", b, "\n"))
|
||||
if (a <= b) cat(paste(a, "is less than or equal to", b, "\n"))
|
||||
if (a > b) cat(paste(a, "is strictly greater than", b, "\n"))
|
||||
if (a >= b) cat(paste(a, "is greater than or equal to", b, "\n"))
|
||||
if (a == b) cat(paste(a, "is equal to", b, "\n"))
|
||||
if (a != b) cat(paste(a, "is not equal to", b, "\n"))
|
||||
|
||||
invisible()
|
||||
}
|
||||
|
||||
compare('YUP', 'YUP')
|
||||
compare('BALL', 'BELL')
|
||||
compare('24', '123')
|
||||
compare(24, 123)
|
||||
compare(5.0, 5)
|
||||
20
Task/String-comparison/R/string-comparison-2.r
Normal file
20
Task/String-comparison/R/string-comparison-2.r
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
compare <- function(a, b)
|
||||
{
|
||||
cat(paste(a, "is of type", class(a), "and", b, "is of type", class(b), "\n"))
|
||||
|
||||
printer <- function(a, b, msg) cat(paste(a, msg, b, "\n"))
|
||||
|
||||
op <- c(`<`, `<=`, `>`, `>=`, `==`, `!=`)
|
||||
msgs <- c(
|
||||
"is strictly less than",
|
||||
"is less than or equal to",
|
||||
"is strictly greater than",
|
||||
"is greater than or equal to",
|
||||
"is equal to",
|
||||
"is not equal to"
|
||||
)
|
||||
|
||||
sapply(1:length(msgs), function(i) if(op[[i]](a, b)) printer(a, b, msgs[i]))
|
||||
|
||||
invisible()
|
||||
}
|
||||
24
Task/String-comparison/Scala/string-comparison.scala
Normal file
24
Task/String-comparison/Scala/string-comparison.scala
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
object Compare extends App {
|
||||
def compare(a: String, b: String) {
|
||||
if (a == b) println(s"'$a' and '$b' are lexically equal.")
|
||||
else println(s"'$a' and '$b' are not lexically equal.")
|
||||
|
||||
if (a.equalsIgnoreCase(b)) println(s"'$a' and '$b' are case-insensitive lexically equal.")
|
||||
else println(s"'$a' and '$b' are not case-insensitive lexically equal.")
|
||||
|
||||
if (a.compareTo(b) < 0) println(s"'$a' is lexically before '$b'.")
|
||||
else if (a.compareTo(b) > 0) println(s"'$a' is lexically after '$b'.")
|
||||
|
||||
if (a.compareTo(b) >= 0) println(s"'$a' is not lexically before '$b'.")
|
||||
if (a.compareTo(b) <= 0) println(s"'$a' is not lexically after '$b'.")
|
||||
|
||||
println(s"The lexical relationship is: ${a.compareTo(b)}")
|
||||
println(s"The case-insensitive lexical relationship is: ${a.compareToIgnoreCase(b)}\n")
|
||||
}
|
||||
|
||||
compare("Hello", "Hello")
|
||||
compare("5", "5.0")
|
||||
compare("java", "Java")
|
||||
compare("ĴÃVÁ", "ĴÃVÁ")
|
||||
compare("ĴÃVÁ", "ĵãvá")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue