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

Changed integers.java constants location and implemented constants in file #237

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
116 changes: 53 additions & 63 deletions src/com/jwetherell/algorithms/numbers/Integers.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ public class Integers {
private static final BigDecimal ZERO = new BigDecimal(0);
private static final BigDecimal TWO = new BigDecimal(2);

Copy link
Author

Choose a reason for hiding this comment

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

Constants start from here now

private static final int BILLION = 1000000000;
private static final int MILLION = 1000000;
private static final int THOUSAND = 1000;
private static final int HUNDRED = 100;
private static final int TEN = 10;

public static final String toBinaryUsingDivideAndModulus(int numberToConvert) {
int integer = numberToConvert;
if (integer<0) throw new IllegalArgumentException("Method argument cannot be negative. number="+integer);
Copy link
Author

Choose a reason for hiding this comment

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

Rest of code is formatting issues that bothered me when trying to implement the constants.

Copy link
Author

Choose a reason for hiding this comment

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

As well as implementing constants obviously

if (integer < 0) throw new IllegalArgumentException("Method argument cannot be negative. number=" + integer);
StringBuilder builder = new StringBuilder();
int temp = 0;
while (integer > 0) {
Expand All @@ -24,7 +30,7 @@ public static final String toBinaryUsingDivideAndModulus(int numberToConvert) {

public static final String toBinaryUsingShiftsAndModulus(int numberToConvert) {
int integer = numberToConvert;
if (integer<0) throw new IllegalArgumentException("Method argument cannot be negative. number="+integer);
if (integer < 0) throw new IllegalArgumentException("Method argument cannot be negative. number=" + integer);
StringBuilder builder = new StringBuilder();
int temp = 0;
while (integer > 0) {
Expand All @@ -37,7 +43,7 @@ public static final String toBinaryUsingShiftsAndModulus(int numberToConvert) {

public static final String toBinaryUsingBigDecimal(int numberToConvert) {
int integer = numberToConvert;
if (integer<0) throw new IllegalArgumentException("Method argument cannot be negative. number="+integer);
if (integer < 0) throw new IllegalArgumentException("Method argument cannot be negative. number=" + integer);
StringBuilder builder = new StringBuilder();
BigDecimal number = new BigDecimal(integer);
BigDecimal[] decimals = null;
Expand All @@ -51,11 +57,11 @@ public static final String toBinaryUsingBigDecimal(int numberToConvert) {

public static final String toBinaryUsingDivideAndDouble(int numberToConvert) {
int integer = numberToConvert;
if (integer<0) throw new IllegalArgumentException("Method argument cannot be negative. number="+integer);
if (integer < 0) throw new IllegalArgumentException("Method argument cannot be negative. number=" + integer);
StringBuilder builder = new StringBuilder();
double temp = 0d;
while (integer > 0) {
temp = integer/2d;
temp = integer / 2d;
integer = (int) temp;
builder.append((temp > integer) ? 1 : 0);
}
Expand All @@ -64,85 +70,70 @@ public static final String toBinaryUsingDivideAndDouble(int numberToConvert) {

public static final boolean powerOfTwoUsingLoop(int numberToCheck) {
int number = numberToCheck;
if (number == 0)
return false;
if (number == 0) return false;
while (number % 2 == 0) {
number /= 2;
}
if (number > 1)
return false;
return true;
return number == 1;
}

public static final boolean powerOfTwoUsingRecursion(int numberToCheck) {
int number = numberToCheck;
if (number == 1)
return true;
if (number == 0 || number % 2 != 0)
return false;
if (number == 1) return true;
if (number == 0 || number % 2 != 0) return false;
return powerOfTwoUsingRecursion(number / 2);
}

public static final boolean powerOfTwoUsingLog(int numberToCheck) {
int number = numberToCheck;
double doubleLog = Math.log10(number) / Math.log10(2);
int intLog = (int) doubleLog;
if (Double.compare(doubleLog, intLog) == 0)
return true;
return false;
return Double.compare(doubleLog, intLog) == 0;
}

public static final boolean powerOfTwoUsingBits(int numberToCheck) {
int number = numberToCheck;
if (number != 0 && ((number & (number - 1)) == 0))
return true;
return false;
return number != 0 && ((number & (number - 1)) == 0);
}

// Integer to English
private static final Map<Integer,String> singleDigits = new HashMap<Integer,String>();
private static final Map<Integer, String> singleDigits = new HashMap<Integer, String>();
static {
singleDigits.put(0,"zero");
singleDigits.put(1,"one");
singleDigits.put(2,"two");
singleDigits.put(3,"three");
singleDigits.put(4,"four");
singleDigits.put(5,"five");
singleDigits.put(6,"six");
singleDigits.put(7,"seven");
singleDigits.put(8,"eight");
singleDigits.put(9,"nine");
singleDigits.put(10,"ten");
singleDigits.put(11,"eleven");
singleDigits.put(12,"twelve");
singleDigits.put(13,"thirteen");
singleDigits.put(14,"fourteen");
singleDigits.put(15,"fifteen");
singleDigits.put(16,"sixteen");
singleDigits.put(17,"seventeen");
singleDigits.put(18,"eighteen");
singleDigits.put(19,"nineteen");
singleDigits.put(0, "zero");
singleDigits.put(1, "one");
singleDigits.put(2, "two");
singleDigits.put(3, "three");
singleDigits.put(4, "four");
singleDigits.put(5, "five");
singleDigits.put(6, "six");
singleDigits.put(7, "seven");
singleDigits.put(8, "eight");
singleDigits.put(9, "nine");
singleDigits.put(10, "ten");
singleDigits.put(11, "eleven");
singleDigits.put(12, "twelve");
singleDigits.put(13, "thirteen");
singleDigits.put(14, "fourteen");
singleDigits.put(15, "fifteen");
singleDigits.put(16, "sixteen");
singleDigits.put(17, "seventeen");
singleDigits.put(18, "eighteen");
singleDigits.put(19, "nineteen");
}

private static final Map<Integer,String> multiDigits = new HashMap<Integer,String>();
private static final Map<Integer, String> multiDigits = new HashMap<Integer, String>();
static {
multiDigits.put(10,"ten");
multiDigits.put(20,"twenty");
multiDigits.put(30,"thirty");
multiDigits.put(40,"forty");
multiDigits.put(50,"fifty");
multiDigits.put(60,"sixty");
multiDigits.put(70,"seventy");
multiDigits.put(80,"eighty");
multiDigits.put(90,"ninety");
multiDigits.put(10, "ten");
multiDigits.put(20, "twenty");
multiDigits.put(30, "thirty");
multiDigits.put(40, "forty");
multiDigits.put(50, "fifty");
multiDigits.put(60, "sixty");
multiDigits.put(70, "seventy");
multiDigits.put(80, "eighty");
multiDigits.put(90, "ninety");
}

private static final int BILLION = 1000000000;
private static final int MILLION = 1000000;
private static final int THOUSAND = 1000;
private static final int HUNDRED = 100;
private static final int TEN = 10;

private static final String handleUnderOneThousand(int number) {
StringBuilder builder = new StringBuilder();
int x = number;
Expand Down Expand Up @@ -173,19 +164,18 @@ private static final String handleUnderOneThousand(int number) {

public static final String toEnglish(int number) {
int x = number;
if (x>Integer.MAX_VALUE || x<=Integer.MIN_VALUE) throw new IllegalArgumentException("Number has to be <= Integer.MAX_VALUE and > Integer.MIN_VALUE. number="+x);
if (x > Integer.MAX_VALUE || x <= Integer.MIN_VALUE) throw new IllegalArgumentException("Number has to be <= Integer.MAX_VALUE and > Integer.MIN_VALUE. number=" + x);
StringBuilder builder = new StringBuilder();
if (x==0) {
//Zero is a special case
if (x == 0) {
// Zero is a special case
builder.append(singleDigits.get(x));
return builder.toString();
}
boolean billion = false;
boolean million = false;
boolean thousand = false;
if (x<0) {
if (x < 0) {
builder.append("negative ");
// Make the number positive
x = x * -1;
}
int m = x / BILLION;
Expand All @@ -208,7 +198,7 @@ public static final String toEnglish(int number) {
builder.append(handleUnderOneThousand(m)).append("-thousand");
x = x % THOUSAND;
}
if (billion || million || thousand && x!=0) builder.append(" ");
if ((billion || million || thousand) && x != 0) builder.append(" ");
builder.append(handleUnderOneThousand(x));
return builder.toString();
}
Expand Down