June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,20 @@
function lis(arr::Vector)
if length(arr) == 0 return copy(arr) end
L = Vector{typeof(arr)}(length(arr))
L[1] = [arr[1]]
for i in 2:length(arr)
nextL = []
for j in 1:i
if arr[j] < arr[i] && length(L[j]) ≥ length(nextL)
nextL = L[j]
end
end
L[i] = vcat(nextL, arr[i])
end
return L[indmax(length.(L))]
end
@show lis([3, 2, 6, 4, 5, 1])
@show lis([0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15])

View file

@ -0,0 +1,61 @@
# Project : Longest increasing subsequence
# Date : 2017/11/23
# Author : Gal Zsolt (~ CalmoSoft ~)
# Email : <calmosoft@gmail.com>
tests = [[3, 2, 6, 4, 5, 1], [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]]
res = []
for x=1 to len(tests)
lis(tests[x])
showarray(res)
end
func lis(X)
N = len(X)
P = list(N)
M = list(N)
for nr = 1 to len(P)
P[nr] = 0
next
for nr = 1 to len(M)
P[nr] = 0
next
len = 0
for i=1 to N
lo = 1
hi = len
while lo <= hi
mid = floor((lo+hi)/2)
if X[M[mid]]<X[i]
lo = mid + 1
else
hi = mid - 1
ok
end
if lo>1
P[i] = M[lo-1]
ok
M[lo] = i
if lo>len
len = lo
ok
next
res = list(len)
if len>0
k = M[len]
for i=len to 1 step -1
res[i] = X[k]
k = P[k]
next
ok
return res
func showarray(vect)
see "{"
svect = ""
for n = 1 to len(vect)
svect = svect + vect[n] + ", "
next
svect = left(svect, len(svect) - 2)
see svect
see "}" + nl

View file

@ -0,0 +1,40 @@
fn lower_bound<T: PartialOrd>(list: &Vec<T>, value: &T) -> usize {
if list.is_empty() {
return 0;
}
let mut lower = 0usize;
let mut upper = list.len();
while lower != upper {
let middle = lower + upper >> 1;
if list[middle] < *value {
lower = middle + 1;
} else {
upper = middle;
}
}
return lower;
}
fn lis<T: PartialOrd + Copy>(list: &Vec<T>) -> Vec<T> {
if list.is_empty() {
return Vec::new();
}
let mut subseq: Vec<T> = Vec::new();
subseq.push(*list.first().unwrap());
for i in list[1..].iter() {
if *i <= *subseq.last().unwrap() {
let index = lower_bound(&subseq, i);
subseq[index] = *i;
} else {
subseq.push(*i);
}
}
return subseq;
}
fn main() {
let list = vec![3, 2, 6, 4, 5, 1];
println!("{:?}", lis(&list));
let list = vec![0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15];
println!("{:?}", lis(&list));
}

View file

@ -1,25 +1,36 @@
object LongestIncreasingSubsequence extends App {
def longest(l: Array[Int]) = l match {
case _ if l.length < 2 => Array(l)
case l =>
def increasing(done: Array[Int], remaining: Array[Int]): Array[Array[Int]] = remaining match {
case Array() => Array(done)
case Array(head, _*) =>
(if (head > done.last) increasing(done :+ head, remaining.tail) else Array()) ++
increasing(done, remaining.tail) // all increasing combinations
}
val all = (1 to l.length).flatMap(i => increasing(l take i takeRight 1, l.drop(i+1))).sortBy(-_.length)
all.takeWhile(_.length == all.head.length).toArray // longest from all increasing combinations
}
val tests = Map(
"3,2,6,4,5,1" -> Array("2,4,5", "3,4,5"),
"0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15" -> Array("0,2,6,9,11,15", "0,2,6,9,13,15", "0,4,6,9,13,15", "0,4,6,9,11,15")
"3,2,6,4,5,1" -> Seq("2,4,5", "3,4,5"),
"0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15" ->
Seq("0,2,6,9,11,15", "0,2,6,9,13,15", "0,4,6,9,13,15", "0,4,6,9,11,15")
)
def asInts(s: String): Array[Int] = s split "," map Integer.parseInt
assert(tests forall {case (given, expect) =>
val lis = longest(asInts(given))
println(s"$given has ${lis.size} longest increasing subsequences, e.g. "+lis.last.mkString(","))
expect contains lis.last.mkString(",")
def lis(l: Array[Int]): Seq[Array[Int]] =
if (l.length < 2) Seq(l)
else {
def increasing(done: Array[Int], remaining: Array[Int]): Seq[Array[Int]] =
if (remaining.isEmpty) Seq(done)
else
(if (remaining.head > done.last)
increasing(done :+ remaining.head, remaining.tail)
else Nil) ++ increasing(done, remaining.tail) // all increasing combinations
val all = (1 to l.length)
.flatMap(i => increasing(l take i takeRight 1, l.drop(i + 1)))
.sortBy(-_.length)
all.takeWhile(_.length == all.head.length) // longest of all increasing combinations
}
def asInts(s: String): Array[Int] = s split "," map (_.toInt)
assert(tests forall {
case (given, expect) =>
val allLongests: Seq[Array[Int]] = lis(asInts(given))
println(
s"$given has ${allLongests.length} longest increasing subsequences, e.g. ${
allLongests.last
.mkString(",")
}")
allLongests.forall(lis => expect.contains(lis.mkString(",")))
})
}