My Solution has comlpete the tasks, although it shouldn't... The output is "Liebe, Leier, Liebe, Leier", though it's meant to be just "Liebe, Leier, Liebe".
I'm sorry for posting a solution, but I don't get why this went through?! Even though the output is incorrect...
public class Solution {
public static void main(String[] args) throws Exception {
ArrayList<String> liste = new ArrayList<>();
liste.add("Rose"); // 0
liste.add("Liebe"); // 1
liste.add("Leier"); // 2
liste = korrigieren(liste);
for (String s : liste) {
System.out.println(s);
}
}
public static ArrayList<String> korrigieren(ArrayList<String> list) {
// schreib hier deinen Code
ArrayList<String> toRemove = new ArrayList<>();
ArrayList<String> toAdd = new ArrayList<>();
for(String i : list) {
System.out.println("next");
if (i.contains("R") || i.contains("r") && !(i.contains("l") || i.contains("L"))) {
System.out.println("R");
toRemove.add(i);
System.out.println("R has been added to toRemove");
}
else if (i.contains("L") || i.contains("l") && !(i.contains("R") || i.contains("r"))) {
System.out.println("L");
toAdd.add(i);
System.out.println("L has been added");
}
}
list.removeAll(toRemove);
list.addAll(toAdd);
System.out.println(list.size());
return list;
}
}
A incorrect solution
In der Diskussion
Kommentare (4)
- Beliebt
- Neu
- Alt
Du musst angemeldet sein, um einen Kommentar schreiben zu können
Gellert Varga
6 Juni 2020, 21:01
0
Gellert Varga
6 Juni 2020, 20:53
I.) Are you quite sure that the verifier has accepted exactly the same task?...
II.) I have tried your program and I have the following comments:
1) The logical 'AND' operation has a higher precedence than the logical 'OR'.
I enclose a table on this.
The 'AND' operation is executed before the 'OR'.
The 'AND' takes precedence.
2) Proper application of parentheses changes the meaning of the 'if' condition (changes the order of the executions of the logical operations).
You wrote:
if (i.contains("R") || i.contains("r") && !(i.contains("l") || i.contains("L")))
I corrected to:
if ((i.contains("R") || i.contains("r")) && !(i.contains("l") || i.contains("L")))
((And same error in the 'else-if' thread!))
The two versions above do not mean the same condition.
In the second case, I think the program already works well.
0
Fabian GU
8 Juni 2020, 11:48
Thanks for your answer! Now you have explained it, it's really obvious! The missing parentheses, is something I should've thought of... But yeah I'm sure, the code I wrote passed the verification, don't know how, but it did...
Thanks again, for your explanation!
0
Gellert Varga
8 Juni 2020, 17:26
You're welcome:)
0