拿起你的手机!它在振动!


14

您刚购买了一部新手机,但是您不太喜欢它的振动方式,因此您决定要创建自己的振动模式。因此,您编写了一个使用关键字的程序longshortpause根据这些关键字使手机振动。

任务

创建一个小程序,它接受的字符串longshortpause并输出表示一个电话振动的语音声音的另一个字符串;Rrrr - Rr

long声音就是Rrrr
short声音Rr
(套管问题)
pause是破折号-
所有声音都用破折号分隔,周围有空格' - '

测试用例

输入:    long long short long short
输出:Rrrr - Rrrr - Rr - Rrrr - Rr

输入:   long long long short short short
输出:Rrrr - Rrrr - Rrrr - Rr - Rr - Rr

输入:   short short short pause short short short
输出:Rr - Rr - Rr - - - Rr - Rr - Rr

输入:   long short short long long pause short short
输出:Rrrr - Rr - Rr - Rrrr - Rrrr - - - Rr - Rr

这是一个问题,因此答案将以字节计分,而赢得的字节数最少。


5
这不是一个主观标准吗?我想使用空字符串,这听起来像是一部振动的手机。

6
您的“听起来”规则太含糊了。我建议只需要精确的字符串。打高尔夫需要精确的标准,因此我们可以优化代码而无需争论改进是否有效。
xnor

4
我们是否必须用来分隔声音-?在您的示例中就是这种情况,但未在任何地方指定。
JAD

12
所有示例均使用大写字母小写,后跟小写字母的副本。这是规则吗?
xnor

3
在重新打开之前需要做的事情:1)指定我们必须使用的确切字符串(或字符串集),包括大小写限制; 2)明确输入和/或输出可以是单词数组还是单词数组字符,3)指定作为字符串输出时必须使用的确切分隔符。
毛茸茸的

Answers:


12

派克(Pyke22 20字节

cFh.o6.&\R*\-|l4)J" - 

在这里尝试!

c                      -  split(input, " ")
 Fh.o6.&\R*\-|l4)      -  for i in ^:
  h                    -        ^[0]
   .o                  -       ord(^)
     6.&               -      ^ & 6
        \R*            -     ^
           \-|         -    ^ or "-"
              l4       -   ^.title()
                 J" -  - " - ".join(^)

这个答案的关键是将转化["long", "short", "pause"][4, 2, 0]。它获取每个单词的第一个字母的代码点,并AND以6 开头。通过幸运的巧合,它转换为我们想要的值。(在找到此解决方案之前,我搜索了许多其他较长的解决方案)。完成后,我们可以将整数列表进一步转换为["RRRR", "RR", ""]乘以int "R",再将int int,["RRRR", "RR", "-"]并最终对其进行标题以将int为["Rrrr", "Rr", "-"]。然后,我们通过以下方式加入结果列表" - "


进行转换的很酷的方法!
tisaconundrum

该解决方案在Pyth中非常荒谬:j" - "m|*\M.&Chd6\-c:-)
Xcoder先生17年

OP还为示例添加了空间,但未指定示例,我要求对此进行澄清。
乔纳森·艾伦

@JonathanAllan字节\xef\xa6.o.&分别。这是向后兼容的更改,如果设置了高位,它将像旧的2字节命令一样运行。我使用这种方式编写代码的目的是为了使读者更轻松,并且因为Pyke从技术上讲不再使用代码页,而且我不想插入不起作用的随机字节
蓝色,

14

JavaScript, 70 63字节

感谢Luke,节省了2个字节

a=>a.replace(/./g,a=>[['Rr','rr','-',' - ']['onp '.search(a)]])

在线尝试!


6
外面的[]s 不错!
尼尔

11

哈斯克尔71 66 59字节

g 'o'="Rr"
g 'n'="rr"
g 'p'="-"
g ' '=" - "
g _=""
f=(g=<<)

在线尝试!

哦,对,=<<是concatMap。

利用"long""short"都有字母的事实o


您无需支付2个字节即可获得f=无需分配即可使用的无点功能
发布Rock Garf Hunter,

通过切换到lambdabot haskell,可以使用lambdacase保存一个完整的字节:(>>=(\case 'o'->"Rr";'n'->"rr";'p'->"-";' '->" - ";_->""))
BlackCap,

7

JavaScript(ES6),65 59字节

s=>s.split` `.map(x=>x<'m'?'Rrrr':x<'q'?'-':'Rr').join` - `

let f =

s=>s.split` `.map(x=>x<'m'?'Rrrr':x<'q'?'-':'Rr').join` - `

console.log(f("long long short long short")); // => Rrrr - Rrrr - Rr - Rrrr - Rr
console.log(f("long long long short short short")); // => Rrrr - Rrrr - Rrrr - Rr - Rr - Rr
console.log(f("short short short pause short short short")); // => Rr - Rr - Rr - - - Rr - Rr - Rr
console.log(f("long short short long long pause short short")); // => Rrrr - Rr - Rr - Rrrr - Rrrr - - - Rr - Rr


7

05AB1E33 27 25 21字节

#εÇн6&'m×™'-)éθ}… - ý

在线尝试!

说明

#                       # split input on spaces
 ε             }        # apply to each
  Çн                    # get the character code of the head
    6&                  # AND with 6
      'm×               # repeat "m" this many times
         ™              # title case
          '-)           # wrap in a list with "-"
             éθ         # get the longest string       
                … - ý   # join to string using " - " as separator

使用泥泞鱼的派克答案中AND 6技巧节省了3个字节






4

R,77字节

cat(c('Rrrr','Rr','-')[match(scan(,''),c('long','short','pause'))],sep=' - ')

通过STDIN获取输入,检查输入是否匹配longshortpause将匹配交换为RrrrRr-分别。

然后将其打印为-带有空格的分隔符,以匹配所需的输出。


您可以通过从匹配切换为%in%来节省2个字节:scan(,'') %in% c('long','short','pause')
YCR

@YCR我认为那不会起作用。a %in% b检查中是否a存在中的条目b,同时match(a, b)返回匹配项的实际索引。由于我们可以假定输入是有效的,因此using %in%将仅返回TRUEs 的向量。
JAD

阿夫,是的。我已经用c('long','short','pause')进行了测试。
YCR

这是因为当您将布尔向量输入时[,它将被解释为[which(bool) == TRUE],在您的示例中将是[c(1,2,3)],这反过来又会给出正确的输出。
JAD

相反的sep=' - ',你可以使用s=' - '2个字节少
裂谷

4

Röda73 57 47 46 40 44字节

f&a{a~=*`s\w+|l;Rr;ong;rr;p\w+;-; ; - `/";"}

在线尝试!

+4个字节(由于规则更改)(必须使用 Rrrr而不是任何4个字母的变体)。

先前的代码:

{[[split()|["Bzzz"]if[_="long"]else["Bz"]if[_1="short"]else["-"]]&" - "]}

使用MmmMm短1个字节。
ATaco

@ATaco在问题中说“长声音应为4个字符长,而短声音应为2个字符长”
fergusq

ATaco,请注意,问题中已进一步指定了标准。
tisaconundrum

4

C(gcc)93 77 76字节

-2字节感谢Scepheo!
-1字节感谢Cyoce!

将以NULL终止的** char或等效值作为输入。

f(char**a){for(;*a;*++a&&printf(" - "))printf(**a&6?**a&1?"Rr":"Rrrr":"-");}

在线尝试!

说明:

f(char**a){
  // While the string at the current position is not NULL
  for(;*a;
    // Advances the pointer to the next string
    // Then if the current string is not NULL, prints a delimiter
    *++a&&printf(" - ")
  )
    /* 
      If the 1st char of the string is not a 'p'
        If the 1st char is not a 'l'
          Prints "Rr"
        Else
          Prints "Rrrr"
      Else:
        Prints "-"
     */
    printf(**a&6?**a&1?"Rr":"Rrrr":"-");
}

2
我想,你可以结合a++,*a*++a救两个字节,并采取模糊的“声音”定义的优势,使用*a,而不是"Rrrr"挽救另一个四个字节。
Scepheo

您的第二个建议是天才!
scottinet

1
您可以将for循环的增量阶段的一部分移到主体上,而不用逗号分隔吗?
Cyoce

这确实节省了一个字节。接得好!
scottinet

3

[R,72个字节

从stdin接受输入,打印到stdout。

cat(sapply(scan(,''),switch,long="vvvv",short="vv",pause="-"),sep=" - ")

在线尝试!


3

批处理,88字节

@set/ps=
@set s=%s: = - %
@set s=%s:long=Rrrr%
@set s=%s:short=Rr%
@echo %s:pause=-%

在STDIN上输入。不幸的是,循环开销花费了26个字节,因此这仅仅是无聊的替换。


2
建议删除所有@s的修改
Stephen

@Stephen是的,我收到了通知...
Neil

真好!不过,只有两件事:我计算出答案的长度为84个字节,而不是88个字节。此外,OP已替换为,MmmmMmRrrrRr来更新您的答案c:
Matheus Avellar '17

从哪个平台开始批处理?我怀疑MS-DOS 6.22在增强的命令模式下会做XP会做什么。
TOOGAM

@TOOGAM是的,当我说批处理时,我通常是指Windows NT的CMD.EXE版本。
尼尔


2

PHP, 113 bytes

<?$s=[];for($i=1;$i<$argc;$i++){$c=$argv[$i][0];$s[]=($c<'m')?'Rrrr':(($c<'q')?'-':'Rr');}echo implode(' - ',$s);

Try it online!

First attempt at code golf, so probably a lot of optimisations available!


2

Vim (52 bytes)

:s/long/Rrrr/ge|s/short/Rr/ge|s/pause/-/ge|s/ / - /genter

Can probably be made shorter...


Stringing the commands together like this stops the train if one of them errors IE if there's no pause or something in the given string, the replacements after the one that failed won't work. You can either split them on separate lines or put an e flag at the end
nmjcman101

Corrected the error. I still feel there ought to be some way to speed it up, but the only other way I thought of (after turning "pause" into a dash, s/[^ -]/r/, capitalize the first R after every space, trim four-r to two-r) came out longer.
David Heyman

1

Excel, 100 bytes

=REPLACE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"long","- Bzzz"),"short","- Bz"),"pause","- -"),1,2,"")

Per examples, Input is SPACE separated string, as is output.

Question itself does not mention a SPACE requirement, allowing for a slightly shorter 97 byte solution:

=REPLACE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"long","-Bzzz"),"short","-Bz"),"pause","--"),1,1,"")

1

AutoIt, 145 bytes

EXECUTE(STRINGREPLACE('MSGBOX(0,0,STRINGSTRIPWS(====INPUTBOX(0,0),"PAUSE",""),"LONG","Rrrr"),"SHORT","Rr")," "," - "),4))',"=","STRINGREPLACE("))

(AutoIt is really bad choice for code golf, tried my best to make it small as possible)


Welcome to the site! :)
James

1

Alice, 37 bytes

/ lRnrhR
\""orgrp-""!yi'."?-e"ySNo?@/

Try it online!

Explanation

This program makes the following substitutions:

  • l, hR
  • o, n, gr
  • p-
  • Space → Space
  • Everything else → Nothing
"longhp "!i.?eyN?"RrrrR- "y' " - "So@

"longhp "    Push this string
!            Immediately move to tape
i            Take input string
.            Duplicate
?ey          Remove all instances of the characters "longhp " from copy
N            Remove the remaining characters from the original, leaving only "longhp "
?"RrrrR- "y  Replace the characters in "longhp " with the corresponding characters in "RrrrR- "
' " - "S     Replace all spaces with " - "
o            Output
@            Terminate

1

Sed, 50 bytes

Takes input from stdin, prints to stdout

s/l\w*/Rrrr -/g
s/s\w*/Rr -/g
s/p\w*/- -/g
s/ -$//

Edit - saved 2 bytes

Sed, 40 bytes

Copying idea from Nitrodon's answer

s/[srtaue]//g
y/lhongp/RRrrr-/
s/ / - /g

Edit: saved another 2 bytes



0

Paradoc (v0.2.10), 21 bytes (CP-1252)

Wμ‹6&'r\°"-":Ãu}« rTc

Try it online!

Takes a string on the stack and results in a string on the stack. Prepend i to turn into a full program that reads from STDIN.

Uses &6 like the Pyke answer and everybody else, but joins the tokens together slightly differently, by adding a "-" token after each noise, deleting the last one, and then joining these tokens by spaces. Seems to save a byte over joining by " - ".

Explanation:

W                     .. Break into words
 μ             }      .. Map over this block:
  ‹                   .. Take the first character
   6&                 .. Binary AND with 6, to get 4, 2, or 0
     'r               .. Character "r"
       \              .. Swap top two of stack
        °             .. Replicate, to get "rrrr", "rr", or ""
         "-"          .. Push string "-"
            :         .. Duplicate on stack
             Ã        .. Compute the max...
              u       .. ... underneath the top of the stack (so, of the
                      .. second and third elements on the stack, i.e. the
                      .. string of "r"s and "-")
                      .. The mappped block ends here; we now have
                      .. something like ["rrrr", "-", "-", "-", "rr", "-"]
                «     .. Take all but the last
                  r   .. Join with spaces (this built-in's name is two
                      .. characters, the first of which is a space)
                   Tc .. Title-case

v0.2.11 will support shaving two more bytes by replacing with x and "-" with '-.



0

Ruby, 67 bytes

p ARGV[0].split(' ').map{|w|w<'m'?'Rrrr':w<'q'?'-':'Rr'}.join ' - '

This is Johan Karlsson's JavaScript solution ported to Ruby. If you like this answer, you should upvote Johan's answer.

The key idea is to compare the word strings 'short', etc. to a single character in order to distinguish between words.

| Word  | < 'm' | < 'q' | Output |
|-------|-------|-------|--------|
| short | false | false | 'Rr'   |
| long  | true  | N/A   | 'Rrrr' |
| pause | false | true  | '-'    |

Or, in alphabetical order:

  • long
  • m
  • pause
  • q
  • short

Try it online!


0

Ruby, 78 bytes

p ARGV[0].chars.map{|c|{p:'-',o:'Rr',g:'rr',' '.to_sym=>' - '}[c.to_sym]}.join

The only important parts of the input are p, o, g, and space... ignore the rest.

  • short becomes o
  • long becomes og
  • pause becomes p

Try it online!



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.