รับโทรศัพท์ของคุณ! มันสั่น!


14

คุณเพิ่งได้รับโทรศัพท์ใหม่ แต่คุณไม่ชอบวิธีที่มันสั่นสะเทือนคุณตัดสินใจที่จะสร้างรูปแบบการสั่นสะเทือนของคุณเอง ดังนั้นคุณได้เขียนโปรแกรมที่คุณใช้คำหลักที่long, shortและpauseเพื่อให้โทรศัพท์ของคุณสั่นตามคำหลักเหล่านี้

งาน

สร้างโปรแกรมขนาดเล็กที่ยอมรับสตริงของlong, shortและpauseและผลสตริงอื่นที่เป็นตัวแทนของเสียงการออกเสียงของโทรศัพท์สั่น;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) ระบุตัวคั่นที่แน่นอนที่ต้องใช้เมื่อแสดงผลเป็นสตริง
ปุย

คำตอบ:


12

Pyke , 22 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 โดยบังเอิญโชคดีที่มันเปลี่ยนเป็นค่าที่เรากำลังมองหา (ฉันค้นหาวิธีแก้ปัญหาอื่น ๆ อีกต่อไปค่อนข้างน้อยก่อนที่จะค้นหาวิธีนี้) หลังจากที่ทำเรายังสามารถเปลี่ยนรายการ ints ที่เป็น["RRRR", "RR", ""]โดยการคูณ int ของเราโดย"R"ที่แล้วกลายเป็นและในที่สุดชื่อปลอกมันจะได้รับ["RRRR", "RR", "-"] ["Rrrr", "Rr", "-"]จากนั้นเราจะเข้าร่วมรายการผลลัพธ์โดย" - "


วิธีที่ยอดเยี่ยมที่จะไปกับการเปลี่ยนแปลง!
tisaconundrum

วิธีการแก้ปัญหาคล้ายกับ absurdly ใน Pyth: j" - "m|*\M.&Chd6\-c:-)
Mr. Xcoder

อีกทั้ง OP เพิ่มพื้นที่ลงในตัวอย่าง แต่ไม่ได้ระบุพวกเขาฉันขอคำชี้แจงเกี่ยวกับเรื่องนั้น
Jonathan Allan

@JonathanAllan ไบต์\xefและ\xa6มี.oและ.&ตามลำดับ มันเป็นการเปลี่ยนแปลงที่เข้ากันได้แบบย้อนกลับโดยที่ถ้าตั้งค่าบิตสูงมันจะทำงานเหมือนคำสั่ง 2 ไบต์เก่า ฉันเขียนมันด้วยวิธีนี้เพื่อให้ง่ายขึ้นสำหรับผู้อ่านและเนื่องจาก Pyke ในทางเทคนิคไม่ได้ใช้หน้ารหัสอีกต่อไปและฉันไม่ต้องการแทรกไบต์แบบสุ่มที่ไม่ทำงาน
Blue

14

JavaScript, 70 63 ไบต์

บันทึก 2 ไบต์ขอบคุณLuke

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

ลองออนไลน์!


6
เคล็ดลับที่ดีกับ[]s ภายนอก!
Neil

11

Haskell , 71 66 59 ไบต์

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

ลองออนไลน์!

โอ้ถูกต้องแล้ว=<<concatMap

ใช้ประโยชน์จากความจริงที่ว่า"long"และทั้งสองมีตัวอักษร"short"o


คุณไม่จำเป็นต้องจ่าย 2 ไบต์สำหรับf=ฟังก์ชั่นที่ไม่มีจุดได้รับอนุญาตโดยไม่ได้รับมอบหมาย
Post Rock Garf Hunter

คุณสามารถบันทึกหนึ่งไบต์ทั้งหมดด้วย lambdacase โดยเปลี่ยนเป็น lambdabot haskell:(>>=(\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

05AB1E , 33 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

บันทึก 3 ไบต์โดยใช้AND 6กลอุบายจากคำตอบ pyke ของ muddyfish




5

เรติน่า , 31 29 ไบต์

ขอบคุณ 1 ไบต์ที่บันทึกโดยDom Hastings
ขอบคุณที่Neilบันทึกไว้ 1 ไบต์

[^lhp ]

l
hrr
h
Rr
p
-
 
 - 

ลองออนไลน์!

นี่คือวิธีการที่แตกต่างกันกว่า @ DomHastings ของคำตอบ



4

R , 77 ไบต์

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

จะเข้าผ่าน STDIN ตรวจสอบว่าการป้อนข้อมูลที่ตรงกับlong, shortหรือpauseและแลกเปลี่ยนการแข่งขันสำหรับRrrr, Rrหรือ-ตามลำดับ

จากนั้นจะถูกพิมพ์ด้วย-เบาะที่มีช่องว่างเป็นตัวคั่นจับคู่เอาต์พุตที่ต้องการ


คุณสามารถบันทึก 2 ไบต์โดยสลับจากการจับคู่เป็น% ใน%: scan(,'') %in% c('long','short','pause')
YCR

@YCR ฉันไม่คิดว่ามันจะได้ผล a %in% bตรวจสอบว่ารายการในรายการaมีอยู่หรือไม่ในbขณะที่match(a, b)ส่งคืนดัชนีจริงของการแข่งขัน เนื่องจากเราสามารถสมมติว่าอินพุตถูก%in%ต้องการใช้จะส่งคืนเวกเตอร์ของTRUE s
JAD

Arf จริง ฉันทดสอบด้วย c ('ยาว', 'สั้น', 'หยุดชั่วคราว')
YCR

นั่นเป็นเพราะเมื่อคุณป้อนเวกเตอร์บูลีนเข้าไป[มันจะถูกตีความว่า[which(bool) == TRUE]ในตัวอย่างของคุณจะเป็นแบบไหน[c(1,2,3)]ซึ่งจะเกิดขึ้นเพื่อให้ได้ผลลัพธ์ที่ถูกต้อง
JAD

แทน sep=' - 'ใช้s=' - 'น้อยกว่า 2 ไบต์
ระแหง

4

Röda , 73 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["-"]]&" - "]}

การใช้MmmและMmสั้นลง 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

ข้อเสนอแนะที่สองของคุณคืออัจฉริยะ!
กอตติเนต์

1
คุณสามารถย้ายส่วนหนึ่งของขั้นตอนการเพิ่มของ for-loop ไปที่เนื้อความแทนที่จะเป็นการคั่นด้วยเครื่องหมายจุลภาคได้หรือไม่?
Cyoce

นี่ช่วยประหยัดไบต์ได้อย่างแน่นอน จับดี!
กอตติเนต์


3

แบตช์ 88 ไบต์

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

ใช้อินพุตบน STDIN น่าเสียดายที่ลูปโอเวอร์เฮดมีราคา 26 ไบต์ดังนั้นนี่เป็นเพียงการเปลี่ยนที่น่าเบื่อ


2
แนะนำการแก้ไขการลบทั้งหมดของ@s
สตีเฟ่น

@ สตีเฟ่นใช่ฉันได้รับการแจ้งเตือน ...
Neil

ดี! เพียงสองสิ่ง: ฉันนับคำตอบนั้นมีความยาว 84 ไบต์ไม่ใช่ 88 นอกจากนี้ OP ได้แทนที่MmmmและMmด้วยRrrrและRrจะเป็นการดีที่จะอัปเดตคำตอบของคุณ c:
Matheus Avellar

รุ่นที่แพลตฟอร์มใด ฉันสงสัยว่า MS-DOS 6.22 จะทำสิ่งที่ XP ทำเมื่ออยู่ในโหมดคำสั่งขั้นสูง
TOOGAM

@TOOGAM ใช่เมื่อฉันพูด Batch ฉันมักจะหมายถึงเวอร์ชัน CMD.EXE ของ Windows NT
Neil



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



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!




โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.