all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
15
Task/String-comparison/0DESCRIPTION
Normal file
15
Task/String-comparison/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
{{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:
|
||||
|
||||
* 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
|
||||
* Comparing two strings to see if one is lexically ordered after than the other
|
||||
* How to achieve both case sensitive comparisons and case insensitive comparisons within the language
|
||||
* How the language handles comparison of numeric strings if these are not treated lexically
|
||||
* 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.
|
||||
|
||||
Here "generic/polymorphic" comparison means that the function or operator you're using doesn't always do string comparison, but bends the actual semantics of the comparison depending on the types one or both arguments; with such an operator, you achieve string comparison only if the arguments are sufficiently string-like in type or appearance. In contrast, a "coercive/allomorphic" comparison function or operator has fixed string-comparison semantics regardless of the argument type; instead of the operator bending, it's the arguments that are forced to bend instead and behave like strings if they can, and the operator simply fails if the arguments cannot be viewed somehow as strings. A language may have one or both of these kinds of operators; see the Perl 6 entry for an example of a language with both kinds of operators.
|
||||
|
||||
'''See also:'''<!-- Note that this part might go away, as it is handled by the “basic data operation” template -->
|
||||
* [[Integer comparison]]
|
||||
* [[Character matching]]
|
||||
2
Task/String-comparison/1META.yaml
Normal file
2
Task/String-comparison/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Basic Data Operations
|
||||
17
Task/String-comparison/AWK/string-comparison.awk
Normal file
17
Task/String-comparison/AWK/string-comparison.awk
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
BEGIN {
|
||||
a="BALL"
|
||||
b="BELL"
|
||||
|
||||
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 not lexically before than the second" }
|
||||
if (a <= b) { print "The first string is not lexically after than the second" }
|
||||
|
||||
# to make a case insensitive comparison convert both strings to the same lettercase:
|
||||
a="BALL"
|
||||
b="ball"
|
||||
if (tolower(a) == tolower(b)) { print "The first and second string are the same disregarding letter case" }
|
||||
|
||||
}
|
||||
9
Task/String-comparison/BASIC/string-comparison-1.basic
Normal file
9
Task/String-comparison/BASIC/string-comparison-1.basic
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
10 LET "A$="BELL"
|
||||
20 LET B$="BELT"
|
||||
30 IF A$ = B$ THEN PRINT "THE STRINGS ARE EQUAL": REM TEST FOR EQUALITY
|
||||
40 IF A$ <> B$ THEN PRINT "THE STRINGS ARE NOT EQUAL": REM TEST FOR INEQUALITY
|
||||
50 IF A$ > B$ THEN PRINT A$;" IS LEXICALLY HIGHER THAN ";B$: REM TEST FOR LEXICALLY HIGHER
|
||||
60 IF A$ < B$ THEN PRINT A$;" IS LEXICALLY LOWER THAN ";B$: REM TEST FOR LEXICALLY LOWER
|
||||
70 IF A$ <= B$ THEN PRINT A$;" IS NOT LEXICALLY HIGHER THAN ";B$
|
||||
80 IF A$ >= B$ THEN PRINT A$;" IS NOT LEXICALLY LOWER THAN ";B$
|
||||
90 END
|
||||
3
Task/String-comparison/BASIC/string-comparison-2.basic
Normal file
3
Task/String-comparison/BASIC/string-comparison-2.basic
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
10 LET A$="BELT"
|
||||
20 LET B$="belt"
|
||||
30 IF UPPER$(A$)=UPPER$(B$) THEN PRINT "Disregarding lettercase, the strings are the same."
|
||||
8
Task/String-comparison/Burlesque/string-comparison.blq
Normal file
8
Task/String-comparison/Burlesque/string-comparison.blq
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
blsq ) "abc""abc"==
|
||||
1
|
||||
blsq ) "abc""abc"!=
|
||||
0
|
||||
blsq ) "abc""Abc"cm
|
||||
1
|
||||
blsq ) "ABC""Abc"cm
|
||||
-1
|
||||
23
Task/String-comparison/D/string-comparison.d
Normal file
23
Task/String-comparison/D/string-comparison.d
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import std.stdio, std.string, std.algorithm;
|
||||
|
||||
void main() {
|
||||
auto s = "abcd";
|
||||
|
||||
/* Comparing two strings for exact equality */
|
||||
assert (s == "abcd"); // same object
|
||||
|
||||
/* Comparing two strings for inequality */
|
||||
assert(s != "ABCD"); // different objects
|
||||
|
||||
/* Comparing the lexical order of two strings;
|
||||
-1 means smaller, 0 means equal, 1 means larger */
|
||||
|
||||
assert(s.icmp("Bcde") == -1); // case insensitive
|
||||
assert(s.cmp("Bcde") == 1); // case sensitive
|
||||
|
||||
assert(s.icmp("Aabc") == 1); // case insensitive
|
||||
assert(s.cmp("Aabc") == 1); // case sensitive
|
||||
|
||||
assert(s.icmp("ABCD") == 0); // case insensitive
|
||||
assert(s.cmp("ABCD") == 1); // case sensitive
|
||||
}
|
||||
6
Task/String-comparison/J/string-comparison-1.j
Normal file
6
Task/String-comparison/J/string-comparison-1.j
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
eq=: -: NB. equal
|
||||
ne=: -.@-: NB. not equal
|
||||
gt=: {.@/:@,&boxopen *. ne NB. lexically greater than
|
||||
lt=: -.@{.@/:@,&boxopen *. ne NB. lexically less than
|
||||
ge=: {.@/:@,&boxopen +. eq NB. lexically greater than or equal to
|
||||
le=: -.@{.@/:@,&boxopen NB. lexically less than or equal to
|
||||
6
Task/String-comparison/J/string-comparison-2.j
Normal file
6
Task/String-comparison/J/string-comparison-2.j
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
'ball' (eq , ne , gt , lt , ge , le) 'bell'
|
||||
0 1 0 1 0 1
|
||||
'ball' (eq , ne , gt , lt , ge , le) 'ball'
|
||||
1 0 0 0 1 1
|
||||
'YUP' (eq , ne , gt , lt , ge , le) 'YEP'
|
||||
0 1 1 0 1 0
|
||||
23
Task/String-comparison/NetRexx/string-comparison.netrexx
Normal file
23
Task/String-comparison/NetRexx/string-comparison.netrexx
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
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 all whitespace in a comparison
|
||||
we need to use strict comparative operators */
|
||||
|
||||
if ' cat ' == 'cat' then
|
||||
say "this will not print because comparison is strict"
|
||||
36
Task/String-comparison/Perl-6/string-comparison.pl6
Normal file
36
Task/String-comparison/Perl-6/string-comparison.pl6
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
sub compare($a,$b) {
|
||||
my $A = "{$a.WHAT.^name} '$a'";
|
||||
my $B = "{$b.WHAT.^name} '$b'";
|
||||
|
||||
if $a eq $b { say "$A and $B are lexically equal" }
|
||||
if $a ne $b { say "$A and $B are not lexically equal" }
|
||||
|
||||
if $a gt $b { say "$A is lexically after $B" }
|
||||
if $a lt $b { say "$A is lexically before than $B" }
|
||||
|
||||
if $a ge $b { say "$A is not lexically before $B" }
|
||||
if $a le $b { say "$A is not lexically after $B" }
|
||||
|
||||
if $a === $b { say "$A and $B are identical objects" }
|
||||
if $a !=== $b { say "$A and $B are not identical objects" }
|
||||
|
||||
if $a eqv $b { say "$A and $B are generically equal" }
|
||||
if $a !eqv $b { say "$A and $B are not generically equal" }
|
||||
|
||||
if $a before $b { say "$A is generically after $B" }
|
||||
if $a after $b { say "$A is generically before $B" }
|
||||
|
||||
if $a !after $b { say "$A is not generically before $B" }
|
||||
if $a !before $b { say "$A is not generically after $B" }
|
||||
|
||||
say "The lexical relationship of $A and $B is { $a leg $b }" if $a ~~ Stringy;
|
||||
say "The generic relationship of $A and $B is { $a cmp $b }";
|
||||
say "The numeric relationship of $A and $B is { $a <=> $b }" if $a ~~ Numeric;
|
||||
say '';
|
||||
}
|
||||
|
||||
compare 'YUP', 'YUP';
|
||||
compare 'BALL', 'BELL';
|
||||
compare 24, 123;
|
||||
compare 5.1, 5;
|
||||
compare 5.1e0, 5 + 1/10;
|
||||
17
Task/String-comparison/Python/string-comparison.py
Normal file
17
Task/String-comparison/Python/string-comparison.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
def compare(a, b):
|
||||
print("\n%r is of type %r and %r is of type %r"
|
||||
% (a, type(a), b, type(b)))
|
||||
if a < b: print('%r is strictly less than %r' % (a, b))
|
||||
if a <= b: print('%r is less than or equal to %r' % (a, b))
|
||||
if a > b: print('%r is strictly greater than %r' % (a, b))
|
||||
if a >= b: print('%r is greater than or equal to %r' % (a, b))
|
||||
if a == b: print('%r is equal to %r' % (a, b))
|
||||
if a != b: print('%r is not equal to %r' % (a, b))
|
||||
if a is b: print('%r has object identity with %r' % (a, b))
|
||||
if a is not b: print('%r has negated object identity with %r' % (a, b))
|
||||
|
||||
compare('YUP', 'YUP')
|
||||
compare('BALL', 'BELL')
|
||||
compare('24', '123')
|
||||
compare(24, 123)
|
||||
compare(5.0, 5)
|
||||
23
Task/String-comparison/REXX/string-comparison.rexx
Normal file
23
Task/String-comparison/REXX/string-comparison.rexx
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
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 all whitespace in a comparison
|
||||
we need to use strict comparative operators */
|
||||
|
||||
if ' cat ' == 'cat' then
|
||||
say "this will not print because comparison is strict"
|
||||
16
Task/String-comparison/Racket/string-comparison.rkt
Normal file
16
Task/String-comparison/Racket/string-comparison.rkt
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#lang racket
|
||||
|
||||
;; Comparing two strings for exact equality
|
||||
(string=? "foo" "foo")
|
||||
|
||||
;; Comparing two strings for inequality
|
||||
(not (string=? "foo" "bar"))
|
||||
|
||||
;; Comparing two strings to see if one is lexically ordered before than the other
|
||||
(string<? "abc" "def")
|
||||
|
||||
;; Comparing two strings to see if one is lexically ordered after than the other
|
||||
(string>? "def" "abc")
|
||||
|
||||
;; How to achieve both case sensitive comparisons and case insensitive comparisons within the language
|
||||
(string-ci=? "foo" "FOO")
|
||||
9
Task/String-comparison/Run-BASIC/string-comparison.run
Normal file
9
Task/String-comparison/Run-BASIC/string-comparison.run
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
a$ = "dog"
|
||||
b$ = "cat"
|
||||
if a$ = b$ then print "the strings are equal" ' test for equalitY
|
||||
if a$ <> b$ then print "the strings are not equal" ' test for inequalitY
|
||||
if a$ > b$ then print a$;" is lexicallY higher than ";b$ ' test for lexicallY higher
|
||||
if a$ < b$ then print a$;" is lexicallY lower than ";b$ ' test for lexicallY lower
|
||||
if a$ <= b$ then print a$;" is not lexicallY higher than ";b$
|
||||
if a$ >= b$ then print a$;" is not lexicallY lower than ";b$
|
||||
end
|
||||
6
Task/String-comparison/Tcl/string-comparison-1.tcl
Normal file
6
Task/String-comparison/Tcl/string-comparison-1.tcl
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
if {$a eq $b} {
|
||||
puts "the strings are equal"
|
||||
}
|
||||
if {$a ne $b} {
|
||||
puts "the strings are not equal"
|
||||
}
|
||||
6
Task/String-comparison/Tcl/string-comparison-2.tcl
Normal file
6
Task/String-comparison/Tcl/string-comparison-2.tcl
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
if {[string compare $a $b] < 0} {
|
||||
puts "first string lower than second"
|
||||
}
|
||||
if {[string compare $a $b] > 0} {
|
||||
puts "first string higher than second"
|
||||
}
|
||||
3
Task/String-comparison/Tcl/string-comparison-3.tcl
Normal file
3
Task/String-comparison/Tcl/string-comparison-3.tcl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
if {[string equal -length 3 $x "abc123"]} {
|
||||
puts "first three characters are equal"
|
||||
}
|
||||
18
Task/String-comparison/UNIX-Shell/string-comparison.sh
Normal file
18
Task/String-comparison/UNIX-Shell/string-comparison.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.
|
||||
Loading…
Add table
Add a link
Reference in a new issue