创建一个移位器


10

要“转移”多个n位置,请执行以下操作:

  • 删除最后一位n数字
  • n将第一个数字的副本添加到数字的开头

例如,要将数字移31415三位,请取出最后三位数字以获得31,然后再将最后一位数字的三份添加3到末尾,给出33331

如果数字为负数,则应从左数起取数字,而第一个数字应从右数“填充”。例如,将字符串31415移至-3个位置可得到15555

如果数字为0,则不应进行任何更改。

如果移位大于程序长度,则可能会开始删除所填充的数字。这意味着,如果给定的数字大于字符串的长度,则在整个字符串变为一位数字后,将不再进行任何更改。

挑战

给定两个数字ns,返回n移到多个s位置。

测试用例

   n,  s -> result
7243,  1 -> 7724
 452, -1 -> 522
  12,  1 -> 11
 153,  4 -> 111

这是,因此最短的有效提交(以字节为单位)获胜。


获奖标准是什么?
Xcoder先生17年

5
我认为我不了解“转变”的作用。这被标记为[binary],这使我认为它与二进制移位有关。在测试案例中以二进制形式显示数字也许会有所帮助?
硕果累累

2
它移动并“伸展”了第一个或最后一个数字。什么不清楚?
路易斯·门多

2
是的,他们问的很清楚
wrymug

3
移位的定义很明确:整数乘或除以基的幂。但是没有一个测试用例有效,因此这个问题似乎使用了特质定义,而不是说它是什么。
彼得·泰勒

Answers:


2

APL(Dyalog),32字节

匿名函数,将筛选作为左参数,将数字(作为字符串)作为右参数。

{a←|⍺⋄(≢⍵)↑(a-⍺)↓(a⍴⊃⍵),⍵,a⍴⊃⌽⍵}

在线尝试!

{为左右参数的 匿名函数

|⍺ 位移的绝对值

a← 店

 然后

⌽⍵ 倒数

 选择第一个(即最后一个)数字

a⍴ř它ESHAPE长度

⍵, 在号码前

(), 附加以下内容:

  ⊃⍵ 第一位数字

  a⍴ř它ESHAPE长度

()↓ 删除以下字符数:

  a-⍺一个负移位

()↑ 采用以下字符数:

  ≢⍵ 原始号码的长度



2

Haskell,69个字节

s#n|l<-[1..abs n]=take(length s)$drop(-2*n)$(s!!0<$l)++s++(last s<$l)

将数字作为字符串。在线尝试!

怎么运行的:

s#n                         -- s: input number as a string
                            -- n: shift amount

(s!!0<$l)++s++(last s<$l)   -- make a string:
                            -- n times the first digit, the whole number, n times the last digit
                            -- e.g. "567" 2 -> 5556777
drop(-2*n)                  -- drop the first (-2 * n) chars, i.e. 0 if n>0
take(length s)              -- take as may chars as there are in s

2

MATL,12字节

tn:i-yn1&Xl)

输入为:要转换为字符串的数字;数量移位。

在线尝试!验证所有测试用例

消费者输入'452'-1'

t     % Implicitly input string. Duplicate
      % STACK: '452', '452'
n     % Number of elements
      % STACK: '452', 3
:     % Range
      % STACK: '452', [1 2 3]
i     % Input number
      % STACK: '452', [1 2 3], -1
-     % Subtract, element-wise
      % STACK: '452', [2 3 4]
y     % Duplicate from below
      % STACK: '452', [2 3 4], '452'
n     % Number of elements
      % STACK: '452', [2 3 4], 3
1     % Push 1
      % STACK: '452', [2 3 4], 3, 1
&Xl   % Clamp, with three inputs. Applies min function, then max
      % STACK: '452', [2 3 3]
)     % Reference indexing. Implicitly display
      % STACK: '522'

1
到目前为止,这是最少的字节好工作!
K Split X

@KSplitX谢谢!顺便说一句,我刚刚添加了一个解释
路易斯·门多

1

J,37个字节

这是J中的一种情况,其中明确的动词似乎是正确的(唯一的?)选择,但是我很想知道是否对此进行了默认的重写:

4 :'(-x)(|.!.((x>0)&{({:,{.)":y))":y'

J的内置移位动词可让您配置“填充”字符:

|.!.f  NB. f is the fill character

确定是使用第一个字符还是最后一个字符作为填充字符的逻辑很简单

(x>0)&{ ({: , {.)

在线尝试!


1

J,23个字节

(_&(]{.,],{:)~|)}.~_2*]

输入n和输出是包含数字的字符串。

在线尝试!

说明

(_&(]{.,],{:)~|)}.~_2*]  Input: 'integer n' as a string (LHS), integer s (RHS)
(              )         Extend 'n' by copying its head and tail 's' times
              |            Abs(s)
 _&(]       )~             Nest 'Abs(s)' times on 'n'
          {:                 Tail
        ],                   Append the tail to itself
     {.                      Head
       ,                     Prepend the head to the previous
                   _2*]  Multiply 's' by -2
                }.~      Drop abs(-2*s) from the head if 's' < 0 else from the tail

美丽。我怀疑有比我更好的方法了……
约拿书

尽管我注意到您将n作为字符串(可能是错误地)假定为id不允许使用。虽然它只会为我节省4个字符……
约拿书


1

05AB1E,16个字节

0‹©iR}¹ÄF¨¬ì}®iR

在线尝试!

说明

0‹                 # input_1 is negative
  ©                # store a copy in register
   iR}             # if true (input_1 is negative), reverse input_2
      ¹ÄF          # abs(input_1) times do
         ¨         # remove the last element
          “       # prepend the head
            }      # end loop
             ®iR   # if input_1 was negative, reverse result

0

Python 2,87个字节

f=lambda n,s:s<0and f(n[::-1],-s)[::-1]or n[0]*min(len(n),s)+n[:[0,len(n)-s][len(n)>s]]

在线尝试!

将数字作为字符串,将移位作为整数。返回一个字符串。

我尝试将反转嵌入到函数中,而不是进行递归调用,但是我似乎无法正确实现。



0

Haskell,108个字节

哦,这比我想的要糟。

n#s=print$show n&s
r=reverse
n&s|s<0=r$r n&abs s|(a:b,c)<-splitAt s n=take(length n)$(a<$[0..s])++b++c|1>0=n

在线尝试!

不打高尔夫球

n # s = print $ show n & s
n & s
  | s < 0                = reverse (reverse n & abs s)
  | (a:b,c)<-splitAt s n = take (length n) (replicate s a ++ b ++ c)
  | otherwise            = n

0

Clojure,121个字节

#(let[T(if(pos? %2)reverse vec)](apply str(concat(repeat %2(first %))(T(drop(Math/abs %2)(T %)))(repeat(- %2)(last %)))))

太糟糕了,还要处理负面输入。


0

Pyth,28个字节

AQ=Y<+*hJ?K<0H`G_`GHJlJ?KY_Y

在线尝试测试一些输入

说明

AQ=Y<+*hJ?K<0H`G_`GHJlJ?KY_Y
AQ                           | Split Q into 2 parts, G and H.
        J?K<0H`G_`G          | If 0 < H, J = str(G). Else, J = reverse(str(G)). Return J
       h                     | Find 1st element
      *            H         | Repeat H times
     +              J        | Concatenate with J
    <                lJ      | Find 1st length(J) elements
  =Y                         | Assign to Y.
                       ?KY_Y | If K, implicit print Y. Else implicit print reverse(Y).


0

JavaScript,80字节

(n,s,k=n.length,p=s<=0)=>n.slice(p*-s,p?k:-s)[p?"padEnd":"padStart"](k--,n[p*k])

将输入作为数字和数字“移位”量的字符串表示形式。返回一个字符串。

测试片段

let f=
(n,s,k=n.length,p=s<=0)=>n.slice(p*-s,p?k:-s)[p?"padEnd":"padStart"](k--,n[p*k])

I.value="31415";J.value="3";D.oninput()
<div id=D oninput="O.value=I.value.length&J.value.length?f(I.value,+J.value):''">n: <input id=I size=10> s: <input id=J size=2><br><input id=O disabled>

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.