戴上帽子的猫


15

受到即将到来的冬季狂欢活动的启发

客体

添加一顶帽子 ^´`在输入的每一个字单韵母。

规则

  • hatvowel必须随意选择。每个帽子必须以相同的概率(33%)出现,并且在单词中的有效元音内,元音必须具有相同的概率(如果单词具有2个有效元音,则每个可能性必须为50%)-或最接近您的语言所拥有的
  • AEIOUaeiou视为元音(对不起y
  • 输入中带有帽子的元音请勿干扰规则(您可以将其视为辅音)
  • 如果输入没有元音,则不会被修改
  • 必须保留大写。

例子

winter> wintér
bash> bâsh
rhythm> rhythm
rng ftw> rng ftw
cat in the hat> cât ìn thê hát
dès> dès
tschüss> tschüss
principî> prìncipî
PROGRAMMING PUZZLES & code golf>PROGRÂMMING PÚZZLES & codé gòlf

获奖

这是所以最短的代码胜出


“随机(或您的语言拥有的最接近的语言)”,因此,如果我的语言没有随机命令,我可以做确定性的事情?
致命

@LuisMendo更好吗?
罗德

@Fatalize甚至不是get the current time随机的?
罗德

@Rod我没有任何语言介意。我只是说你必须强加它是随机的(以及什么样的随机性),否则根本没有意义。
Fatalize 2016年

规范说我们必须添加^或反引号,但是示例tschuss显示了变音符号。是否需要变音符号?
水平河圣

Answers:


1

Perl 6,101个字节

~*.words.map: {(my$p=m:ex:i/<[aeiou]>/».to.pick)~~Mu:D??~S/.**{$p}<(/{("\x300".."\x302").pick}/!!$_}

尝试一下

展开:

~      # stringify (join with spaces)
*\     # Whatever lambda (input parameter)
.words # split into words

.map:  # for each word

{ # bare block lambda with implicit parameter 「$_」
  (

    my $p =

    m
    :exhaustive               # all possible matches
    :ignorecase
    /<[aeiou]>/\              # match a vowel

    ».to.pick                 # pick one of the positions

  ) ~~ Mu:D                   # is it defined ( shorter than 「.defined」 )

  ??                          # if 「$p」 is defined

    ~                         # stringify

    S/
      . ** {$p}               # match 「$p」 positions

      <(                      # ignore them
    /{
      ("\x300".."\x302").pick # pick one of the "hats" to add
    }/


  !!                          # if 「$p」 is not defined    
    $_                        # return the word unchanged
}

2

C#,273个 267字节

using System.Linq;A=s=>{var r=new System.Random();var a=s.Split(' ');return string.Join(" ",a.Select(w=>w.Select((c,i)=>"AEIOUaeiou".Any(d=>c==d)?i:-1).Where(x=>x>=0).ToList()).Select((l,i)=>l.Any()?a[i].Insert(l[r.Next(l.Count)]+1,""+(char)r.Next(768,771)):a[i]));};

复制演示

我真的觉得自己在作弊,因为我仍然在通过组合字符创建的重音元音中添加了帽子。如果不可接受,请告诉我,以便添加样板代码声明此答案不具有竞争力。

在每个输入单词(如果有)的随机元音之后,这个东西在U + 0300U + 0301U + 0302之间添加一个随机字符。

非高尔夫(仅lambda身体)

var r=new System.Random();
// Split sentence to array of words
var a=s.Split(' ');
// Join the (hat-ed) words back to sentence
return string.Join(
    " ",
    // Select an IEnum of list of integers indicating the positions of vowels
    a.Select(w=>
        w.Select((c,i)=>
            // If it's vowel, return position (>=0), else return -1
            "AEIOUaeiou".Any(d=>c==d)?i:-1
        // Filter vowels only
        ).Where(x=>x>=0)
        .ToList()
    // Convert each list of integers to hat-ed words
    ).Select((l,i)=>
        l.Any()
            // Insert "something" into the word...
            ?a[i].Insert(
                // ...at the position after a random vowel in that word...
                l[r.Next(l.Count)]+1,
                // "something" is a random integer in [0x0300, 0x0303), then casted to UTF16 i.e. [U+0300, U+0302]
                ""+(char)r.Next(768,771))
            // List is empty => just return original word
            :a[i]));

1

Mathematica,226个字节

Join@@((p=Position[w=#,Alternatives@@(v=Characters@"aeiouAEIOU")];If[p!={},i=#&@@RandomChoice@p;w[[i]]=FromCharacterCode[ToCharacterCode["àèìòùÀÈÌÒÙ"][[#&@@Position[v,w[[i]]]]]+RandomInteger@2]];w)&/@Split[#,{##}~FreeQ~" "&])&

未命名函数,将字符列表作为输入并返回字符列表。易于阅读的版本,也略有差异:

 1  v = Characters["aeiouAEIOU"];
 2  a = ToCharacterCode["àèìòùÀÈÌÒÙ"];
 3  Join @@ (
 4    (p = Position[w = #, Alternatives @@ v]; 
 5      If[p != {},
 6        i = First[RandomChoice[p]]; 
 7        w[[i]] =
 8          FromCharacterCode[
 9            a[[ First[ Position[ v, w[[i]] ] ] ]] + RandomInteger[2]
10          ]
11        ]; w
12    ) &
13  ) /@ Split[#1, FreeQ[{##1}, " "] &] &

第13行在所有空格处将输入拆分为单词(字符的子列表);每个字都由第4-12行定义的函数进行运算,结果在第3行的单个列表中再次结合在一起。

第4行设置p到索引列表,指示单词的哪些字符w是元音。如果有任何元音(第5行),我们会随机选择一个这样的索引i(第6行),然后将单词的单个字符重置为新字符(第7-10行)。最后,我们输出(可能已修改)的单词w

要选择新字符,我们找到要替换的元音在字符串中的位置,v然后从中选择相应的字符代码a。但是要随机选择三顶帽子,我们需要使用该代码并在转换回字符之前在0和2之间添加一个随机整数(第9行)。(幸运的是,所有带帽子的元音都以连续三重UTF-8字符代码的形式出现。)


1

Python 3,170个字节

from random import *
c=choice
print(' '.join([w,w[:i]+c('̀́̂')+w[i:]][i>0]for w in input().split()for i in[c([j+1 for j,x in enumerate(w)if x in 'aeiouAEIOU']or[0])]))

取消高尔夫:

from random import choice
print(' '.join([
                   w,  # Don't modify the word if no vowels were found

                   w[:i] + choice('̀́̂') + w[i:]
               ][i > 0]
               for w in input().split()
                   for i in [choice([j + 1 for j, x in enumerate(w) if x in 'aeiouAEIOU']
                                    or [0])  # choice requires a non-empty sequence
                             ]))

1
import和之间不需要空格*
Xcoder先生17年

j+1 for可以j+1for
乔纳森·
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.