-
Notifications
You must be signed in to change notification settings - Fork 169
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
Exercise3.4 #49
Open
gimenezr
wants to merge
4
commits into
justomiguel:bootcamp_roberta
Choose a base branch
from
gimenezr:exercise3.4
base: bootcamp_roberta
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Exercise3.4 #49
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* To change this license header, choose License Headers in Project Properties. | ||
* To change this template file, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
package romannumberconversion; | ||
|
||
/** | ||
* | ||
* @author roberta | ||
*/ | ||
public enum RomanNumber { | ||
|
||
I(1), | ||
V(5), | ||
X(10), | ||
L(50), | ||
C(100), | ||
D(500), | ||
M(1000); | ||
|
||
private final int value; | ||
|
||
RomanNumber(int value) { | ||
this.value = value; | ||
} | ||
|
||
public int getValue() { | ||
return this.value; | ||
} | ||
|
||
/** | ||
* Devuelve el valor numérico de la letra romana | ||
* | ||
* @param l | ||
* @return | ||
*/ | ||
public static int parse(String l) { | ||
for (RomanNumber r : RomanNumber.values()) { | ||
if (r.toString().equals(l)) { | ||
return r.value; | ||
} | ||
} | ||
|
||
throw new IllegalArgumentException("Not a romand symbol!"); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* To change this license header, choose License Headers in Project Properties. | ||
* To change this template file, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
package romannumberconversion; | ||
|
||
/** | ||
* | ||
* @author roberta | ||
*/ | ||
public class RomanNumberConversion { | ||
|
||
private String romanNumber; | ||
|
||
public static String decToRoman(int dec) { | ||
if (dec <= 0) { | ||
return " There isn't zero in Roman number System"; | ||
} | ||
StringBuilder resul = new StringBuilder(); | ||
while (dec >= 1000) { | ||
resul.append("M"); | ||
dec -= 1000; | ||
} | ||
while (dec >= 900) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Creo firmemente que a esto se le puede pegar una vuelta de rosca por que sino la complejidad algoritmica se va a la nubes |
||
resul.append("CM"); | ||
dec -= 900; | ||
} | ||
while (dec >= 500) { | ||
resul.append("D"); | ||
dec -= 500; | ||
} | ||
while (dec >= 400) { | ||
resul.append("CD"); | ||
dec -= 400; | ||
} | ||
while (dec >= 100) { | ||
resul.append("C"); | ||
dec -= 100; | ||
} | ||
while (dec >= 90) { | ||
resul.append("XC"); | ||
dec -= 90; | ||
} | ||
while (dec >= 50) { | ||
resul.append("L"); | ||
dec -= 50; | ||
} | ||
while (dec >= 40) { | ||
resul.append("XL"); | ||
dec -= 40; | ||
} | ||
while (dec >= 10) { | ||
resul.append("X"); | ||
dec -= 10; | ||
} | ||
while (dec >= 9) { | ||
resul.append("IX"); | ||
dec -= 9; | ||
} | ||
while (dec >= 5) { | ||
resul.append("V"); | ||
dec -= 5; | ||
} | ||
while (dec >= 4) { | ||
resul.append("IV"); | ||
dec -= 4; | ||
} | ||
while (dec >= 1) { | ||
resul.append("I"); | ||
dec -= 1; | ||
} | ||
|
||
return resul.toString(); | ||
|
||
} | ||
|
||
// Returns a string containing the entered Roman numeral in numeral form. | ||
public String romanToDec(String s) { | ||
|
||
int resul = 0; | ||
int last_digit = 0; | ||
int current_digit = 0; | ||
|
||
for (int i = 0; i < s.length(); i++) { | ||
current_digit = RomanNumber.parse(s.substring(i, i + 1)); | ||
|
||
//This is the tricky part. | ||
//If the last number is smaller than the curren number, subtract the last number from the current number | ||
//Otherwise, just add the current number. We must also skip the first number from this rule simply because | ||
//e.g. someone enters 1799 in which case it would subtract 1 from 7 | ||
if (last_digit < current_digit && last_digit != 0) { | ||
current_digit -= last_digit; | ||
resul -= last_digit; | ||
resul += current_digit; | ||
last_digit = current_digit; | ||
current_digit = 0; | ||
} else { | ||
last_digit = current_digit; | ||
resul += current_digit; | ||
current_digit = 0; | ||
} | ||
} | ||
|
||
return String.valueOf(resul); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* To change this license header, choose License Headers in Project Properties. | ||
* To change this template file, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
|
||
package romannumberconversion; | ||
|
||
import org.junit.After; | ||
import org.junit.AfterClass; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* | ||
* @author roberta | ||
*/ | ||
public class RomanNumberConversionTest { | ||
|
||
public RomanNumberConversionTest() { | ||
} | ||
|
||
@BeforeClass | ||
public static void setUpClass() { | ||
} | ||
|
||
@AfterClass | ||
public static void tearDownClass() { | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
} | ||
|
||
|
||
|
||
/** | ||
* Test of decToRoman method, of class RomanNumberConversion. | ||
*/ | ||
@Test | ||
public void testDecToRoman() { | ||
System.out.println("decToRoman"); | ||
int dec = 5; | ||
String expResult = "V"; | ||
String result = RomanNumberConversion.decToRoman(dec); | ||
assertEquals(expResult, result); | ||
|
||
} | ||
|
||
/** | ||
* Test of toArabic method, of class RomanNumberConversion. | ||
*/ | ||
@Test | ||
public void testToArabic() { | ||
System.out.println("toArabic"); | ||
String s = "XXI"; | ||
RomanNumberConversion instance = new RomanNumberConversion(); | ||
String expResult = "21"; | ||
String result = instance.romanToDec(s); | ||
assertEquals(expResult, result); | ||
|
||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
En vez de hacer esto no podemos hacer una division? Por ejemplo si yo divido por 1000 el cociente entero me dice cuantas veces deberia poner determinada letra que me decis?