将数字转换为字母,反之亦然


12

根据数字及其等效字母的列表,输入字符串,用等效数字/字母替换该字符串的字符,然后输出结果。

清单

  • 1 =一个
  • 2 = b
  • 3 = c
  • 4 =天
  • 5 = e
  • 6 = f
  • 7 = g
  • 8 =小时
  • 9 =我
  • 0 = j

输入值

第1124章

输出量

t89s9s1str9n7aabd

规则

  • 数字仅等效于小写字母。
  • 列表外的任何字符都将保留原样。
  • 输入字符串不能包含空格。
  • 允许完整的程序或功能。
  • 由于它是代码高尔夫球,因此最少的字节获胜。

2
欢迎来到PPCG。好挑战!
ADAM

2
我们可以交换大写字母吗?
ADAM

仅小写字母,大写字母将不在列表范围内。
Noir Antares

输入内容将仅包含字母和数字,还是可能包含其他非空格字符(例如标点符号)?
sundar-恢复莫妮卡

输入字符串可以包含除空格以外的任何其他字符,但是列表之外的任何内容在输出字符串中将保持不变。
Noir Antares

Answers:







5

视网膜0.8.2,12字节

T`a-j09-1`Ro

在线尝试!说明:

T``

执行音译。

a-j09-1

源列表是字母a-j,然后0,然后数字9-1

Ro

目的地清单源列表颠倒,即数字1-9,然后0,然后的字母j-a






3

MS-SQL,71个字节

SELECT TRANSLATE(v,'1234567890abcdefghij','abcdefghij1234567890')FROM t

新的SQL 2017函数TRANSLATE执行单个字符替换,因此非常适合此目的。在先前的挑战中看到我的类似答案

根据我们的IO规则,输入通过带有varchar列v的预先存在的表t进行。要仅替换小写字母,必须使用区分大小写的排序规则创建表:

CREATE TABLE t(v varchar(max) COLLATE Latin1_General_CS_AS)


2

Pyth,13个字节

Xz+jk+0_S9<GT

在这里尝试!

说明

Xz+jk+0_S9<GT – Full program.
       _S9    – Yield [9, 8, 7, ..., 1]
     +0       – Prepend a 0.
   jk         – Join to a single string.
  +       <GT – And append the first 10 letters of the alphabet to it.
                Yields 0987654321abcdefghij.
Xz            – Transliterates the input from the above to the above reversed.


1

REXX,57个字节

#=1234567890
a='abcdefghij'
say translate(arg(1),a #,# a)

1

C(gcc)81 72字节

感谢Giacomo Garabello的建议。

f(char*s){for(;*s++=*s-97U<11?(*s-6)%10+48:*s-48U<11?(*s-9)%10+97:*s;);}

在线尝试!


您可以通过更改输入字符串(而不是将其打印出来putchar(...)->)删除6个字符,*s=...并通过替换*s-48U<11为“ *s<59 在线尝试”
Giacomo Garabello

@GiacomoGarabello *s<59如果$输入的字符类似,则会中断,这就是为什么我这样做了*s-48U([0..47]变成[-48 ..- 1],这变成了一个大的无符号值。)因为问题没有指定是否非-数字是有效的,我接受了更为保守的解释。
ErikF

很公平。OP最近评论说,其他所有字符均有效,但空格无效。但是,你仍然可以使用我的第一个建议
贾科莫Garabello

1

Powershell,94个字节

-join($args|% t*y|%{if(1+($p=($s='1a2b3c4d5e6f7g8h9i0j').IndexOf($_))){$s[$p-bxor1]}else{$_}})

测试脚本:

$f = {
-join($args|% t*y|%{if(1+($p=($s='1a2b3c4d5e6f7g8h9i0j').IndexOf($_))){$s[$p-bxor1]}else{$_}})
}

&$f "thisisastring1124"

说明

  • $args -参数字符串
  • $args|% t*y-扩展到|ForEach-Object -Method ToCharArray相当于"$args".ToCharArray()
  • ($s='1a2b3c4d5e6f7g8h9i0j').IndexOf($_)-在字符串中找到一个字符,返回位置;如果找不到,则返回-1。让$ s存储一个字符串,该字符串在相邻的位置上包含一个成对的char,这些字符的最后一位不同:0 + 1、2 + 3、4 + 5...。
  • if(1+($p=...)) -如果找到位置
  • $p-bxor1 -位xor 1

不错的把戏-bxor1
AdmBorkBork



0

K4,38个字节

解:

{(,/|x,a)(,/a:0 1_'10 11#'.Q`a`n)?x}@'

说明:

查找列表中的每个字符,"abcdefghij1234567890"然后在列表"1234567890abcdefghijX"中建立索引X原始字符。

需要找到一种较短的方式来构建琴弦...

{(,/|x,a)(,/a:0 1_'10 11#'.Q`a`n)?x}@'
{                                  }@' / apply (@) lambda {} to each (')
                                 ?x    / lookup x in
         (                      )      / do this together
                          .Q`a`n       / index into .Q with a (gives a-z) and n (gives 0-9)
                   10 11#'             / take 10 from first list and 11 from second list
              0 1_'                    / drop 0 from first list and 1 from second list
            a:                         / save as a
          ,/                           / flatten
 (      )                              / do this together
     x,a                               / prepend x to a
    |                                  / reverse it
  ,/                                   / flatten

奖金:

38个字节的另一种解决方案:

{(a,b,x)?[(b:1_11#.Q.n),a:10#.Q.a]x}@'

0

Yabasic,135字节

从控制台获取输入并输出到控制台。

Input""s$
For i=1To Len(s$)
c$=Mid$(s$,i,1)
n=asc(c$)-96
If-38>n Then?chr$(143+n+11^(n=-48));ElsIf n<11Then?n*(10>n),"";Else?c$;Fi
Next

在线尝试!



0

sed,44个字节

y/1234567890abcdefghij/abcdefghij1234567890/

我承认,这有点无聊。

测试:

$ echo 'thisisastring1124' | sed 'y/1234567890abcdefghij/abcdefghij1234567890/'
t89s9s1str9n7aabd
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.