tasks a-s

This commit is contained in:
Ingy döt Net 2013-04-10 23:57:08 -07:00
parent 47bf37c096
commit b83f433714
12433 changed files with 156208 additions and 123 deletions

View file

@ -0,0 +1,4 @@
Write 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>.

View file

@ -0,0 +1,2 @@
---
note: Sorting

View file

@ -0,0 +1,22 @@
with Ada.Text_IO; use Ada.Text_IO;
procedure Order is
type IntArray is array (Positive range <>) of Integer;
List1 : IntArray := (1, 2, 3, 4, 5);
List2 : IntArray := (1, 2, 1, 5, 2, 2);
List3 : IntArray := (1, 2, 1, 5, 2);
List4 : IntArray := (1, 2, 1, 5, 2);
type Animal is (Rat, Cat, Elephant);
type AnimalArray is array (Positive range <>) of Animal;
List5 : AnimalArray := (Cat, Elephant, Rat, Cat);
List6 : AnimalArray := (Cat, Elephant, Rat);
List7 : AnimalArray := (Cat, Cat, Elephant);
begin
Put_Line (Boolean'Image (List1 > List2)); -- True
Put_Line (Boolean'Image (List2 > List3)); -- True
Put_Line (Boolean'Image (List3 > List4)); -- False, equal
Put_Line (Boolean'Image (List5 > List6)); -- True
Put_Line (Boolean'Image (List6 > List7)); -- True
end Order;

View file

@ -0,0 +1,7 @@
List1 := [1,2,1,3,2]
List2 := [1,2,0,4,4,0,0,0]
MsgBox % order(List1, List2)
order(L1, L2){
return L1.MaxIndex() < L2.MaxIndex()
}

View file

@ -0,0 +1,19 @@
DIM list1(4) : list1() = 1, 2, 1, 5, 2
DIM list2(5) : list2() = 1, 2, 1, 5, 2, 2
DIM list3(4) : list3() = 1, 2, 3, 4, 5
DIM list4(4) : list4() = 1, 2, 3, 4, 5
IF FNorder(list1(), list2()) PRINT "list1<list2" ELSE PRINT "list1>=list2"
IF FNorder(list2(), list3()) PRINT "list2<list3" ELSE PRINT "list2>=list3"
IF FNorder(list3(), list4()) PRINT "list3<list4" ELSE PRINT "list3>=list4"
END
DEF FNorder(list1(), list2())
LOCAL i%, l1%, l2%
l1% = DIM(list1(),1) : l2% = DIM(list2(),1)
WHILE list1(i%) = list2(i%) AND i% < l1% AND i% < l2%
i% += 1
ENDWHILE
IF list1(i%) < list2(i%) THEN = TRUE
IF list1(i%) > list2(i%) THEN = FALSE
= l1% < l2%

View file

@ -0,0 +1,24 @@
( 1 2 3 4 5:?List1
& 1 2 1 5 2 2:?List2
& 1 2 1 5 2:?List3
& 1 2 1 5 2:?List4
& Cat Elephant Rat Cat:?List5
& Cat Elephant Rat:?List6
& Cat Cat Elephant:?List7
& ( gt
= first second
. !arg:(?first,?second)
& out
$ ( (.!first)+(.!second)
: ((.!first)+(.!second)|2*(.!first))
& FALSE
| TRUE
)
)
& gt$(!List1,!List2)
& gt$(!List2,!List3)
& gt$(!List3,!List4)
& gt$(!List4,!List5)
& gt$(!List5,!List6)
& gt$(!List6,!List7)
);

View file

@ -0,0 +1,23 @@
#include <iostream>
#include <vector>
int main() {
std::vector<int> a;
a.push_back(1);
a.push_back(2);
a.push_back(1);
a.push_back(3);
a.push_back(2);
std::vector<int> b;
b.push_back(1);
b.push_back(2);
b.push_back(0);
b.push_back(4);
b.push_back(4);
b.push_back(0);
b.push_back(0);
b.push_back(0);
std::cout << std::boolalpha << (a < b) << std::endl; // prints "false"
return 0;
}

View file

@ -0,0 +1,11 @@
int list_cmp(int *a, int la, int *b, int lb)
{
int i, l = la;
if (l > lb) l = lb;
for (i = 0; i < l; i++) {
if (a[i] == b[i]) continue;
return (a[i] > b[i]) ? 1 : -1;
}
if (la == lb) return 0;
return la > lb ? 1 : -1;
}

View file

@ -0,0 +1 @@
#define list_less_or_eq(a,b,c,d) (list_cmp(a,b,c,d) != 1)

View file

@ -0,0 +1,6 @@
(defun list< (a b)
(cond ((not b) nil)
((not a) t)
((= (first a) (first b))
(list< (rest a) (rest b)))
(t (< (first a) (first b)))))

View file

@ -0,0 +1,3 @@
(defun list< (a b)
(let ((x (find-if-not #'zerop (mapcar #'- a b))))
(if x (minusp x) (< (length a) (length b)))))

View file

@ -0,0 +1,3 @@
void main() {
assert([1,2,1,3,2] >= [1,2,0,4,4,0,0,0]);
}

View file

@ -0,0 +1,6 @@
[] <. _ = true
_ <. [] = false
(x::xs) <. (y::ys) | x == y = xs <. ys
| else = x < y
[1,2,1,3,2] <. [1,2,0,4,4,0,0,0]

View file

@ -0,0 +1,73 @@
package main
import "fmt"
// If your numbers happen to be in the range of Unicode code points (0 to 0x10ffff), this function
// satisfies the task:
func lessRune(a, b []rune) bool {
return string(a) < string(b) // see also bytes.Compare
}
// Otherwise, the following function satisfies the task for all integer
// and floating point types, by changing the type definition appropriately.
type numericType int
func lessNT(a, b []numericType) bool {
l := len(a)
if len(b) < l {
l = len(b)
}
for i := 0; i < l; i++ {
if a[i] != b[i] {
return a[i] < b[i]
}
}
return l < len(b)
}
var testCases = [][][]numericType{
{{0}, {}},
{{}, {}},
{{}, {0}},
{{-1}, {0}},
{{0}, {0}},
{{0}, {-1}},
{{0}, {0, -1}},
{{0}, {0, 0}},
{{0}, {0, 1}},
{{0, -1}, {0}},
{{0, 0}, {0}},
{{0, 0}, {1}},
}
func main() {
// demonstrate the general function
for _, tc := range testCases {
fmt.Printf("order %6s before %6s : %t\n",
fmt.Sprintf("%v", tc[0]),
fmt.Sprintf("%v", tc[1]),
lessNT(tc[0], tc[1]))
}
fmt.Println()
// demonstrate that the byte specific function gives identical results
// by offsetting test data to a printable range of characters.
for _, tc := range testCases {
a := toByte(tc[0])
b := toByte(tc[1])
fmt.Printf("order %6q before %6q : %t\n",
string(a),
string(b),
lessByte(a, b))
}
}
func toByte(a []numericType) []byte {
b := make([]byte, len(a))
for i, n := range a {
b[i] = 'b' + byte(n)
}
return b
}

View file

@ -0,0 +1,10 @@
class CList extends ArrayList implements Comparable {
CList() { }
CList(Collection c) { super(c) }
int compareTo(Object that) {
assert that instanceof List
def n = [this.size(), that.size()].min()
def comp = [this[0..<n], that[0..<n]].transpose().find { it[0] != it[1] }
comp ? comp[0] <=> comp[1] : this.size() <=> that.size()
}
}

View file

@ -0,0 +1,9 @@
CList a, b; (a, b) = [[], []]; assert ! (a < b)
b = [1] as CList; assert (a < b)
a = [1] as CList; assert ! (a < b)
b = [2] as CList; assert (a < b)
a = [2, -1, 0] as CList; assert ! (a < b)
b = [2, -1] as CList; assert ! (a < b)
b = [2, -1, 0] as CList; assert ! (a < b)
b = [2, -1, 0, -17] as CList; assert (a < b)
a = [2, 8, 0] as CList; assert ! (a < b)

View file

@ -0,0 +1,2 @@
Prelude> [1,2,1,3,2] < [1,2,0,4,4,0,0,0]
False

View file

@ -0,0 +1,11 @@
procedure main()
write( if list_llt([1,2,1,3,2],[1,2,0,4,4,0,0,0]) then "true" else "false" )
end
procedure list_llt(L1,L2) #: returns L2 if L1 lexically lt L2 or fails
every i := 1 to min(*L1,*L2) do
if L1[i] << L2[i] then return L2
else if L1[i] >> L2[i] then fail
if *L1 < *L2 then return L2
end

View file

@ -0,0 +1 @@
before=: -.@(-: /:~)@,&<~

View file

@ -0,0 +1,49 @@
(,0) before ''
0
'' before ''
0
'' before ,0
1
(,_1) before ,0
1
(,0) before ,0
0
(,0) before ,_1
0
(,0) before 0 _1
1
(,0) before 0 0
1
(,0) before 0 1
1
0 _1 before ,0
0
0 0 before ,0
0
0 0 before ,1
1
(,'b') before ''
0
'' before ''
0
'' before ,'b'
1
(,'a') before ,'b'
1
(,'b') before ,'b'
0
(,'b') before ,'a'
0
(,'b') before 'ba'
1
(,'b') before 'bb'
1
(,'b') before 'bc'
1
'ba' before ,'b'
0
'bb' before ,'b'
0
'bb' before ,'c'
1

View file

@ -0,0 +1,34 @@
import java.util.Arrays;
import java.util.List;
public class ListOrder{
public static boolean ordered(double[] first, double[] second){
if(first.length == 0) return true;
if(second.length == 0) return false;
if(first[0] == second[0])
return ordered(Arrays.copyOfRange(first, 1, first.length),
Arrays.copyOfRange(second, 1, second.length));
return first[0] < second[0];
}
public static <T extends Comparable<? super T>> boolean ordered(List<T> first, List<T> second){
int i = 0;
for(; i < first.size() && i < second.size();i++){
int cmp = first.get(i).compareTo(second.get(i));
if(cmp == 0) continue;
if(cmp < 0) return true;
return false;
}
return i == first.size();
}
public static boolean ordered2(double[] first, double[] second){
int i = 0;
for(; i < first.length && i < second.length;i++){
if(first[i] == second[i]) continue;
if(first[i] < second[i]) return true;
return false;
}
return i == first.length;
}
}

View file

@ -0,0 +1,4 @@
DEFINE order ==
[equal] [false]
[[[[size] dip size <=] [[<=] mapr2 true [and] fold]] [i] map i and]
ifte.

View file

@ -0,0 +1,8 @@
print [1 2] = [1 2]
print [1 2] = [1 2 3]
print [1 3] = [1 2]
print [1 2 3] = [1 2]
make "list1 [1 2 3 4 5 6]
make "list2 [1 2 3 4 5 7]
print :list1 = :list2

View file

@ -0,0 +1,5 @@
true
false
false
false
false

View file

@ -0,0 +1,8 @@
order[List1_, List2_] := With[{
L1 = List1[[1 ;; Min @@ Length /@ {List1, List2}]],
L2 = List2[[1 ;; Min @@ Length /@ {List1, List2}]]
},
If [Thread[Order[L1, L2]] == 0,
Length[List1] < Length[List2],
Thread[Order[L1, L2]] == 1
]]

View file

@ -0,0 +1,13 @@
"<<"(a,b):=block([n:min(length(a),length(b))],
catch(for i thru n do (if a[i]#b[i] then throw(is(a[i]<b[i]))),
throw(is(length(a)<length(b)))))$
infix("<<")$
[1,2,3] << [1,2,4];
true
[1,2,3] << [1,2];
false
[1,2] << [1,2];
false

View file

@ -0,0 +1,3 @@
:- pred lt(list(int)::in, list(int)::in) is semidet.
lt([], [_|_]).
lt([H1|T1], [H2|T2]) :- H1 =< H2, T1 `lt` T2.

View file

@ -0,0 +1,3 @@
:- pred lt(list(T)::in, list(T)::in) is semidet <= comparable(T).
lt([], [_|_]).
lt([H1|T1], [H2|T2]) :- H1 =< H2, T1 `lt` T2.

View file

@ -0,0 +1,27 @@
:- module comparable.
:- interface.
:- import_module int, float, integer, list.
:- typeclass comparable(T) where [
pred '<'(T::in, T::in) is semidet,
pred '=<'(T::in, T::in) is semidet
].
:- instance comparable(int).
:- instance comparable(float).
:- instance comparable(integer).
:- instance comparable(list(T)) <= comparable(T).
:- implementation.
:- instance comparable(int) where [
pred('<'/2) is int.(<),
pred('=<'/2) is int.(=<)
].
% likewise for float and integer...
:- instance comparable(list(T)) <= comparable(T) where [
pred('<'/2) is lt, % the 'lt' above.
pred('=<'/2) is lte % 'lt' with: lte([], []).
].
% pred lt
% pred lte

View file

@ -0,0 +1,6 @@
:- pred test(list(T), list(T), io, io) <= comparable(T).
:- mode test(in, in, di, uo) is det.
test(A, B) -->
io.write(A), io.write_string(" < "), io.write(B),
io.write_string(" : "), io.write_string(S), io.nl,
{ A < B -> S = "yes" ; S = "no" }.

View file

@ -0,0 +1,2 @@
# [1;2;1;3;2] < [1;2;0;4;4;0;0;0];;
- : bool = false

View file

@ -0,0 +1,8 @@
let rec ordered_lists = function
| x1::tl1, x2::tl2 ->
(match compare x1 x2 with
| 0 -> ordered_lists (tl1, tl2)
| 1 -> false
| _ -> true)
| [], _ -> true
| _ -> false

View file

@ -0,0 +1,20 @@
(* copy-paste the code of ordered_lists here *)
let make_num_list p n =
let rec aux acc =
if Random.int p = 0 then acc
else aux (Random.int n :: acc)
in
aux []
let print_num_list lst =
List.iter (Printf.printf " %d") lst;
print_newline()
let () =
Random.self_init();
let lst1 = make_num_list 8 5 in
let lst2 = make_num_list 8 5 in
print_num_list lst1;
print_num_list lst2;
Printf.printf "ordered: %B\n" (ordered_lists (lst1, lst2))

View file

@ -0,0 +1 @@
val ordered_lists : 'a list * 'a list -> bool

View file

@ -0,0 +1 @@
lex(u,v)<1

View file

@ -0,0 +1,17 @@
my @a = <1 2 4>;
my @b = <1 2 4>;
say @a," before ",@b," = ", @a before @b;
@a = <1 2 4>;
@b = <1 2>;
say @a," before ",@b," = ", @a before @b;
@a = <1 2>;
@b = <1 2 4>;
say @a," before ",@b," = ", @a before @b;
for 1..10 {
my @a = (^100).roll((2..3).pick);
my @b = @a.map: { Bool.pick ?? $_ !! (^100).roll((0..2).pick) }
say @a," before ",@b," = ", @a before @b;
}

View file

@ -0,0 +1,36 @@
#!/usr/bin/perl -w
use strict ;
sub orderlists {
my $firstlist = shift ;
my $secondlist = shift ;
my $first = shift @{$firstlist } if @{$firstlist} ;
my $second ;
#keep stripping elements from the first list as long as there are any
#or until the second list is used up!
while ( @{$firstlist} ) {
if ( @{$secondlist} ) { #second list is not used up yet!
$second = shift @{$secondlist} ;
if ( $first < $second ) {
return 1 ;
}
if ( $first > $second ) {
return 0 ;
}
}
else { #second list used up, defined to return false
return 0 ;
}
$first = shift @{$firstlist} ;
}
return 0 ; #in all remaining cases return false
}
my @firstnumbers = ( 43 , 33 , 2 ) ;
my @secondnumbers = ( 45 ) ;
if ( orderlists( \@firstnumbers , \@secondnumbers ) ) {
print "The first list comes before the second list!\n" ;
}
else {
print "The first list does not come before the second list!\n" ;
}

View file

@ -0,0 +1,2 @@
: (> (1 2 0 4 4 0 0 0) (1 2 1 3 2))
-> NIL

View file

@ -0,0 +1,8 @@
int(0..1) order_array(array a, array b)
{
if (!sizeof(a)) return true;
if (!sizeof(b)) return false;
if (a[0] == b[0])
return order_array(a[1..], b[1..]);
return a[0] < b[0];
}

View file

@ -0,0 +1 @@
(string)a < (string)b;

View file

@ -0,0 +1,70 @@
DataSection
Array_1:
Data.i 5 ;element count
Data.i 1, 2, 3, 4, 5 ;element data
Array_2:
Data.i 6
Data.i 1, 2, 1, 5, 2, 2
Array_3:
Data.i 5
Data.i 1, 2, 1, 5, 2
Array_4:
Data.i 5
Data.i 1, 2, 1, 5, 2
Array_5:
Data.i 4
Data.i 1, 2, 1, 6
Array_6:
Data.i 5
Data.i 1, 2, 1, 6, 2
EndDataSection
#False = 0
#True = 1
;helper subrountine to initialize a dataset, *dataPtr points to the elementcount followed by the element data
Procedure initArrayData(Array a(1), *dataPtr)
Protected elementCount = PeekI(*dataPtr)
Dim a(elementCount - 1)
For i = 0 To elementCount - 1
*dataPtr + SizeOf(Integer)
a(i) = PeekI(*dataPtr)
Next
EndProcedure
;helper subroutine that returns 'True' or 'False' for a boolean input
Procedure.s booleanText(b)
If b: ProcedureReturn "True": EndIf
ProcedureReturn "False"
EndProcedure
Procedure order(Array a(1), Array b(1))
Protected len_a = ArraySize(a()), len_b = ArraySize(b()), elementIndex
While elementIndex <= len_a And elementIndex <= len_b And a(elementIndex) = b(elementIndex)
elementIndex + 1
Wend
If (elementIndex > len_a And elementIndex <= len_b) Or (elementIndex <= len_b And a(elementIndex) <= b(elementIndex))
ProcedureReturn #True
EndIf
EndProcedure
Dim A_1(0): initArrayData(A_1(), ?Array_1)
Dim A_2(0): initArrayData(A_2(), ?Array_2)
Dim A_3(0): initArrayData(A_3(), ?Array_3)
Dim A_4(0): initArrayData(A_4(), ?Array_4)
Dim A_5(0): initArrayData(A_5(), ?Array_5)
Dim A_6(0): initArrayData(A_6(), ?Array_6)
If OpenConsole()
PrintN(booleanText(order(A_1(), A_2()))) ;False
PrintN(booleanText(order(A_2(), A_3()))) ;False
PrintN(booleanText(order(A_3(), A_4()))) ;False
PrintN(booleanText(order(A_4(), A_5()))) ;True
PrintN(booleanText(order(A_5(), A_6()))) ;True
Print(#crlf$ + #crlf$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf

View file

@ -0,0 +1,2 @@
>>> [1,2,1,3,2] < [1,2,0,4,4,0,0,0]
False

View file

@ -0,0 +1,2 @@
rascal>[2,1,3] < [5,2,1,3]
bool: true

View file

@ -0,0 +1,2 @@
>> ([1,2,1,3,2] <=> [1,2,0,4,4,0,0,0]) < 0
=> false

View file

@ -0,0 +1,5 @@
(define (lex<? a b)
(cond ((null? b) #f)
((null? a) #t)
((= (car a) (car b)) (lex<? (cdr a) (cdr b)))
(else (< (car a) (car b)))))

View file

@ -0,0 +1,2 @@
- List.collate Int.compare ([1,2,1,3,2], [1,2,0,4,4,0,0,0]) = LESS;
val it = false : bool

View file

@ -0,0 +1,24 @@
$$ MODE TUSCRIPT
MODE DATA
$$ numlists=*
1'2'1'3'2
1'2'0'4'4'0'0'0
1'2'3'4'5
1'2'1'5'2'2
1'2'1'6
1'2'1'6'2
1'2'4
1'2'4
1'2
1'2'4
$$ MODE TUSCRIPT
list1="1'2'5'6'7"
LOOP n,list2=numlists
text=CONCAT (" ",list1," < ",list2)
IF (list1<list2) THEN
PRINT " true: ",text
ELSE
PRINT "false: ",text
ENDIF
list1=VALUE(list2)
ENDLOOP

View file

@ -0,0 +1,10 @@
proc numlist< {A B} {
foreach a $A b $B {
if {$a<$b} {
return 1
} elseif {$a>$b} {
return 0
}
}
return 0
}