Skip to content

Commit

Permalink
Applying suggestions
Browse files Browse the repository at this point in the history
Restoring MustUseStringCharAtOrCodePointAt analyzer functionality
Deleting unused usesConditional variable
Removing tests/hamming/.meta/src/reference/java/Hamming.java and tests/hamming/.meta/tests.toml
  • Loading branch information
manumafe98 committed Feb 2, 2024
1 parent bce48a6 commit 5f7c0b9
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 51 deletions.
5 changes: 5 additions & 0 deletions src/main/java/analyzer/exercises/hamming/HammingAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public void analyze(Solution solution, Analysis analysis) {
return;
}

if (!walker.usesStringCharAtOrCodePointAt()) {
analysis.addComment(new MustUseStringCharAtOrCodePointAt());
return;
}

if (!walker.constructorMayCalculateDistance()) {
analysis.addComment(new CalculateDistanceInConstructor());
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/analyzer/exercises/hamming/HammingWalker.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ public boolean usesCharacterLiterals() {
return !hammingClass.findAll(CharLiteralExpr.class).isEmpty();
}

public boolean usesStringCharAtOrCodePointAt() {
return usesMethod("charAt") || usesMethod("codePointAt");
}

public boolean shouldUseStreamFilterAndCount() {
return usesMethod("reduce");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package analyzer.exercises.hamming;

import analyzer.Comment;

/**
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/hamming/must_use_string_char_at_or_code_point_at.md">Markdown Template</a>
*/
class MustUseStringCharAtOrCodePointAt extends Comment {
@Override
public String getKey() {
return "java.hamming.must_use_string_char_at_or_code_point_at";
}

@Override
public Type getType() {
return Type.ACTIONABLE;
}
}
1 change: 0 additions & 1 deletion src/main/java/analyzer/exercises/twofer/TwoferWalker.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
class TwoferWalker implements Consumer<Node> {
boolean hasHardCodedTestCases;
boolean usesIfStatement;
boolean usesConditional;
boolean hasMethodCall;
boolean usesFormat;
int returnCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class HammingAnalyzerTest {
private static Stream<Arguments> testCases() {
return Stream.of(
Arguments.of("UsesCharacterLiterals.java.txt", new Comment[]{new AvoidCharacterLiterals()}),
Arguments.of("MustUseCharAtOrCodePointAt.java.txt", new Comment[]{new MustUseStringCharAtOrCodePointAt()}),
Arguments.of("NestedValidation.java.txt", new Comment[]{new CalculateDistanceInConstructor()}),
Arguments.of("NestedCalculation.java.txt", new Comment[0]),
Arguments.of("OptimalWithCalculationInGetHammingDistance.java.txt", new Comment[]{new CalculateDistanceInConstructor()}),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.util.stream.IntStream;

/** Optimal solution, but uses String.toCharArray. */
class Hamming {
private final int hammingDistance;

Hamming(String leftStrand, String rightStrand) {
validateStrandsHaveEqualLength(leftStrand, rightStrand);

char[] left = leftStrand.toCharArray();
char[] right = rightStrand.toCharArray();

hammingDistance = (int) IntStream.range(0, leftStrand.length())
.filter(index -> left[index] != right[index])
.count();
}

private void validateStrandsHaveEqualLength() {
if (leftStrand.length() == rightStrand.length()) {
return;
}
if (leftStrand.isEmpty()) {
throw new IllegalArgumentException("left strand must not be empty.");
}
if (rightStrand.isEmpty()) {
throw new IllegalArgumentException("right strand must not be empty.");
}
throw new IllegalArgumentException(
"leftStrand and rightStrand must be of equal length.");
}

int getHammingDistance() {
return hammingDistance;
}
}
20 changes: 0 additions & 20 deletions tests/hamming/.meta/src/reference/java/Hamming.java

This file was deleted.

30 changes: 0 additions & 30 deletions tests/hamming/.meta/tests.toml

This file was deleted.

0 comments on commit 5f7c0b9

Please sign in to comment.