告诉我的朋友他们属于哪个“逗号俱乐部”


16

受到像这样的硅谷“三个逗号俱乐部”场景的启发,在这一挑战中,您将告诉十个人每个人所属的“逗号俱乐部”。

如果您不熟悉“逗号俱乐部”一词,请让我解释一下:如果您拥有的钱在$ 1,000.00到$ 999,999.99之间,则您就是一个逗号俱乐部;如果您的帐户价格在$ 1,000,000.00到$ 999,999,999.99之间,则您在两个逗号俱乐部中;这些“俱乐部”通过三逗号俱乐部重复出现,因为地球上没有一个人拥有超过一万亿美元的资产(日元很快就会变成另一个故事)。因此,根据美国和英国最常见的标记标准,您的银行帐户中的逗号数表示您所属的逗号俱乐部。相同的“逗号”规则适用于负数(尽管您不希望成为负逗号俱乐部):负数(包括在内)[-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'3

2
是否保证f中的项是肯定的,或者-$1,234.56也将在1-逗号俱乐部中?
乔纳森·艾伦

2
@not_a_robot您能谈谈弗莱的评论吗?以任何格式输出成对的名称和数字是否足够?
马丁·恩德

1
@FryAmTheEggman不,输出格式必须为"<name> is in the <number of commas>-club."
blacksite

1
测试用例不是规格。即使是这样,也没有测试案例可以解决上述乔纳森·艾伦提出的大型底片问题。
彼得·泰勒

Answers:


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.`)

接受[friend,amount]数组的数组,并返回“ friend is in the n-逗号club”的数组。字符串。通过添加额外的尾随零来工作,以便0逗号俱乐部的长度为6-8、1逗号俱乐部的长度为9-11、2逗号俱乐部的长度为12-15等。

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


我认为这不适用于负数。例如,它将Nelly(-123.45)放入1逗号俱乐部。我建议短一点Math.log10(n*n)/6|0
Arnauld,

@Arnauld啊,是的,我忘了|0截断为零,因此它为Jamie提供了正确的答案。
尼尔

8

PostgreSQL,61个字节

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

@x是的绝对值xlog(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

双向链接(函数),它将银行余额列表作为数字(小数/整数),并将名称列表作为字符串,并返回字符串列表。

在线尝试!-页脚ç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


!我是第一个计算而不是计算字符的人吗?
泰特斯

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库进行字符串压缩。

受到@Neil 解决方案的启发。

@ETHproductions节省了7个字节

在线尝试!


1
我认为您可以Vw0 x4 l /3-2|0在中间部分保存6个字节:-)
ETHproductions

1
我认为您可以通过以相反的顺序输入来节省一个字节: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调用将产生NaNs表示否定。看起来log10(abs(a))会工作。
blacksite '17

糟糕,您是对的,上面的示例未包含Nelly。我看错了规格...我知道负值应该是0逗号。

代替abs(x),使用pmax(a,1)-这将取小于1的任何值并将其设为1,从而为负数提供良好的结果。并且,代替floor(log10(...)/3)您可以使用log10(...)%/%3。我认为它可以减少到66个字节(并纠正负数)。
Gregor-恢复莫妮卡

1
还值得注意的是,其中的7个字节是... ,cat()然后\n将带有字符串的向量打印到控制台可能被认为是足够好的(* ahem *就像Python的答案一样)。
Gregor-恢复莫妮卡

在3之后有一个额外的括号。它还为Jamie输出-1俱乐部。使用pmax(a,1)可以解决此问题。
BLT

3

JavaScript,59个字节

@ETHproductions节省了3个字节

@Cyoce节省了2个字节

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

演示版


1
与保存一个字节`${}`,你可以咖喱的功能n=>m=>...,而不是(n,m)=>...
Cyoce

2

Vim,72个字节

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

我应该以某种方式表明会有回报,但不确定如何。这只是一个基本的正则表达式答案,我相信任何正则表达式语言都可以击败它。我本来会使用V,但我认为/默认情况下,V中的替代命令用作分隔符,而且我想不出如何使其不抱怨除法的方法。

将输入作为OP的表,并返回值作为表,但财务信息替换为“在X逗号俱乐部”

在线尝试!


我不认为这些\-=增加了任何东西。同样,如果您删除了输入中的多余空格,并且也更改了正则表达式,则\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."}

假设2D数组输入为

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.'

在线尝试!

完整程序,包含两个列表的元组-名称为字符串,银行余额为数字-并输出结果字符串。


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(20715911095 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
最重要的是,这是一个代码段。通常,我们只允许使用函数或完整程序。至于打高尔夫球,您可以删除许多空间。尝试:["%s is in the %d-comma club."%(p,v.count(','))for p,v in zip(f,a)]另外,您必须包括一个字节数。
Conor O'Brien

您可以执行print'\n'.join([your array])并实际打印输出。
Elronnd

2
欢迎光临本站!负数(例如-123.45)相减一(因为您计算了-)。但是,为什么不拿一个数字列表保存呢(我误解了吗?)。您需要添加import语句,按照目前的情况,这只是一个代码段,而不是程序或函数(因为默认值是,除非有充分的理由,否则不建议更改它们)。
乔纳森·艾伦,

1
谢谢,@ JonathanAllan。我目前正在尝试改善这一点,因此不必计算进口量。我意识到Python的math.ceil功能可能会给我的成绩增加很多开销。
blacksite '17

2
替换(-len(str(abs(v)).split('.')[0])为可以节省9个字节len(str(int(float(v))))。在您的测试集中,没有负数(在示例中只有一个)。如果您想成为一名完全主义者,请花5个字节来做len(str(abs(int(float(v)))))-这只会为您节省4个字节。
iwaseatenbyagrue '17

1

Perl 6,107 95字节

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

这不是我最骄傲的工作,如果我忘记了任何高尔夫技巧,请多加注意。编辑:-12字节感谢@Ven


为什么要闲逛^@f.elems?顺便说一句,你不需要那个.elemsfor ^@f工作。您无需命名您的$x$_而是使用。而且不要使用printf,而要使用"{"interpolation"}"
Ven

我什至不明白你为什么需要那个。为什么不去for @f呢?您可以将其$++用作索引(并索引到@a)。
Ven

生病编辑只要IM上的PC :)
哈佛尼加德

1
@Ven感谢您对$ ++的注意。为我的“真实”程序省去了很多麻烦。刚拿起的Perl 6
哈佛尼加德

您确定插值不保存字节吗?
Ven

1

Python 2,87字节

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

稍旧(90字节):

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

将输入作为元组列表(名称,金额)。

我正在学校的手机上进行此操作,因此稍后再进行测试。


1

dc,56 54字节

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

这从堆栈中获取输入,应该在堆栈顶部预加载名字,第一个数字,第二个名字,第二个数字,等等。

这是加载堆栈并运行宏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

产生通常的输出,

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.

这是代码的解释:

[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

请注意,在编辑1中,我将dZrX-(d = dup,Z =数字长度,r =交换,X =分数,-=减法)替换为1/Z(数字除以1,其默认小数位数为0会截断为整数;然后Z =数字长度),节省两个字节。


1

迅速166个 158 145字节

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)}

这是字典:

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
]

在这里尝试!


0

Clojure,108个字节

(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)

不知道对浮点数的操作是否比对字符序列的操作要短。不知道非功能性答案是否正确,它会返回一系列答案。


0

Rebol,118个字节

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."]]

取消数组声明:

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."]
]

输出:

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

爪哇8 154 141字节

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

不打高尔夫球

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
您可以删除String.valueOf和使用(((""+Math.abs(m.get(k).longValue())).length()-1)/3)
凯文·克鲁伊森

1
并且您也可以删除圆括号k->
凯文·克鲁伊森
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.