How to resolve the algorithm Order two numerical lists step by step in the Wren programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Order two numerical lists step by step in the Wren programming language
Table of Contents
Problem Statement
Write a 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 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 true. If the second list or both run out of elements the result is false. Note: further clarification of lexicographical ordering is expounded on the talk page here and here.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Order two numerical lists step by step in the Wren programming language
Source code in the wren programming language
var orderLists = Fn.new { |l1, l2|
var len = (l1.count <= l2.count) ? l1.count : l2.count
for (i in 0...len) {
if (l1[i] < l2[i]) return true
if (l1[i] > l2[i]) return false
}
return (l1.count < l2.count)
}
var lists = [
[1, 2, 3, 4, 5],
[1, 2, 1, 5, 2, 2],
[1, 2, 1, 5, 2],
[1, 2, 1, 5, 2],
[1, 2, 1, 3, 2],
[1, 2, 0, 4, 4, 0, 0, 0],
[1, 2, 0, 4, 4, 1, 0, 0],
[1, 2, 0, 4, 4, 1, 0, 1]
]
for (i in 0...lists.count) System.print("list[%(i)] : %(lists[i])")
System.print()
for (i in 0...lists.count-1) {
var res = orderLists.call(lists[i], lists[i+1])
System.print("list[%(i)] < list[%(i+1)] -> %(res)")
}
You may also check:How to resolve the algorithm Anagrams/Deranged anagrams step by step in the Clojure programming language
You may also check:How to resolve the algorithm Gamma function step by step in the Factor programming language
You may also check:How to resolve the algorithm Averages/Median step by step in the Scala programming language
You may also check:How to resolve the algorithm Infinity step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Church numerals step by step in the AppleScript programming language