替换别名以形成短语


9

C指令启发#define

挑战

给定一个带有一些别名的短语,以及每个别名文本一个数组。输出初始短语,将每个别名替换为其相应的文本。

别名由一个尖锐字符定义,#后跟其在数组中的索引(索引可以从零或一开始)定义。别名可以在其文本内包含另一个别名,您必须解析所有别名(可能是递归的)。您可以假设别名永远不会陷入无限循环。别名不会有前导零(#02不是index 2处的别名,而是index 0后面是文本的别名2)。

您可以假设数组长度不超过20个项目。

您可以编写程序,函数或什至#define-都不错:)

您还可以使用另一种更适合您的语言的输入法。

phrase: "#0 & #3"
array: [
    "Programming #1",
    "Puzzles",
    "Code",
    "#2 Golf"
]
output: "Programming Puzzles & Code Golf"

一步步:

0> "#0 & #3"
1> "Programming #1 & #2 Golf"
2> "Programming Puzzles & Code Golf"

由于这是 ,最短答案以字节为单位!

另一个样品

phrase: "#0!"
array: [
    "We are #1",
    "#2",
    "#3",
    "#4 !",
    "graduating"
]
output: "We are graduating !!"

phrase: "##0#1#0#21#3#4"
array: [
    "a",
    "m",
    "z",
    "n",
    "g"
]
output: "#amaz1ng"

phrase: "##1#23"
array: [
    "WEIRD",
    "0 C",
    "AS"
]
output: "WEIRD CAS3"

phrase: "#1#7#6y#4#7#10s#7b#11#0#0#11r#7#0h#6#5#2#5#9#4."
array: [
    "t",
    "#12#3",
    "#11ga#3",
    "#0#10v#11",
    "#0h#10#8g",
    "#7#8",
    "a#8",
    " ",
    "n",
    "o",
    "i",
    "e",
    "P#9s#10"
]
output: "Positive anything is better than negative nothing."

上面的示例使用索引从零开始的Array。


如果使用1-indexing选项,是否不必担心前导0 #0出现而不应该出现的情况?还是#01有效的,但不是别名(即,它照原样保留)?
FryAmTheEggman

@FryAmTheEggman。在这种情况下,您应该忽略#01
删除了

轻松使用0-9的python,令人费解的尝试将0-19最小化:D
Antti Haapala

1
在看起来很简单的问题中隐藏着令人惊讶的复杂性。好问题!
乔什

Answers:


4

JavaScript(ES6)58

递归函数

f=(s,h,r=s.replace(/#(1?\d)/g,(_,x)=>h[x]))=>r==s?r:f(r,h)

测试

f=(s,h,r=s.replace(/#(1?\d)/g,(_,x)=>h[x]))=>r==s?r:f(r,h)

// Version without default parameters, same length but using a global
// f=(s,h)=>(r=s.replace(/#(1?\d)/g,(_,x)=>h[x]))==s?r:f(r,h)

console.log=x=>O.textContent+=x+'\n'

;[
  ['##1#23',['WEIRD','0 C','AS']],
  ["#0!",["We are #1","#2","#3","#4 !","graduating"]],
  ["##0#1#0#21#3#4",["a","m","z","n","g"]],
  ["##0#1#0#21#13#4",["a","m","z","","g","","","","","","","","","n"]],
  ["#1#7#6y#4#7#10s#7b#11#0#0#11r#7#0h#6#5#2#5#9#4.",
    ["t","#12#3","#11ga#3","#0#10v#11","#0h#10#8g","#7#8","a#8"," ","n","o","i","e","P#9s#10"]
  ],
  ["#0 & #3",["Programming #1","Puzzles","Code","#2 Golf"]]
].forEach(t=>{
  var a=t[0],b=t[1]
  console.log(a+' ['+b+']\n -> '+f(a,b))
})
<pre id=O></pre>


我添加了另一个测试案例别名为指数大于9
除去

2

Mathematica,74个字节

FixedPoint[#~StringReplace~a&,a=0;a=Reverse["#"<>ToString@a++->#&/@#2];#]&

不太复杂。其中大多数只是用于创建索引。


1
@WashingtonGuedes已修复。
LegionMammal978 '16

2

朱莉娅112 107 66字节

f(s,x)=(r=replace(s,r"#1?\d",m->x[1+parse(m[2:end])]))==s?s:f(r,x)

这是一个递归函数,它接受一个字符串和一个数组并返回一个字符串。它使用基于0的索引。

我们首先构造一个字符串r作为输入字符串s,用正则表达式的所有匹配项#1?\d替换为x的元素,该元素对应于1 +从匹配项中解析出的整数。如果等于s,则返回s,否则递归,将r作为字符串传递。


1

C,269 232

#define f(p,a,l)char*n,o[999],i=0,c,x;for(strcpy(o,p);o[i];)o[i]==35&isdigit(o[i+1])?c=o[i+1]-48,c=isdigit(o[i+2])&c?10*c+o[i+2]-48:c,n=a[c<l?c:c/10],x=strlen(n),memmove(o+i+x,o+i+2+c/10,strlen(o+i)),i=!memmove(o+i,n,x):++i;puts(o);

根据要求,单一#define解决问题!C宏不能递归,因此必须迭代解决该问题。该宏带有3个参数;短语p,数组a和数组的长度l我只从非解决方案中删除了空格;我知道我可以保存更多的字符,但是我认为这不会使我的字符数降至200以下。这绝对不是一个有竞争力的解决方案。 解决方案已全面解决。函数形式的非解决方案:

f(char*p,char**a,int l){
  char o[999]={0},i=0;
  strcpy(o,p);
  while(o[i]){
    if(o[i]=='#'&&isdigit(o[i+1])){
      int c = o[i+1]-'0';
      if(isdigit(o[i+2])&&c)
        c=10*c+o[i+2]-'0';
      if(c>=l)
        c/=10;
      char *n=a[c];
      memmove(o+i+strlen(n),o+i+2+c/10,strlen(o+i));
      memmove(o+i,n,strlen(n));
      i=0;
    }else{
      i++;
    }
  }
  puts(o);
}

和测试代码:

void test(char *phrase, char **array, int length) {
  f(phrase, array, length);
}

main() {
  const char *t1[] = {
    "Programming #1","Puzzles","Code","#2 Golf"
  };
  test("#0 & #3", t1, 4);

  const char *t2[] = {
    "We are #1","#2","#3","#4 !","graduating"
  };
  test("#0!", t2, 5);

  const char *t3[] = {
    "a","m","z", "n","g"
  };
  test("##0#1#0#21#3#4", t3, 5);

  const char *t4[] = {
    "WEIRD","0 C","AS"
  };
  test("##1#23", t4, 3);

  const char *t5[] = {
    "t","#12#3","#11ga#3","#0#10v#11","#0h#10#8g","#7#8","a#8"," ","n","o","i","e","P#9s#10"
  };
  test("#1#7#6y#4#7#10s#7b#11#0#0#11r#7#0h#6#5#2#5#9#4.", t5, 13);
}

编辑:工作一些高尔夫魔术。正如我认为的那样,它是如此简短和难以理解。


男人,好人!!!...如果可以的话,我会再次投票赞成;)
删除
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.