扫一下元音!


18

注意:标题是故意拼写错误的。

给定字符串s,交换每2个单词的第一个元音运行。对于这个挑战,y被认为是元音。

例如,给定输入“ Great day sir”:

1. Input: "great day sir"
2. Identify pairs of words: "[great day] [sir]" (No word for sir to pair with)
3. Identify the first vowel runs in each word: "[gr[ea]t d[ay]] [s[i]r]"
4. Swap the vowel runs in each pair: "[gr[ay]t d[ea]] [s[i]r]"
5. Return/print: "grayt dea sir"

当有不同长度的元音运行时,您仍然需要交换整个运行。当一个单词有多个元音运行时,您仍然只交换第一个。当一对单词的第一个或第二个单词没有元音时,则不要将这些单词的元音交换。

您可能会假设输入仅包含一种字母和文字空间或另一种常量定界符。

I / O的标准方法和标准漏洞适用。领导/追踪一切都可以。

测试用例:

Input -> Output

"great day sir" -> "grayt dea sir"
"ppcg is the best" -> "ppcg is the best" (When there is no vowel to swap, don't swap vowels."
"this is a test case" -> "this is e tast case"
"loooooooooooooong word" -> "long woooooooooooooord"
"great night" -> "grit neaght"
"anything goes" -> "oenything gas"
"qwrtpsdfghjklzxcvbnm aaaaaaaa hi there" -> "qwrtpsdfghjklzxcvbnm aaaaaaaa he thire"
"this is a long test case in case you could not tell" -> "this is o lang tast cese an cise ou cyould net toll"

1
对于那些可以看到已删除帖子的人,沙箱帖子在此处
SparklePony同志17年

1
如果第一个单词没有元音,可以交换第二个和第三个单词的元音吗?还是元音只能在两个单词的间隔之间交换?例如,应ppcg is awesome成为ppcg is awesomeppcg as iwesome
DJMcMayhem

@DJMcMayhem元音只能在两个单词的运行之间交换。我会编辑。
SparklePony同志17年

我相信输出this is a long test case in case you could not tell应该是this is o lang tast cese an cise ou cyould net toll,因为元音运行youou将被交换。
Bashful Beluga

@BashfulBeluga是的,我的错。我会解决。
“ SparklePony同志” 17年

Answers:


9

V42,41个字节

ò2Eá
òͨ[aeiouy]«©¨ƒ ƒ©¨[aeiouy]«©/³²±
Íî

在线尝试!

十六进制转储:

00000000: f232 45e1 0af2 cda8 5b61 6569 6f75 795d  .2E.....[aeiouy]
00000010: aba9 a883 2083 a9a8 5b61 6569 6f75 795d  .... ...[aeiouy]
00000020: aba9 2fb3 b2b1 0acd ee                   ../......

说明:

ò       ò                                   " Recursively:
 2E                                         "   Move to the end of two words forward
   á<cr>                                    "   And append a newline

这会将两个单词的所有组放在各自的行上,例如:

this is
a long
test case
in case
you could
not tell

现在,我们运行一些花哨的正则表达式魔术:

Í                                           " Globally substitute
 ¨[aeiouy]«©                                "   A vowel (capture group 1)
            ¨<131>                          "   Followed by as few characters as possible, then a space
                   <131>©                   "   Followed by as few characters as possible (capture group 2)
                         ¨[aeiouy]«©        "   Followed by a vowel again
                                    /       " With:
                                     ³²±    "   Capture groups '3', '2', '1'
Í                                           " Remove all:
 î                                          "   Newlines

您的正则表达式不需要两个元音组之间的单词结尾。 在线尝试!
nmjcman101

@ nmjcman101您在看我的旧版本吗?因为这就是我现在所拥有的
DJMcMayhem

我的TIO链接没有解决任何问题,我只是更改了输入。它奇怪地交换字母。
nmjcman101

@ nmjcman101啊,我知道了。立即修复!
DJMcMayhem

6

Japt39 37字节

他们说那会很丑,但是我没有听……这是:

¸ò ®efQ="%y+" ?Z£XrQZg°T fQP PÃ:ZÃc ¸

在线测试!

说明

 ¸  ò ® efQ="%y+" ?Z£    XrQ    Zg° T fQ    P PÃ :ZÃ c ¸
UqS ò mZ{Zef"%y+" ?ZmXYZ{Xr"%y+"Zg++T f"%y+"P P} :Z} c qS
             Implicit: U = input string, such as     "abc def ghi jkl mno"
UqS          Split on spaces, splitting into words.  ["abc","def","ghi","jkl","mno"]
ò            Group into runs of two items.           [["abc","def"],["ghi","jkl"],["mno"]]
mZ{          For each pair Z:
 Zef"%y+"?     If not every item contains a run of vowels (%y = [AEIOUYaeiouy]),
 :Z            return Z.                             [              ["ghi","jkl"]        ]
 ZmXYZ{        Otherwise, for each item X in Z:
  Xr"%y+"        Replace each run of vowels with
  Zg++T           the item at the next index in Z,   [["def","abc"]               ["mno"]]
  f"%y+"P         but only the first run of vowels.  [["e",  "a"  ]               ["o"  ]]
  P              Replace only for the first match.   [["ebc","daf"]               ["mno"]]
 }
}                                                    [["ebc","daf"],["ghi","jkl"],"mno"]]
c            Flatten back into a single array.       ["ebc","def","ghi","jkl","mno"]
qS           Re-join on spaces.                      "ebc daf ghi jkl mno"
             Implicit: output result of last expression

5

的JavaScript(ES6),62个 106 98 101字节

s=>s.match(/(\w+)( (\w+))?/g).map(m=>m.replace(/([aeiouy]+)(\w* \w*?)([aeiouy]+)/g,'$3$2$1')).join` `


4

视网膜,65字节

((\w*?)([aeiouy]+)(\w* \w*?)([aeiouy]+)|(\w+ ))(\w*)
$2$5$4$3$6$7

在线尝试!包括测试用例。我想使用条件组引用,但无法以66个字节工作,更不用说65个或更少的字节了。


4

视网膜,50字节

\S+ \S+ 
$&¶
%O$^`(?<=\b[^aeiouy]*)[aeiouy]+
$`
¶

在线尝试!

−2字节感谢Martin。

  • 第一步是将每个单词对拆分为自己的行(是换行符)。这使我们可以使用.*在两个单词内。
  • 接下来,对于每一行,我们在每个单词中找到第一个元音块,并按位置降序对其进行排序。

我尝试删除双打,[aeiouy]+但无法获得经济利益。
Kobi

1
这是稍微短了分类阶段来交换运行:tio.run/...
马丁安德

@MartinEnder-好人!我无法整理工作。我尝试了另一种删除[aeiouy]重复项的版本,但无法进行重复操作。我认为它可能与您的建议配合使用:tio.run/…–
Kobi

3

Python 2 2,148个字节

from re import*
v="([aeiouy]+)"
print sub(r"(\w+)(?: (\w+))?",lambda m:sub(v+"(.* .*?)"+v,lambda g:''.join(g.groups()[::-1]),m.group()),raw_input())

在线尝试!

代码高尔夫变得令人上瘾!

分割成对的单词,然后抓住2组元音和中间的字符串,颠倒顺序并将其用作替代


3

哈斯克尔177个 173 171 169字节

unwords.s.words
s(x:y:z)=maybe[x,y]id(do(a,b)<-k x;(c,d)<-k y;j[b c,d a])++s z
s x=x
v=(`elem`"aeiouy")
j=Just
k s=do(a,(x:y,r))<-j$span v<$>break v s;j(x:y,\n->a++n++r)

在线尝试!

这是以下朴素解决方案的直接缩短,因此这里应该有一些更好的方法:

swapvowels :: String -> String
swapvowels = unwords . swapPairs . words

swapPairs :: [String] -> [String]
swapPairs (word1:word2:rest) =
   case (,) <$> extractVowels word1 <*> extractVowels word2 of
     Just ((vowels1, rebuild1), (vowels2, rebuild2))
       -> [rebuild1 vowels2, rebuild2 vowels1] ++ swapPairs rest
     Nothing -> [word1,word2] ++ swapPairs rest
swapPairs rest = rest

extractVowels :: String -> Maybe (String, String -> String)
extractVowels s = do
    let isVowel l = l `elem` "aeiouy"
    (a,b) <- Just $ break isVowel s 
    (w@(_:_),r) <- Just $ span isVowel b 
    return (w, \n -> a ++ n ++ r)

2

Java(OpenJDK 8)363 304 + 25字节

-34个字节,感谢@KevinCruijssen

打高尔夫球:

l->{String s[]=l.split(" "),a,b;Pattern p=Pattern.compile("[aeiouy]+");for(int i=0;i<s.length-1;i+=2){Matcher m=p.matcher(s[i]),n=p.matcher(s[i+1]);a=m.find()?m.group():null;b=n.find()?n.group():null;if(a!=null&b!=null){s[i]=s[i].replaceFirst(a,b);s[i+1]=s[i+1].replaceFirst(b,a);}}return l.join(" ",s);}

在线尝试!

取消高尔夫:

String swapVowels(String line) {
    String[] parts = line.split(" ");
    Pattern pattern = Pattern.compile("([aeiouy]+)");
    for (int i = 0; i < parts.length - 1; i += 2) {
        Matcher matcherL = pattern.matcher(parts[i]), matcherR = pattern.matcher(parts[i + 1]);
        String vowelRunL = matcherL.find() ? matcherL.group() : null, vowelRunR = matcherR.find() ? matcherR.group() : null;
        if (vowelRunL != null & vowelRunR != null) {
            parts[i] = parts[i].replaceFirst(vowelRunL, vowelRunR);
            parts[i + 1] = parts[i + 1].replaceFirst(vowelRunR, vowelRunL);
        }
    }
    return String.join(" ", parts);
}

2
您可以删除输入((l)->l->)周围的括号。您可以添加import java.util.regex.*;到字节数,然后删除所有其他java.util.regex.。您可以在正则表达式("([aeiouy]+)"-> "[aeiouy]+")中删除括号。您可以更改String[]s=l.split(" ");String s[]=l.split(" "),a,b;,然后可以删除Stringfor循环内部;您可以更改String.join(" ",s);l.join(" ",s);这是所有的结合。[ 329个字节 ]
Kevin Cruijssen

@KevinCruijssen确实!编辑,谢谢!:-)
Bashful Beluga



1

Python 3中198个 196 192字节

  • 保存了6个字节:感谢Zachary Tif(m and n)如果m和n并删除了正则表达式字符串的多余r,则索引i从1开始而不是0
from re import*
s=search
a=input().split()
v="[aeiouy]+"
j=1
while j<len(a):
 i=j-1;m=s(v,a[j]);n=s(v,a[i])
 if m and n:a[i]=sub(v,m[0],a[i],1);a[j]=sub(v,n[0],a[j],1)
 j+=2
print(' '.join(a))

在线尝试!


1
我认为您可以减少程序的三个字节:一个通过删除字符串前的r,另一个通过更改i+1<len(a)i<=len(a),第三个通过更改if(m and n)if m and n
扎卡里

1
谢谢。但i+1<len(a)不能改变i<=len(a),否则它会尝试评估a[j],即a[i+1]i=len(a)并造成index out of range错误:
officialaimm

对不起,我读的是i<len(a)+1,哎呀!
扎卡里

1
这行得通吗?repl.it/IlX1
扎卡里

1
在某些行的末尾有多余的空格,我计算出192个字节。
扎卡里
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.