别名还是N8?


10

numeronym(也称为“数字收缩”)是当一个字使用数字缩短。一种常见的收缩方法是使用替换后的子字符串的长度来替换除第一个和最后一个字符以外的所有字符。例如,使用i18n的代替internationalization或,L10n而不是localization。(L大写字母是因为小写字母看起来与相似1)。

当然,同一个词组中的几个词可能具有相同的缩写形式,因此您的工作是将一组词转换为它们的别名,或者,如果存在一些具有相同词名的不同词,则程序应给出一个的A7s R4t缩写,Ambiguous Result是的(是的,我知道这个结果本身就是模棱两可的结果。)

规则:

  • 使用程序或功能,然后打印或返回结果。
  • 输入被视为单个字符串。
  • 输出是由空格分隔的单词的单个字符串。
  • 您的程序无需转换长度为3的单词,也不必转换较短的单词。
  • 如果l(小写的ell)将在1(一个)之前出现,则应将其设置为大写。
  • 如果I(一只大写的眼睛)出现在1(一只)之前,则应将其设为小写。
  • 输入将是可打印的ASCII和空格。单词之间用空格隔开。
  • 最短的代码获胜。

例子:

A7s R4t -> A7s R4t (OR: A1s R1t, etc)
Ambiguous Result -> A7s R4t
Billy goats gruff -> B3y g3s g3f
Thanks for the Memories -> T4s f1r the M6s (one possible answer, NOT: Thnks fr th Mmrs)
Programming Puzzles & Code Golf -> P9g P5s & C2e G2f
globalization -> g11n
localizability -> L12y
Internationalization or antidisestablishmentarianism -> i18n or a26m
Internationalization or InternXXXXXalization -> A7s R4t
what is this fiddle and faddle -> A7s R4t
A be see -> A be s1e (OR: A be see)
see sea -> s1e s1a (OR: see sea)
2B or not 2B -> 2B or not 2B (OR: 2B or n1t 2B. 2 letters, don't change, don't count as ambiguous)
this example is this example -> t2s e5e is t2s e5e (same words aren't ambiguous)
l1 -> l1 (2 letters, don't change.)
I1 -> I1 (2 letters, don't change.)

编辑:如果没有人得到参考:Thnks fr th Mmrs


输入中也可以包含数字吗?如果是这样,则输入l1变为L1或保持为l1
门把手

我从强调设定不同的是example examplee5e e5e,但是这将是很好的,具有覆盖本测试用例。
彼得·泰勒

为您的两个问题(2B or not 2B及之后的问题)添加了测试用例
mbomb007

Answers:


1

J,92个字节

(' 'joinstring(<&(]`(rplc&('I1';'i1')&(rplc&('l1';'L1'))&({.,([:":#-2:),{:))@.(#>3:))&>&;:))

一连串的组合呼叫。第一部分只是一个fork:一个文字joinstring,和一个函数(在J中1 (10 + +) 2(10 + (1 + 2)))。第二部分是功能。这两个rplc调用将在它们可能混淆时替换I / l。 &是组成,并用值咖喱构成一个运算符。(因此3&+提供了一个期望添加第二个参数的函数)。最后,最后一部分是采用第一个,length-2(由字符串化组成)和最后一个的函数。&>&;:末尾的位将此函数(应应用于每个自变量)组成>(取消装箱),因为;:(拆分单词)返回一个装箱的列表(以便每个元素可以具有不同的长度)。

例:

     (' 'joinstring(<&(]`(rplc&('I1';'i1')&(rplc&('l1';'L1'))&({.,([:":#-2:),{:))@.(#>3:))&>&;:)) 'hey you baby Iooooooooneey I1'
hey you b2y i11y I1

4

CachéObjectScript,231字节

r(a,b) s o=$REPLACE(o,a,b) q
z(s) f i=1:1:$L(s," ") s u=$P(s," ",i),l=$L(u),o=$S(l<4:u,1:$E(u)_(l-2)_$E(u,l)) d:l>3 r("I1","i1"),r("l1","L1") d  g:r z+4
    . i '(l<4!(v(o)=u!'$D(v(o)))) s r=1 q
    . s v(o)=u,t=t_o_" "
    q t
    q "A7s R4t"

如果不是针对该讨厌的$REPLACE调用(这不是标准的一部分),这将是符合ool's标准的MUMPS 。在纯M中重新实现它需要占用80个字节,因此我没有走这条路。

入口点是$$z("your string here"),返回"y2r s4g h2e",依此类推。


3
您可以链接到该语言吗?我还没听说
mbomb007

我的意思是,这是您在Google(docs.intersystems.com/cache20152/csp/docbook/…)上搜索时出现的东西-MUMPS的专有风格。
senshin 2015年

3

C#,280个 274字节

第一次来这里的高尔夫球手!最近喜欢阅读这些,所以我想自己可以尝试一下!可能不是最好的解决方案,但是好吧!

class B{static void Main(string[] a){string[] n=Console.ReadLine().Split(' ');string o="";int j,i=j=0;for(;j<n.Length;j++){int s=n[j].Length;n[j]=((s<4)?n[j]:""+n[j][0]+(s-2)+n[j][s-1])+" ";o+=n[j];for(;i<j;i++)if(n[j]==n[i]){o="A7s R4t";j=n.Length;}}Console.WriteLine(o);}}

同样的事情没有解决:

class B
{
    static void Main(string[] a)
    {            
        string[] n = Console.ReadLine().Split(' ');
        string o = "";
        int j, i = j = 0;
        for(; j < n.Length;j++)
        {
            int s = n[j].Length;

            n[j] = ((s<4) ? n[j] : "" + n[j][0] + (s - 2) + n[j][s - 1]) + " ";
            o += n[j];
            for (; i < j; i++)
            {
                if (n[j] == n[i]) { o = "A7s R4t"; j=n.Length;}
            }                              
        }
        Console.WriteLine(o);
    }
}

谢谢你们!


欢迎来到PPCG!c:
Deusovi

2

Perl,131120字节

我为使用-p开关添加了一个字节:

s/\B(\w+)(\w)/length($1)."$2_$1"/ge;$_="A7s R4t\n"if/(\w\d+\w)(\w+)\b.*\1(?!\2)/;s/_\w+//g;s/I1\w/\l$&/g;s/l1\w/\u$&/g;

说明

# Replace internal letters with count, but keep them around for the next test.
s/\B(\w+)(\w)/length($1)."$2_$1"/ge;
# Detect ambiguous result
$_ = "A7s R4t\n" if
    # Use negative look-ahead assertion to find conflicts
    /(\w\d+\w)(\w+)\b.*\1(?!\2)/;
# We're done with the internal letters now
s/_\w+//g;
# Transform case of initial 'I' and 'l', but only before '1'
s/I1\w/\l$&/g;
s/l1\w/\u$&/g;

不确定是否正确,但我希望“ like”中的“ l”在转换为“ l2e”时不会大写,因为后面没有跟“ 1”。
manatwork 2015年

仅测试了几种情况,但似乎可以工作:s/(\w)(\w+)(\w)/$1.length($2)."$3_$2"/ges/\B(\w+)(\w)/length($1)."$2_$1"/ge
manatwork 2015年

谢谢,@ manatwork。我忽略了[Il]仅在之前进行casewap交换的要求1-也节省了几个字节!
Toby Speight 2015年

1

JavaScript(ES6),165个字节

s=>(r=s.replace(/\S+/g,w=>(m=w.length-2)<2?w:(i=d.indexOf(n=((m+s)[0]<2&&{I:"i",l:"L"}[w[0]]||w[0])+m+w[m+1]))>=0&d[i+1]!=w?v=0:d.push(n,w)&&n,v=d=[]),v?r:"A7s R4t")

说明

s=>(                         // s = input string
  r=s.replace(               // r = result of s with words replaced by numeronyms
    /\S+/g,                  // match each word
    w=>                      // w = matched word
      (m=w.length-2)         // m = number of characters in the middle of the word
        <2?w:                // if the word length is less than 4 leave it as it is
      (i=d.indexOf(          // check if this numeronym has been used before
        n=                   // n = numeronymified word
          ((m+s)[0]<2&&      // if the number starts with 1 we may need to replace
            {I:"i",l:"L"}    //     the first character with one of these
              [w[0]]||w[0])+ // return the replaced or original character
          m+w[m+1]
      ))>=0&d[i+1]!=w?       // set result as invalid if the same numeronym has been
        v=0:                 //     used before with a different word
      d.push(n,w)&&n,        // if everything is fine return n and add it to the list
    v=                       // v = true if result is valid
      d=[]                   // d = array of numeronyms used followed by their original word
  ),
  v?r:"A7s R4t"              // return the result
)

测试


1

JavaScript ES6、162

w=>(v=(w=w.split` `).map(x=>(l=x.length-2+'')>1?((l[0]>1||{I:'i',l:'L'})[x[0]]||x[0])+l+x[-~l]:x)).some((a,i)=>v.some((b,j)=>a==b&w[i]!=w[j]))?'A7s R4t':v.join` `

少打高尔夫球

// Less golfed
f=w=>{
  w = w.split` ` // original text splitted in words
  v = w.map(x=> { // build modified words in array v

    l = x.length - 2 // word length - 2
    if (l > 1) // if word length is 4 or more
    {
      a = x[0] // get first char of word
      l = l+'' // convert to string to get the first digit
      m = l[0] > 1 || {I:'i', l:'L'} // only if first digit is 1, prepare to remap I to i and l to L
      a = m[a] || a // remap
      return a + l + x[-~l] // note: -~ convert back to number and add 1
    }
    else
      return x // word unchanged
  })
  return v.some((a,i)=>v.some((b,j)=>a==b&w[i]!=w[j])) // look for equals Numeronyms on different words
  ? 'A7s R4t' 
  : v.join` `
}  

测试


1

Python 2,185字节

d={}
r=''
for w in input().split():
 l=len(w);x=(w[0]+`l-2`+w[-1]).replace('l1','L1').replace('I1','i1')
 if l<4:x=w
 if d.get(x,w)!=w:r='A7s R4t';break
 d[x]=w;r+=x+' '
print r.strip()

1

Python 3、160

我很想找到一种替换这些替换呼叫的好方法。

def f(a):y=[(x,(x[0]+str(len(x)-2)+x[-1]).replace('l1','L1').replace('I1','i1'))[len(x)>3]for x in a.split()];return('A7s R4t',' '.join(y))[len(set(y))==len(y)]

有一些测试用例:

assert f('Billy goats gruff') == 'B3y g3s g3f'
assert f('Programming Puzzles & Code Golf') == 'P9g P5s & C2e G2f'
assert f('Internationalization or InternXXXXXalization') == 'A7s R4t'

1

因子,48 35字节,无竞争

它是一堆lambda,在技术上不符合我最初设法忽略的非常烦人的艰巨要求。

[ " " split [ a10n ] map " " join ]

使用english词汇。

或者,如果我们内联a10n库字,则为131个字节(带有自动导入):

: a ( b -- c ) " " split [ dup length 3 > [ [ 1 head ] [ length 2 - number>string ] [ 1 tail* ] tri 3append ] when ] map " " join ;

这可以处理i / L东西和A7s R4t吗?
罗伯特·弗雷泽

@RobertFraser我确定可以,尽管我现在不在使用Factor编译器。我将在通过时添加通过单元测试。:D

是的,对要求感到抱歉。在完成问题一周后,我感到“哦,不,我做了什么”,但为时已晚。我可能会制作一个更简单的版本作为单独的挑战。
mbomb007 '16
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.