Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,4 +1,7 @@
Write function that orders two lists or arrays filled with numbers.
Write a function that orders two lists or arrays filled with numbers.
The function should accept two lists as arguments and return <code>true</code> if the first list should be ordered before the second, and <code>false</code> otherwise.
The order is determined by [[wp:Lexicographical order#Ordering of sequences of various lengths|lexicographic order]]: Comparing the first element of each list. If the first elements are equal, then the second elements should be compared, and so on, until one of the list has no more elements. If the first list runs out of elements the result is <code>true</code>. if the second list or both run out of elements the result is <code>false</code>.
The order is determined by [[wp:Lexicographical order#Ordering of sequences of various lengths|lexicographic order]]: Comparing the first element of each list.
If the first elements are equal, then the second elements should be compared, and so on, until one of the list has no more elements.
If the first list runs out of elements the result is <code>true</code>.
If the second list or both run out of elements the result is <code>false</code>.

View file

@ -0,0 +1,24 @@
# syntax: GAWK -f ORDER_TWO_NUMERICAL_LISTS.AWK
BEGIN {
split("1,2,1,5,2",list1,",")
split("1,2,1,5,2,2",list2,",")
split("1,2,3,4,5",list3,",")
split("1,2,3,4,5",list4,",")
x = compare_array(list1,list2) ? "<" : ">=" ; printf("list1%slist2\n",x)
x = compare_array(list2,list3) ? "<" : ">=" ; printf("list2%slist3\n",x)
x = compare_array(list3,list4) ? "<" : ">=" ; printf("list3%slist4\n",x)
exit(0)
}
function compare_array(arr1,arr2, ans,i) {
ans = 0
for (i=1; i<=length(arr1); i++) {
if (arr1[i] != arr2[i]) {
ans = 1
break
}
}
if (length(arr1) != length(arr2)) {
ans = 1
}
return(ans)
}

View file

@ -0,0 +1,11 @@
function arraycompare(a, b)
for i = 1, #a do
if b[i] == nil then
return true
end
if a[i] ~= b[i] then
return a[i] < b[1]
end
end
return true
end

View file

@ -0,0 +1,20 @@
function randomarray()
local t = {}
for i = 1, math.random(1, 10) do
t[i] = math.random(1, 10)
end
return t
end
math.randomseed(os.time())
for i = 1, 10 do
local a = randomarray()
local b = randomarray()
print(
string.format("{%s} %s {%s}",
table.concat(a, ', '),
arraycompare(a, b) and "<=" or ">",
table.concat(b, ', ')))
end

View file

@ -0,0 +1,22 @@
lists: procedure options (main); /* 8 June 2014 */
declare a(10) fixed initial (1, 2, 3, 4, 5, 8, 9, 10, 16, 17),
b(15) fixed initial (5, 6, 7, 8, 9, 10, 11, 12, 15, 16, 17, 18, 20, 22, 23);
put skip list (compare(a, b));
put skip list (compare(b, a));
put skip list (compare(a, a));
compare: procedure (a, b) returns (bit (1));
declare (a, b)(*) fixed;
declare (i, m, n) fixed binary;
m = hbound(a,1); n = hbound(b,1);
do i = 1 to min(m, n);
return (a(i) < b(i));
end;
return (m < n);
end compare;
end lists;

View file

@ -1,19 +1,23 @@
/*REXX pgm finds if list1<list2 (both contain nums), returns true|false.*/
@.=
@.1=1 2 1 5 2
@.2=1 2 1 5 2 2
@.3=1 2 3 4 5
@.4=1 2 3 4 5
do i=2 while @.i\==''; m=i-1
what=' 'word("< ≥", 1+(FNorder(@.m, @.i)=='false'))" "
say right('['@.m"]", 35) what '['@.i"]"; say
end /*i*/
/*REXX pgm determines if a list < previous list, & returns true | false*/
@. =
@.1 = 1 2 1 5 2
@.2 = 1 2 1 5 2 2
@.3 = 1 2 3 4 5
@.4 = 1 2 3 4 5 /* [↓] compare list to previous.*/
do j=2 while @.j\==''; p=j-1 /*P is the previous.*/
answer=FNorder(@.p, @.j) /*obtain the answer.*/
if answer=='true' then is= ' < ' /*convert from true */
else is= ' ' /*convert from false*/
say right('['@.p"]", 40) is '['@.j"]"; say
end /*i*/ /* [↑] display (+ a blank line)*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────FNORDER subroutine──────────────────*/
FNorder: procedure; parse arg x,y; wx=words(x); wy=words(y)
FNorder: procedure; parse arg x,y; wx=words(x); wy=words(y)
do j=1 for min(wx,wy)
if word(x,j)<word(y,j) then return 'true'
end /*j*/
do k=1 for min(wx,wy)
a=word(x,k); b=word(y,k)
if a<b then return 'true'
else if a>b then return 'false'
end /*k*/
if wx<wy then return 'true'
return 'false'

View file

@ -0,0 +1,5 @@
def lessThan1(a: List[Int], b: List[Int]): Boolean =
if (b.isEmpty) false
else if (a.isEmpty) true
else if (a.head != b.head) a.head < b.head
else lessThan1(a.tail, b.tail)

View file

@ -0,0 +1,6 @@
def lessThan2(a: List[Int], b: List[Int]): Boolean = (a, b) match {
case (_, Nil) => false
case (Nil, _) => true
case (a :: _, b :: _) if a != b => a < b
case _ => lessThan2(a.tail, b.tail)
}

View file

@ -0,0 +1,5 @@
def lessThan3(a: List[Int], b: List[Int]): Boolean =
a.zipAll(b, Integer.MIN_VALUE, Integer.MIN_VALUE)
.find{case (a, b) => a != b}
.map{case (a, b) => a < b}
.getOrElse(false)

View file

@ -0,0 +1,14 @@
val tests = List(
(List(1, 2, 3), List(1, 2, 3)) -> false,
(List(3, 2, 1), List(3, 2, 1)) -> false,
(List(1, 2, 3), List(3, 2, 1)) -> true,
(List(3, 2, 1), List(1, 2, 3)) -> false,
(List(1, 2), List(1, 2, 3)) -> true,
(List(1, 2, 3), List(1, 2)) -> false
)
tests.foreach{case test @ ((a, b), c) =>
assert(lessThan1(a, b) == c, test)
assert(lessThan2(a, b) == c, test)
assert(lessThan3(a, b) == c, test)
}