画螺旋词[关闭]


11

场景

给定一个ASCII小写字母和空格的输入,绘制一个螺旋形的字符,以跟踪不包括空格的原始字符输入。

规则

1)字符应以向外的方式逆时针向左旋转。如果不能左转,则笔直移动。

Given: abcdefg
Output:
  g
baf   
cde   

图像追踪第一个例子

2)只要不违反规则#1,字符就可以变成先前的字符。同样,如果该字符成螺旋形,则该字符将大写。字符大写后,无论重复使用多少次,字符都将保持大写。

Given: apples appeal
Output:
PAs
PLe
ea

图像追踪第二个例子


1
那么,如果单词继续旋转但违反了下一个字符,程序将停止吗?
马特

我认为让这成为一个很好的挑战的原因是您必须“倒退堆栈”,以便在使规则#1始终有效的时候讲话并继续。
蒂姆·雷迪

5
当前的措辞使规则2看起来完全是可选的。如果必须这样做,那么我认为有必要提供更全面的测试套件。
彼得·泰勒

1
输出应该是什么输入abcdefghab
彼得·泰勒

Answers:


2

JavaScript中,225个 221 212字节

-9个字节,感谢Conor O'Brien

请注意,您的文字大小写相互冲突。您的第一个测试用例从螺旋的中间开始。您的第二个测试用例从螺旋的顶部中间开始。我选择了第一个测试用例,因为这是我看到的第一个用例。您已经一年多没有编辑问题了,因此对此推测表示抱歉。

第一个测试用例:

9<-8<-7
|     |
2<-1  6
|     |
3->4->5

第二个测试用例:

2<-1<-6
|     |
3->4->5
|     |
7->8->9

没有任何其他要求,这里是高尔夫球规则。我100%确信,如果社区对此进行筹码,则可以大幅度削减。这将返回一个多行数组。

s=>eval("s=[...s.replace(/ /g,'')];i=0;k=j=1;a=[[],[],[]];b='00122210';c=b*100+'';for(;;){for(l=0;l<8;l++){if(!s[i])break;if(a[k][j]==s[i])s[i]=s[i].toUpperCase();a[k][j]=s[i];k=b[l];j=c[l];i++}if(!s[i])break}a")

美化代码段(将多行字符串打印到控制台)。请注意我的测试用例#2和OP的测试用例#2的不同之处(如果尚未使用,请参见上文):

(如果对代码片段有更多经验的人想要将其修复为HTML输入,可以随时对其进行编辑,则需要上床睡觉)。

f=
s=>eval("s=[...s.replace(/ /g,'')];i=0;k=j=1;a=[[],[],[]];b='00122210';c=b*100+'';for(;;){for(l=0;l<8;l++){if(!s[i])break;if(a[k][j]==s[i])s[i]=s[i].toUpperCase();a[k][j]=s[i];k=b[l];j=c[l];i++;g(a)}if(!s[i])break}a")

//replace argument here to change output
var arr = f("apples appeal");

function g(ar) {
  var str = "";

  for (x = 0; x < 3; x++) {
    for (y = 0; y < 3; y++) {
      str += ar[y][x] || " ";
    }
    str += "\n";
  }
  console.log(str);
}

脱节和解释

f=(input)=>{
    //remove spaces
    input = input.replace(/ /g, "");

    //convert to array (so I can uppercase individual letters)
    input = input.split("");

    //position in input
    var pos = 0;

    //positions inside output
    var xPos = 1;
    var yPos = 1;

    //output container (3 rows, 3 columns)
    var arr = [[],[],[]];

    //counterclockwise indexes for x
    var xOrder = "00122210";

    //counterclockwise indexes for y
    //var yOrder = "12221000"
    var yOrder = xOrder * 100 + "";

    //loop infinitely (breaks when input[pos] is undefined)
    for (;;) {
        //loop around circle
        for (var i = 0; i < 8; i++) {
            if (!input[pos]) {
                break;
            }

            //if item already in array equal next item in input, set next item in input to caps before
            if (arr[xPos][yPos] == input[pos]) {
                input[pos] = input[pos].toUpperCase(); 
            }

            //write or overwrite in array with current from input
            arr[xPos][yPos] = input[pos];

            //increment last because we do not prime our loops
            xPos = xOrder[i];
            yPos = yOrder[i];
            pos++;
        }
        if(!input[pos]) {
            break;
        }
    }
    return arr;
}

1
a.split("")等价于[...a]; s=>{...;return a;}等效于s=>eval("...;a")(并且;在非高尔夫版本中是可选的);所有分号后接a }是可选的
Conor O'Brien

@ ConorO'Brien谢谢:)
斯蒂芬

您会在测试用例中得到什么输出apples appeal?我看到ppa eas aLe,这绝对是不正确的,因为s l旁边没有p
彼得·泰勒

@PeterTaylor根据我的程序所基于的第一个测试用例的螺旋顺序,输出是正确的。第二个测试用例使用不同的螺旋顺序(从顶部开始)。我为代码段的每次迭代添加了一个日志。看一下-可能更有意义。
斯蒂芬

根据第一个测试用例的逻辑,输出将为eppa apas lple s
彼得·泰勒
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.