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,16 @@
(use '[flatland.useful.seq :only (partition-between)])
(defn nonconsecutive? [[x y]]
(not= (inc x) y))
(defn string-ranges [coll]
(let [left (first coll)
size (count coll)]
(cond
(> size 2) (str left "-" (last coll))
(= size 2) (str left "," (last coll))
:else (str left))))
(defn format-with-ranges [coll]
(println (clojure.string/join ","
(map string-ranges (partition-between nonconsecutive? coll)))))

View file

@ -0,0 +1,18 @@
USING: formatting io kernel math math.parser sequences
splitting.monotonic ;
IN: rosetta-code.range-extraction
: make-range ( seq -- str )
[ first ] [ last ] bi "%d-%d" sprintf ;
: make-atomic ( seq -- str ) [ number>string ] map "," join ;
: extract-range ( seq -- str )
[ - -1 = ] monotonic-split
[ dup length 2 > [ make-range ] [ make-atomic ] if ] map
"," join ;
{
0 1 2 4 6 7 8 11 12 14 15 16 17 18 19 20 21 22
23 24 25 27 28 29 30 31 32 33 35 36 37 38 39
} extract-range print

View file

@ -0,0 +1,46 @@
# Project : Range extraction
# Date : 2017/11/08
# Author : Gal Zsolt [~ CalmoSoft ~]
# Email : <calmosoft@gmail.com>
int = "0,1,2,4,6,7,8,11,12,14,15,16,17,18,19,20,21,22,23,24,25,27,28,29,30,31,32,33,35,36,37,38,39"
int = str2list(substr(int, ",", nl))
sumint = []
intnew = 1
for n=1 to len(int)
flag = 0
nr = 0
intnew = 0
for m=n to len(int)-1
if int[m] = int[m+1] - 1
intnew = m+1
flag = 1
nr = nr + 1
else
exit
ok
next
if flag = 1 and nr > 1
if intnew != 0
add(sumint, [n,intnew])
n = m
ok
else
add(sumint, [n,""])
ok
next
showarray(sumint)
func showarray(vect)
see "["
svect = ""
for n = 1 to len(vect)
if vect[n][2] != ""
svect = svect +"" + int[vect[n][1]] + "-" + int[vect[n][2]] + ", "
else
svect = svect +"" + int[vect[n][1]] + ", "
ok
next
svect = left(svect, len(svect) - 2)
see svect
see "]" + nl

View file

@ -6,7 +6,7 @@ struct RangeFinder<'a, T: 'a> {
arr: &'a [T],
}
impl<'a, T> Iterator for RangeFinder<'a, T> where T: PartialEq + Add<u8, Output=T> + Copy {
impl<'a, T> Iterator for RangeFinder<'a, T> where T: PartialEq + Add<i8, Output=T> + Copy {
type Item = (T, Option<T>);
fn next(&mut self) -> Option<Self::Item> {
if self.index == self.length {
@ -40,10 +40,12 @@ impl<'a, T> RangeFinder<'a, T> {
}
fn main() {
let n = [0,1,2,3];
for (i, (lo, hi)) in RangeFinder::new(&n).enumerate() {
if i > 0 {print!(", ")}
let input_numbers : &[i8] = &[0, 1, 2, 4, 6, 7, 8, 11, 12, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 27, 28, 29, 30, 31, 32, 33, 35, 36,
37, 38, 39];
for (i, (lo, hi)) in RangeFinder::new(&input_numbers).enumerate() {
if i > 0 {print!(",")}
print!("{}", lo);
if hi.is_some() {print!("-{}", hi.unwrap())}
}

View file

@ -1 +1 @@
impl<'a, T> Iterator for RangeFinder<'a, T> where T: PartialEq + Add<u8, Output=T> + Copy {
impl<'a, T> Iterator for RangeFinder<'a, T> where T: PartialEq + Add<i8, Output=T> + Copy {