英文:
Hi, everybody. I am solving a 6 kyu problem on code wars and my code doesn't work
问题
public static List<Integer> sumConsecutives(List<Integer> s) {
List<Integer> mainList = new ArrayList<>();
int i = 0;
while (i < s.size()) {
int current = s.get(i);
int sum = current;
while (i + 1 < s.size() && s.get(i + 1) == current) {
sum += current;
i++;
}
mainList.add(sum);
i++;
}
return mainList;
}
Please note that the provided code contains a corrected version of the sumConsecutives
method. This code should work as intended for the problem you described.
英文:
everybody. I am solving a 6 kyu problem on code wars and my code doesn't work. Here are the instructions for the task:
You are given a list/array which contains only integers (positive and negative). Your job is to sum only the numbers that are the same and consecutive. The result should be one list.
Extra credit if you solve it in one line. You can assume there is never an empty list/array and there will always be an integer.
Same meaning: 1 == 1
1 != -1
#Examples:
[1,4,4,4,0,4,3,3,1] # should return [1,12,0,4,6,1]
"""So as you can see sum of consecutives 1 is 1
sum of 3 consecutives 4 is 12
sum of 0... and sum of 2
consecutives 3 is 6 ..."""
[1,1,7,7,3] # should return [2,14,3]
[-5,-5,7,7,12,0] # should return [-10,14,12,0]
Here's my code:
public static List<Integer> sumConsecutives(List<Integer> s) {
List<Integer> checkList = s;
List<Integer> mainList = new ArrayList<>();
int j = 1;
int i = 0;
while (i < s.size() - 1){
mainList.add(i);
mainList.set(i, s.get(i));
while(true){
if (s.get(i) == s.get(j)){
mainList.set(i, mainList.get(i) + s.get(j));
s.remove(j);
}
else break;
}
j++;
i++;
}
if (checkList.get(checkList.size() - 1) != checkList.get(checkList.size() - 2)){
mainList.add(i, checkList.get(j - 1));
}
System.out.println(mainList);
return mainList;
}
Here's my error:
Test Results:
ConsecutivesTest
test
Log
Basic Tests
Input: {1,4,4,4,0,4,3,3,1}
Test Failed
Stack Trace
java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(AbstractList.java:161)
at Consecutives.sumConsecutives(Consecutives.java:21)
at ConsecutivesTest.test(ConsecutivesTest.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:39)
at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.Iterator.forEachRemaining(Iterator.java:116)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
at org.junit.vintage.engine.VintageTestEngine.executeAllChildren(VintageTestEngine.java:79)
at org.junit.vintage.engine.VintageTestEngine.execute(VintageTestEngine.java:70)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
at io.qualified.junit5.RunTests.main(RunTests.java:25)
Everything works in my IDE, but not on the site.
答案1
得分: 1
由于底层实现对 s
列表的移除操作不支持,导致了异常。我建议您可以使用一个快速且不太规范的解决方案,将输入列表复制一份,像这样:
List<Integer> sCopy = new ArrayList<>(s);
这样做可以确保可以进行移除操作。如果您有更多时间可以投入,我建议重新考虑方法来避免移除操作。在函数或方法中修改输入值被认为是一种不好的做法。良好的代码实践遵循这个黄金规则:函数或方法应避免产生副作用。
例如,看一下这个示例:
public static List<Integer> sumConsecutive(List<Integer> s){
int current = s.get(0);
int count = 1;
List<Integer> result = new ArrayList<>();
for (Integer i: s.subList(1, s.size())){ //第一个已经计算过了
if (current == i){
count++;
} else {
result.add(current * count);
current = i;
count = 1;
}
}
result.add(current * count); //这样可以避免跳过最后一个
return result;
}
英文:
The reason is that the implementation that is underlying the s
list does not support removal from the List
, leading to the exception. I would suggest you, as a quick-and-dirt solution, to do a copy of the input list like this:
List<Integer> sCopy = new ArrayList<>(s);
and in this way you will be sure that removal is possible. If you have more time to spend I would revise the approach to avoid that removal operation. It is considered a bad practice to modify input values in a function/method. Clean code approaches have this golden rule: function/methods should avoid having side effects.
For example, look at this:
public static List<Integer> sumConsecutive(List<Integer> s){
int current = s.get(0);
int count = 1;
List<Integer> result = new ArrayList<>();
for (Integer i: s.subList(1, s.size())){ //first one is already counted
if (current == i){
count++;
} else {
result.add(current * count);
current = i;
count = 1;
}
}
result.add(current * count); //this avoid skipping the last one
return result;
}
答案2
得分: -1
谢谢大家。通过这段代码,一切都运行良好。
public static List<Integer> sumConsecutives(List<Integer> s) {
List<Integer> checkList = new ArrayList<>(s);
List<Integer> sCopy = new ArrayList<>(s);
List<Integer> mainList = new ArrayList<>();
int j = 1;
int i = 0;
while (i < sCopy.size() - 1){
mainList.add(i);
mainList.set(i, sCopy.get(i));
while(true){
if (sCopy.get(i) == sCopy.get(j)){
mainList.set(i, mainList.get(i) + sCopy.get(j));
sCopy.remove(j);
}
else break;
}
j++;
i++;
}
if (checkList.get(checkList.size() - 1) != checkList.get(checkList.size() - 2)){
mainList.add(i, checkList.get(checkList.size() - 1));
}
System.out.println(mainList);
return mainList;
}
英文:
Thanks you all. With this code, everything works well.
public static List<Integer> sumConsecutives(List<Integer> s) {
List<Integer> checkList = new ArrayList<>(s);
List<Integer> sCopy = new ArrayList<>(s);
List<Integer> mainList = new ArrayList<>();
int j = 1;
int i = 0;
while (i < sCopy.size() - 1){
mainList.add(i);
mainList.set(i, sCopy.get(i));
while(true){
if (sCopy.get(i) == sCopy.get(j)){
mainList.set(i, mainList.get(i) + sCopy.get(j));
sCopy.remove(j);
}
else break;
}
j++;
i++;
}
if (checkList.get(checkList.size() - 1) != checkList.get(checkList.size() - 2)){
mainList.add(i, checkList.get(checkList.size() - 1));
}
System.out.println(mainList);
return mainList;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论