混合字符串中的字符


10

您必须编写一个函数/程序,该函数通过stdin/ command-line参数/ function参数接受输入,将字符串中的字符混合,然后通过来输出最终的字符串stdout

输入将首先包含一个字符串(非空或null),一个空格,然后是偶数个全部由空格分隔的非负数。如果通过函数参数获取输入,则字符串将是参数之一,而由空格分隔的整数将是另一个。您必须在与连续的数字对相对应的索引处交换字符串的字符。

例如:

Hello_world! 0 6

必须导致

wello_Horld!

假设条件

  • 您可以在基于0的索引和基于1的索引之间进行选择,并可以假设给定的索引始终在范围内。
  • 该字符串不得超过100个字符,并且只能包含范围!~(字符代码0x21至0x7E,包括0x21至0x7E)的ASCII字符。请参阅ASCII表以获取参考。
  • 一对中的两个索引可能相同(在这种情况下,该步骤不会交换任何内容)。

计分

这是代码高尔夫球,因此最短的提交(以字节为单位)获胜。

测试用例

Hello_world! 0 6 => wello_Horld!
First 1 2 1 0 0 4 => tFisr
(Second!$$) 8 7 10 1 => ()econd$!$S
~Third~ 0 0 6 6 0 6 6 0 => ~Third~

2
对于未来的挑战,让我推荐一个沙盒,您可以在其中获得反馈并在将挑战发布到主要组件之前完善您的挑战(如果有人发现您需要解决的挑战中的严重缺陷,这可以最大程度地降低使现有答案无效的风险)。
马丁·恩德

为什么要求在stdin上输入而不要求在命令行输入?
lrn 2015年

@lrn,对。增加了2个选项。
Spikatrix 2015年

我在下面看到许多解决方案,这些解决方案假定它们可以将索引列表作为数组传递给实现的函数。按照我阅读定义的方式,输入是单个字符串,其中包含索引以及它们在其上操作的字符串,并且从输入字符串中提取索引是需要使用的代码的一部分。您能说明哪种解释正确吗?
Reto Koradi 2015年

@RetoKoradi,否。输入不是完整的字符串。它有一个字符串,然后是数字。数字不包含在字符串中。
Spikatrix

Answers:


6

CJam,11个字节

rr{irie\r}h

怎么运行的

这是一种略有不同的方法,在该方法中,我只运行了一个do-while循环,直到输入中剩下成对的数字。

r                 e# Read the first string
 r                e# Read the first number of the first number pair in the input
  {      }h       e# Do a do-while loop
   i              e# Convert the first number from the pair to integer
    ri            e# Read the second number from the pair and convert to intger
      e\          e# String X Y e\ works by swapping the Xth index with the Yth index in the
                  e# String
        r         e# This is our exit condition of the do-while loop. If we still have
                  e# a number on the input left, that means there are more pairs to swap.
                  e# Otherwise, we exit the loop and the result is printed automatically

在这里在线尝试


6

Python 3,89 86字节

[*s],*L=input().split()
while L:a,b,*L=map(int,L);s[a],s[b]=s[b],s[a]
print(*s,sep="")

打开所有东西的包装。(使用@potato节省了3个字节)


保存一些字节,然后执行此操作:[*s],*L=input().split()然后可以将其删除。顺便说一句,我真的很喜欢您的解决方案,即使它打高尔夫球也很优雅。
土豆

@potato哦,哇,我不知道您可以像这样将两个解压缩在一起(我以为您只能在3.5中这样做)。谢谢!
Sp3000

4

CJam,13个字节

r[q~]2/{~e\}/

在这里测试。

说明

r             e# Read the first token, i.e. the string.
 [q~]         e# Read the rest of the input, eval it and wrap it in an array.
     2/       e# Split the array into pairs of consecutive elements.
       {   }/ e# For each pair.
        ~     e# Unwrap the array.
         e\   e# Swap the corresponding elements in the string.

哇 没想到回答那么快!
Spikatrix

2

C(137羽)

f(char*T,int*V,int L){int C=0;for(int j=0;j<strlen(T);C=++j){for(int i=L-1;i+1;i--)if(C==V[i]){C=V[i-i%2*2+1];i-=i%2;}printf("%c",T[C]);}}

解释来了...

争论

T = char *类型的单词。

V =偶数个整数元素的数组。

L = V的长度

输出量

混合弦

它是如何工作的 ?

反之亦然,扫掠数组V的数目,并跟踪字符串的所有进度直到实际点之后,放置字符串的第n个元素。

输入= T =“第一”,V = {1,2,1,0,0,4}

V反转= {4,0,0,1,2,1}

V[0] = 4th element -> index 0
0 -> 1
1->2

4th element 't' receives the second = 'r'

V[1] = 0 -> index 4
4 isnt mentionned after so , no changes

0 element='F' receives the fourth= 't'

V[3] = 1st element -> index 0
no changes

V[4] = 2 -> index 1
no changes after ..

在这里尝试


1
@ Agawa001,您可以打更多的高尔夫球。int不需要返回类型(可能会导致意外行为),并且int作为参数的int变量不需要使用,变量,而不是在循环中声明,可以在循环外部的某个位置声明,putchar而不是使用printfetc
Spikatrix

2

Python的3 - 161 149

import sys
t=sys.stdin.read().split()
q=list(t[0])
c=1
i=int
while c<len(t):n=q;a=i(t[c]);b=i(t[c+1]);n[a]=q[b];n[b]=q[a];q=n;c+=2;
print(''.join(q))

通过交换一些变量并使用;Tim的注释中的内容,可以发挥更多的作用。

我希望它看起来像打高尔夫球,但不会那么多。


1
你可以打很多球。将更while改为while c<len(t):line1;line2;line3...c=c+2c+=2
蒂姆

@Tim谢谢您的帮助!
ASCIIThenANSI

c不应该从0开始吗?
蒂姆(Tim)

@Tim Nope。c实际上是索引t(输入)以获得我们需要交换的头寸。但是既然t[0]是字符串,我们就需要交换,t[1]t[2]保持第一对交换。
ASCIIThenANSI 2015年

啊,我明白了,是的。抱歉,我的解决方案将输入拆分了,所以我猜你也做过同样的事情:)
蒂姆(Tim)

2

C,109个 107 102字节

i;f(l){l=sizeof(a)/sizeof(*a);char t;for(;i<l;i+=2){t=s[a[i]];s[a[i]]=s[a[i+1]];s[a[i+1]]=t;}puts(s);}

注意:s并且a需要声明为全局数组。s是您要交换的字符串,并且aint包含所有数字值的数组。

如果以上代码不起作用,请尝试使用void f(){...}代替f(){...}

取消程式码:

int a[]={1, 2, 1, 0, 0, 4};//Integer elements
char s[]="First";          //String to be swapped

i; //Auto initialized to 0 and defaults to type int
void f(l){ //Variables defaults to type int
  l=sizeof(a)/sizeof(*a); //Gets number of elements in array a
  char t;

  for(;i<l;i+=2){ 

    t=s[a[i]];
    s[a[i]]=s[a[i+1]];
    s[a[i+1]]=t;  //Swap each character

  }

  puts(s); //Print the final char array
}

在这里测试


嗯,你的代码更小:)
Abr001am,2015年

大声笑在哪里变量声明?多数民众赞成在欺骗您的代码时:p
Abr001am

@ Agawa001,我没有包括变量声明,因为字节随每个测试用例而变化。
Spikatrix

这与问题中定义的输入不匹配。输入是单个字符串。除非我完全误解了问题,否则您需要从输入字符串中提取索引值。
Reto Koradi 2015年

1

Python 3、135

x=input().split()
y=list(x[0])
z=[int(i)for i in x[1:]]
while z:p,c=y[z[0]],y[z[1]];y[z[0]],y[z[1]]=c,p;del z[0],z[0]
print(''.join(y))

说明:

x=input().split()         # Split the input into a list at each space
y=list(x[0])              # First item in list (the word) into a list of chars
z=[int(i)for i in x[1:]]  # Make the list of numbers, into integers
while z:                  # Loop untill the list z is empty
    p,c=y[z[0]],y[z[1]]   # Assign p to the first char and c to the second
    y[z[0]],y[z[1]]=c,p   # Swap around using p and c
    del z[0],z[0]         # Remove the first 2 items in the list of integers
print(''.join(y))         # Print out the altered list as a string

1

C,70字节

给定输入字符串的最大长度为100,我决定使表示整数数组结尾的“ NULL”字节明确0xFF。大概这算不上额外的投入,尽管(最多)花费7 3个字节可以将其转换为基于1的索引,并'\0'用作数组的末尾。

f(s,i,t)char*s,*i;{for(;~*i;)t=s[*i],s[*i]=s[*++i],s[*i++]=t;puts(s);}

几乎只是使用tmp变量进行常规交换,并使用逗号运算符引入序列点以具有定义的行为(与xor交换的某些表现形式(其字符数较少但导致未定义的行为不同)不同)。

编辑:根据要求,您可以对其进行测试:http : //rextester.com/OVOQ23313


我不认为您可以假设您得到一个包含要交换索引的数组。索引是输入字符串的一部分,您需要将其从字符串中解析出来,作为发布(和计数)代码的一部分。根据描述:“输入将首先包含一个字符串,一个空格,然后是偶数个全部由空格分隔的非负数。”
Reto Koradi 2015年

1

飞镖-123

假设命令行上的输入会自动在空格处分割。否则,需要一个首字母x=x[0].split(' ');将字符串拆分为文本和索引。

main(x,{c,i:1,a,t}){c=x[0].split("");n()=>a=int.parse(x[i++]);for(;i<x.length;t=c[n()],c[a]=c[n()],c[a]=t);print(c.join());}

具有更多空白:

main(x,{c,i:1,a,t}){
  c=x[0].split("");
  n()=>a=int.parse(x[i++]);
  for(;i<x.length;t=c[n()],c[a]=c[n()],c[a]=t);
  print(c.join());
}

dartpad.dartlang.org上运行/测试。


您知道我可以在此测试的任何在线编译器吗?
Spikatrix

将链接添加到DartPad。
lrn 2015年

1

Rebol-71

s: take i: split input" "foreach[a b]i[swap at s do a at s do b]print s

取消高尔夫:

s: take i: split input " " 
foreach [a b] i [swap at s do a at s do b]
print s

我该如何测试?是否有任何在线编译器可以对此进行测试?
Spikatrix

@CoolGuy-是的,您可以在try.rebol.nl上对其进行测试。 该input函数将无法从那里调用STDIN。解决方法是简单设置input为您要测试的值。这是第一个测试的完整示例- input: "hello_World 1 7" s: take i: split input" "foreach[a b]i[swap at s do a at s do b]print s 然后在Rebol 3 NB中单击Do。Rebol使用基于1的索引。
draegtun 2015年

@CoolGuy-另外,您也可以从rebolsource.net
draegtun 2015年

0

C,143字节

main(a,v,i)char** v;{i=2;char s[101],t;strcpy(s,v[1]);for(;i<a;i+=2){t=s[atoi(v[i])];s[atoi(v[i])]=s[atoi(v[i+1])];s[atoi(v[i+1])]=t;}puts(s);}

上面的程序从命令行参数获取输入,将字符串复制到数组中,交换相应的字符,然后输出修改后的字符串。

取消程式码:

main(int a,char** v,int i){ //Arguments of main 
  i = 2;
  char s[101],t;

  strcpy(s,v[1]); //Copy string literal into an array

  for(;i<a;i+=2){
    t=s[atoi(v[i])];
    s[atoi(v[i])]=s[atoi(v[i+1])];
    s[atoi(v[i+1])]=t;  //Swap each character
  }

  puts(s); // Output the final string
}

您是否假设数字只有一位?考虑到输入最多可以包含100个字符,因此我认为这无效。还要看一下第三个示例,它10作为索引之一。
Reto Koradi 2015年

@RetoKoradi,感谢您发现这一点。我修复了代码。
Spikatrix

0

JavaScript(ES6),95

95个字节(带单个字符串输入)(下面的功能f)

75个字节,带有2个参数,字符串和数字数组(下面的函数g)

(仅EcmaScript 6,仅Firefox)

f=i=>
(
  n=i.split(' '),
  s=[...n.shift()],
  n.map((v,i)=>i&1?[s[v],s[w]]=[s[w],s[v]]:w=v),
  s.join('')
)

g=(s,n)=>
  n.map((v,i)=>i&1?[s[v],s[w]]=[s[w],s[v]]:w=v,s=[...s])
  &&s.join('')

// TEST
out=x=>O.innerHTML+=x+'\n'

;[['Hello_world! 0 6', 'wello_Horld!']
,['First 1 2 1 0 0 4','tFisr']
,['(Second!$$) 8 7 10 1','()econd$!$S']
,['~Third~ 0 0 6 6 0 6 6 0','~Third~']]
.forEach(t=>{
  u=f(t[0]),
  ok=u==t[1],
  out('Test '+(ok?'OK: ':'FAIL: ')+t[0]+'\n Result:' +u + '\n Check: '+t[1]+'\n')
})
<pre id=O></pre>

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.