An หรือ An An?


21

ในภาษาอังกฤษมีความสนุกสนานและความแตกต่างอย่างง่ายระหว่างanและa: คุณใช้anเมื่อก่อนหน้าคำที่ขึ้นต้นด้วยเสียงสระและaเมื่อคำนั้นเริ่มต้นด้วยเสียงพยัญชนะ

เพื่อความเรียบง่ายในการท้าทายนี้ให้anนำหน้าคำที่ขึ้นต้นด้วยสระ ( aeiou) และaนำหน้าคำที่ขึ้นต้นด้วยพยัญชนะ

อินพุต

สตริงประกอบด้วยอักขระ ASCII เพียงพิมพ์ที่มี[?]ปรากฏอยู่ในสถานที่ที่คุณต้องเลือกที่จะใส่หรือan จะปรากฏต่อหน้าคำเสมอ คุณสามารถสันนิษฐานได้ว่าประโยคนั้นจะถูกต้องตามหลักไวยากรณ์และจัดรูปแบบเหมือนปกติa[?]

เอาท์พุต

สตริงอินพุตที่[?]ถูกแทนที่ด้วยคำที่เหมาะสม ( anหรือa) คุณต้องกังวลเกี่ยวกับตัวพิมพ์ใหญ่!

เมื่อใดที่จะลงทุน

ใช้อักษรตัวพิมพ์ใหญ่คำถ้าไม่มีตัวอักษรนำหน้า (เป็นคำแรกในอินพุต) หรือหากเป็นตัวอักษรตัวใดตัวหนึ่ง.?!ตามด้วยช่องว่าง

ตัวอย่าง

Input: Hello, this is [?] world!
Output: Hello, this is a world!

Input: How about we build [?] big building. It will have [?] orange banana hanging out of [?] window.
Output: How about we build a big building. It will have an orange banana hanging out of a window.

Input: [?] giant en le sky.
Output: A giant en le sky.

Input: [?] yarn ball? [?] big one!
Output: A yarn ball? A big one!

Input: [?] hour ago I met [?] European.
Output: A hour ago I met an European.

Input: Hey sir [Richard], how 'bout [?] cat?
Output: Hey sir [Richard], how 'bout a cat?

นี่คือดังนั้นรหัสที่สั้นที่สุดในหน่วยไบต์ชนะ!


โอเคขอบคุณ. เราสามารถสมมติว่าไม่มีอินพุตจะมีช่องว่างพิเศษระหว่างคำ [?]และถึง?
DJMcMayhem

8
a / a ต้องเป็นตัวพิมพ์ใหญ่ในระหว่างการป้อนข้อมูลเมื่อเริ่มต้นประโยคหรือไม่? ("นี่คือการทดสอบ [?]. [?] การทดสอบ") ถ้าเป็นเช่นนั้นเครื่องหมายวรรคตอนจะจบลงด้วยประโยคอะไร? สิ่งที่เกี่ยวกับประโยคในเครื่องหมายคำพูดหรือวงเล็บ? หรือตัวย่อที่ลงท้ายด้วยจุด ("เช่น [?] อินพุตเช่นนี้")? กฎการใช้อักษรตัวพิมพ์ใหญ่มีกรณีพิเศษแปลก ๆ มากมายดังนั้นโปรดระบุไว้อย่างชัดเจนว่าโปรแกรมของเราทำอะไรหรือไม่จำเป็นต้องจัดการ
DLosc

1
คุณช่วยอธิบายให้ชัดเจนว่าควรใช้อักษรตัวใหญ่เมื่อใด ตัวละครตัวแรก?
DJMcMayhem

31
คุณควรเพิ่มกรณีทดสอบ[?] hour ago I met [?] European.เพื่อทำให้ทุกคนประจบประแจง
Martin Ender

1
ตอนนี้เราต้องมี[?] hour ago I met [?] horse.
บีกเกอร์

คำตอบ:


6

V , 41 ไบต์

ÍãÛ?Ý ¨[aeiou]©/an
ÍÛ?Ý/a
Í^aü[.!?] a/A

ลองออนไลน์! ซึ่งสามารถใช้ในการตรวจสอบกรณีทดสอบทั้งหมดได้อย่างสะดวกโดยไม่ต้องมีจำนวนไบต์เพิ่ม

สิ่งนี้ใช้ประโยชน์จาก "การบีบอัด Regex" ของ V มันใช้อักขระที่ไม่สามารถพิมพ์ได้จำนวนมากดังนั้นนี่คือ hexdump:

0000000: cde3 db3f dd85 20a8 5b61 6569 6f75 5da9  ...?.. .[aeiou].
0000010: 2f61 6e0a cddb 3fdd 2f61 0acd 5e61 fc5b  /an...?./a..^a.[
0000020: 2e21 3f5d 2093 612f 41                   .!?] .a/A

แต่น่าเสียดายที่ OP กล่าวว่า "คุณไม่ต้องกังวลเกี่ยวกับโครงสร้างเงินทุน!" (เน้นที่เหมือง)
El'endia Starman

1
@ El'endiaStarman โอ้ฉันเข้าใจผิดว่า ฉันสามารถแก้ไขได้ แต่ฉันไม่รู้ว่าจะใช้ประโยชน์อะไรเนื่องจาก OP ไม่ได้ระบุ
DJMcMayhem

@ El'endiaStarman แก้ไขแล้ว
DJMcMayhem

7

Perl, 48 ไบต์

ที่บันทึกไว้ 1 ไบต์เนื่องจากTon Hospel

#!perl -p
s;\[\?];A.n x$'=~/^ [aeiou]/i^$"x/[^.?!] \G/;eg

นับ Shebang เป็นหนึ่งจะถูกนำเข้าจาก stdin

คำอธิบาย

#!perl -p               # for each line of input, set $_, auto-print result

s;                      # begin regex substitution, with delimiter ;
\[\?]                   # match [?] literally, and replace with:
;
A.n x$'=~/^ [aeiou]/i   # 'A', concatenate with 'n' if post-match ($')
                        #   matches space followed by a vowel
^$"x/[^.?!] \G/         # if the match is preceded by /[^.?!] /, xor with a space
                        #   this will change An -> an

;eg                     # regex options eval, global

ตัวอย่างการใช้งาน

$ echo Hello, this is [?] world! | perl a-an.pl
Hello, this is a world!

$ echo How about we build [?] big building. It will have [?] orange banana hanging out of [?] window. | perl a-an.pl
How about we build a big building. It will have an orange banana hanging out of a window.

$ echo [?] giant en le sky. [?] yarn ball? | perl a-an.pl
A giant en le sky. A yarn ball?

$ echo [?] hour ago I met [?] European. | perl a-an.pl
A hour ago I met an European.

2
คุณช่วยอธิบายเรื่องนี้ได้ไหม
sudee

1
การสนับสนุนการใช้อักษรตัวพิมพ์ใหญ่หลังจาก/[.?!]/ตามด้วยอวกาศหายไป
Ton Hospel

1
@TonHospel 10 ชั่วโมงที่แล้วปัญหาไม่ได้พูดถึงเรื่องนี้
โม่

2
โอเคการเปลี่ยนสเป็คในทันทีนั้นไม่ยุติธรรมเลย PS: ฉันชอบใช้\Gbackwarsds PPS สั้นกว่าเล็กน้อย:s;\[\?];A.n x$'=~/^ [aeiou]/^$"x/[^.?!] \G/;eg
Ton Hospel

1
@sudee อัปเดตเพื่อรวมคำอธิบาย
primo

7

Ruby, 78 72 ไบต์

->s{s.gsub(/(^|\. )?\K\[\?\]( [aeiou])?/i){"anAn"[$1?2:0,$2?2:1]+"#$2"}}
  • บันทึกแล้ว 6 ไบต์ขอบคุณ@Jordan

Ungolfed

def f(s)
    s.gsub(/(^|\. )?\[\?\]( [aeiou])?/i) do |m|
        capitalize = $1
        vowel = $2
        replacement = if vowel then
            capitalize ? "An" : "an"
        else
            capitalize ? "A" : "a"
        end
        m.sub('[?]', replacement)
    end
end

2
"anAn"[...]ฉลาดจริงๆ 👍🏻คุณสามารถบันทึกได้สองสามไบต์โดยการข้ามด้านในsub:s.gsub(/(^|\. )?\K\[\?\] ([aeiou])?/i){"anAn"[$1?2:0,$2?2:1]+" #$2"}
จอร์แดน

6

PHP, 207 ไบต์

foreach(explode("[?]",$s)as$i=>$b){$r=Aa[$k=0|!strstr(".!?",''==($c=trim($a))?".":$c[strlen($c)-1])].n[!preg_match("#^['\"´`\s]*([aeiou]|$)#i",$d=trim($b))];echo$i?$r.$b:$b;$a=$i?''==$d?a:$b:(''==$d?".":a);}

ฉันชอบวิธีแก้ปัญหาที่สมบูรณ์มากขึ้นเป็นครั้งคราว ...
แต่ฉันต้องยอมรับว่านี่มันเกินความจริงเล็กน้อยแม้ว่ามันจะไม่เสร็จสิ้นก็ตาม

บันทึกเป็นไฟล์เรียกใช้php <filename>ด้วยอินพุตจาก STDIN

กรณีทดสอบ

How about we build [?] big building ... with [?] orange banana hanging out of [?] window.
=>  How about we build a big building ... with an orange banana hanging out of a window.

Hello, this is [?] world!               =>  Hello, this is a world!
Should I use [?] '[?]' or [?] '[?]'?    =>  Should I use an 'an' or an 'a'?
[?] elephant in [?] swimsuit.           =>  An elephant in a swimsuit.

How I met your moth[?].                 =>  How I met your motha.
b[?][?][?] short[?]ge!                  =>  banana shortage!

ชำรุด

foreach(explode("[?]",$s)as$i=>$b)
{
    $r=
        // lookbehind: uppercase if the end of a sentence precedes
        Aa[$k=0|!strstr(".!?",''==($c=trim($a))?".":$c[strlen($c)-1])]
        .
        // lookahead: append "n" if a vowel follows (consider quote characters blank)
        n[!preg_match("#^['\"´`\s]*([aeiou]|$)#i",$d=trim($b))]
    ;
    // output replacement and this part
    echo$i?$r.$b:$b;
    // prepare previous part for next iteration
    $a=$i               // this part was NOT the first:
        ?   ''==$d
            ? a             // if empty -> a word ($r from the previous iteration)
            : $b            // default: $b
        :  (''==$d      // this WAS the first part:
            ? "."           // if empty: end of a sentence (= uppercase next $r)
            : a             // else not
        )
    ;
    // golfed down to `$a=!$i^''==$d?a:($i?$b:".");`
}

3
โหวตขึ้นสำหรับ "Banana ขาดแคลน"! LOL
MonkeyZeus

@MonkeyZeus: ลอง[?][?][?]s [?]lert!
Titus

ทั้งหมดที่ฉันนึกได้คือ Donkey Kong อกหักกังวลเกี่ยวกับปัญหาการขาดแคลนในขณะนี้: (
MonkeyZeus

5

Minkolang 0.15 , 75 ไบต์

od4&r$O."]?["30$Z3&00w4X"Aa"I2-"Aa ."40$Z,*2&$rxr$O" aeiou"od0Z1=3&"n"r5X$r

ลองที่นี่!

คำอธิบาย

od                                                                    Take character from input and duplicate (0 if input is empty)
  4&                                                                  Pop top of stack; jump 4 spaces if not 0
    r$O.                                                              Reverse stack, output whole stack as characters, and stop.

    "]?["                                                             Push "[?]" on the stack
         30$Z                                                         Pop the top 3 items and count its occurrences in the stack
              3&                                                      Pop top of stack; jump 3 spaces if not 0
                00w                                                   Wormhole to (0,0) in the code box

                3X                                                    Dump the top 3 items of stack
                  "Aa"                                                Push "aA"
                      I2-                                             Push the length of stack minus 2
                         "Aa ."40$Z,                                  Push ". aA" and count its occurrences, negating the result
                                    *                                 Multiply the top two items of the stack
                                     2&$r                             Pop top of stack and swap the top two items if 0
                                         x                            Dump top of stack
                                          r                           Reverse stack
                                           $O                         Output whole stack as characters
                                             " aeiou"                 Push a space and the vowels
                                                     od               Take a character from input and duplicate
                                                       0Z             Pop top of stack and count its occurrences in the stack (either 1 or 2)
                                                         1=           1 if equal to 1, 0 otherwise
                                                           3&         Pop top of stack; jump 3 spaces if not 0
                                                             "n"      Push "n" if top of stack is 0

                                                             r        Reverse stack
                                                              5X      Dump top five items of stack
                                                                $r    Swap top two items of stack

โปรดทราบว่าเนื่องจาก Minkolang เป็นแบบ Toroidal เมื่อตัวนับโปรแกรมเลื่อนออกจากขอบด้านขวามันจะปรากฏขึ้นทางด้านซ้าย สามารถเล่นกอล์ฟได้อย่างแน่นอน แต่เนื่องจากฉันต้องเพิ่ม 21 ไบต์เนื่องจากสเป็คฉันอาจไม่ลอง


6
ฉันเป็นคนเดียวที่ต้องการไปเล่นตื่นเต้นหลังจากอ่านคำอธิบายนั้น?
Magic Octopus Urn

3

JavaScript (ES6), 90 86 87 85

แก้ไขอีกครั้งเมื่อข้อมูลจำเพาะของการเปลี่ยนเป็นตัวพิมพ์ใหญ่เปลี่ยนไป (มีเหตุผลมากกว่าตอนนี้)

แก้ไขอีก 1 ไบต์บันทึกขอบคุณ @Huntro

แก้ไขเพิ่มเติมอีก 2 ไบต์เพื่อจัดการคำพูดและสิ่งที่ชอบตามที่ IsmaelMiguel ชี้ให้เห็น (แม้ว่าฉันจะไม่ทราบว่ามีการร้องขอโดย op) โปรดทราบว่าก่อนหน้านี้ฉันนับ 86 ไบต์ แต่เป็น 85

พยายามที่จะปฏิบัติตามกฎการใช้อักษรตัวใหญ่ที่ระบุไว้ในเหตุการณ์ความคิดเห็นหากมันไม่สมบูรณ์ (อย่างน้อย)

x=>x.replace(/([^!?.] )?\[\?](\W*.)/g,(a,b,c)=>(b?b+'a':'A')+(/[aeiou]/i.test(c)?'n'+c:c))

ทดสอบ

f=x=>x.replace(/([^!?.] )?\[\?](\W*.)/g,(a,b,c)=>(b?b+'a':'A')+(/[aeiou]/i.test(c)?'n'+c:c))

function go() {
  var i=I.value, o=f(i)
  O.innerHTML = '<i>'+i+'</i>\n<b>'+o+'</b>\n\n'+O.innerHTML 
}

go()
#I { width:80% }
<input value='How about we build [?] big building. It will have [?] orange banana hanging out of [?] window.' id=I><button onclick='go()'>GO</button><pre id=O></pre>


ไม่ควร[?][?]ให้Ana? และไม่ควร[?][?] a.ผลิตAna a.?
Ismael Miguel

@IsmaelMiguel ฉันไม่เข้าใจสิ่งที่คุณหมายถึง แต่ต่อไป[?] will always appear before a word. You can assume that the sentence will be grammatically correct and formatted like normal.
edc65

เข้าใจแล้ว แต่รหัสของคุณให้ผลลัพธ์แปลก ๆ สำหรับ[?] "[?]".( An "A"ราคานั้นไม่เกี่ยวข้อง) และสำหรับ[?] "A".(มันใช้งานได้ดีสำหรับ[?] A.)
Ismael Miguel

@IsmaelMiguel [?] "[?]"ไม่ถูกต้อง [?] will always appear before a word และ "[?]" ไม่ใช่คำ
edc65

2
การหลบหนีของวินาที]นั้นไม่จำเป็น /(\w )?\[\?](\W*.)/g
Huntro

2

แบตช์ 136 ไบต์

@set/ps=
@for %%v in (a e i o u)do @call set s=%%s:[?] %%v=an %%v%%
@set s=%s:[?]=a%
@if %s:~0,1%==a set s=A%s:~1%
@echo %s:. a=. A%

รับบรรทัดอินพุตบน STDIN


2

PHP, 100 92 ไบต์

<?=preg_filter(["/\[\?]\K(?= [aeiou])/i","/([.?!] |^)\K\[\?]/","/\[\?]/"],[n,A,a],$argv[1]);

มันเป็นไปได้ที่จะเล่นกอล์ฟต่อไปในการแสดงออกปกติ

ให้การแจ้งเตือนเกี่ยวกับค่าคงที่ไม่ได้กำหนด แต่ยังคงใช้งานได้

แก้ไข: บันทึกได้ 8 ไบต์ขอบคุณ primo


นอกจากนี้ควรเป็นไปได้ที่จะทำให้อาร์เรย์สำรองของคุณเปลี่ยนไป[n,A,a]โดยใช้การยืนยันแบบมองไปรอบ ๆ ( \Kและ(?= ))
โม่

2

Python 3.5.1, 153 147 124 Bytes

*s,=input().replace('[?]','*');print(*[('a','A')[i<1or s[i-2]in'.?!']+'n'*(s[i+2]in 'aeiouAEIOU')if c=='*'else c for i,c in enumerate(s)],sep='')

อินพุต:

[?] apple [?] day keeps the doctor away. [?] lie.

ผลผลิต:

An apple a day keeps the doctor away. A lie.

123 Bytes version - สิ่งนี้ไม่ได้จัดการกับกฎการใช้อักษรตัวพิมพ์ใหญ่

s=list(input().replace('[?]','*'));print(*['a'+'n'*(s[i+2]in 'aeiouAEIOU')if c=='*'else c for i,c in enumerate(s)],sep='')

ไอเดียมัน!


1
ยินดีต้อนรับสู่ Codegolf คุณสามารถใช้;และเล่นกอล์ฟได้
ABcDexter

1
m.start() forควรจะm.start()for, ควรจะเป็นs[i+2] in 'aeiouAEIOU' s[i+2]in'aeiouAEIOU'โกนหนวดอย่างง่ายดายขนาด 3 ไบต์เนื่องจากช่องว่าง
Erik the Outgolfer

1
('an','a')[s[i+2]in'aeiouAEIOU']กลับด้านคุณสามารถใช้'a'+'n'*(s[i+2]in'aeiouAEIOU')เพื่อแก้ไขและบันทึก 2 ไบต์ ที่นี่คุณสามารถค้นหาเคล็ดลับการเล่นกอล์ฟได้มากมาย
Rod

1
ชุมชนแห่งนี้น่ารักมากเมื่อเห็นว่ามีคนจำนวนเท่าใดยินดีที่จะช่วยเหลือผู้มาใหม่และให้คำแนะนำการเล่นกอล์ฟ!
yo

1
ว้าวenumerate()เท่ห์ ขอบคุณ @chepner
Gurupad Mamadapur

2

Java, 180 178 ไบต์

โพสต์แรกของฉันที่นี่ฉันใช้ส่วนหนึ่งของโพสต์Kevin Cruijssenแต่และด้วยวิธีการที่แตกต่างกันเขาช่วยฉันลดอีกหน่อยขอบคุณเขา!

String c(String s){String x[]=s.split("\\[\\?]",2),r=x[0];return x.length>1?r+(r.matches("(.+[.!?] )|(^)$")?"A":"a")+("aeiouAEIOU".contains(""+x[1].charAt(1))?"n":"")+c(x[1]):r;}

นี่มัน ungolfed:

static String c(String s) {
        String x[] = s.split("\\[\\?\\]", 2), r = x[0];
        return x.length > 1 ? r + (r.matches("(.+[.!?] )|(^)$") ? "A" : "a")
                + ("aeiouAEIOU".contains("" + x[1].charAt(1)) ? "n" : "") + c(x[1]) : r;
    }

และก็ได้ผล

คำอธิบายง่ายๆฉันใช้การอนุมัติซ้ำเพื่อค้นหาทุก[?]อย่าง

ฉันหาวิธีใช้งานการจับคู่กับกรณีที่ไม่รู้สึกตัว (ไม่แน่ใจว่าเป็นไปได้)

178bytes: ขอบคุณ Martin Ender!


1
ยินดีต้อนรับสู่ PPCG! ฉันไม่คิดว่าคุณต้องหลบหนีจาก]ใน regex ของคุณ
Martin Ender

คุณพูดถูกเพียงคนเดียวที่เปิด
เพียงพอ

2

05AB1E , 38 36 35 ไบต์

2FžNžM‚NèSðì…[?]©ìDu«D®'a'nN׫::}.ª

ลองออนไลน์หรือตรวจสอบกรณีทดสอบทั้งหมดตรวจสอบกรณีทดสอบทั้งหมด

คำอธิบาย:

2F            # Loop 2 times:
  žN          #  Push consonants "bcdfghjklmnpqrstvwxyz"
  žM          #  Push vowels "aeiou"
             #  Pair them together into a list
     Nè       #  And use the loop-index to index into this pair
  S           #  Convert this string to a list of characters
   ðì         #  Prepend a space in front of each character
     …[?]     #  Push string "[?]
         ©    #  Store it in variable `®` (without popping)
          ì   #  And prepend it in front of each string in the list as well
  }D          #  Then duplicate the list
    u         #  Uppercase the characters in the copy
     «        #  And merge the two lists together
              #   i.e. for the vowel-iteration we'd have ["[?] a","[?] e","[?] i","[?] o",
              #    "[?] u","[?] A","[?] E","[?] I","[?] O","[?] U"]
   D          #  Duplicate it
    ®         #  Push "[?]" from variable `®`
     'a      '#  Push "a"
       'n    '#  Push "n"
         N×   #  Repeated the 0-based index amount of times (so either "" or "n")
           «  #  And append it to the "a"
    :         #  Replace all "[?]" with "an"/"a" in the duplicated list
     :        #  And then replace all values of the lists in the (implicit) input-string
 }.ª          #  After the loop: sentence-capitalize everything (which fortunately retains
              #  capitalized words in the middle of sentences, like the "European" testcase)
              # (and after the loop the result is output implicitly)

1
มีข้อบกพร่องเล็กน้อยในนั้น มันพิมพ์ใหญ่แต่ละคำหลังจาก "an" ตัวอย่างเช่น "[?] สีส้ม" กลายเป็น "สีส้ม" ดูเหมือนว่าจะทำงานถ้าคุณเพิ่ม]หลังจาก::
Dorian

@Dorian Woops .. ฉันลบออกใน}ภายหลังเพราะฉันคิดว่ามันจะช่วยประหยัด byte แต่คุณถูกต้องแน่นอนว่ามันล้มเหลวสำหรับ[?] vowelกรณี .. ขอบคุณที่แจ้งให้เราทราบ!
Kevin Cruijssen

1

C #, 204 235 ไบต์

string n(string b){for(int i=0;i<b.Length;i++){if(b[i]=='['){var r="a";r=i==0||b[i-2]=='.'?"A":r;r=System.Text.RegularExpressions.Regex.IsMatch(b[i+4].ToString(),@"[aeiouAEIOU]")?r+"n":r;b=b.Insert(i+3,r);}}return b.Replace("[?]","");}

โปรแกรมเต็มรูปแบบ Ungolfed:

using System;

class a
{
    static void Main()
    {
        string s = Console.ReadLine();
        a c = new a();
        Console.WriteLine(c.n(s));
    }

    string n(string b)
    {
        for (int i = 0; i < b.Length; i++)
        {
            if (b[i] == '[')
            {
                var r = "a";
                r = i == 0 || b[i - 2] == '.' ? "A" : r;
                r = System.Text.RegularExpressions.Regex.IsMatch(b[i + 4].ToString(), @"[aeiouAEIOU]") ? r + "n" : r;
                b = b.Insert(i + 3, r);
            }
        }
        return b.Replace("[?]", "");
    }
}

ฉันแน่ใจว่าสิ่งนี้สามารถปรับปรุงได้โดยเฉพาะอย่างยิ่งส่วน Regex แต่ไม่สามารถคิดอะไรตอนนี้


มันทำงานโดยไม่มีการนำเข้าหรือไม่
แมว

อ๊ะลืมที่จะรวมการนำเข้า regex ด้วย
Yodle

1
รหัส golfed ควรทำงานตามที่เป็นในรูปแบบใด ๆ - ถ้ามันไม่ทำงานโดยไม่ต้องนำเข้า regex แล้วการนำเข้า regex ควรไปในรหัส golfed ด้วย
cat

โอเคขอบคุณ. ยังคงรีดผ้าวิธีการตอบ การนับและคำตอบรวมถึง System.Text.RegularExpressions ทันที
Yodle

ตอนนี้ดูดีแล้ว :) คุณสามารถตรวจสอบCode Golf Metaและแท็กคำถามที่พบบ่อยที่นั่น
แมว

1

Java 7, 239 214 213 ไบต์

String c(String s){String x[]=s.split("\\[\\?\\]"),r="";int i=0,l=x.length-1;for(;i<l;r+=x[i]+(x[i].length()<1|x[i].matches(".+[.!?] $")?65:'a')+("aeiouAEIOU".contains(x[++i].charAt(1)+"")?"n":""));return r+x[l];}

กรณีที่ไม่ได้รับการทดสอบ &:

ลองที่นี่

class M{
  static String c(String s){
    String x[] = s.split("\\[\\?\\]"),
           r = "";
    int i = 0,
        l = x.length - 1;
    for (; i < l; r += x[i]
                     + (x[i].length() < 1 | x[i].matches(".+[.!?] $") 
                        ? 65
                        : 'a')
                     + ("aeiouAEIOU".contains(x[++i].charAt(1)+"")
                        ? "n"
                        : ""));
    return r + x[l];
  }

  public static void main(String[] a){
    System.out.println(c("Hello, this is [?] world!"));
    System.out.println(c("How about we build [?] big building. It will have [?] orange banana hanging out of [?] window."));
    System.out.println(c("[?] giant en le sky."));
    System.out.println(c("[?] yarn ball? [?] big one!"));
    System.out.println(c("[?] hour ago I met [?] European. "));
    System.out.println(c("Hey sir [Richard], how 'bout [?] cat?"));
    System.out.println(c("[?] dog is barking. [?] cat is scared!"));
  }
}

เอาท์พุท:

Hello, this is a world!
How about we build a big building. It will have an orange banana hanging out of a window.
A giant en le sky.
A yarn ball? A big one!
A hour ago I met an European. 
Hey sir [Richard], how 'bout a cat?
A dog is barking. A cat is scared!

ฉันพยายามใช้โซลูชันแบบเรียกซ้ำฉันสิ้นสุดด้วย 2 ไบต์มากกว่านั้นคุณ :( จำเป็นต้องปรับปรุงบางที .. แต่เนื่องจากฉันใช้ regex ของคุณฉันไม่ชอบโพสต์
AxelH

@AxelH บางทีคุณอาจโพสต์ไว้ใน ideone และลิงค์ที่นี่? เราอาจจะเห็นอะไรบางอย่างเกี่ยวกับกอล์ฟ ;)
Kevin Cruijssen

นี่มันเป็นideone.com/z7hlViฉันไม่พบ approch ดีกว่าisEmptyใช้ ^$regex ฉันเชื่อว่าฉันลงท้ายด้วย 202;)
AxelH

@AxelH อ่าดี อืมฉันนับ 195 ไบต์แทน 202? Btw, คุณสามารถตีมันได้ถึง 180 โดยทำผลตอบแทนโดยตรงด้วยการประกอบอื่น ๆ : String c(String s){String x[]=s.split("\\[\\?\\]",2),r=x[0];return x.length>1?r+(r.matches("(.+[.!?] )|(^)$")?"A":"a")+("aeiouAEIOU".contains(""+x[1].charAt(1))?"n":"")+c(x[1]):r;}สั้นกว่าคำตอบลูปของฉันแน่นอน :)
Kevin Cruijssen

โอ้ใช่ฉันจัดการเพื่อวางถ้ากลุ่มในหนึ่งบรรทัดในตอนท้ายลืมที่จะแทนที่ ขอบคุณ;
AxelH

1

แร็กเก็ต 451 ไบต์ (โดยไม่ต้อง regex)

เห็นได้ชัดว่ามันเป็นคำตอบที่ยาว แต่แทนที่และด้วยตัวพิมพ์ใหญ่ด้วย:

(define(lc sl item)(ormap(lambda(x)(equal? item x))sl))
(define(lr l i)(list-ref l i))(define(f str)(define sl(string-split str))
(for((i(length sl))#:when(equal?(lr sl i)"[?]"))(define o(if(lc(string->list"aeiouAEIOU")
(string-ref(lr sl(add1 i))0))#t #f))(define p(if(or(= i 0)(lc(string->list".!?")
(let((pr(lr sl(sub1 i))))(string-ref pr(sub1(string-length pr))))))#t #f))
(set! sl(list-set sl i(if o(if p"An""an")(if p"A""a")))))(string-join sl))

การทดสอบ:

(f "[?] giant en le [?] sky.")
(f "[?] yarn ball?")
(f "[?] hour ago I met [?] European. ")
(f "How about we build [?] big building. It will have [?] orange banana hanging out of [?] window.")
(f "Hello, this is [?] world!")

เอาท์พุท:

"A giant en le a sky."
"A yarn ball?"
"A hour ago I met an European."
"How about we build a big building. It will have an orange banana hanging out of a window."
"Hello, this is a world!"

รุ่นโดยละเอียด:

(define(contains sl item)
  (ormap(lambda(x)(equal? item x))sl))

(define(lr l i)
  (list-ref l i))

(define(f str)
  (define sl(string-split str))
  (for((i(length sl))#:when(equal?(lr sl i)"[?]"))
    (define an   ; a or an
      (if(contains(string->list "aeiouAEIOU")
                  (string-ref(lr sl(add1 i))0))
         #t #f ))
    (define cap   ; capital or not
      (if(or(= i 0)(contains(string->list ".!?")
                            (let ((prev (lr sl(sub1 i)))) (string-ref prev
                                       (sub1(string-length prev))))))
         #t #f))
    (set! sl(list-set sl i (if an (if cap "An" "an" )
                                 (if cap "A" "a")))))
  (string-join sl))


มันเป็นภาษาที่ยอดเยี่ยม แต่ไม่ได้มีไว้สำหรับเล่นกอล์ฟ
rnso


1

เรติน่า , 66 60 ไบต์

i`\[\?\]( ([aeiou]?)[a-z&&[^aeiou])
a$.2*n$1
(^|[.?!] )a
$1A

ลองออนไลน์

คำอธิบาย:

ทำการค้นหาแบบตรง[?]ตามตัวอักษรตามด้วยสระหรือพยัญชนะที่สระเสียงตัวเลือกจะถูกบันทึกไว้ในกลุ่มการจับภาพ 2 และการจับคู่ทั้งหมดในกลุ่มการจับ 1:

i`\[\?\]( ([aeiou]?)[a-z&&[^aeiou])

แทนที่สิ่งนี้ด้วย an a, ตามด้วยความยาวของจำนวนกลุ่มที่สองของn(เช่น 0 หรือ 1 n), ตามด้วยตัวอักษรของกลุ่มการดักจับ 1:

a$.2*n$1

จากนั้นจับคู่aจุดเริ่มต้นของสตริงหรือหลัง.?!ช่องว่างใดช่องหนึ่งบวก:

(^|[.?!] )a

และเป็นตัวพิมพ์ใหญ่ที่ A โดยไม่ลบอักขระอื่นของกลุ่มการดักจับ 1:

$1A

1

Java (JDK) , 154 ไบต์

s->{String v="(?= [aeiou])",q="(?i)\\[\\?]",b="(?<=^|[?.!] )";return s.replaceAll(b+q+v,"An").replaceAll(q+v,"an").replaceAll(b+q,"A").replaceAll(q,"a");}

ลองออนไลน์!

คำอธิบาย:

s->{
    String v="(?= [aeiou])",          // matches being followed by a vowel
    q="(?i)\\[\\?]",                  // matches being a [?]
    b="(?<=^|[?.!] )";                // matches being preceded by a sentence beginning
    return s.replaceAll(b+q+v,"An")   // if beginning [?] vowel, you need "An"
        .replaceAll(q+v,"an")         // if           [?] vowel, you need "an"
        .replaceAll(b+q,"A")          // if beginning [?]      , you need "A"
        .replaceAll(q,"a");}          // if           [?]      , you need "a"

1

C (gcc) , 225 207 202 201 ไบต์

ขอบคุณ ceilingcat สำหรับ -24 ไบต์

#define P strcpy(f+d,index("!?.",i[c-2])+!c?
c;d;v(i,g,f)char*i,*g,*f;{for(d=0;i[c];c++,d++)strcmp("[?]",memcpy(g,i+c,3))?f[d]=i[c]:(index("aeiouAEIOU",i[c+4])?P"An ":"an "),d++:P"A ":"a "),d++,c+=3);}

ลองออนไลน์!


0

Groovy, 73 162 ไบต์

def a(s){s.replaceAll(/(?i)(?:(.)?( )?)\[\?\] (.)/){r->"${r[1]?:''}${r[2]?:''}${'.?!'.contains(r[1]?:'.')?'A':'a'}${'aAeEiIoOuU'.contains(r[3])?'n':''} ${r[3]}"}}

แก้ไข: ประณามการพิมพ์ใหญ่ที่ซับซ้อนทุกอย่างที่นี่


สิ่งนี้ใช้ประโยชน์จากจุดเริ่มต้นของประโยคหรือไม่?
ติตัส

Nope ฉันเห็นแล้วว่าคำอธิบายการท้าทายมีการเปลี่ยนแปลงในระหว่างนี้ ...
norganos

"ให้เวลา [?] ชั่วโมงกับ [?] open cellar door" แบ่งรหัสของคุณ: groovyconsole.appspot.com/edit/5159915056267264
Magic Octopus Urn

คำอธิบายการท้าทายยังคงไม่สอดคล้องกันอย่างสมบูรณ์ ก่อนอื่นมันบอกว่า "คุณต้องกังวลเกี่ยวกับการใช้อักษรตัวใหญ่!" และหลังจากนั้นก็มีกฎสำหรับการโอนเป็นทุน
norganos

มันสอดคล้องกัน คุณต้องกังวลเกี่ยวกับการใช้อักษรตัวใหญ่ (นั่นคือคุณต้องจัดการมัน) จากนั้นจะอธิบายวิธี
edc65

0

C # 209 ไบต์

string A(string b){var s=b.Split(new[]{"[?]"},0);return s.Skip(1).Aggregate(s[0],(x,y)=>x+(x==""||(x.Last()==' '&&".?!".Contains(x.Trim().Last()))?"A":"a")+("AEIOUaeiou".Contains(y.Trim().First())?"n":"")+y);}

ที่จัดรูปแบบ

string A(string b)
{
    var s = b.Split(new[] { "[?]" }, 0);
    return s.Skip(1).Aggregate(s[0], (x, y) => x + (x == "" || (x.Last() == ' ' && ".?!".Contains(x.Trim().Last())) ? "A" : "a") + ("AEIOUaeiou".Contains(y.Trim().First()) ? "n" : "") + y);
}

0

Perl 6 , 78 ไบต์

{S:i:g/(^|<[.?!]>' ')?'[?] '(<[aeiou]>?)/{$0 xx?$0}{<a A>[?$0]}{'n'x?~$1} $1/}

คำอธิบาย:

{
  S
    :ignorecase
    :global
  /
    ( # $0
    | ^             # beginning of line
    | <[.?!]> ' '   # or one of [.?!] followed by a space
    ) ?             # optionally ( $0 will be Nil if it doesn't match )

    '[?] '          # the thing to replace ( with trailing space )

    ( # $1
      <[aeiou]> ?   # optional vowel ( $1 will be '' if it doesn't match )
    )

  /{
    $0 xx ?$0      # list repeat $0 if $0
                   # ( so that it doesn't produce an error )
  }{
    < a A >[ ?$0 ] # 'A' if $0 exists, otherwise 'a'
  }{
    'n' x ?~$1     # 'n' if $1 isn't empty
                   # 「~」 turns the Match into a Str
                   # 「?」 turns that Str into a Bool
                   # 「x」 string repeat the left side by the amount of the right

  # a space and the vowel we may have borrowed
  } $1/
}

ทดสอบ:

#! /usr/bin/env perl6
use v6.c;
use Test;

my &code = {S:i:g/(^|<[.?!]>' ')?'[?] '(<[aeiou]>?)/{<a A>[?$0]~('n'x?~$1)} $1/}

my @tests = (
  'Hello, this is [?] world!'
  => 'Hello, this is a world!',

  'How about we build [?] big building. It will have [?] orange banana hanging out of [?] window.'
  => 'How about we build a big building. It will have an orange banana hanging out of a window.',

  '[?] giant en le sky.'
  => 'A giant en le sky.',

  '[?] yarn ball?'
  => 'A yarn ball?',

  '[?] hour ago I met [?] European.'
  => 'A hour ago I met an European.',

  "Hey sir [Richard], how 'bout [?] cat?"
  => "Hey sir [Richard], how 'bout a cat?",
);

plan +@tests;

for @tests -> $_ ( :key($input), :value($expected) ) {
  is code($input), $expected, $input.perl;
}
1..6
ok 1 - "Hello, this is a world!"
ok 2 - "How about we build a big building. It will have an orange banana hanging out of a window."
ok 3 - "A giant en le sky."
ok 4 - "A yarn ball?"
ok 5 - "A hour ago I met an European."
ok 6 - "Hey sir [Richard], how 'bout a cat?"

คุณสามารถลบช่องว่างออกจาก} $1ท้ายได้}$1ไหม(ทำให้เป็น)?
Cyoce

@Cyoce มีวิธีการทำเช่นนั้น แต่มันเพิ่มความซับซ้อนมากขึ้นในที่อื่น {S:i:g/(^|<[.?!]>' ')?'[?]'(' '<[aeiou]>?)/{<a A>[?$0]~('n'x?~$1.substr(1))}$1/}
Brad Gilbert b2gills

ตกลงฉันไม่แน่ใจว่า Perl จะแยกว่า
Cyoce

0

Lua, 131 ไบต์

function(s)return s:gsub("%[%?%](%s*.)",function(a)return"a"..(a:find("[AEIOUaeiou]")and"n"or"")..a end):gsub("^.",string.upper)end

ถึงแม้ว่า lua จะเป็นภาษาที่แย่มากสำหรับการเล่นกอล์ฟ แต่ฉันรู้สึกว่าฉันทำได้ดีมาก


0

Pip , 62 55 54 50 ไบต์

รับสตริงเป็นอาร์กิวเมนต์บรรทัดคำสั่ง

aR-`([^.?!] )?\[\?]( [^aeiou])?`{[b"aA"@!b'nX!cc]}

ลองออนไลน์!

คำอธิบาย:

a                                                   Cmdline argument
 R                                                  Replace...
  -`                           `                    The following regex (case-insensitive):
    ([^.?!] )?                                      Group 1: not end-of-sentence (nil if it doesn't match)
              \[\?]                                 [?]
                   ( [^aeiou])?                     Group 2: not vowel (nil if there is a vowel)
                                {                }  ... with this callback function (b = grp1, c = grp2):
                                 [              ]   List (concatenated when cast to string) of:
                                  b                 Group 1
                                   "aA"@!b          "a" if group 1 matched, else "A"
                                          'nX!c     "n" if group 2 didn't match, else ""
                                               c    Group 2

0

แร็กเก็ต (พร้อม regex) 228 ไบต์

(define(r a b c)(regexp-replace* a b c))
(define(f s)
(set! s(r #rx"[a-zA-Z ]\\[\\?\\] (?=[aeiouAEIOU])"s" an "))
(set! s(r #rx"[a-zA-Z ]\\[\\?\\]"s" a"))
(set! s(r #rx"\\[\\?\\] (?=[aeiouAEIOU])"s"An "))
(r #rx"\\[\\?\\]"s"A"))

การทดสอบ:

(f "[?] giant en le [?] sky.")
(f "[?] yarn ball?")
(f "[?] apple?")
(f "[?] hour ago I met [?] European. ")
(f "How about we build [?] big building. It will have [?] orange banana hanging out of [?] window.")
(f "Hello, this is [?] world!")

เอาท์พุท:

"A giant en le a sky."
"A yarn ball?"
"An apple?"
"A hour ago I met an European. "
"How about we build a big building. It will have an orange banana hanging out of a window."
"Hello, this is a world!"

0

Python 3 , 104 103 ไบต์

-1 ไบต์, ไม่ใช้ Escape ]

lambda s:r('(^|[.?!] )a',r'\1A',r('a( [aeiouAEIOU])',r'an\1',r('\[\?]','a',s)));from re import sub as r

ลองออนไลน์!

เริ่มต้นโดยการแทนที่ปรากฏทั้งหมด[?]ด้วยa,
แล้วแทนที่ตามด้วยสระด้วยaan
แล้วแทนที่ในช่วงเริ่มต้นของการป้อนข้อมูลหรือประโยคที่aA

สมมติว่า[?]ไม่มีทางสัมผัสคำอื่นและตัวพิมพ์เล็กaไม่ควรเริ่มประโยค


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