Code Golf:数字范围

时间:2011-01-11 15:50:28

标签: language-agnostic code-golf

挑战

通过用范围替换连续的运行来压缩一长串数字。

实施例

输入

1, 2, 3, 4, 7, 8, 10, 12, 13, 14, 15
输入保证按升序排列,不包含重复项。

输出

1 - 4, 7, 8, 10, 12 - 15
请注意,两个数字的范围应保持不变。 (7, 8;不是7 - 8

规则

您可以从命令行或标准输入接受排序的整数列表(或等效数据类型)作为方法参数。(选择较短的代码选择哪个选项)
您可以通过打印字符串输出字符串列表,也可以返回单个字符串或字符串集。

参考实施

(C#)

IEnumerable<string> Sample(IList<int> input) {
    for (int i = 0; i < input.Count; ) {
        var start = input[i];
        int size = 1;
        while (++i < input.Count && input[i] == start + size)
            size++;

        if (size == 1)
            yield return start.ToString();
        else if (size == 2) {
            yield return start.ToString();
            yield return (start + 1).ToString();
        } else if (size > 2)
            yield return start + " - " + (start + size - 1);
    }
}

8 个答案:

答案 0 :(得分:6)

Python,98个字符

def f(a):
 for x in a:
  if x-1not in a or x+1not in a:print x,"-"if x+1in a and x+2in a else",",

Python - 86个字符

这个结尾不包括额外的','

f=lambda a:''.join(`x`+",-"[(x+1in a)&x+2in a]for x in a if(x-1in a)&(x+1in a)^1)[:-1]

答案 1 :(得分:4)

Python,83个字符

def f(l,a=2):
 for x in l:
  b,a=a,(x+1in l)*(x-1in l)
  if a<1:print',- '[b],`x`,

演示:

>>> l=[1, 2, 3, 4, 7, 8, 10, 12, 13, 14, 15]
>>> f(l)
  1 - 4 , 7 , 8 , 10 , 12 - 15

答案 2 :(得分:1)

Ruby,165个字符

a=[]
def o(a)print "#{@s}#{a[0]}#{"#{a.size<3?',':' -'} #{a[-1]}"if a.size>1}";@s=', 'end
ARGV[0].split(', ').each{|n|if a[0]&&a[-1].succ!=n;o(a);a=[]end;a<<n;};o(a)

答案 3 :(得分:1)

C ++,166个字符

#define o std::cout
void f(std::vector<int> v){for(int i=0,b=0,z=v.size();i<z;)i==z-1||v[i+1]>v[i]+1?b?o<<", ":o,(i-b?o<<v[b]<<(i-b>1?" - ":", "):o)<<v[i],b=++i:++i;}

难道你们都不喜欢滥用?:运营商吗? ;)

更易阅读的版本:

#define o std::cout
void f(std::vector<int> v){
    for(int i=0,b=0,z=v.size();i<z;)
        i==z-1||v[i+1]>v[i]+1 ?
            b?o<<", ":o,
            (i-b?o<<v[b]<<(i-b>1?" - ":", "):o)<<v[i],
            b=++i
        :++i;
}

答案 4 :(得分:1)

Common Lisp,442/206 chars

(defun d (l)
  (if l
      (let ((f (car l))
        (r (d (cdr l))))
      (if r
          (if (= (+ f 1) (caar r))
          (push `(,f ,(cadar r)) (cdr r))
          (push `(,f ,f) r))
          `((,f ,f))
          ))
      nil))

(defun p (l)
  (mapc #'(lambda (x)
          (if (= (car x) (cadr x))
          (format t "~a " (car x))
          (if (= (+ 1 (car x)) (cadr x))
              (format t "~a ~a " (car x) (cadr x))
              (format t "~a-~a " (car x) (cadr x)))))
      (d l)))

“d”函数将输入列表重写为规范形式。为了好玩,我完全递归地做了这件事。 “p”函数将输出格式化为参考实现的等价物。

答案 5 :(得分:0)

F#,188个字符

let r(x::s)=
 let f=printf
 let p x=function|1->f"%A "x|2->f"%A %A "x (x+1)|n->f"%A-%A "x (x+n-1)
 let rec l x n=function|y::s when y=x+n->l x (n+1)s|y::s->p x n;l y 1 s|[]->p x n
 l x 1 s

更具可读性:

let range (x::xs) =
  let f = printf
  let print x = function
    | 1 -> f "%A " x
    | 2 -> f "%A %A " x (x+1)
    | n -> f "%A-%A " x (x+n-1)
  let rec loop x n = function
    | y::ys when y=x+n ->
        loop x (n+1) ys
    | y::ys ->
        print x n
        loop y 1 ys
    | [] ->
        print x n
  loop x 1 xs

答案 6 :(得分:0)

Ruby:123个字符

def y(n) t=[];r=[];n.each_with_index do |x,i| t<<x;if(x.succ!=n[i+1]);r=((t.size>2)?r<<t[0]<<-t[-1]:r+t);t=[];end;end;r;end

更易读

def y(n) 
t=[];r=[];
n.each_with_index do |x,i|
 t << x
 if (x.succ != n[i+1])
    r = ((t.size > 2) ? r << t[0] << -t[-1] : r+t)  
    t=[]
 end
 end
 r
end

执行类似

 > n=[1, 2, 3, 4, 7, 8, 10, 12, 13, 14, 15]
 > y n
 => [1, -4, 7, 8, 10, 12, -15]

答案 7 :(得分:0)

PHP 95 chars

(实际上它是python之后的第二语言

给定$a=array(numbers);

交易算法:

for($i=0;$i<count($a);$i++){$c=$i;while($a[$i+2]==$a[$i]+2)$i++;echo $a[$c],$i-$c>1?'-':',';}