บอกเพื่อนของฉันว่าเป็นใคร


16

แรงบันดาลใจจากฉาก "สามคอมม่าคลับ" ของซิลิคอนวัลเลย์เช่นนี้ในความท้าทายนี้คุณจะบอกคนสิบคนว่า "คอมม่าคลับ" ที่แต่ละคนเป็นเจ้าของ

หากคุณไม่คุ้นเคยกับคำว่า "comma club" ให้ฉันอธิบาย: คุณอยู่ใน one-comma club ถ้าเงินที่คุณมีอยู่ในช่วงรวม $ 1,000.00 ถึง $ 999,999.99 คุณอยู่ในสโมสรสองคอมมาถ้าอยู่ในช่วง $ 1,000,000.00 ถึง $ 999,999,999.99 "คลับ" เหล่านี้ทำซ้ำผ่านสโมสรสามคอมม่าเนื่องจากไม่มีบุคคลในโลก (AFAIK) ที่เป็นเจ้าของมากกว่าหนึ่งล้านล้านดอลลาร์สหรัฐ (เงินเยนของญี่ปุ่นจะเป็นเรื่องที่แตกต่างอย่างรวดเร็ว) ดังนั้นจำนวนเครื่องหมายจุลภาคบัญชีธนาคารของคุณตามมาตรฐานสัญกรณ์ที่พบมากที่สุดในสหรัฐอเมริกาและสหราชอาณาจักรหมายถึงสโมสรจุลภาคที่คุณเป็นเจ้าของ กฎ "เครื่องหมายจุลภาค" เดียวกันนี้ใช้กับจำนวนลบ (แม้ว่าคุณจะไม่ต้องการอยู่ในเครื่องหมายจุลภาคเชิงลบ): จำนวนลบในช่วงรวม [-0.01, -999

กรณีทดสอบ

Friend    Amount
John      100000
Jamie     0.05
Kylie     1549001.10
Laura     999999999.99
Russ      986000000
Karla     1
Reid      99.99
Mark      999.99
Manson    1000.01
Lonnie    999999999999.00
Nelly     -123.45

คำตอบที่ถูกต้อง:

John is in the 1-comma club.
Jamie is in the 0-comma club.
Kylie is in the 2-comma club.
Laura is in the 2-comma club.
Russ is in the 2-comma club.
Karla is in the 0-comma club.
Reid is in the 0-comma club.
Mark is in the 0-comma club.
Manson is in the 1-comma club.
Lonnie is in the 3-comma club.
Nelly is in the 0-comma club.

ไม่ว่าการตั้งค่าอาเรย์ใด ๆ ก็ตามคุณจะต้องได้friendsอาเรย์และamountsอาเรย์นั้นจะไม่นับรวมคะแนนของคุณ ดังนั้นสำหรับ Python รหัสต่อไปนี้จะไม่นับ:

f = ['John', 'Jamie', 'Kylie', 'Laura', 'Russ', 'Karla', 'Reid', 'Mark', 'Manson', 'Lonnie']
a = ['100000', '0.05', '1549001.10', '999999999.99', '986000000', '1', '99.99', '999.99', '1000.01', '999999999999.00']

แก้ไข: โปรดดูกรณีทดสอบที่แก้ไข

ฉันลบเครื่องหมายจุลภาคสตริงจริงออกจากกรณีทดสอบเพื่อทำให้สิ่งต่าง ๆ ยากขึ้นเมื่อเทียบกับเพียงแค่เครื่องหมายจุลภาคที่นับ


2
ฉันไม่คิดว่าคุณทำรูปแบบผลลัพธ์ชัดเจนมาก เป็น"Name number,Name number,..."ที่ยอมรับหรือไม่?
FryAmTheEggman

2
รายการใน f รับประกันว่าจะเป็นค่าบวกหรือจะ-$1,234.56อยู่ในสโมสร 1 เครื่องหมายจุลภาคหรือไม่
Jonathan Allan

2
@not_a_robot คุณช่วยพูดความคิดเห็นของ Fry ได้ไหม? มันเพียงพอที่จะส่งออกคู่ของชื่อและหมายเลขในรูปแบบใด ๆ ?
Martin Ender

1
@FryAmTheEggman ไม่เอาต์พุตต้องอยู่ในรูปแบบ"<name> is in the <number of commas>-club."
blacksite

1
กรณีทดสอบไม่ได้ระบุ และถึงแม้ว่าพวกเขาจะไม่มีกรณีทดสอบที่ครอบคลุมปัญหาของฟิล์มเนกาทีฟขนาดใหญ่ที่โจนาธานอัลลันกล่าวไว้ข้างต้น
Peter Taylor

คำตอบ:


10

JavaScript (ES6), 80 ไบต์

a=>a.map(([s,n])=>s+` is in the ${n.toFixed(n<0?3:4).length/3-2|0}-comma club.`)

ใช้อาร์เรย์ของ [เพื่อนจำนวน] อาร์เรย์และส่งกลับอาร์เรย์ของ "เพื่อนอยู่ในสโมสร n-comma" เงื่อนไข ทำงานโดยการเพิ่มศูนย์ต่อท้ายพิเศษเพื่อให้มีความยาว 6-8 สำหรับ 0-comma club, 9-11 สำหรับ 1-comma club, 12-15 สำหรับ 2-comma club เป็นต้น

https://jsfiddle.net/cau40vmk/1/


ฉันไม่คิดว่าวิธีนี้ใช้กับจำนวนลบได้ ตัวอย่างเช่นมันจะใส่ Nelly (-123.45) ใน 1-comma club Math.log10(n*n)/6|0ผมขอแนะนำให้สั้นลง
Arnauld

@Arnauld Ah ใช่ฉันลืม|0ตัดให้เหลือศูนย์ดังนั้นจึงให้คำตอบที่ถูกต้องสำหรับ Jamie
Neil

8

PostgreSQL 61 ไบต์

SELECT f||' is in the '||div(log(@a),3)||'-comma club.'FROM p

@xคือค่าสัมบูรณ์ของx, log(x)คือลอการิทึมฐาน 10 และdiv(y, x)คำนวณหาผลหารจำนวนเต็มของ y / x


ติดตั้ง:

CREATE TEMP TABLE p AS
SELECT * FROM (VALUES
    ('John', 100000),
    ('Jamie', 0.05),
    ('Kylie', 1549001.10),
    ('Laura', 999999999.99),
    ('Russ', 986000000),
    ('Karla', 1),
    ('Reid', 99.99),
    ('Mark', 999.99),
    ('Manson', 1000.01),
    ('Lonnie', 999999999999.00),
    ('Nelly', -123.45)
) AS p (f, a)

เอาท์พุท:

            ?column?            
--------------------------------
 John is in the 1-comma club.
 Jamie is in the 0-comma club.
 Kylie is in the 2-comma club.
 Laura is in the 2-comma club.
 Russ is in the 2-comma club.
 Karla is in the 0-comma club.
 Reid is in the 0-comma club.
 Mark is in the 0-comma club.
 Manson is in the 1-comma club.
 Lonnie is in the 3-comma club.
 Nelly is in the 0-comma club.
(11 rows)

ยินดีที่ได้เห็นคำตอบ SQL ที่แข่งขันได้ - ทำได้ดีมาก!
Toby Speight

6

เยลลี่ , 34  32 ไบต์

AḞbȷL’⁶;“£ṙƬs⁾`¬ụṂ“¢<ỴȦ8£l»jµ€⁹żY

ลิงก์ dyadic (ฟังก์ชัน) ซึ่งรับรายการยอดคงเหลือของธนาคารเป็นตัวเลข (ทศนิยม / จำนวนเต็ม) และรายการของชื่อเป็นสตริงและส่งคืนรายการสตริง

ลองออนไลน์! - ส่วนท้ายçYเพียงเรียกใช้ฟังก์ชันและรวมรายการผลลัพธ์ด้วยการป้อนบรรทัดดังนั้นจึงมีเอาต์พุตที่ดีเมื่อรันเป็นโปรแกรมแบบเต็ม

อย่างไร?

AḞbȷL’⁶;“£ṙƬs⁾`¬ụṂ“¢<ỴȦ8£l»jµ€⁹żY - Main link: bankBalances, names
                             €    - for each bankBalance:
A                                 - absolute value (treat negatives and positives the same)
 Ḟ                                - floor (get rid of any pennies)
  bȷ                              - convert to base 1000
    L                             - length (number of digits in base 1000)
     ’                            - decrement by one
      ⁶;                          - concatenate a space with that   ...because -------.
        “         “       »       - compressed list of strings:                       ↓
         £ṙƬs⁾`¬ụṂ                -     " is in the"  ← cannot compress a trailing space :(
                   ¢<ỴȦ8£l        -     "-comma club."
                           j      - join that list of strings with the "number plus space"
                            µ     - monadic chain separation (call that result L)
                              ⁹   - right argument (names)
                               ż  - zip with L

4

PHP, 76 74 ไบต์

// data as associative array
$d=[Poorman=>-1234,John=>100000,Jamie=>0.05,Kylie=>1549001.10,Laura=>999999999.99,Russ=>1000000000,Karla=>1,Reid=>99.99,Mark=>999.99,Manson=>1000.01,Lonnie=>999999999999.00];
// code
foreach($d as$n=>$a)printf("$n is in the %d-comma club.
",log($a*$a,1e6));

โชคดีที่ฉันไม่จำเป็นต้องโยนให้ int (อย่างที่ฉันต้องทำใน C); PHP %dไม่ว่าโดยปริยายสำหรับ


Gee! ฉันเป็นคนแรกที่จะคำนวณแทนการนับตัวอักษรหรือไม่
ติตัส

4

Mathematica (86 ไบต์)

การตั้งค่า (ที่มีชื่อเป็นสตริงเงินเป็นตัวเลข):

n = {"John", "Jamie", "Kylie", "Laura", "Russ", "Karla", "Reid", "Mark", "Manson", "Lonnie", "Nelly"};
m = {100000, 0.05, 1549001.10, 999999999.99, 1000000000, 1, 99.99, 999.99, 1000.01, 999999999999.00, -123.45}

ความพยายาม:

MapThread[#~~" is in the "~~ToString@Max[Floor@Log[10^3,#2],0]~~"-comma club."&,{n,m}]

ฟังก์ชันสตริงทั้งหมดของ Mathematica มีชื่อ "String" อยู่ด้วยดังนั้นฉันคิดว่าบันทึกสั้นกว่า การMax[...,0]จัดการกับตัวเลขติดลบหรือลบอนันต์สำหรับคนที่มีค่าระหว่าง -1 ถึง 1 ดอลลาร์ บันทึกของจำนวนลบประกอบด้วยสิ่งในจินตนาการ แต่ Mathematica ก็เพิกเฉยต่อสิ่งนั้นเมื่อจดFloor!


4

Japt , 36 ไบต์

ซึ่งจะใช้จำนวนเงินเป็นอินพุตแรกชื่อเป็นที่สอง

V+`   e {w0 x4 l /3-2|0}-¬mµ club

คำอธิบาย

V+`   e {w0 x4 l /3-2|0}-¬mµ club
V+                                   // Second input +
  `                                  // compressed string:
      e                              // " is in the " 
        {              }             // Insert here:
         w0                          //   The larger of 0 and the first input
            x4                       //   Rounded to the 4th decimal
               l                     //   Length
                        -¬mµ club    // "-comma club"
                                     // A closing backtick is auto-inserted at the end of the program

Japt ใช้ไลบรารี shocoสำหรับการบีบอัดสตริง

แรงบันดาลใจจากนีล @ วิธีการแก้ปัญหา

บันทึกแล้ว 7 ไบต์ด้วย @ETHproductions

ลองออนไลน์!


1
ฉันคิดว่าคุณสามารถทำได้Vw0 x4 l /3-2|0ในส่วนตรงกลางประหยัด 6 bytes :-)
ETHproductions

1
ฉันคิดว่าคุณสามารถบันทึก byte โดยการป้อนในลำดับที่ตรงข้าม:V+`...{w0...
ETHproductions

3

MATL, 50 48 43 ไบต์

`j' is in the 'i|kVn3/XkqV'-comma club'&hDT

ลองที่MATL Online

คำอธิบาย

`                   % Do...While loop
  j                 % Explicitly grab the next input as a string
  ' is in the the ' % Push this string literal to the stack
  i                 % Grab the next input as a number
  |                 % Compute the absolute value
  k                 % Round towards zero
  V                 % Convert to a string
  n3/Xk             % Divide the length of the string by 3 and round up
  q                 % Subtract one
  V                 % Convert to a string
  '-comma club'     % Push this string literal to the stack
  &h                % Horizontally concatenate the entire stack
  D                 % Display the resulting string
  T                 % Push TRUE to the stack, causing an infinite loop which automatically
                    % terminates when we run out of inputs
                    % Implicit end of do...while loop

3

R, 68 ไบต์

การตั้งค่า:

f <- c('John', 'Jamie', 'Kylie', 'Laura', 'Russ', 'Karla', 'Reid', 'Mark', 'Manson', 'Lonnie', 'Nelly')
a <- c(100000, 0.05, 1549001.10, 999999999.99, 986000000, 1, 99.99, 999.99, 1000.01, 999999999999.00, -123.45)

การแก้ไขปัญหา:

cat(paste0(f, " is in the ",floor(log10(abs(a))/3)),"-comma club.\n"))

จดบันทึกฐาน 10 ของค่าสัมบูรณ์ของบัญชีปัดเศษลงแล้วพิมพ์ชื่อ

ฉันไม่แน่ใจว่ามันจะเล็กกว่านี้ถ้าaเป็นเวกเตอร์ตัวละคร ...


1
สิ่งที่เกี่ยวกับกรณีของ Nelly ใครมี -123.45 ดอลล่าร์? การlog10โทรนั้นจะสร้างNaNผลลบ ดูเหมือนว่าlog10(abs(a))จะทำงาน
blacksite

โอ๊ะคุณพูดถูกแล้วตัวอย่างข้างต้นไม่รวม Nelly และฉันอ่านข้อกำหนดผิดไป ... ฉันเข้าใจว่าค่าลบควรเป็น 0 คอมม่าคลับ
Joe

แทนที่จะabs(x)ใช้pmax(a,1)- สิ่งนี้จะใช้อะไรน้อยกว่า 1 และทำให้เป็น 1 เพื่อให้ได้ผลลัพธ์ที่ดีสำหรับตัวเลขลบ และแทนที่จะคุณสามารถใช้floor(log10(...)/3) log10(...)%/%3ฉันคิดว่ามันลดลงเหลือ 66 ไบต์ (และถูกต้องสำหรับเชิงลบ)
Gregor --reinstate Monica--

1
นอกจากนี้ยังมีข้อสังเกตว่าการมี 7 ไบต์เต็มรูปแบบคือcat()และ\n... การพิมพ์เวกเตอร์ที่มีสตริงในคอนโซลอาจถือว่าดีพอ (* ahem * เช่นคำตอบของ Python)
Gregor --reinstate Monica--

นี่คือวงเล็บพิเศษหลังจาก 3 มันยังส่งออก -1 สโมสรสำหรับเจมี่ การใช้ pmax (a, 1) แก้ไขได้
BLT

3

JavaScript ขนาด 59 ไบต์

บันทึกแล้ว 3 ไบต์ด้วย @ETHproductions

บันทึก 2 ไบต์ขอบคุณ @Cyoce

n=>m=>n+` is in the ${m>1?Math.log10(m)/3|0:0}-comma club.`

การสาธิต


1
บันทึกไบต์ด้วย`${}`และคุณสามารถแก้ไขฟังก์ชันn=>m=>...เป็น(n,m)=>...
Cyoce

2

เป็นกลุ่ม 72 ไบต์

:%s;\v +\-=(\d+).*;\=' is in the '.(len(submatch(1))-1)/3.' comma club'

ยังไงก็เถอะฉันควรจะแสดงให้เห็นว่ามีการย้อนกลับ แต่ไม่แน่ใจว่าจะทำอย่างไร นี่เป็นเพียงคำตอบ regex ขั้นพื้นฐานที่ฉันแน่ใจว่าจะพ่ายแพ้โดยภาษา regex ใด ๆ ฉันจะใช้ V แต่ฉันคิดว่าคำสั่งทดแทนใน V ใช้/เป็นตัวคั่นตามค่าเริ่มต้นและฉันไม่สามารถหาวิธีที่จะทำให้มันไม่บ่นเกี่ยวกับการหาร

รับอินพุตเป็นตารางของ OP และส่งกลับค่าเป็นตาราง แต่ด้วยข้อมูลทางการเงินที่ถูกแทนที่ด้วย "อยู่ใน X comma club"

ลองออนไลน์!


ฉันไม่คิดว่า\-=จะเพิ่มอะไรอีก นอกจากนี้หากคุณลบช่องว่างเพิ่มเติมในอินพุตของคุณและเปลี่ยน regex ด้วย\v (\d*).*;คุณสามารถบันทึก 4 ไบต์
DJMcMayhem

2

05AB1E , 32 ไบต์

Äï€g3/î<“-comma†Ú“«“€ˆ€†€€ “ì‚ø»

ลองออนไลน์!

คำอธิบาย

Ä                                 # absolute value of input
 ï                                # convert to int
  €g                              # length of each
    3/                            # divided by 3
      î                           # round up
       <                          # decrement
        “-comma†Ú“«               # append string "-comma club" to each number
                   “€ˆ€†€€ “ì     # prepend string "is in the " to each number
                             ‚    # pair with second input
                              ø   # zip
                               »  # join by spaces and newlines

2

Python 2, 69 ไบต์

ตั้งค่าอาร์เรย์ของเรา:

n = ['John', 'Jamie', 'Kylie', 'Laura', 'Russ', 'Karla', 'Reid', 'Mark', 'Manson', 'Lonnie']
a = [100000, 0.05, 1549001.10, 999999999.99, 1000000000, 1, 99.99, 999.99, 1000.01, 999999999999.00]

และฟังก์ชั่นของเราสามารถเป็น:

f=lambda n,a:'%s is in %s comma club'%(n,min(3,(len(str(int(a)))/3)))

ให้เรา:

>>> [f(x,y) for x,y in zip(n,a)]
['John is in 2 comma club', 'Jamie is in 0 comma club', 'Kylie is in 2 comma club', 'Laura is in 3 comma club', 'Russ is in 3 comma club', 'Karla is in 0 comma club', 'Reid is in 0 comma club', 'Mark is in 1 comma club', 'Manson is in 1 comma club', 'Lonnie is in 3 comma club']

หากอาร์เรย์ต้องเหมือนกันกับที่มีให้ในคำถามโซลูชันจะมีราคา 76 ไบต์:

f=lambda n,a:'%s is in %s comma club'%(n,min(3,(len(str(int(float(a))))/3)))

2

Powershell, 82 ไบต์

$args[0]|%{$_[0]+" is in the "+(('{0:N}'-f$_[1]-split',').Count-1)+"-comma club."}

สมมติว่าอินพุตแบบสองมิติของ

cc.ps1 @(@("John",100000),@("Jamie",0.05),@("Kylie",1549001.10),@("Laura",999999999.99),@("Russ",986000000),@("Karla",1),@("Reid",99.99),@("Mark",999.99),@("Manson",1000.01),@("Lonnie",999999999999.00),@("Nelly",-123.45))

ผลลัพธ์คือ John is in the 1-comma club. Jamie is in the 0-comma club. Kylie is in the 2-comma club. Laura is in the 2-comma club. Russ is in the 2-comma club. Karla is in the 0-comma club. Reid is in the 0-comma club. Mark is in the 0-comma club. Manson is in the 1-comma club. Lonnie is in the 3-comma club. Nelly is in the 0-comma club.


2

Haskell, 71 ไบต์

n#a=n++" is in the "++(show.truncate.logBase 1e3.abs$a)++"-comma club."

กำหนดโอเปอเรเตอร์ '#' ที่ให้คำตอบที่ถูกต้อง เช่น:

*Main> "John"#100000
"John is in the 1-comma club."

น่าเสียดายที่ Haskell ไม่มีlog10ฟังก์ชั่นกะทัดรัดเช่นภาษาอื่น ๆ อีกมากมาย แต่มันมีlogBaseฟังก์ชั่นที่มีประโยชน์ซึ่งหมายความว่าเราไม่จำเป็นต้องหารคำตอบของเราด้วย 3 น่าเสียดายที่logBase 1000 0.05เป็นจำนวนลบดังนั้นเราต้องใช้ ยาวtruncateมากกว่าที่floorจะปัดเศษมัน

โปรแกรมเต็มรูปแบบรวมถึงกรณีทดสอบ:

(#) :: (RealFrac n, Floating n) => [Char] -> n -> [Char]
n#a=n++" is in the "++(show.truncate.logBase 1e3.abs$a)++"-comma club."

testCases = [
 ("John",      100000),
 ("Jamie",     0.05),
 ("Kylie",     1549001.10),
 ("Laura",     999999999.99),
 ("Russ",      986000000),
 ("Karla",     1),
 ("Reid",      99.99),
 ("Mark",      999.99),
 ("Manson",    1000.01),
 ("Lonnie",    999999999999.00),
 ("Nelly",     -123.45)]

main = putStrLn $ unlines $ map (uncurry (#)) testCases

ให้ผลลัพธ์ต่อไปนี้:

John is in the 1-comma club.
Jamie is in the 0-comma club.
Kylie is in the 2-comma club.
Laura is in the 2-comma club.
Russ is in the 2-comma club.
Karla is in the 0-comma club.
Reid is in the 0-comma club.
Mark is in the 0-comma club.
Manson is in the 1-comma club.
Lonnie is in the 3-comma club.
Nelly is in the 0-comma club.

1

K, 66 ไบต์

    /n is names and a is amounts
    n
    ("John";"Jamie";"Kylie";"Laura";"Russ";"Karla";"Reid";"Mark";"Manson";"Lonnie")
    a
    ("100000";"0.05";"1549001.10";"999999999.99";"1000000000";,"1";"99.99";"999.99";"1000.01";"999999999999.00")
    /the function
    {x," is in the ",($(#.q.cut[3;*"."\:y])-1)," comma club"}./:+(n;a)
    /output
    ("John is in the 1 comma club";"Jamie is in the 0 comma club";"Kylie is in the 2 comma club";"Laura is in the 2 comma club";"Russ is in the 3 comma club";"Karla is in the 0 comma club";"Reid is in the 0 comma club";"Mark is in the 0 comma club";"Manson is in the 1 comma club";"Lonnie is in the 3 comma club")

1

Python 2.7 , 89 86 84 ไบต์

for g,b in zip(*input()):print g,'is in the',`(len('%d'%b)+~(b<0))/3`+'-comma club.'

ลองออนไลน์!

โปรแกรมเต็มรูปแบบที่รับ tuple ของสองรายการ - ชื่อเป็นสตริง, ธนาคารยอดคงเหลือเป็นตัวเลข - และพิมพ์สตริงผลลัพธ์


1

C #, 125 ไบต์

(p,n)=>{for(int x=0;x<p.Length;x++)Console.Write(p[x]+" is in the "+(n[x]<1e3?0:n[x]<1e6?1:n[x]<1e9?2:3)+"-comma club.\n");};

ฟังก์ชั่นไม่ระบุชื่อซึ่งพิมพ์ผลลัพธ์ในรูปแบบของ OP

โปรแกรมเต็มรูปแบบพร้อมกรณีทดสอบ:

using System;

class CommaClub
{
    static void Main()
    {
        Action<string[], double[]> f =
        (p,n)=>{for(int x=0;x<p.Length;x++)Console.Write(p[x]+" is in the "+(n[x]<1e3?0:n[x]<1e6?1:n[x]<1e9?2:3)+"-comma club.\n");};

        // test cases:
        string[] personArr = new[] {"John", "Jamie", "Kylie", "Laura", "Russ", "Karla", "Reid", "Mark", "Manson", "Lonnie", "Nelly"};
        double[] amountArr = new[] {100000, 0.05, 1549001.10, 999999999.99, 1000000000, 1, 99.99, 999.99, 1000.01, 999999999999.00, -123.45};
        f(personArr, amountArr);
    }
}

1

Python 3 ( 207 159 110 95 86 ไบต์ขอบคุณ @iwaseatenbyagrue)

การตั้งค่าเล็กน้อย:

f = ['John', 'Jamie', 'Kylie', 'Laura', 'Russ', 'Karla', 'Reid', 'Mark', 'Manson', 'Lonnie', 'Nelly']
a = [100000, 0.05, 1549001.10, 999999999.99, 986000000, 1, 99.99, 999.99, 1000.01, 999999999999.00, -123.45]

ความพยายามของฉัน:

['%s is in the %d-comma club.'%(p,-(-len(str(int(abs(v))))//3)-1) for p,v in zip(f,a)]

ผล:

['John is in the 1-comma club.', 'Jamie is in the 0-comma club.', 'Kylie is in the 2-comma club.', 'Laura is in the 2-comma club.', 'Russ is in the 2-comma club.', 'Karla is in the 0-comma club.', 'Reid is in the 0-comma club.', 'Mark is in the 0-comma club.', 'Manson is in the 1-comma club.', 'Lonnie is in the 3-comma club.', 'Nelly is in the 0-comma club.']

แก้ไข: การแปลงทุกอย่างเป็นค่าสัมบูรณ์ช่วยฉัน 15 ไบต์


1
Most importantly, this is a code snippet, Typically we only allow functions or full programs. As for golfing, You can remove many spaces. Try: ["%s is in the %d-comma club."%(p,v.count(','))for p,v in zip(f,a)] Also, you must include a byte count.
Conor O'Brien

You can do print'\n'.join([your array]) and actually print output.
Elronnd

2
Welcome to the site! Negatives (e.g. -123.45) are off by one (since you count the -). But why not take a list of numbers and save (have I misinterpreted?). You'd need to add import statements, and as it stands this is a snippet not a program or function (as the defaults are, and changing from them is discouraged unless there is good reason).
Jonathan Allan

1
Thanks, @JonathanAllan. I'm currently trying to improve this so I don't have to count the imports. I realize that Python's math.ceil function likely adds quite a bit of overhead to my score.
blacksite

2
You can save 9 bytes by replacing (-len(str(abs(v)).split('.')[0]) with len(str(int(float(v)))). In your test set, there are no negative numbers (in the examples, there is one). If you wanted to be completist, spend 5 bytes to do len(str(abs(int(float(v))))) - which then only saves you 4 bytes.
iwaseatenbyagrue

1

Perl 6, 107 95 bytes

for @f {printf "%s is in the %d-comma club.\n",@f[$++],(abs(@a[$++].split(".")[0]).chars-1)/3;}

Not my proudest work, give me a heads up if ive forgotten any golfing techniques. EDIT: -12 bytes thanks to @Ven


why parens around ^@f.elems? btw you don't need that .elems, for ^@f works. You don't need to name your $x, use $_ instead. And don't use printf, use "{"interpolation"}".
Ven

I don't even understand why you need that. Why not go for for @f? You can use $++ as an index to go along with it (and index into @a).
Ven

ill edit as soon as im on the pc :)
Håvard Nygård

1
@Ven thanks for the heads up about the $++. Saves me alot of trouble in my "real" programs. Just picked up Perl 6.
Håvard Nygård

Are you sure interpolations don't save bytes?
Ven

1

Python 2, 87 bytes

for n,a in input():print n+' is in the %dd-comma club.'%'{:20,.2f}'.format(a).count(',')

Slightly older (90 bytes):

for n,a in input():print n+' is in the '+`'{:20,.2f}'.format(a).count(',')`+'-comma club.'

Takes input as a list of tuples (name, amount).

I'm doing this on my phone at school so I'll test it later.


1

dc, 56 54 bytes

[P[ is in the ]Pd*vdZrX-1-3/n[-comma club.]pstz0<g]sglgx

This takes input from the stack, which should be pre-loaded with the first name at top of stack, the first number, the second name, second number, etc.

Here is an example of loading the stack and running the macro, g:

#!/usr/bin/dc
_123.45         [Nelly]
999999999999.00 [Lonnie]
1000.01         [Manson]
999.99          [Mark]
99.99           [Reid]
1               [Karla]
986000000       [Russ]
999999999.99    [Laura]
1549001.10      [Kylie]
0.05            [Jamie]
100000          [John]
[P[ is in the ]Pd*v1/Z1-3/n[-comma club.]pstz0<g]sglgx

which produces the usual output,

John is in the 1-comma club.
Jamie is in the 0-comma club.
Kylie is in the 2-comma club.
Laura is in the 2-comma club.
Russ is in the 2-comma club.
Karla is in the 0-comma club.
Reid is in the 0-comma club.
Mark is in the 0-comma club.
Manson is in the 1-comma club.
Lonnie is in the 3-comma club.
Nelly is in the 0-comma club.

Here's an exegesis of the code:

[P[ is in the ]Pd*v1/Z-1-3/n[-comma club.]pstz0<g]sglgx

[                    # begin macro string
P                    # print and pop person name
[ is in the ]P       # print and pop ' is in the '
# Get absolute value of number by squaring and square root
d*v                  # d=dup, *=multiply, v=root
1/                   # 1/ truncates to integer since scale is 0
Z                    # Z=number length
1-3/n                # n=print and pop (#digits - 1)//3
[-comma club.]p      # print '-comma club.' and newline
st                   # pop '-comma club.' off stack into register t
z0<g                 # Do macro g if 0 is less than z=stack height
]                    # end macro string
sg                   # Save macro g
lgx                  # Load g and do its initial execution

Note, in edit 1 I replaced dZrX- (d=dup, Z=number length, r=swap, X=fraction, -=subtract) with 1/Z (divide number by 1, which with default scale of zero truncates to integer; then Z=number length), saving two bytes.


1

Swift, 166 158 145 bytes

var c="comma club",i=0
f.map{k,v in var a=abs(v);print(k,(1000..<1000000~=a ?1:1000000..<1000000000~=a ?2:1000000000..<1000000000000~=a ?3:0),c)}

And here is the dictionary:

var f = [
    "John": 100000, "Jamie": 0.05, "Kylie" : 1549001.10,
    "Laura": 999999999.99,"Russ":1000000000,"Karla": 1,
    "Reid": 99.99,"Mark": 999.99, "Manson": 1000.01,
    "Lonnie": 999999999999.00, "Nelly": -123.45
]

Try it here!


0

Clojure, 108 bytes

(def f ["John", "Jamie", "Kylie", "Laura", "Russ", "Karla", "Reid", "Mark", "Manson", "Lonnie", "Nelly"])
(def a ["100000", "0.05", "1549001.10", "999999999.99", "986000000", "1", "99.99", "999.99", "1000.01", "999999999999.00", "-123.45"])

(map #(str %" is in the "(quot(-(count(nth(partition-by #{\.}(drop-while #{\-}%2))0))1)3)"-comma club.")f a)

Not sure if operating on floats would be shorted than on sequences of characters. Not sure if non-function answer is fine which returns a sequence of answers.


0

Rebol, 118 bytes

d: charset"0123456789"forskip s 2[c: 0 parse s/2[opt"-"any[3 d and d(++ c)]]print[s/1"is in the"join c"-comma club."]]

Ungolfed with array declaration:

s: [
    {John} {100000}
    {Jamie} {0.05}
    {Kylie} {1549001.10}
    {Laura} {999999999.99}
    {Russ} {986000000}
    {Karla} {1}
    {Reid} {99.99}
    {Mark} {999.99}
    {Manson} {1000.01}
    {Lonnie} {999999999999.00}
    {Nelly} {-123.45}
    {Baz} {1.12345678}     ;; added extra Baz test case
]

d: charset "0123456789"
forskip s 2 [
    c: 0
    parse s/2 [
        opt "-"
        any [3 d and d (++ c)]
    ]
    print [s/1 "is in the" join c "-comma club."]
]

Output:

John is in the 1-comma club.
Jamie is in the 0-comma club.
Kylie is in the 2-comma club.
Laura is in the 2-comma club.
Russ is in the 2-comma club.
Karla is in the 0-comma club.
Reid is in the 0-comma club.
Mark is in the 0-comma club.
Manson is in the 1-comma club.
Lonnie is in the 3-comma club.
Nelly is in the 0-comma club.
Baz is in the 0-comma club.

0

Java 8 154 141 bytes

m.keySet().stream().map(k->k+" is in the "+(((""+Math.abs(m.get(k).longValue()))).length()-1)/3+"-comma club.").forEach(System.out::println);

Ungolfed

public static void main(String[] args) {
    Map<String, Number> m = new LinkedHashMap<String, Number>(){{
        put("John", 100000);
        put("Jamie", 0.05);
        put("Kylie", 1549001.10);
        put("Laura", 999999999.99);
        put("Russ", 1000000000);
        put("Karla", 1);
        put("Reid", 99.99);
        put("Mark", 999.99);
        put("Manson", 1000.01);
        put("Lonnie", 999999999999.00);
        put("Nelly", -123.45);
    }};
    m.keySet().stream().map(k->k+" is in the "+(((""+Math.abs(m.get(k).longValue()))).length()-1)/3+"-comma club.").forEach(System.out::println);
}

1
You can remove the String.valueOf and use (((""+Math.abs(m.get(k).longValue())).length()-1)/3) instead.
Kevin Cruijssen

1
And you can also remove the parenthesis around k->.
Kevin Cruijssen
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.