我在Codewars上做了一个名为" Duplicate Encoder"的kata。 我写的代码正确地完成了它的工作,但junit(4.12)坚持认为它不是出于某种原因。无论是在网站上还是在我的IDE(Eclipse)中。我不知道为什么会这样。有人可以对这个问题有所启发吗?感谢。
要测试的课程:
package com.danman;
import java.util.*;
public class Person {
static String encode(String word){
word = word.toLowerCase();
List<String> bank = new ArrayList<>();
StringBuilder wordTwo = new StringBuilder("");
//1: create a list of all unique elements in the string
for (int n = 0; n < word.length(); n++) {
String temp = word.substring(n, n+1);
if (temp.equals(" ")){continue;}
bank.add(temp);
}
for (int r = 0; r <word.length(); r++){
List<String> bankTwo = bank;
Iterator<String> it = bankTwo.iterator();
String tempTwo = word.substring(r, r+1);
int count = 0;
//2: iterate through the list of elements and append the appropriate token to the StringBuilder
while (it.hasNext()){
if (it.next().equals(tempTwo)){
++count;
}
}
if (count <= 1){
wordTwo.append("(");
} else {
wordTwo.append(")");
}`enter code here`
}
word = wordTwo.toString();
return word;
}
public static void main(String[] args) {
Person rinus = new Person();
System.out.println(rinus.encode("Prespecialized"));
}
Junit文件:
package com.danman;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class PersonTest {
@Test
public void test() {
assertEquals(")()())()(()()(", Person.encode("Prespecialized"));
assertEquals("))))())))", Person.encode(" ()( "));
}
答案 0 :(得分:0)
据我了解你的代码,首先断言是好的。我不知道为什么编码In [1378]: with open('test.txt','w') as f:
for _ in range(3):
f.write('%10d,%20s\n'%(234, datetime.datetime.now().strftime("%d/%m/%y %H:%M")))
......:
In [1379]: cat test.txt
234, 22/06/16 15:18
234, 22/06/16 15:18
234, 22/06/16 15:18
应该返回" ()( "
。您遍历给定字符串中的字符列表(该列表中不包括空格),检查银行列表中的字中是否存在多个字符。当您检查是否有多个空格时,答案为否,追加"))))())))"
,因为计数值将等于(
(由于空格从银行列表中排除)。
第二个断言应该是
0