Order two numerical lists: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎Tcl: Added implementation)
(+Java)
Line 15: Line 15:
(list< (rest a) (rest b)))
(list< (rest a) (rest b)))
(t (< (first a) (first b)))))</lang>
(t (< (first a) (first b)))))</lang>
=={{header|Java}}==
{{works with|Java|1.5+}}
{{trans|Common Lisp}}
There are a few methods here. The method named "ordered" which works on arrays is a translation of [[#Common Lisp|Common Lisp]]. The other two are loose translations of [[#Tcl|Tcl]] (some tweaks were needed to get the length checks to work out) and are probably better options.
<lang java5>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 boolean ordered(List<Double> first, List<Double> second){
int i = 0;
for(; i < first.size() && i < second.size();i++){
if(first.get(i) == second.get(i)) continue;
if(first.get(i) < second.get(i)) 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;
}
}</lang>
=={{header|Pike}}==
=={{header|Pike}}==
<lang Pike>int(0..1) order_array(array a, array b)
<lang Pike>int(0..1) order_array(array a, array b)

Revision as of 16:00, 28 November 2011

Order two numerical lists is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Write function that orders two lists or arrays filled with numbers. The function should accept two lists as arguments and return true if the first list should be ordered before the second, and false otherwise.

The order is determined by 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 true. false otherwise.

Common Lisp

<lang Lisp>(defun list< (a b)

 (cond ((not a) t)
       ((not b) nil)
       ((= (first a) (first b))
        (list< (rest a) (rest b)))
       (t (< (first a) (first b)))))</lang>

Java

Works with: Java version 1.5+
Translation of: Common Lisp

There are a few methods here. The method named "ordered" which works on arrays is a translation of Common Lisp. The other two are loose translations of Tcl (some tweaks were needed to get the length checks to work out) and are probably better options. <lang java5>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 boolean ordered(List<Double> first, List<Double> second){ int i = 0; for(; i < first.size() && i < second.size();i++){ if(first.get(i) == second.get(i)) continue; if(first.get(i) < second.get(i)) 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; } }</lang>

Pike

<lang Pike>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];

}</lang>

Tcl

<lang tcl>proc numlist< {A B} {

   foreach a $A b $B {
       if {$a<$b} {
           return 1
       } elseif {$a>$b} {
           return 0
       }
   }
   return 0

}</lang>