Skip to content

Instantly share code, notes, and snippets.

@soulninja-dev
Created March 12, 2021 12:20
Show Gist options
  • Save soulninja-dev/aac2e0b6e7fed73db4a2e0c63f4ddcd5 to your computer and use it in GitHub Desktop.
Save soulninja-dev/aac2e0b6e7fed73db4a2e0c63f4ddcd5 to your computer and use it in GitHub Desktop.
Solution for Resistor Color - Exercism

Using Array

class ResistorColor {
	private static final String[] colorArray;
    colorArray = new String[]{"black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white"};
    int colorCode(String color) {
    	int index = 0;
    	for(int i = 0;i < colorArray.length;i++)
    	{
    		if(color.equals(colorArray[i]))
    			index = i;
    	} 
    	return index;
    }

    String[] colors() {
    	
    	return colorArray;
    }
}

Using List

class ResistorColor {
	static List<String> colors = Arrays.asList("black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white");
	// function to return colorCode and takes in String color
	int colorCode(String color) {
		return colors.indexOf(color);
	}
	
	// function to return colorsArray
	String[] colors() {
		return (String[]) colors.toArray(new String[0]);
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment