จัดเรียงฟังก์ชันอาร์กิวเมนต์ในบรรทัดของตนเอง


16

กำหนดอินพุตของสตริงที่แทนนิยามฟังก์ชันส่งออกสตริงพร้อมบรรทัดใหม่และช่องว่างที่แทรกเพื่อให้อาร์กิวเมนต์ของฟังก์ชันคั่นระหว่างบรรทัดใหม่และจัดตำแหน่ง

สตริงอินพุตจะเป็นไปตามรูปแบบต่อไปนี้:

  • ครั้งแรกก็จะเริ่มต้นด้วยคำนำหน้าซึ่งมักจะเป็นอย่างน้อยหนึ่งตัวยาวและไม่ได้มีการใด ๆ ,()ของตัวละคร

  • วงเล็บเปิด ( () จะทำเครื่องหมายจุดเริ่มต้นของรายการอาร์กิวเมนต์

  • รายการข้อโต้แย้งที่เป็นศูนย์หรือมากกว่านั้นจะตามมา สิ่งเหล่านี้ถูกคั่นด้วยสตริง", "(เครื่องหมายจุลภาคและช่องว่าง) ไม่มีข้อโต้แย้งใด ๆ ,()จะมีของตัวละคร

  • วงเล็บปิด ( )) จะทำเครื่องหมายจุดสิ้นสุดของรายการอาร์กิวเมนต์

  • สุดท้ายเป็น postfix อาจจะพบซึ่งเป็นศูนย์หรือมากกว่าตัวอักษรและ อาจ,()ประกอบด้วยตัวอักษร

สตริงอินพุตจะประกอบด้วย ASCII ที่พิมพ์ได้เท่านั้น (ซึ่งหมายความว่าจะไม่มีการขึ้นบรรทัดใหม่)

ผลลัพธ์จะต้อง:

  • คำนำหน้าคัดลอกคำต่อคำและวงเล็บเปิด

  • รายการอาร์กิวเมนต์เวลานี้คั่นด้วยไม่ใช่", "แต่ด้วยเครื่องหมายจุลภาคขึ้นบรรทัดใหม่และช่องว่างมากเท่าที่จำเป็นเพื่อจัดตำแหน่งอักขระแรกของแต่ละอาร์กิวเมนต์ในแนวตั้ง

  • คำสั่ง paren และ postfix แบบปิด (ถ้ามี) คำต่อคำ

เนื่องจากนี่คือรหัสที่สั้นที่สุดเป็นไบต์จะเป็นผู้ชนะ

กรณีทดสอบ (รูปแบบ: อินพุตบรรทัดเดียวแล้วตามด้วยเอาต์พุตตามด้วยบรรทัดใหม่สองบรรทัด):

def foo(bar, baz, quux):
def foo(bar,
        baz,
        quux):

int main() {
int main() {

fn f(a: i32, b: f64, c: String) -> (String, Vec<i32>) {
fn f(a: i32,
     b: f64,
     c: String) -> (String, Vec<i32>) {

function g(h) {
function g(h) {

def abc(def, ghi, jkl, mno)
def abc(def,
        ghi,
        jkl,
        mno)

x y z(x, y, z) x, y, z)
x y z(x,
      y,
      z) x, y, z)

คำตอบ:


7

Haskell, 115 ไบต์

import Data.Lists
f x|(a,b:c)<-span(/='(')x,(d,e)<-span(/=')')c=a++b:intercalate(",\n "++(a>>" "))(splitOn", "d)++e

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

*Main> putStrLn $ f "fn f(a: i32, b: f64, c: String) -> (String, Vec<i32>) {"
fn f(a: i32,
     b: f64,
     c: String) -> (String, Vec<i32>) {

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

bind
  a: everything before the first (
  b: the first (
  c: everything after the first (
  d: everything of c before the first )
  e: everything of c from the first ) to the end

construct the output string by concatenating
  a
  b
  splitting d at the argument separator ", " and rejoining it with ",\n " followed by (length a) spaces    
  e

a>>" "ฉลาดจริงๆ ...
หญิง Clavilis

4

Japt 23 ไบต์

¡Y?X:Xr',",
"+SpUb'(}')

ทดสอบออนไลน์!

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

               // Implicit: U = input string
¡        }')   // Map each item X and index Y in U.split(")") to:
Y?X            //  If Y is non-zero, X. This keeps e.g. "(String, Vec<i32>)" from being parsed.
:Xr',",\n"+    //  Otherwise, X with each comma replaced with ",\n" concatenated with
SpUb'(         //  U.indexOf("(") spaces.
               // Implicit: re-join with ")", output

3

Perl, 62 52 + 2 = 54 ไบต์

s/\(.*?\)/$a=$"x length$`;$&=~s|(?<=,)[^,]+|\n$a$&|gr/e

ต้องการ-pธง:

$ echo "x y z(x, y, z) x, y, z)
fn f(a: i32, b: f64, c: String) -> (String, Vec<i32>) {" | \
perl -pe's/\(.*?\)/$a=$"x length$`;$&=~s|(?<=,)[^,]+|\n$a$&|gr/e'
x y z(x,
      y,
      z) x, y, z)
fn f(a: i32,
     b: f64,
     c: String) -> (String, Vec<i32>) {

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

# '-p' reads first line into $_ and will also auto print at the end
s/\(.*?\)/             # Match (...) and replace with the below
  $a=$"x length$`;     # $` contains all the content before the matched string
                       # And $" contains a literal space 
  $&=~s|               # Replace in previous match
    (?<=,)[^,]+        # Check for a , before the the string to match
                       # This will match ' b: f64', ' c: String'
  |\n$a$&|gr/e         # Replace with \n, [:spaces:] and all the matched text

3

เรติน่า 31 ไบต์

(?<=^([^(])*\([^)]*,) 
¶ $#1$* 

สังเกตช่องว่างที่ท้ายบรรทัดทั้งสอง

เราแทนที่ทุกพื้นที่ที่มี regex ^([^(])*\([^)]*,มาก่อน สตริงการแทนที่จะเป็นบรรทัดใหม่และจำนวนของการจับที่มี([^(])*บวกหนึ่งช่องว่าง

คำอธิบายที่สอดคล้องกันมากขึ้นมาในภายหลัง

ลองออนไลน์ได้ที่นี่


3

ES6, 68 67 ไบต์

s=>s.replace(/\(.*?\)/,(s,n)=>s.replace/, /g, `,
 `+` `.repeat(n)))

สิ่งนี้ทำงานโดยการแยกรายการอาร์กิวเมนต์จากสตริงเดิมและแทนที่แต่ละตัวคั่นอาร์กิวเมนต์ด้วยการเยื้องคำนวณจากตำแหน่งของรายการอาร์กิวเมนต์ภายในสตริงเดิม

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


ผมก็สงสัยว่าทำไมคุณทำแทน.split`, `.join(...) .replace(...)ปรากฎว่าอีกอันคือไบต์ที่สั้นกว่า:s=>s.replace(/\(.*?\)/,(s,n)=>s.replace(/, /g,`,\n `+` `.repeat(n)))
ETHproductions

2

Pyth, 35 30 ไบต์

+j++\,b*dhxz\(c<zKhxz\)", ">zK

ลองที่นี่!

คำอธิบาย:

+j++\,b*dhxz\(c<zKhxz\)", ">zK    # z = input()

                 Khxz\)           # Get index of the first ")"
               <z                 # Take the string until there...
              c        ", "       # ...and split it on the arguments
 j                                # Join the splitted string on...
  ++                              # ...the concatenation of...
    \,b                           # ...a comma followed by a newline...
       *dhxz\(                    # ...followed by the right amount of spaces = index of the first "(" + 1
+                         >zK     # Concat the resulting string with the postfix

2

Groovy, 137 89 95 ไบต์

Groovy ไม่ใช่ "เครื่องมือที่เหมาะสมกับงาน" ™ แก้ไข: มันทำงานได้ดีเมื่อคุณมีคนที่มีสมองใช้ ...

f={s=(it+' ').split(/\0/)
s[0].replace(',',',\n'+(' '*it.indexOf('(')))+')'+s[1..-1].join(')')}

แบบทดสอบ:

println f("def foo(bar, baz, quux):")
println f("int main() {")
println f("fn f(a: i32, b: f64, c: String) -> (String, Vec<i32>) {")
println f("function g(h) {")
println f("def abc(def, ghi, jkl, mno)")
println f("x y z(x, y, z) x, y, z)")

ค่อนข้างอวดดี:

f = {String it ->
    def str = (it + ' ').split(/\)/)
    return (str[0].replace (',', ',\n'+(' ' * it.indexOf('('))) + ')' + str[1])
}


1

JavaScript (ES6), 85

s=>s.replace(/^.*?\(|[^),]+, |.+/g,(x,p)=>[a+x,a=a||(p?`
`+' '.repeat(p):a)][0],a='')

ทดสอบ

f=s=>s.replace(/^.*?\(|[^),]+, |.+/g,(x,p)=>[a+x,a=a||(p?`
`+' '.repeat(p):a)][0],a='')

console.log=x=>O.textContent+=x+'\n'

;['def foo(bar, baz, quux):',
  'int main() {',
  'fn f(a: i32, b: f64, c: String) -> (String, Vec<i32>) {',
  'function g(h) {',
  'def abc(def, ghi, jkl, mno)',
  'x y z(x, y, z) x, y, z)']
.forEach(t=>console.log(t+'\n'+f(t)+'\n'))
<pre id=O></pre>


ฉันขอโทษที่ฉันทำผิดพลาดกำลังรันโค้ดในคอนโซลของฉันและผลลัพธ์เป็นดังนี้: "x y z(xคุณสามารถเห็น"สาเหตุที่ฉันคิดว่ามันเป็นที่ว่างหนึ่งครั้ง ดังนั้นการลบ
andlrc

@ dev-null ที่เกิดขึ้นกับฉันตลอดเวลา
edc65

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