手头的任务:
给定长度为N且从0到N-1索引的字符串S,打印 它的偶数索引和奇数索引字符为2空格分隔 单行上的字符串。
编写测试用例,使第一行包含一个 整数,N(测试用例数)。随后N的每一行i 行包含一个字符串。
这是我的代码:
N = int(raw_input())
for i in range(0,N):
string = raw_input()
evenlist = []
oddlist = []
for item, char in enumerate(strg):
if item % 2 == 0:
evenlist.append(char)
else:
oddlist.append(char)
print ''.join(evenlist), ''.join(oddlist)
示例运行:
The first input is:
2
Hacker
Rank
Expected output is:
Hce akr
Rn ak
但我明白了:
HceRn akrak
以下是assignment的链接,可以更好地解释这个问题。
答案 0 :(得分:4)
更简单的方法是使用字符串切片:
>>> my_str = 'Hacker'
>>> '{} {}'.format(my_str[::2], my_str[1::2])
'Hce akr'
因此,您的整个代码可以写成:
for _ in range(int(raw_input())):
my_str = raw_input()
print '{} {}'.format(my_str[::2], my_str[1::2])
答案 1 :(得分:2)
你也可以这样做:
inp = raw_input("Enter your input: ")
final = "{} {}".format("".join(inp[k] for k in range(len(inp)) if k % 2 == 0), "".join(inp[k] for k in range(len(inp)) if k % 2 != 0))
答案 2 :(得分:1)
在Python-3中
for i in range(int(input())):
STDIN = input()
print(f"{STDIN[::2]} {STDIN[1::2]}")
输入:
2
Hacker
Rank
输出:
Hce akr Rn ak
答案 3 :(得分:0)
///在Java中只是为了更新尚未为//循环添加的多个字符串的上述答案
import java.io.*;
import java.util.*;
public class dummy {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of test cases :");
int s=sc.nextInt();
//for loop for multiple strings as per the input
for(int m=0;m<= s;m++)
{
System.out.print("\n Enter the string : ");
String s1=sc.next();
f(s1);
}
}
private static void f(String s1){
char c[]=s1.toCharArray();
int i,j;
for (i = 0; i <c.length;i++)
{
System.out.print(c[i]);
i+=1;
}
System.out.print(" ");
for (j = 1; j<c.length;j++)
{
System.out.print(c[j]);
j+=1;
}
}
}
答案 4 :(得分:0)
尝试以下代码:
N = int(input())
evenlist = [] oddlist = []
for i in range(0,N):
string=input()
evenlist.clear()
oddlist.clear()
for item, char in enumerate(string):
if item % 2 == 0:
evenlist.append(char)
else: oddlist.append(char)
print(''.join(evenlist),''.join(oddlist))
print('')
答案 5 :(得分:0)
解决黑客排名问题的方法是使用字符串切片和列表。
n= int(input())
r = []
if(n>=1 and n<= 10):
for i in range(n):
r.append(input())
for s in r:
print("{} {}".format(s[::2],s[1::2]))
答案 6 :(得分:0)
num = int(input())
mylist = []
for words in range (num):
words = input()
mylist.append(words)
#print(mylist)
i=0
#Considering 0th position as even place here.
while i<len(mylist):
print("Letters at even places = {} Letters at odd places ={}".
format(mylist[i][0:len(mylist[i]):2], mylist[i][1:len(mylist[i]):2]))
i+=1
这是针对您问题的python解决方案。
答案 7 :(得分:0)
const menu = (
<Menu onClick={onClick}>
{
this.state.getSoftware.map((data,i) => (<Menu.item key={i}>{data.Name}</Menu.item>))
}
</Menu>
);
答案 8 :(得分:0)
import java.io.*;
import java.util.*;
public class Solution {
private static void f(String s) {
// TODO Auto-generated method stub
char c[]=s.toCharArray();
int i,j;
for (i = 0; i <c.length;i++)
{
System.out.print(c[i]);
i+=1;
// System.out.print(" ");
}
System.out.print(" ");
for (j = 1; j<c.length;j++)
{
System.out.print(c[j]);
j+=1;
}
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int s=sc.nextInt();
String s1=sc.next();
f(s1);
String s2=sc.next();
System.out.println();
f(s2);
}
}
答案 9 :(得分:0)
t = int(input())
for i in range(t):
s = input()
idx = 0
newstr = ''
newstr2 = ''
for letter in s:
if idx % 2 == 0:
newstr = newstr + s[idx]
else:
newstr2 = newstr2 + s[idx]
idx += 1
print(newstr, newstr2)
答案 10 :(得分:0)
Try the below algorithm. It is in Java. However the basic procedure is to
scan the input word string along its length in increments of 2. The even
index characters are printed first using a for loop starting with i = 0 and
by adding an additional i++ inside the loop. The odd
characters are printed starting the for loop from 1 and incrementing by steps
of 2.
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.Arrays;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for(int iter= 1;iter<=n;iter++)
{
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
String myString = scanner.nextLine();
char[] myCharArray = myString.toCharArray();
int t = myString.length();
for(int i = 0; i < t; i++)
{
// Print each sequential character on the same line
System.out.print(myCharArray[i]);
i++;
}
System.out.print(" ");
for(int k = 1; k < t; k++)
{
// Print each sequential character on the same line
System.out.print(myCharArray[k]);
k++;
}
System.out.print("\n");
}
}
}
答案 11 :(得分:0)
i=0
for t in range(int(input())):
S = input()
y = len(S)
print(S[i:y+1:2],S[i+1:y+1:2] )
#output= "satisfies all the test cases of HackerRank...."
答案 12 :(得分:0)
#using for循环和条件语句
text=input()
for items in range(len(text)):
if items%2==0:
print(text[items],end='')
print(end=' ')
for items in range(len(text)):
if items%2!=0:
print(text[items],end='')
答案 13 :(得分:0)
N = int(input())
for _ in range(0,N):
my_str = input()
print ('{} {}'.format(my_str[::2], my_str[1::2]))
#use this,将非常适合此
答案 14 :(得分:0)
我是编码方面的新手,但是在所有已解决的测试用例中都可以很好地工作。
T=int(input())
for i in range(T):
S=input("")
for item in range(length(S)):
if item%2==0:
print(S[item],end="")
print(end=" ")
for item in range(length(S)):
if item%2!=0:
print(S[item],end="")
print(end='\n')
答案 15 :(得分:0)
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner s=new Scanner(System.in);
String str;
int T=s.nextInt();
for(int i=0;i<T;i++){
str=s.next();
char c[]=str.toCharArray();
int j,k;
for(j=0;j<c.length;j++){
System.out.print(c[j]);
j+=1;
}
System.out.print(" ");
for(k=1;k<c.length;k++){
System.out.print(c[k]);
k+=1;
}
System.out.println();
}
s.close();
}
}
答案 16 :(得分:0)
这也可以通过连接单词来实现
Num_set = int(input())
for item in range(Num_set):
word = input()
new = ""
second = ""
for i in range(len(word)):
if i%2 == 0:
new = new + word[i]
else:
second = second +word[i]
print(new +" "+second)