缩小数字字符串


12

鉴于串12任意长度的,写一些代码(并不一定是一个功能了,一切都会好起来)计算多少步它需要字符串收缩到最终形式,下面这个标准:

如果您的字符串是112112,这意味着您必须打印1,两个1和2,如下所示: 1112。当您再次执行该操作时,必须打印1和2。得到12。然后打印一张2,获得2。这是最终形式,因为此字符串不再更改。您的代码将输出3,因为您需要3个步骤才能到达最终表单。

其他规定

  • 如果字符串长度不均匀,则最后一个数字保持不变。

  • 每个不再更改的字符串(例如222222)都被视为最终形式。

  • 您不能使用任何外部来源。

  • 您的代码必须与每个字符串的工作12

  • 最短的代码获胜,因为它是代码高尔夫球。

  • 您的代码应打印出每一步。

  • 每种输入法都可以。

例子

Input >> 122122122121212212

Your code has to print:
211222111111222
11222111222
122111222
2111222
111222
1222
222
Steps:7 (you can omit the "Steps")
---- ---- ---- ----
Input >> 22222221

Your code has to print:
22222211
2222221
2
---- ---- ---- ----
Input >> 2222

Your code has to print:
0

编辑:大量编辑。很抱歉。


3
“如果您的字符串是112112,则意味着您必须像这样打印1、2和1:1112。”我不明白。
Fabinout 2014年

7
尝试大声朗读。它是“一一”,“二一”和“一二”。我的意思是“ 1次1”,“ 2次1”和“ 1次2”。
Vereos 2014年

5
如果正则表达式“甚至没有用”,为什么要禁止它们?
JB 2014年

1
正则表达式限制已删除。
Vereos 2014年

1
@ProgramFOX“一个1,两个1和一个2” :1 11 2。每两个数字是一对:该对的第一个数字表示渲染该对中的第二个数字的次数。没有配对伙伴的任何最终奇数位均按原样呈现。
apsillers 2014年

Answers:


7

Ruby 1.9 +,73个字符

我认为no-regex规则既愚蠢又任意,因此这是一个基于regex 的恶意解决方案:

gets
($.+=1;puts$_.gsub!(/(.)(.)/){$2*$1.to_i})until~/^(22)*[12]?$/
p~-$.

测试运行:

$ ruby 21.rb <<< 122122122121212212
211222111111222
11222111222
122111222
2111222
111222
1222
222
7

最后一行是步骤数。

编辑:正则表达式限制已由Vereos删除。


3

Ç - 156 154

我的第一个代码高尔夫在这里!

f(char*s,c){puts(s);char*a=s,*b=s,m=50,x,y;while(*a){
x=*a++;y=*a++;if(y)m&=x&y;*b++=y?:x;x-49?*b++=y:(*b=0);}
*b=*a;return m/50?printf("%i",c),c:f(s,c+1);}

测试:

char c[] = "12211122211222221";
f(c,0);

输出:

12211122211222221
21112211222221
111221222211
121122221
21222211
1122221
122221
22211
22111
2211
221
10

2

GolfScript:69个字符

0\{\)\.p.[]\{~10base{.,1>{(\(@\{''+*}++~@\+\.}{~+0.}if}do;}~.@=!}do;(

内部循环的每次迭代都会找到字符串中的前2个数字,并使用它们形成form的块{num1 num2 '' + *}。评估此块后,我们将获得所需的这些数字读数。重复此操作,直到没有更多字符为止。然后,重复该循环,同时跟踪迭代次数和打印次数。

样品:

echo '12211122211222221' | ruby golfscript.rb g.gs
"12211122211222221"
"21112211222221"
"111221222211"
"121122221"
"2122221"
"1122221"
"122221"
"22211"
"22111"
"2211"
"221"
10

2

Python-126

def f(s):
 j=0
 while 1:
    n="";i=0
    for c in s:n+=c*i;i=[int(c),0][i>0]
    if i:n+=`i`
    if s==n:break
    s=n;print s;j+=1
 print j

这不会打印输入值。如果需要,请print s;在右移之前n="";

注意:您说的是“功能”,所以这是一个功能。这是不是函数的版本(127个字符):

s=raw_input();j=0
while 1:
    n="";i=0
    for c in s:n+=c*i;i=[int(c),0][i>0]
    if i:n+=`i`
    if s==n:break
    s=n;print s;j+=1
print j

(如果我可以让用户将数字粘贴到118(在第一行的引号之间粘贴数据)):

s="";j=0
while 1:
    n="";i=0
    for c in s:n+=c*i;i=[int(c),0][i>0]
    if i:n+=`i`
    if s==n:break
    s=n;print s;j+=1
print j

样品运行:

211222111111222
11222111222
122111222
2111222
111222
1222
222
7

另外,这些解决方案均适用于包含较大数字(最多9个)的字符串,但是某些字符串会产生越来越大的输出(例如99


1

JavaScript的107

(需要箭头功能支持,例如在Firefox中)

for(p=prompt,s=p(),k=0;r=s.match(/.?.?/g).map(a=>a>2?a[1]+(a<13?'':a[1]):a).join(''),p(s),r!=s;s=r)k++;p(k)
  • s 是输入字符串

  • 每回合,我们使用正则表达式.?.?将其s分解为两个字符的字符串数组,然后将map这些字符串简化为它们的形式并将该数组粘在一起

  • r 存储当前回合的结果,以便与前一回合进行比较 s

  • k 是圆形柜台

  • 我们严重滥用prompt(别名为p)作为输入和输出机制,因为它可以向用户显示消息


for(p=prompt,s=p(),k=0;
    r=s.match(/.?.?/g).map(
        a=>
            a>2?                     // if a is more than one char
                a[1]+(a<13?'':a[1])  // double the second char if the first char is 2
               :a                    // if not two chars, return a
    ).join(''),                      // glue new array together
     p(s),                           // print s
     r!=s;                           // test if string has changed
    s=r)
        k++;
p(k)

1

Perl-50(+2)个字节

$a-=print while"$_"ne(s/(.)(.)/$2x$1/ge,$_);$_=-$a

需要-pl命令行开关。

用法示例:

$ more in.dat
122122122121212212

$ perl -pl rev-count.pl < in.dat
211222111111222
11222111222
122111222
2111222
111222
1222
222
7

$ more in.dat
22222221

$ perl -pl rev-count.pl < in.dat
22222211
2222221
2

$ more in.dat
2222

$ perl -pl rev-count.pl < in.dat
0

1

PHP,240

<?php
$s=$_GET['i'];$h=$s;for($t=1;$h!=r($h);$t++){echo$h.'<br>';$h=r($h);}echo r($s)==$s?0:$h.'<br>'.$t;function r($c){for($i=0;$i<strlen($c)-1;$i+=2){$s.=$c[$i]==1?$c[$i+1]:$c[$i+1].$c[$i+1];if($i+3==strlen($c))$s.=$c[$i+2];}return$s;}?>

示例:http//skyleo.de/codegolf.php?i = 211222111111222

211222111111222
11222111222
122111222
2111222
111222
1222
222
7

我有点不喜欢codegolf ._。也许我不应该只使用Java和PHP(我应该认为更复杂)


1
CodeGolf并不是要找到最复杂的解决方案,而是要最简单的解决方案。
primo 2014年

您实际上不需要这样做,str_split因为您可以访问字符串中的各个字符,就像PHP中的数组一样。
Gareth 2014年

是的,过了一段时间我以为自己是我,但是我懒得编辑它。但是现在我改变了。
Leo Pflug'2

0

R,158

s=utf8ToInt(scan(,""))-48;i=0;while(any(!!s-2)){t=(a<-sum(s|T))%%2;u=s[seq(a-t)];cat(s<-c(rep(u[c(F,T)],u[c(T,F)]),s[a*t]),"\n",sep="");i=i+1};cat("Steps:",i)

例:

122122122121212212

211222111111222
11222111222
122111222
2111222
111222
1222
222
Steps: 7

0

MATHEMATICA,117

s="122122122121212212";
Grid@FixedPointList[(Partition[#,2,2,1,{}]/.{{1,1}->{1},{1,2}->{2},{2,1}->{1,1}}//Flatten)&,FromDigits/@Characters@s]

122122122121212212
211222111111222
11222111222
122111222
2111222
111222
1222
222
222

0

POWERSHELL,2

根据Vereos在OP注释中对我的问题的回答“您可以使用任何可以缩短代码的输入方法”,以下脚本可以达到以下效果:

$a

针对“ 122122122121212212”运行的示例:

# Input method is to assign answer to $a:
$a = @"
211222111111222
11222111222
122111222
2111222
111222
1222
222
Steps: 7
"@

# Execute script
$a

# Answer follows:
211222111111222
11222111222
122111222
2111222
111222
1222
222

显然,这不是一个严肃的输入-目的是说明我的观点,即允许任何输入方法都可以使产生答案所需的实际代码变得微不足道。因此,需要更严格地指定输入法。


0

J,41个字符

作为函数(请原谅!对它们并不十分满意):

(,":@#)@}:@(([:;_2&(<@((".@[#])/)\))^:a:)
分解图
                _2&(            )\          NB. on non-overlapping 2-grams:
                       (      )/            NB.   insert between the two chars:
                        ".@[                NB.     read left operand ([) as a number
                            #]              NB.     and repeat right this many times,
                    <@(           )         NB.   then put it in a box
            ([:;                   )        NB. open and concatenate the boxes
           (                        ^:a:)   NB. repeat until fixpoint (keeping intermediates)
        }:@                                 NB. drop first element (the input)
(,":@#)@                                    NB. append length of array (# steps)
样品运行
   (,":@#)@}:@(([:;_2&(<@((".@[#])/)\))^:a:) '1121121'
1121121
11121  
121    
21     
11     
5      
   (,":@#)@}:@(([:;_2&(<@((".@[#])/)\))^:a:) '122122122121212212'
122122122121212212
211222111111222   
11222111222       
122111222         
2111222           
111222            
1222              
7                 

0

Perl,107个字符

其他的perl代码显然胜过了这一点,但是就它的价值而言,这就是事实。我使用了-l开关,但要花一个额外的字符:

$_=<>;while(1){$l=$_;$_=join"",map{($a,$b)=split//;$b?$b x$a:$a}grep/./,split/(..?)/;last if$l eq$_;print}

一个更清晰的版本:

$x = <>; while(1) { $l = $x; $x = join "", map{ ($a,$b) = split//, $_; $b ? $b x $a: $a } grep /./, split /(..?)/, $x; last if $l eq $x; print $x}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.