ทำให้คลื่นสตริง


19

กำหนดสตริงเป็นอินพุตเอาต์พุตสตริงโดยใช้อัลกอริทึมต่อไปนี้:

1. Split the String by " " (find the words): "Hello World" -> ["Hello","World"]
2. Find the vowel count of each component: [2,1]   ( ["H[e]ll[o]","W[o]rld"] )
3. For each of the components, output the first n letter where n is the number 
   of vowels it contains: ["He","W"]
4. Join the list to a single string and reverse it: "HeW" -> "WeH"

รายละเอียด

  • คุณสามารถรับอินพุตและให้เอาต์พุตด้วยรูปแบบมาตรฐานและประเภทข้อมูลเดียวที่ใช้ได้สำหรับทั้งอินพุตและเอาต์พุตคือประเภทสตริงดั้งเดิมของภาษาของคุณ ไม่อนุญาตให้ป้อนข้อมูลโดยตรงเป็นรายการของคำแต่ละคำ

  • คุณรับประกันได้ว่าจะไม่มีช่องว่างติดต่อกัน

  • เสียงสระมี"a","e","i","o","u","A","E","I","O","U"แต่ไม่ถือว่าเป็นสระ"y","Y"

  • คุณรับประกันได้ว่าจะมีเพียงตัวอักษรและช่องว่างเท่านั้นที่ปรากฏในอินพุต แต่ไม่มีบรรทัดใหม่

  • เอาต์พุตต้องคำนึงถึงขนาดตัวพิมพ์

  • คุณไม่สามารถรับประกันได้ว่าแต่ละคำจะมีสระ หากไม่มีเสียงสระปรากฏอยู่ในคำนั้นคุณไม่จำเป็นต้องแสดงผลลัพธ์ใด ๆ

กรณีทดสอบ

Input -> Output
---------------

""                                  -> ""
"Hello World"                       -> "WeH"
"Waves"                             -> "aW"
"Programming Puzzles and Code Golf" -> "GoCauPorP"
"Yay Got it"                        -> "iGY" 
"Thx for the feedback"              -> "eeftf"                  
"Go Cat Print Pad"                  -> "PPCG"   
"ICE CREAM"                         -> "RCCI"

เกณฑ์การให้คะแนน

ส่งที่ถูกต้องที่สั้นที่สุดสำหรับแต่ละชนะภาษานี้เป็นรหัสกอล์ฟขอให้โชคดีและสนุก!


Sandboxสำหรับผู้ที่สามารถดูโพสต์ที่ถูกลบ


ขออภัยสำหรับการลบชั่วคราว!
Mr. Xcoder

6
ไม่ทราบว่าทำไมฉันคิดว่านี่จะเป็น PCG เกี่ยวกับคลื่นสตริง (ในขณะที่ทฤษฎีสตริง ) (เช่นในความผันผวนในสนาม) อาจถึงเวลานอนแล้ว
Marc.2377

2
@ Mr.Xcoder: โปรดเพิ่มกรณีทดสอบด้วยสระตัวพิมพ์ใหญ่ ขอบคุณ!
nimi

เพิ่มแล้ว @nimi มันเป็นแค่อัลกอริทึมเดียวกันไม่ว่าในกรณีใด
นาย Xcoder

1
@ Mr.Xcoder: ใช่ แต่อย่างน้อยสองคำตอบก็ผิด (ตอนนี้ทั้งสองคง)
nimi

คำตอบ:


7

Haskell, 59 ไบต์

map fst.reverse.(>>=zip<*>filter(`elem`"aeiouAEIOU")).words

ลองออนไลน์!

       words     -- split into a list of words
  (>>=      )    -- apply a function to every word and collect the results in a
                 -- single list
     zip<*>filter(`elem`"aeiouAEIOU")
                 -- f <*> g x = f x (g x), i.e. zip x (filter(...)x)
                 -- we now have a list of pairs of (all letters of x, vowel of x)
                 -- with the length of number of vowels
 reverse         -- reverse the list
map fst          -- drop vowels from the pairs

6

V , 31 ไบต์

Í /ò
òÄøã[aeiou]
|DJ@"|D-òÍî
æ

ลองออนไลน์!

00000000: cd20 2ff2 0af2 c4f8 e35b 6165 696f 755d  . /......[aeiou]
00000010: 0a01 7c44 4a40 227c 442d f2cd ee0a e6    ..|DJ@"|D-.....

และคำอธิบาย:

Í               " Substitute Every space
  /             " With
   ò            " Newlines
                " This puts us on the last line of the buffer
ò               " Recursively:
 Ä              "   Duplicate the current line
  ø             "   Count:
   ã            "   Case insensitive
    [aeiou]     "   The number of vowels
<C-a>           "   Increment this number
     |          "   Go to the beginning of this line
DJ              "   Delete the number of vowels, and remove a newline that was accidentally made.
                "   Also, my name! :D
  @"            "   Run the unnamed register, which is the number of vowels that we deleted
    |           "   And move to the n'th column in this line
     D          "   Delete everything on this line after the cursor, keeping the first *n* characters
      -         "   Move up a line
       ò        " End the loop
        Íî      " Remove all newlines
æ               " And reverse:
                "   (implicit) The current line

สิ่งนี้สามารถอ่านได้อย่างน่าประหลาดใจ ... คุณช่วยเพิ่มคำศัพท์เกี่ยวกับวิธีการใช้งานได้ไหม?
Mr. Xcoder

ฉันประทับใจกับความถี่ที่ฉันเห็นæใช้ฉันดูเหมือนจะจำได้ว่ามันถูกเพิ่มเข้ามาเร็ว ๆ นี้และเป็นหนึ่งในคำสั่งที่มีประโยชน์มากขึ้น
nmjcman101

@ nmjcman101 ใช่ฉันเห็นด้วยอย่างยิ่ง æมีประโยชน์อย่างมาก ฉันควรจะเพิ่มมันมานานแล้ว øดีมากจริงๆมันยอดเยี่ยมที่คำตอบนี้ใช้ทั้งคู่
DJMcMayhem

ดูเหมือนว่าจะทำงานได้โดยไม่ต้องใช้คนแรก|( ลองออนไลน์! ) ซึ่งไม่ได้อยู่ในคำอธิบายของคุณ แต่ฉันไม่รู้ V; มันจำเป็นหรือไม่
CAD97

@ CAD97 อ่าฉันพลาดไปในคำอธิบาย ใช้งานได้กับทุกกรณีทดสอบ แต่จะหยุดเมื่อมีสระ 10 คำขึ้นไปในคำ (เพราะ<C-a>วางเคอร์เซอร์ที่ท้ายคำ) tio.run/##K/v//3Cvgv7hTVyHNx1uObzj8OLoxNTM/…
DJMcMayhem

5

Brachylogขนาด 17 ไบต์

ṇ₁{{∋ḷ∈Ṿ}ᶜ}ᶻs₎ᵐc↔

ลองออนไลน์!

คำอธิบาย

นั่นเป็นการแปลปัญหาโดยตรง:

Example input: "Hello World"

ṇ₁                  Split on spaces:         ["Hello", "World"]
  {       }ᶻ        Zip each word with:      [["Hello",2],["World",1]]
   {    }ᶜ            The count of:
    ∋ḷ∈Ṿ                Chars of the words that when lowercased are in "aeiou"

            s₎ᵐ     Take the first substring of length <the count> of each word: ["He","W"]
               c    Concatenate:             "HeW"
                ↔   Reverse:                 "WeH"


4

อลิซ 32 ไบต์

/.'*%-.m"Re.oK"
\iu &wN.;aoi$u@/

ลองออนไลน์!

คำอธิบาย

/....
\...@/

นี่เป็นเพียงกรอบงานสำหรับโค้ดเชิงเส้นใน Ordinal (โหมดการประมวลผลสตริง) ตีแผ่โปรแกรมเราได้รับ:

i' %w.."aeiou".u*&-Nm;Ro.$K@

นี่คือสิ่งที่มันทำ:

i           Read all input.
' %         Split the input around spaces.
w           Push the current IP address to the return address stack to mark
            the beginning of the main loop. Each iteration will process one
            word, from the top of the stack to the bottom (i.e. in reverse 
            order).

  ..          Make two copies of the current word.
  "aeiou"     Push this string.
  .u*         Append an upper case copy to get "aeiouAEIOU".
  &-          Fold substring removal over this string. What that means is that
              we push each character "a", "e", ... in turn and execute -
              on it. That will remove all "a"s, all "e"s, etc. until all
              vowels are removed from the input word.
  N           Compute the multiset complement of this consonant-only version
              in the original word. That gives us only the vowels in the word.
              We now still have a copy of the input word and only its vowels
              on top of the stack.
  m           Truncate. This reduces both strings to the same length. In particular,
              it shortens the input word to how many vowels it contains.
  ;           Discard the vowels since we only needed their length.
  R           Reverse the prefix.
  o           Print it.
  .           Duplicate the next word. If we've processed all words, this
              will give an empty string.

$K          Jump back to the beginning of the loop if there is another word
            left on the stack.
@           Otherwise, terminate the program.

4

JavaScript (ES6), 76 ไบต์

s=>s.split` `.map(w=>w.split(/[aeiou]/i).map((_,i)=>o=i?w[i-1]+o:o),o='')&&o

กรณีทดสอบ



3

JavaScript (ES6), 96 ไบต์

s=>[...s.split` `.map(w=>w.slice(0,(m=w.match(/[aeiou]/gi))&&m.length)).join``].reverse().join``


คำที่ไม่มีสระ ( Thx) ควรไม่มีเอาท์พุท กรณีทดสอบของคุณให้ผลลัพธ์ทั้งคำ
Justin Mariner

@JustinMariner แก้ไขแล้ว!
darrylyeo

3

Pyth - 19 ไบต์

_jkm<dl@"aeiou"rd0c

ลองที่นี่

คำอธิบาย:

_jkm<dl@"aeiou"rd0c
                  c  # Split implicit input on whitespace
   m                 # For each word d...
               rd0   # ...take the lower-case conversion...
       @"aeiou"      # filter it to only vowels...
      l              # and find the length of this string (i.e., the number of vowels in the word)
    <d               # Take the first # characters of the word (where # is the length from above)
 jk                  # Join on empty string (can't use s, because that will screw up when the input is the empty string)
_                    # Reverse the result (and implicitly print)

ฉันสามารถมี 18 ไบต์ถ้าไม่ใช่สำหรับสตริงว่าง:

_sm<dl@"aeiou"rd0c

1
@DigitalTrauma: ฉันเพิ่งเพิ่มคำอธิบาย
Maria

1
@- สี่แยกดีกว่า regex ตรงนี้ โอ้ฉันเห็นแล้ว - คุณมีแลมบ์ดา / แผนที่เพียงหนึ่งเดียวเมื่อเทียบกับ 2 ของฉัน
บาดเจ็บทางดิจิตอล

3

Pyth, 31

ฉันใช้เวลานานในการเขียนและฉันรู้สึกว่าอาจมีวิธีการที่ดีกว่า แต่นี่คือสิ่งที่ฉันมี:

_jkm<Fd.T,cQ)ml:d"[aeiou]"1crQ0

ทดสอบออนไลน์

                             Q     # input
                            r 0    # to lowercase   
                           c       # split by whitespace
               :d"[aeiou]"1        # lambda: regex to find vowels in string
              l                    # lambda: count the vowels in string
             m                     # map lambda over list of words
          cQ)                      # split input by whitespace
         ,                         # list of (input words, vowel counts)
       .T                          # transpose
    <Fd                            # lambda to get first n chars of string
   m                               # map lambda over list of (input words, vowel counts)
 jk                               # join on on empty strings
_                                 # reverse

> ฉันรู้สึกว่าอาจเป็นวิธีที่ดีกว่า - ฉันได้ 19 ใน Pyth
Maria

1
@Svetlana ที่นั่นฉันแก้ไขมันแล้ว ขอบคุณสำหรับjkเคล็ดลับ
บาดเจ็บทางดิจิตอล

3

โอห์ม 13 ไบต์

z:αv_K_σh;0JR

คำอธิบาย

  • ก่อนอื่นอินพุต (โดยนัย) จะถูกแบ่งในช่องว่างด้วย zการป้อนข้อมูลจะถูกแบ่งออกในช่องว่างโดย
  • จากนั้นจะเริ่มวนรอบ foreach (: ) ด้วยนั้น codeblock αv_K_σhถูกเชื่อมโยง
    • av ดัน aeiou
    • _ ผลักองค์ประกอบองค์ประกอบซ้ำปัจจุบัน
    • Kนับจำนวนการเกิดของaeiouin_
    • _ องค์ประกอบอีกครั้ง
    • σh แบ่งองค์ประกอบออกเป็นส่วนยาว occurencesและใช้องค์ประกอบแรก
      • อย่างมีประสิทธิภาพนี้ใช้occurencesตัวอักษรตัวแรก
  • 0J ผลักสแต็กเข้าร่วม ''
    • 0เป็นสิ่งจำเป็นเพราะมันต้องมีข้อโต้แย้งที่จะเข้าร่วม ถ้าอาร์กิวเมนต์นั้นไม่ใช่อาร์เรย์มันจะรวมเข้ากับสแต็ก
  • R ฝืนผล
  • การพิมพ์โดยนัยของ TOS

3

Ruby , 54 59 + 1 = 55 60 ไบต์

ใช้-pแฟล็กสำหรับ +1 ไบต์

$_=$_.split.map{|w|w[0,w.count("aeiouAEIOU")]}.join.reverse

ลองออนไลน์!


@nimi มันทำตอนนี้
มูลค่าหมึก

เพียงแค่อยากรู้ว่าทำไม-pคุ้มค่าไบต์
Eric Duminil

2
@EricDuminil ดูโพสต์เมตานี้แต่โดยทั่วไปเนื่องจากruby -pe '...'มีเพียงหนึ่งไบต์มากกว่าruby -e '...'และ-eเป็นวิธีที่ถูกต้องในการเรียกใช้สคริปต์
Dom Hastings

3

Japt v2.0a0, 12 10 ไบต์

¸®¯Zè\vìw

ลองมัน


คำอธิบาย

ค่อนข้างมากสิ่งที่สเป็คอธิบาย!

        :Implicit input of string U.
¸       :Split to array on spaces.
®       :Map over the array, replacing each element with itself ...
¯       :  sliced from the 0th character to ...
Zè\v    :  the count (è) of vowels (\v) in the element (Z).
à      :End mapping.
¬       :Join to a string.
w       :Reverse.
        :Implicit output of result.

สิ่งที่ดีที่ฉันตรวจสอบคำตอบที่มีอยู่ก่อนที่จะเขียนของตัวเอง: P Nice one, ฉันไม่คิดว่ามันจะสั้นลง (แม้ว่าแน่นอนฉันอาจจะผิด ... )
ETHproductions

นอกเหนือ: ใน Japt 2.0 คุณสามารถเปลี่ยนทางทฤษฎี"%v"เป็น\v(ตัวอักษร regex ระดับเดียว, เทียบเท่า/\v/) ยังไม่เป็นประโยชน์แน่นอนเนื่องจากฉันยังไม่ได้ใช้ v2.0 เลย)
ETHproductions

@ ETHproductions ฉันพร้อมที่จะวิ่งออกไปจากประตูเมื่อความท้าทายนี้โผล่ขึ้นมาดังนั้นฉันเพิ่งเขียนมันขึ้นมาอย่างรวดเร็วโดยใช้มันอย่างแท้จริง อาจมีวิธีที่สั้นกว่าในการลดจำนวนตัวอักษรให้น้อยลง? การเปลี่ยนแปลงเหล่านั้นไปยัง RegEx จะสะดวกสำหรับการบันทึกสองสามไบต์ รอคอยพวกเขา
Shaggy


2

05AB1E , 14 ไบต์

#RʒDžMDu«Ãg£R?

ลองออนไลน์!

Darn 05AB1E ไม่มีโครงสร้างสำหรับ AEIOUaeiou ಠ_ಠ


1
รอ ... 05AB1E ถูกโจมตีโดย Japt เหรอ?
Mr. Xcoder

@ Mr.Xcoder เกิดขึ้นบ่อยกว่าที่คุณคิด
Erik the Outgolfer

1
#RʒDlžMÃg£R?สำหรับ 12 คุณสามารถลดขนาดของอุปกรณ์ที่ไม่ต้องการAEIOUaeiouได้ นอกจากนี้ทำไมห่าไม่ทำงานนี้โดยไม่?? คุณช่วยโพสต์คำอธิบายได้หรือไม่ฉันไม่คุ้นเคยʒ
Magic Octopus Urn

@carusocomputing ผลลัพธ์ที่น่าเสียดายต้องเป็นแบบตรงตามตัวพิมพ์ใหญ่ - เล็ก
Erik the Outgolfer

2

Mathematica, 145 ไบต์

(s=StringCount[#,{"a","e","i","o","u","A","E","I","O","U"}]&/@(x=StringSplit@#);StringReverse[""<>Table[StringTake[x[[i]],s[[i]]],{i,Tr[1^s]}]])&

ฉันไม่คุ้นเคยกับ Mathematica จริง ๆ แต่ไม่สามารถเว้นระยะห่างระหว่างs[[i]]],และ{i,Length@s}ลบออกได้ใช่ไหม
นาย Xcoder

ใช่แน่นอนฉันพลาดไป ฉันต้องตีกอล์ฟให้มากกว่านี้ด้วย
J42161217

มีวิธีในการส่งสตริงไปยังรายการใน Mathematica หรือไม่? สิ่งที่ชอบ"aeiouAEIOU".ToCharArray()?
caird coinheringaahing

คุณหมายถึงตัวละคร []?
J42161217

2

เรติน่า49 49ไบต์

i`(?=(([aeiou])|\w)+)((?<-2>.)+)\w* ?
$3
O^$`.

ลองออนไลน์! การเชื่อมโยงรวมถึงชุดทดสอบ คำอธิบาย: นี่เป็นแอปพลิเคชั่นของ. Lookahead ค้นหาคำสำหรับสระซึ่งถูกจับในกลุ่ม 2 กลุ่มจะถูกตอกเมื่อตัวอักษรแต่ละตัวถูกจับคู่ดังนั้นจึงจับจำนวนตัวอักษรเท่ากับจำนวนของสระในคำนั้น ส่วนที่เหลือของคำและช่องว่างต่อท้ายจะถูกละเว้นเพื่อให้กระบวนการสามารถเริ่มต้นอีกครั้งด้วยคำถัดไป ในที่สุดตัวอักษรที่เหลือจะกลับรายการ


2

C # (. NET Core) 144 ไบต์

using System.Linq;s=>new string(string.Join("",s.Split(' ').Select(e=>e.Substring(0,e.Count(c=>"aeiouAEIOU".Contains(c))))).Reverse().ToArray())

ลองออนไลน์!

ส่วนที่แย่ที่สุดคือการย้อนกลับ a stringใน C # จะส่งคืน aIEnumerable<char>stringที่คุณจะต้องแปลงกลับไปเป็น



2

Python 3 , 83 81 79 77 ไบต์

lambda z:''.join(i[:sum(y in'aeiouAEIOU'for y in i)]for i in z.split())[::-1]

ลองออนไลน์!



1
เปลี่ยนเป็น python 2 และคุณไม่จำเป็นต้อง()พิมพ์
Griffin

1
@Griffin ใน Python 2 คุณจะต้องraw_input()ใช้แทนที่จะinput()เสีย 4 ไบต์
Mr. Xcoder

1
@ Mr.Xcoder ทำไมคุณไม่ใส่เพียงแค่เครื่องหมายคำพูด?
กริฟ

1
@Griffin Ah ใช่ ในที่สุดจะช่วยประหยัด 2 ไบต์
นาย Xcoder

2

Java 8 , 171 151 ไบต์

-20 ไบต์ขอบคุณ @Lukas Rotter

ฉันรู้สึกว่ามันยังคงต้องการการเล่นกอล์ฟ ... แจ้งให้เราทราบในความคิดเห็นหากคุณมีข้อเสนอแนะใด ๆ

s->{String z="";for(String w:s.split(" "))z+=w.substring(0,w.replaceAll("(?i)[^aeiou]","").length());return new StringBuilder(z).reverse().toString();}

ลองออนไลน์!


Java รองรับ(?i)การไม่สนใจขนาดตัวพิมพ์ใน regex ดังนั้น(?i)[aeiou]ควรทำงาน
Lukas Rotter

คุณยังสามารถลบ{}เครื่องหมายวงเล็บของห่วงสำหรับเนื่องจากมีเพียงหนึ่งคำสั่งที่มีอยู่ในนั้น
Lukas Rotter

แทนที่จะลบความยาวของสตริง regex คุณสามารถใช้^เพื่อหาจำนวนของสระ: z+=w.substring(0,w.replaceAll("(?i)[^aeiou]","").length());
Lukas Rotter


1

เสียงกระเพื่อมสามัญ 218 ไบต์

(defun p(s &aux(j 0)c(v 0)r)(dotimes(i(1+(length s))(apply'concatenate'string r))(cond((or(= i(length s))(eql(setf c(elt s i))#\ ))(setf r(cons(reverse(subseq s j(+ j v)))r)v 0 j(1+ i)))((find c"AEIOUaeiou")(incf v)))))

คำอธิบาย

(defun p(s &aux (j 0) c (v 0) r)               ; j start of word, c current char, v num of wovels, r result
  (dotimes (i                                  ; iteration var
            (1+ (length s))                    ; iteration limit
            (apply 'concatenate 'string r))    ; iteration final result
    (cond ((or (= i (length s))                ; if string is terminated   
               (eql (setf c (elt s i)) #\ ))   ;  or, set current char, and this is a space, then
           (setf r (cons (reverse (subseq s j (+ j v))) r) ; push on result from current word chars as number of vowels
                 v 0                           ; reset number of vowels to 0
                 j (1+ i)))                    ; reset start of current word to next char
          ((find c "AEIOUaeiou")               ; if current char is a wovel
           (incf v)))))                        ;   then increment num of vowels

1

sed, 133 (132 + 1) ไบต์

sed เรียกด้วย-Eธงซึ่งเห็นได้ชัดว่าฉันเพิ่มหนึ่งไบต์
หมายเหตุ: ฉันยังไม่ได้พยายามเล่นกอล์ฟนี้

s/$/\n/
:l
s/(.)(\n.*)/\2\1/
tl
s/\n/ /
h
s/[aoeui]//g
G
:r
s/^(\S*) \S(.*\n\S* )\S/\1 \2/
tr
s/^ //
s/(\n\S*) /\1/
/^\n/!br
s/\s//g

ลองออนไลน์!


1

Clojure, 96 94 ไบต์

#(apply str(mapcat(fn[i](take(count(filter(set"aeiouAEIOU")i))i))(reverse(re-seq #"[^ ]+"%))))

ความยาวนี้ค่อนข้างไร้สาระ mapcatบันทึกสองไบต์


1

Swift 3, 240 bytes

f(s:"Input")นี้เป็นฟังก์ชั่นที่สามารถใช้กับ น่าแปลกที่ฉันไม่คิดว่าจะสามารถเล่นกอล์ฟต่อไปได้:

import Foundation
func f(s:String){var c=s.components(separatedBy:" "),r="";for i in c{let b=i.startIndex;r+=i[b...i.index(b,offsetBy: i.characters.filter{"aeiouAEIOU".contains(String($0))}.count-1)]};print(String(r.characters.reversed()))}

ลองใช้ที่ IBM Sandbox!


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