มีพยางค์กี่ตัวในจำนวนนั้น?


15

ฉันชอบที่จะใช้ตัวเลขและรู้ว่ามีกี่พยางค์ที่อยู่ในนั้นเมื่อพูดเป็นภาษาอังกฤษ

ลอง จำกัด ให้เป็นจำนวนเต็มบวกซึ่งน้อยกว่าหนึ่งพัน

ฉันเป็นคนอังกฤษดังนั้นเราจะไปตามคอลัมน์นับร้อยด้วย 'และ' เมื่อมีตัวเลขที่ไม่ใช่ศูนย์หลังจากนั้น

ความท้าทาย

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

กรณีทดสอบ

|  N  | In words                             | Syllables |
|   1 | one                                  |         1 |
|   2 | two                                  |         1 |
|   3 | three                                |         1 |
|   4 | four                                 |         1 |
|   5 | five                                 |         1 |
|   6 | six                                  |         1 |
|   7 | sev-en                               |         2 |
|   8 | eight                                |         1 |
|   9 | nine                                 |         1 |
|  10 | ten                                  |         1 |
|  11 | el-ev-en                             |         3 |
|  12 | twelve                               |         1 |
|  13 | thir-teen                            |         2 |
|  14 | four-teen                            |         2 |
|  17 | se-ven-teen                          |         3 |
|  20 | twen-ty                              |         2 |
|  21 | twen-ty one                          |         3 |
|  42 | four-ty two                          |         3 |
|  73 | sev-en-ty three                      |         4 |
|  77 | sev-en-ty sev-en                     |         5 |
| 100 | one hund-red                         |         3 |
| 110 | one hund-red and ten                 |         5 |
| 111 | one hund-red and el-ev-en            |         7 |
| 555 | five hund-red and fif-ty five        |         7 |
| 700 | sev-en hund-red                      |         4 |
| 770 | sev-en hund-red and sev-en-ty        |         8 |
| 777 | sev-en hund-red and sev-en-ty sev-en |        10 |
| 999 | nine hund-red and nine-ty nine       |         7 |

1
เราสามารถรับอินพุตเป็นสตริงหรืออาร์เรย์ตัวเลขได้หรือไม่?
เดนนิส

คำตอบ:


11

Python 2 , 84 83 74 67 ไบต์

lambda n:4*(n>99)+2-n%~9/9-0x55561aaaab/4**(n%100)%4+`n`.count('7')

ขอบคุณ @xnor สำหรับการเล่นกอล์ฟ9ไบต์ที่ 16!

ลองออนไลน์!


Python 2 , 79 ไบต์

lambda n:4*(n>99)+([-1]+10*[1]+[3,1]+7*[2]+8*([2]+9*[3]))[n%100]+`n`.count('7')

ตรงไปตรงมา แต่อีกต่อไป

ลองออนไลน์!


สำหรับโซลูชัน 83- ไบต์ของคุณคุณสามารถลดขนาด 3 ไบต์โดยเปลี่ยน-10เป็น~9และสลับไปมาในช่วงบิตสุดท้าย+(0<n%100!=12)-(n%100!=11)แต่ยังคงนานกว่าโซลูชันใหม่ของคุณ
xnor


@xnor นั่นฉลาดจริงๆ! min(n%100,13)%12/~9อาจช่วยด้วยวิธีการที่ฉันพยายามหาคำตอบของฉันด้วยเช่นกัน
เดนนิส


@xnor ขอบคุณอีกครั้ง!
เดนนิส

8

Perl 5 -p , 53 ไบต์

$_=4*/.../+2*/[^0].$/+!/0$/+y/7//-/1[^1]$/-/12$/-/00/

ลองออนไลน์!

อย่างไร

-p commandline flag reads input into $_

$_=4*/.../     # Hundreds place has minimum of 4 sylables (__ HUN-DRED AND),
               # match fails on number <100, and would add 0 here
  +2*/[^0].$/  # Tens place has two syllables if not 0 (__-TY or __TEEN),
               # match fails on numbers <10, and would add 0
  +!/0$/       # Ones place has one syllable if not 0 (__)
               # -- Now adjust for special cases --
  +y/7//       # add a syllable for every 7 present
  -/1[^1]$/    # remove a syllable for 10-19, except 11
  -/12$/       # remove another syllable for 12
  -/00/        # remove the syllable for AND if it's an even hundred

-p commandline flag outputs contents of $_


7

Python 2 , 112 108 ไบต์

f=lambda n:n>99and f(n/100)+3+f(n%100)-(n%100<1)or n>19and f(n/10)-~f(n%10)or int("01111112111312222322"[n])

ลองออนไลน์!

-4 ไบต์ขอบคุณ Shaggy


2
นอกจากนี้[2]*7ส่วนของคุณจะล้มเหลว17เนื่องจากควรเป็น 3 แทนที่จะเป็น 2 ( sev-en-teen)
Kevin Cruijssen

2
-4 ไบต์รวมถึงการแก้ไขสำหรับ 17
Shaggy

@Shaggy Thanks :)
TFeld

@KevinCruijssen แก้ไขแล้ว (ขอขอบคุณ Shaggy)
TFeld


6

วุลแฟรมภาษา101 115 ไบต์

s=StringSplit;Length[Join@@(WordData[#,"Hyphenation"]&/@Join@@s/@
s[IntegerName@#,"-"])]+Boole[#>100&&#~Mod~100!=0]&

คำอธิบาย

(ทดแทนStringSplitเพื่อs)

Length[Join@@(WordData[#,"Hyphenation"]&/@Join@@
StringSplit/@ StringSplit[IntegerName@#,"-"])]+Boole[#>100&&#~Mod~100!=0]&

IntegerName แสดงตัวเลขเป็นภาษาอังกฤษแบบอเมริกัน (เช่นไม่มี "และ" รวมอยู่ในตัวเลขที่มากกว่า 100) ตัวอย่างเช่น 777-> "seven hundred seventy-sevenเช่น

StringSplit[IntegerName@#,"-"] ลบเครื่องหมายขีดคั่นใด ๆ ในการแสดงผล

StringSplit/@ แยกการแสดงผลเป็นคำ

Join@@ ออกจากรายการคำศัพท์โดยไม่ต้องมีรายการแบบฝัง (ในกรณีที่มีเครื่องหมายยัติภังค์ปรากฏ)

WordData[#,"Hyphenation"] แยกคำเดียวออกเป็นพยางค์

Join@@ เก็บรายการคำศัพท์อย่างง่ายไว้ในคำทั้งหมด

Length นับพยางค์

+Boole[#>100&&#~Mod~100!=0]เพิ่ม1การนับพยางค์สำหรับตัวเลขเหล่านั้นที่มากกว่า 100 (เนื่องจากการเพิ่มเติม "และ" ที่ใช้ในการเรนเดอร์ภาษาอังกฤษแบบอังกฤษ), ไม่รวมทวีคูณของ 100


6

Java 11, 105 102 ไบต์

n->(""+"".repeat(8)).charAt(n%100)+(n+"").split("7",9).length-(n>99?2:6)

มีอักขระที่ไม่สามารถพิมพ์ได้จำนวนมาก

-3 ไบต์ขอบคุณ@ OlivierGrégoire

ลองออนไลน์

คำอธิบาย:


n->               // Method with integer as both parameter and return-type
  (""
                  //  Push string with ASCII-value digits 46666666666867777777
 +"".repeat(8))
                  //  Appended with 8 times a string with ASCII-value digits 7888888888
   .charAt(n%100) //  Take the (input modulo-100)'th character of this string (as integer)
  +(n+"").split("7",9).length
                  //  Count the amount of 7s in the input + 1
  -(n>99?         //  And if the input is larger than 99:
     2            //   Subtract 2 (-1 for the 7s+1 count; -5 to map the ASCII-digits to:
                  //               4 → -1; 6 → 1; 7 → 2; 8 → 3;
                  //               and +4 for the inputs above 99)
    :             //  Else:
     6)           //   Subtract 6 (-1 for the 7s+1 count and -5 to map the ASCII-digits to:
                  //               4 → -1; 6 → 1; 7 → 2; 8 → 3)

1
102 ไบต์โดยการเปลี่ยน.split("7",-1)ไป.split("7",9)และจะ-6+(n>99?4:0) -(n>99?2:6)
Olivier Grégoire

1
@ OlivierGrégoireขอบคุณ พลาดไปอย่างสิ้นเชิง-(n>99?2:6)แต่ก็ชัดเจนว่าตอนนี้คุณได้ชี้ให้เห็นแล้ว และ-1ที่จะ9เกิดจากการ จำกัด การป้อนข้อมูลขนาดฉันจะไม่ได้คิดว่าเพื่อขอบคุณ!
Kevin Cruijssen

5

05AB1E , 34 31 ไบต์

т%U7¢I€Ā`Iт@3*X_(X20@X12Q(X11QO

ลองมันออนไลน์หรือตรวจสอบทุก[1,999]กรณีทดสอบ

คำอธิบาย:

ด้วยการตรวจสอบทั้งหมดที่กล่าวถึงจะส่งผลให้ 1 สำหรับความจริงและ 0 สำหรับความเท็จ

т%         # Take modulo-100 of the (implicit) input
           #  i.e. 710 → 10
  U        # Pop and store it in variable `X`
7¢         # Count the amount of 7s in the (implicit) input
           #  i.e. 710 → 1
I€Ā        # Trutify each digit in the input (0 if 0; 1 otherwise)
   `       # And push all of the mapped values to the stack
           #  i.e. 710 → [1,1,0]
Iт@        # Check if the input is larger than or equal to 100
           #  i.e. 710 → 1 (truthy)
   3*      # Multiply that result by 3 (for 'hund-red and')
           #  i.e. 1 → 3
X_         # Check if variable `X` is 0
           #  i.e. 10 → 0 (falsey)
  (        # And negate that (to remove 'and' when #00)
           #  i.e. 0 → 0
X20@       # Check if variable `X` is larger than or equal to 20 (for '-ty')
           #  i.e. 10 → 0 (falsey)
X12Q       # Check if variable `X` is exactly 12
           #  i.e. 10 → 0 (falsey)
    (      # And negate that (to remove 'teen')
           #  i.e. 0 → 0
X11Q       # Check if variable `X` is exactly 11 (for 'el-ev-en' minus 'one one')
           #  i.e. 10 → 0 (falsey)
O          # Sum everything on the stack (and output implicitly)
           #  i.e. [1,1,1,0,3,0,0,0,0] → 6

กรณีนี้ล้มเหลวในการทดสอบ 700 ครั้ง 'Seven Hundred' มี 4 พยางค์ผลตอบแทนนี้ 5
AJFaraday

@AJFaraday ควรได้รับการแก้ไขแล้ว มีI(อินพุต) โดยบังเอิญแทนX(อินพุต mod 100) เมื่อตรวจสอบว่ามีขนาดใหญ่กว่า 20 สำหรับ +1 ของtyหรือไม่
Kevin Cruijssen

ฉันขอโทษที่ส่งคืน 0 สำหรับ 'หนึ่งร้อย'
AJFaraday

@AJFaraday แก้ไขอีกครั้ง .. >(ตรวจสอบว่าการป้อนข้อมูลมีขนาดใหญ่กว่า 100) ถูกแทนที่ด้วย@(ตรวจสอบว่าการป้อนข้อมูลมีขนาดใหญ่กว่าหรือเท่ากับ 100) บางทีฉันควรตรวจสอบกรณีทดสอบเพิ่มเติมด้วยตัวเองอย่างรอบคอบก่อนโพสต์ .. ขอโทษด้วย ..
Kevin Cruijssen

4
โดยวิธีการที่รักหมวกด้านบนลูกบาศก์ Rubix!
AJFaraday

5

ถ่าน , 39 31 ไบต์

I⁻⁺↨E謬Iι²№θ7I§⁺”)∨∧⌈a¡↶”×0⁸⁰N

ลองออนไลน์! การเชื่อมโยงคือการใช้รหัสเวอร์ชันอย่างละเอียด คำอธิบาย:

I⁻⁺

คำนวณการปรับจำนวนพยางค์และแสดงผลลัพธ์เป็นสตริง

↨E謬Iι²

เริ่มต้นด้วยการเปลี่ยนแต่ละตัวเลขที่ไม่ใช่ศูนย์เป็น 1 แล้วถอดรหัสเป็นฐาน 2 ซึ่งจะให้คำตอบที่ถูกต้องสำหรับอินพุตส่วนใหญ่

№θ7

เพิ่ม 1 7สำหรับแต่ละ

I§⁺”)∨∧⌈a¡↶”×0⁸⁰N

ใช้สตริงตัวอักษร10000000001021111111และผนวก 80 ศูนย์จากนั้นทำดัชนีแบบวนรอบโดยอินพุตและลบตัวเลขนั้น


4

เยลลี่ , 28 25 23 ไบต์

9ḊŻ;2+⁵Żċ%ȷ2$ạDṠḄƊ+Dċ7Ɗ

ลองออนไลน์!

มันทำงานอย่างไร

9ḊŻ;2+⁵Żċ%ȷ2$ạDṠḄƊ+Dċ7Ɗ  Main link. Argument: n (integer in [1, ..., 999])

9                        Set the return value to 9.
 Ḋ                       Dequeue; yield [2, 3, 4, 5, 6, 7, 8, 9].
  Ż                      Zero; yield [0, 2, 3, 4, 5, 6, 7, 8, 9].
   ;2                    Concat 2, yield [0, 2, 3, 4, 5, 6, 7, 8, 9, 2].
     +⁵                  Add 10; yield [10, 12, 13, 14, 15, 16, 17, 18, 19, 12].
       Ż                 Zero; yield [0, 10, 12, 13, 14, 15, 16, 17, 18, 19, 12].
         %ȷ2$            Yield n % 1e2.
        ċ                Count the occurrences of the modulus in the array.
                 Ɗ       Combine the three links to the left into a monadic chain.
              D            Decimal; convert n to its array of digits in base 10.
               Ṡ             Take the sign of each decimal digit (0 or 1).
                Ḅ            Convert the array of signs from base 2 to integer.
             ạ           Compute the abs. difference of the results to both sides.
                      Ɗ  Combine the three links to the left into a monadic chain.
                   D       Decimal; convert n to its array of digits in base 10.
                    ċ7     Count the number of 7's.

3

PHP , 190 158 145 141 137 ไบต์

<?for($j=$p=0;$p<strlen($i=$argv[1]);)$j+=str_split($i)[$p++]>0;echo$j+substr_count($i,7)+3*($i>99)-!($i%=100)+($i>19)-($i==12)+($i==11);

ลองออนไลน์!

พอร์ตของโซลูชันของ Kevin Cruijssen (น่าเสียดายที่มันไม่มีความกะทัดรัดเท่ากันใน PHP :))

- 32 45 ต้องขอบคุณ Shaggy!

-3 ขอบคุณ Kevin Crujissen!


ดังนั้นเงินฝากออมทรัพย์มากมายที่จะทำที่นี่! นี่เป็นเพียงบางตัวอย่างที่รวดเร็วมาก
Shaggy

1
145 ไบต์ คุณสามารถบันทึกอีกสองสามไบต์โดยใช้แท็กสั้น ๆ แต่ฉันจำไม่ได้ว่าจะใช้พวกเขาอย่างไรใน TIO (หมายเหตุ: ฉันอยู่ในโทรศัพท์ของฉันดังนั้นจึงยังไม่ได้ทดสอบอินพุตทั้งหมด)
Shaggy

1
@Shaggy 2 ไบต์สามารถเปลี่ยนแปลงได้เมื่อใช้>99และ>19แทนและ>=100 >=20
Kevin Cruijssen

1
@KevinCruijssen จริง ๆ แล้วที่ช่วยประหยัด 3 ไบต์เพราะมันจะไปจาก 100 ถึง 99 :)
34415 NK1406

ฉันยังสามารถบันทึกไบต์อื่นด้วยการวางตัวแปรที่จุดเริ่มต้นของเสียงสะท้อน
NK1406

2

05AB1E , 24 ไบต์

คำตอบของเยลลี่ Port of Dennis

8L>Ć¾šT+¾šsт%¢sSĀJCαs7¢+

ลองออนไลน์! หรือเป็นชุดทดสอบ

คำอธิบาย

8L>                       # push range [2 ... 9]
   Ć                      # enclose, append head
    ¾š                    # prepend 0
      T+                  # add 10 to each
        ¾š                # prepend 0
          sт%¢            # count occurrences of input % 100 in this list
              sS          # push input split into a list of digits
                Ā         # truthify, check each if greater than 0
                 JC       # convert from base-2 to base-10
                   α      # absolute difference
                    s7¢+  # add the amount of 7's in the input

1

05AB1E , 26 ไบต์

€ĀJCI7¢•Ž¢Γ}Þ±6u•¾80׫Iè(O

คำตอบ Charcoalของ@Neilพอร์ตของดังนั้นอย่าลืมโหวตเขาเช่นกันถ้าคุณชอบคำตอบนี้!

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

จำนวนเต็มที่ถูกบีบอัด•Ž¢Γ}Þ±6u•อาจเป็น•8JA•b2TÌǝจำนวนไบต์เดียวกัน

คำอธิบาย:

€Ā                   # Trutify every digit in the (implicit) input
                     # (0 remains 0; everything else becomes 1)
  J                  # Join it together to a single string
   C                 # Convert from binary to integer
I7¢                  # Count the amount of 7s in the input
•Ž¢Γ}Þ±6u           # Push compressed integer 10000000001021111111
          ¾80׫      # Append 80 "0"s
               Iè    # Index the integer (with automatic wraparound) into it
                 (   # Negate the result
O                    # Sum all values on the stack (and output implicitly)

เห็นนี้ 05AB1E คำตอบของฉัน (ส่วนวิธีการบีบอัดจำนวนเต็มขนาดใหญ่? )จะเข้าใจว่าทำไมเป็น•Ž¢Γ}Þ±6u•10000000001021111111

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