स्ट्रिंग तरंगें बनाएं


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"

स्कोरिंग

प्रत्येक भाषा के लिए सबसे कम वैध सबमिशन जीतता है, यह । गुड लक और मजा करें!


जो लोग हटाए गए पोस्ट देख सकते हैं, उनके लिए सैंडबॉक्स


अस्थायी विलोपन के लिए क्षमा करें!
श्री एक्सकोडर

6
न जाने क्यों मुझे लगा कि यह स्ट्रिंग के बारे में ( स्ट्रिंग सिद्धांत के रूप में ) पीसीजी होने वाला है (जैसा कि किसी क्षेत्र में दोलनों में)। शायद यह सो जाने का समय है।
मार्क.2377

2
@ Mr.Xcoder: कृपया अपरकेस स्वरों के साथ एक परीक्षण मामला जोड़ें। धन्यवाद!
nimi

@ मिमी जोड़ा गया। यह सिर्फ एक ही एल्गोरिदम है, कोई फर्क नहीं पड़ता।
मिस्टर एक्सकोडर

1
@ Mr.Xcoder: हाँ, लेकिन कम से कम दो उत्तर गलत मिले (दोनों अब तय किए गए हैं)।
निम्मी

जवाबों:


7

हास्केल, 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

वी , 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

यह आश्चर्यजनक रूप से पठनीय है ... क्या आप कुछ शब्द जोड़ सकते हैं कि यह कैसे काम करता है?
श्री एक्सकोडर

मैं इस बात से प्रभावित हूं कि मैं कितनी बार æइस्तेमाल किया गया हूं, मुझे यह याद है कि इसे हाल ही में जोड़ा गया था और यह अधिक उपयोगी कमांडों में से एक है।
nmjcman101

@ nmjcman101 हाँ, मैं पूरी तरह से सहमत हूँ। æहै अत्यंत उपयोगी है। मुझे इसे बहुत समय पहले जोड़ना चाहिए था। øयह भी वास्तव में अच्छा है, यह अच्छा है कि यह उत्तर दोनों का उपयोग करता है।
डीजेएमकेम

यह पहले के बिना काम करने लगता है |( इसे ऑनलाइन आज़माएं! ), जो आपके स्पष्टीकरण में नहीं है। लेकिन मैं वी नहीं जानता; क्या इसकी जरूरत है?
सीएडी 97

@ CAD97 आह, मैंने अपने स्पष्टीकरण में यह याद किया। यह परीक्षण के सभी मामलों के लिए काम करता है, लेकिन यह तब टूटता है जब एक शब्द में 10 या अधिक स्वर होते हैं (क्योंकि शब्द <C-a>के अंत में कर्सर डालता है)। tio.run/##K/v//3Cvgv7hTVyHNx1uObzj8OLoxNTM/...
DJMcMayhem

5

ब्रेकीलॉग , 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@/

इसे ऑनलाइन आज़माएं!

व्याख्या

/....
\...@/

यह ऑर्डिनल (स्ट्रिंग-प्रोसेसिंग मोड) में रैखिक कोड के लिए सिर्फ एक रूपरेखा है। कार्यक्रम को अनफॉलो करते हुए, हमें यह मिलता है:

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

जावास्क्रिप्ट (ईएस 6), 76 बाइट्स

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

परीक्षण के मामलों


4

पर्ल 5, 47 बाइट्स

45 बाइट्स कोड + 2 के लिए -pa

map$\=$_.$\,(/./g)[0..lc=~y/aeiou//-1]for@F}{

इसे ऑनलाइन आज़माएं!


क्या यह ऊपरी मामले के स्वरों के साथ काम करता है, उदाहरण के लिए "अलबामा"?
नीमी

@nimi oooh, ने +1 के लिए अद्यतन के बारे में नहीं सोचा।
डोम हेस्टिंग्स

3

जावास्क्रिप्ट (ईएस 6), 96 बाइट्स

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


बिना स्वर वाले शब्दों ( Thx) का कोई आउटपुट नहीं होना चाहिए; आपका परीक्षण मामला पूरे शब्द को आउटपुट करता है।
जस्टिन मेरिनर

@JustinMariner फिक्स्ड!
darrylyeo

3

पायथ - 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: मैंने अभी एक स्पष्टीकरण जोड़ा है
मारिया

1
@- चौराहा यहां रेगेक्स की तुलना में काफी बेहतर है। ओह, मैं देखता हूं - मेरे 2 की तुलना में आपके पास सिर्फ एक लंबोदर / नक्शा है।
डिजिटल ट्रॉमा

3

अजगर, ३१

मुझे लिखने में बहुत समय लगा, और मुझे ऐसा लगा कि शायद एक बेहतर दृष्टिकोण है, लेकिन यहाँ मेरे पास है:

_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
@ श्वेतलाना वहाँ मैंने इसे ठीक किया। jkटिप के लिए धन्यवाद ।
डिजिटल ट्रॉमा

3

ओम, 13 बाइट्स

z:αv_K_σh;0JR

व्याख्या

  • सबसे पहले (निहित) इनपुट को रिक्त स्थान पर विभाजित किया जाता है z
  • फिर एक फ़ॉरच लूप शुरू किया जाता है ( :) इसके साथ कोडब्लॉक जुड़ा होता है αv_K_σh
    • av धक्का aeiou
    • _ वर्तमान पुनरावृत्त तत्व को धकेलता है
    • Kकी घटनाओं को गिना aeiouजाता है_
    • _ तत्व फिर से
    • σhतत्व को लंबाई के स्लाइस में विभाजित करता occurencesहै और पहला तत्व लेता है।
      • प्रभावी रूप से यह पहले occurencesवर्ण लेता है
  • 0J ढेर को जोड़ दिया ''
    • 0आवश्यक है, क्योंकि यह एक तर्क यह है कि शामिल किया जाएगा की आवश्यकता है। यदि वह तर्क एक सरणी नहीं है तो यह स्टैक में शामिल हो जाता है
  • R परिणाम को उलट देता है
  • टीओएस का अंतर्निहित प्रिंट

3

रूबी , 54 59 + 1 = 55 60 बाइट्स

-p+1 बाइट के लिए ध्वज का उपयोग करता है ।

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

इसे ऑनलाइन आज़माएं!


@nimi यह अब करता है।
वैल्यू इंक

बस जिज्ञासु, क्यों -pएक बाइट के लायक है?
एरिक डुमिनील

2
@EricDuminil इस मेटा पोस्ट को देखें , लेकिन मूल रूप से, क्योंकि स्क्रिप्ट को निष्पादित करने के लिए ruby -pe '...'केवल एक और बाइट है ruby -e '...'और -eएक मान्य तरीका है।
डोम हेस्टिंग्स

3

जाप 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.

अच्छी बात है कि मैंने अपना जवाब लिखने से पहले मौजूदा उत्तरों की जाँच की: पी नाइस वन, मुझे नहीं लगता कि यह कोई छोटा होगा (हालांकि निश्चित रूप से मैं गलत हो सकता है ...)
ETHproductions

एक तरफ: जाप 2.0 में आप सैद्धांतिक रूप "%v"से \v(एकल वर्ग रेगेक्स शाब्दिक, के बराबर /\v/) बदल सकते हैं। अभी तक उपयोगी नहीं है, ज़ाहिर है, क्योंकि मैंने अभी तक v2.0 लागू नहीं किया है;)
ETHproductions

@ETHproductions, मैं दरवाजे को चलाने के लिए तैयार हो रहा था जब यह चुनौती सामने आ गई तो मैंने इसे जल्दी से लिख लिया, इसका शाब्दिक अर्थ लिया। कम शाब्दिक रूप से ऐसा करने का एक छोटा तरीका हो सकता है, हो सकता है? RegEx में उन परिवर्तनों को कुछ बाइट्स बचाने के लिए आसान होगा; उनके लिए आगे देख रहे हैं
Shaggy


2

05AB1E , 14 बाइट्स

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

इसे ऑनलाइन आज़माएं!

प्रिय 05AB1E में AEIOUaeiou ಠ_E के लिए बिल्ट नहीं है


1
रुको ... 05AB1E Japt द्वारा पीटा गया?
श्री एक्सकोडर

@ Mr.Xcoder अधिक बार लगता है जितना आप शायद सोचते हैं।
आउटगॉल्फ

1
#RʒDlžMÃg£R?12 के लिए, आप बहुत अधिक सिर्फ डुबकी की आवश्यकता को हटाते हुए कम कर सकते हैं AEIOUaeiou। इसके अलावा, बिल्ली इस काम के बिना क्यों नहीं करेगा ?? क्या आप एक स्पष्टीकरण पोस्ट कर सकते हैं, मैं अपरिचित हूंʒ
मैजिक ऑक्टोपस उर

@carusocomputing दुर्भाग्य से आउटपुट केस-संवेदी होना चाहिए।
आउटगॉल्फ

2

मैथेमेटिका, 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]}]])&

मैं वास्तव में मेथेमेटिका से परिचित नहीं हूँ, लेकिन बीच की जगह नहीं कर सकते हैं s[[i]]],और {i,Length@s}हटा दिया जाना?
श्री एक्सकोडर

हाँ, बिल्कुल, मैं चूक गया। मुझे इसे और अधिक करना चाहिए, वह भी
J42161217

क्या मैथेमेटिका में एक सूची में एक स्ट्रिंग डालने का एक तरीका है? कुछ पसंद है "aeiouAEIOU".ToCharArray()?
caird coinheringaahing

आपका मतलब है अक्षर []?
2042 पर J42161217

2

रेटिना , 49 46 बाइट्स

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

इसे ऑनलाइन आज़माएं! लिंक में परीक्षण सूट शामिल है। स्पष्टीकरण: यह .NET के संतुलन समूहों का एक अनुप्रयोग है। लुकहेड स्वरों के लिए शब्द खोजता है, जो समूह 2 में कैप्चर किए जाते हैं। समूह को तब अक्षर के रूप में पॉपअप किया जाता है, इस प्रकार शब्द में स्वरों की संख्या के बराबर अक्षरों की संख्या को कैप्चर किया जाता है। शेष शब्द और किसी भी अनुगामी स्थान को तब अनदेखा कर दिया जाता है ताकि प्रक्रिया अगले शब्द के साथ फिर से शुरू हो सके। अंत में शेष अक्षर उलटे होते हैं।


2

सी # (.NET कोर) , 144 बाइट्स

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

इसे ऑनलाइन आज़माएं!

सबसे बुरी बात यह है कि stringC # में रिटर्न को उलट देना है IEnumerable<char>जिसे आपको वापस से एक में बदलना है string



2

पायथन 3 , 83 81 79 77 बाइट्स

  • श्री एक्सकोडर ने 2 बाइट्स बचाए
  • ग्रिफिन ने 2 बाइट्स बचाए: पायथन 3 से 2 तक स्विच करें
  • 2 बाइट्स को बचाया: लैम्ब्डा का उपयोग
lambda z:''.join(i[:sum(y in'aeiouAEIOU'for y in i)]for i in z.split())[::-1]

इसे ऑनलाइन आज़माएं!



1
अजगर 2 में परिवर्तन और आपको ()प्रिंट की आवश्यकता नहीं है
ग्रिफिन

1
@Griffin पायथन 2 में, आपको raw_input()इसके बजाय input()4 बाइट बर्बाद करना होगा।
श्री Xcoder

1
@ Mr.Xcoder आप केवल उद्धरण के साथ इनपुट क्यों नहीं कर सकते हैं?
ग्रिफिन

1
@Griffin आह, सही है। यह अंततः 2 बाइट्स बचाएगा।
श्री एक्सकोडर

2

जावा 8 , 171 151 बाइट्स

-20 बाइट्स @ लुकास रोटर के लिए धन्यवाद

मुझे लगता है कि यह अभी भी कुछ गोल्फ की जरूरत है ... मुझे टिप्पणी में पता है अगर आपके पास कोई सुझाव है।

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

इसे ऑनलाइन आज़माएं!


जावा (?i)regexs में मामले की अनदेखी का समर्थन करता है । इसलिए (?i)[aeiou]काम भी करना चाहिए।
लुकास रोटर

आप {}लूप के ब्रैकेट को भी हटा सकते हैं , क्योंकि इसमें केवल एक स्टेटमेंट निहित है।
लुकास रॉटर

रेगेक्स स्ट्रिंग की लंबाई घटाने के बजाय आप केवल ^स्वरों की मात्रा का पता लगाने के लिए उपयोग कर सकते हैं: z+=w.substring(0,w.replaceAll("(?i)[^aeiou]","").length());
लुकास रोटर


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

क्लोजर, 96 94 बाइट्स

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

वैसे यह लंबाई काफी हास्यास्पद है। mapcatदो बाइट्स बचाए।


1

स्विफ्ट 3, 240 बाइट्स

यह एक फ़ंक्शन है जिसका उपयोग किया जा सकता है 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()))}

आईबीएम सैंडबॉक्स में इसे आज़माएं!


2
वास्तव में, ऐसा लगता है कि आपके पास इस सबमिशन के लिए सबसे छोटा स्विफ्ट कोड संभव है। मैंने इसे स्विफ्ट में भी हल किया है, और 240 बाइट्स भी प्राप्त किया है! बहुत बढ़िया!
श्री एक्सकोडर
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.