否定弦


12

免责声明:这不是我的挑战,但是ThisGuy说我可以发帖。


有时我想在相反的地方说一个字,例如happinessgo unhappiness。不幸的是,发生这种情况时,我的大脑有时会变得一片空白。然后有一天,又发生了这种情况,我心想:“这就是程序的目的!”

由于英语有很多例外,因此我创建了一个列表,其中包含起始字母的前缀

q or h          -> dis- (honest -> dishonest)
l                -> il-  (legal -> illegal)
m or p           -> im-  (mature -> immature)
r                -> ir-  (responsible -> irresponsible)
everything else  -> un-  (worthy -> unworthy)

任务

给定输入作为字符串,将字符串设为负数并输出结果。您可以假定所有给定的输入都符合上述规则。提交的内容可能是程序或功能,而不是摘要。

输入值

一个字符串,作为参数或从STDIN中获取

输出量

该字符串的取反形式,符合上述规则

如何取胜

这是一个因此最短的代码胜出


4
我们可以假设,我们再也找不回那个开头的单词有q没有u
Business Cat

3
关闭我的头顶,qadiqat,上述qiqirshqwerty。(我玩了很多Scrabble)
AdmBorkBork

4
@wsbltc嗯,数量很少,但是它们几乎都是从其他语言借来的单词,因此它们是否真的算作英语值得怀疑。那么我们可以假设字符串中q的a后面总是跟a u吗?
Business Cat

3
是的,您可以假设它始终具有u

10
这个挑战可能会使学徒非常不高兴……
Spratty

Answers:


10

Python,55个字节

lambda n:'ddiiiiuiimmlrnss'[5-'rlmphq'.find(n[0])::7]+n

在线尝试!


我们需要处理7个不同的起始字母:
g-> dish-> disp-> imm-> iml-> ilr-> ir其他所有内容 ->un

我们可以将所有这些否定存储在单个字符串中,并通过切片提取正确的否定:

 d      i      s
  d      i      s
   i      m
    i      m
     i      l
      i      r
       u      n

'ddiiiiuiimmlrnss'[i::7]

现在我们需要计算起始索引i'rlmphq'.find对于字符串中未包含的所有内容'r',返回0,5 q以及-1。为了从0到6获得所需的值,我们仍然需要从5减去返回值,从而得到以下代码:

'ddiiiiuiimmlrnss'[5-'rlmphq'.find(n[0])::7]

真可爱!
史蒂夫·贝内特

有人可以解释这到底是怎么回事吗?我了解切片符号的含义,但是魔术字符串ddiiiiuiimmlrnssrlmphq数字5是什么,为什么切片跳过7?
基廷厄

@Keatinge添加了解释。希望对您有帮助
ovs'Apr

6

GNU sed,50个字节

包括+1的 -r

s/^q|^h/dis&/
s/^l|^r|^m/i&&/
s/^p/imp/
t
s/^/un/

没有什么花哨。用于&替换中以合并一些替换,t如果出现第一个替换中的一个,则跳过最后一个。

在线尝试!


5

果冻,30 个字节

1ị“qmlrrhp”iị“3bµWI⁼ṡ÷ʠ$»œs5¤;

在线尝试!

怎么样?

1ị“qmlrrhp”iị“3bµWI⁼ṡ÷ʠ$»œs5¤; - Main link: string
1ị                             - 1 index into the string (first character of the string)
           i                   - 1-based index of 1st occurrence of that in (else zero):
  “qmlrrhp”                    -     char list "qmlrrhp"
            ị                  - index into (1-based and modulo):
                            ¤  -     nilad followed by link(s) as a nilad:
             “3bµWI⁼ṡ÷ʠ$»      -         compressed string "dis"+"imili"+"run"="disimilirun"
                         œs5   -         split "equally" into 5: ["dis","im","il","ir","un"]
                             ; - concatenate with the string

请注意,重复的rin “qmlrrhp”位于第5个索引中,如果引用该索引将导致在前一个索引un,因此它也可以等于h或以外的任何值p


4

///59 56字节

/^/\/\/#//#qu/disqu^h/dish^l/ill^m/imm^p/ipp^r/irr^/un/#

在线尝试!

输入在最后一个之后#

怎么运行的:

我进行了优化,将大小减少到56个字节,但是由于使事情复杂化,我将解释原始版本,然后解释高尔夫球。

/#qu/disqu//#h/dish//#l/ill//#m/imm//#p/ipp//#r/irr//#/un/# |everything after the ` |` is not code.
/#qu/disqu/                                                 |replace `qu` with `disqu`
           /#h/dish/                                        |replace `h` with `dish`.
                    /#l/ill/                                |replace `l` with `ill`.
                            /#m/imm/                        |replace `m` with `imm`.
                                    /#p/ipp/                |replace `p` with `ipp`.
                                            /#r/irr/        |replace `r` with `irr`.
                                                    /#/un/  |replace everything else with `un`.
                                                          # |safety control

直觉:挑战很简单,只需根据单词的开头添加否定即可。但是,在///中,您不能只是concatenate if [...],只能按照特定模式替换某些内容。因此,在此程序中,将正词开头替换为负词开头。在#加入以确保一旦加入一个新的开始,没有更多的新的开端将增加。这#也使得“其他一切:联合国”成为可能。

高尔夫在开始时采用了新的替代方法:/^/\/\/#/。这将全部替换^//#,这是原始版本中的常见模式。


3

TI基本(104字节)

Prompt Str0
sub(Str0,1,1→Str2
Str0
If Str2="Q" or Str2="H
"DIS"+Ans
If Str2="L
"IL"+Ans
If Str2="M" or Str2="P
"IM"+Ans
If Str2="R
"IR"+Ans
If Ans=Str0
"UN"+Ans
Ans

需要所有大写字母。

说明:

Prompt Str0             # 4 bytes, input user string to Str0
sub(Str0,1,1→Str2       # 12 bytes, store first character in Str2
Str0                    # 3 bytes, store Str0 in Ans
If Str2="Q" or Str2="H  # 14 bytes, if the first letter was Q or H
"DIS"+Ans               # 8 bytes, store DIS+Ans in Ans
If Str2="L              # 7 bytes, If the first letter was L
"IL"+Ans                # 7 bytes, store IL+Ans in Ans
If Str2="Q" or Str2="H  # 14 bytes, if the first letter was Q or H
"IM"+Ans                # 7 bytes, store DIS+Ans in Ans
If Str2="R              # 7 bytes, if the first letter was R
"IR"+Ans                # 7 bytes, store IR+Ans in Ans
If Ans=Str0             # 6 bytes, if Ans has not been changed (first letter was none of the above)
"UN"+Ans                # 7 bytes, store UN+Ans in Ans
Ans                     # 1 byte, implicitly return Ans

3

JavaScript(71 64 61字节)

w=>({q:a='dis',h:a,l:'il',m:b='im',p:b,r:'ir'}[w[0]]||'un')+w

编辑:

  • @ErtySeidohl(charAt(0)-> [0])节省了7个字节
  • @ edc65节省了3个字节(将共享前缀分配给变量)

var f = w=>({q:a='dis',h:a,l:'il',m:b='im',p:b,r:'ir'}[w[0]]||'un')+w;

function onChange() {
   var word = event.target.value;
   var output = f(word);
   document.getElementById('output').innerHTML = output;
}
Input Word: <input type='text' oninput='onChange()'/><br/>
Output Word: <span id="output">


1
如果您不关心与IE7的向后兼容性,则可以使用w[0]代替w.charAt(0)吗?
Erty Seidohl'4

@ErtySeidohl谢谢!刚刚学到新东西;-)
forrert

我是新来的,但是以just开头的答案是否合法w=>...?实际的功能定义将包括let f=w=>...?(可能在某处的常见问题解答中涵盖了...)
史蒂夫·贝内特

@SteveBennett是的,这是合法的。命名功能
无关紧要

1
w=>({q:a='dis',h:a,l:'il',m:b='im',p:b,r:'ir'}[w[0]]||'un')+w少3字节
edc65

2

批处理,114字节

@set/pw=
@set p=un
@for %%p in (dis.q dis.h il.l im.m im.p ir.r)do @if .%w:~,1%==%%~xp set p=%%~np
@echo %p%%w%

根据自定义前缀列表检查单词的第一个字符,如果是,则将前缀更改为默认值un。特殊外壳qu的成本为21个字节。


2

Haskell,71个字节

f l=maybe"un"id(lookup(l!!0)$zip"qhlmpr"$words"dis dis il im im ir")++l

用法示例:f "legal"-> "illegal"在线尝试!

建立前缀/替换对的查找表,以查找输入字符串的第一个字符,"un"如果没有找到默认值。


2

视网膜,54字节

^[^hqlmpr]
un$+
^[hq]
dis$+
^l
il$+
^[mp]
im$+
^r
ir$+

说明:

             {implicit replace stage}
^[^hqlmpr]   Append un to words starting with none of: hqlmpr
un$+         
^[hq]        Append dis to words starting with h or q
dis$+        
 ^l          Append il to words starting with l
il$+          
^[mp]        Append il to words starting with m or p
im$+    
^r           Append ir to words starting with r
ir$+

第一次使用Retina。这是一种非常简洁的语言。

在线尝试!


首先尝试一种语言非常好!+1
Arjun

并且,您有2500个代表!恭喜你!
Arjun

您的用户索引为26600!
Arjun

那么完美的10基!
Arjun

2

Javascript 72 71 66 61 60 59个字节

w=>('dis....il.im...im.dis.ir'.split('.')[w.charCodeAt(0)-104]||'un')+w

w=>'un.dis.dis.il.im.im.ir'.split('.')['qhlmpr'.indexOf(w[0])+1]+w

是的,它仍然比现有解决方案更长。:)

w=>['un','dis','im','il','ir']['qmlrhp'.search(w[0])%4+1]+w

如果需要任何解释,我可以利用q / h和m / p对,将它们在搜索字符串中的索引与mod 4组合在一起,然后将其用作对前缀数组的查找。


好答案。使用search代替保存1个字节indexOf。而更多一些,我想,用&代替%
edc65

谢谢!我不知道search。无法看到如何使&技巧起作用-如果我的数组只有4个元素,那将是完美的。
史蒂夫·本内特

1

C,109107字节

f(char*s){printf("%s%s",*s-109&&*s-112?*s-108?*s-114?*s-104&&*s-113|s[1]-117?"un":"dis":"ir":"il":"im",s);}

在线尝试!


1

Mathematica,107个字节

StringReplace[StartOfString~~x:#:>#2<>x&@@@{{"q"|"h","dis"},{"l","il"},{"m"|"p","im"},{"r","ir"},{_,"un"}}]

说明:

StartOfString~~x:#:>#2<>x&是一个纯函数,其中第一个参数是要在字符串开头匹配的字符串模式,第二个参数是要放在匹配项前面的字符串。它返回适用于的延迟规则StringReplace。然后将其应用于每个对,{{"q"|"h","dis"},{"l","il"},{"m"|"p","im"},{"r","ir"},{_,"un"}}从而得出规则列表

{
  StartOfString~~x:"q"|"h":>"dis"<>x,
  StartOfString~~x:"l":>"il"<>x,
  StartOfString~~x:"m"|"p":>"im"<>x,
  StartOfString~~x:"r":>"ir"<>x,
  StartOfString~~x_:>"un"<>x
}

最后,该列表被传递StringReplace给字符串操作符。


2
Mathmatica是否对所有内容都具有内置功能?


1

Excel 78字节

=TRIM(MID("  disdisil im im ir un",IFERROR(FIND(LEFT(A1),"qhlmpr"),7)*3,3))&A1

我发现一些竞争者使用不同的方法获得了81个字节:

=IFERROR(CHOOSE(FIND(LEFT(A1),"qhlmpr"),"dis","dis","il","im","im","ir"),"un")&A1

和84个字节:

=IFERROR(TRIM(MID("im disi"&LEFT(A1),MOD(FIND(LEFT(F1),"qlmhrp"),3)*3+1,3)),"un")&A1

0

REXX,78个字节

arg a
s.=un
s.q=dis
s.h=s.q
s.l=il
s.m=im
s.p=im
s.r=ir
p=left(a,1)
say s.p||a

通过大写答复来节省一些字节,例如,有效->重要。



0

Clojure,65个字节

#(str(get{\q"dis"\h"dis"\l"il"\m"im"\p"im"\r"ir"}(first %)"un")%)

好吧,这很无聊...但是我无法再缩短它了。至少很少有空格。


0

OCaml,85岁

(fun s->(match s.[0]with|'q'|'h'->"dis"|'l'->"il"|'m'|'p'->"im"|'r'->"ir"|_->"un")^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.