Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java solution for removing dups from a string #146

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions problems/fibonacci/Fibonacci.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import org.junit.Assert;

public class Fibonacci {


public int FibonacciaAt(int number) {
List<Integer> fibonacciaSequence = new ArrayList<Integer>();
fibonacciaSequence.add(0);
fibonacciaSequence.add(1);

if (number > 2) {
while (number > fibonacciaSequence.size()) {
int nextNumberInSequence = fibonacciaSequence.get(fibonacciaSequence.size()-1) + fibonacciaSequence.get(fibonacciaSequence.size()-2);
fibonacciaSequence.add(nextNumberInSequence);
}

} else {
return fibonacciaSequence.get(number-1);
}

return fibonacciaSequence.get(number-1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should simplify your if-statement. You could (should) completely get rid of the "else" case.

fibonacciaSequence.get(number-1);

is executed in any case, so removing it makes the code easier to understand overall.


}

@Test
public void FibonacciAtTest() {
Assert.assertEquals(this.FibonacciaAt(1), 0);
Assert.assertEquals(this.FibonacciaAt(2), 1);
Assert.assertEquals(this.FibonacciaAt(3), 1);
Assert.assertEquals(this.FibonacciaAt(4), 2);
Assert.assertEquals(this.FibonacciaAt(5), 3);
Assert.assertEquals(this.FibonacciaAt(6), 5);
Assert.assertEquals(this.FibonacciaAt(7), 8);
Assert.assertEquals(this.FibonacciaAt(8), 13);
Assert.assertEquals(this.FibonacciaAt(9), 21);
Assert.assertEquals(this.FibonacciaAt(10), 34);
Assert.assertEquals(this.FibonacciaAt(20), 4181);
Assert.assertEquals(this.FibonacciaAt(30), 514229);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import org.junit.Assert;

public class RemoveDuplicatesFromString {

public static void main (String args[]) {
new RemoveDuplicatesFromString().removeCharacterDuplicatesTest();
}

public String removeDuplicateCharacters(String s) {
List<String> characters = new ArrayList<String>();
StringBuffer sb = new StringBuffer();

for (int c=0; c<s.length();c++) {
Character characterToCompare = s.charAt(c);

if (!characters.contains(characterToCompare.toString())) {
sb.append(characterToCompare);
characters.add(characterToCompare.toString());
}
}

return sb.toString();
}

@Test
public void removeCharacterDuplicatesTest() {
Assert.assertEquals(this.removeDuplicateCharacters("tree traversal"), "tre avsl");
Assert.assertEquals(this.removeDuplicateCharacters("Tree Level"), "Tre Lvl");
Assert.assertEquals(this.removeDuplicateCharacters("Tree level"), "Tre lv");
Assert.assertEquals(this.removeDuplicateCharacters("aabb ccdd"), "ab cd");
}

}