Exercism - Resistor Color Duo
This post shows you how to get Resistor Color Duo exercise of Exercism.
Preparation
Before we click on our next exercise, let’s see what concepts of DART we need to consider

So we need to use the following concepts.
Lists
Lists are ordered collections of items. The position (index) of an item in a list can be used to represent its value.
void main() {
List<String> colors = [
'black',
'brown',
'red',
'orange',
'yellow'
];
// Access by index
print(colors[0]); // 'black'
print(colors[1]); // 'brown'
print(colors[2]); // 'red'
// The index represents the value
// black is at index 0, so its value is 0
// brown is at index 1, so its value is 1
}
List Indexing
You can access elements in a list by their index using square brackets. The first element is at index 0.
void main() {
List<String> colors = ['brown', 'green', 'violet'];
// Access first element
String first = colors[0];
print(first); // 'brown'
// Access second element
String second = colors[1];
print(second); // 'green'
// Access third element (if exists)
String third = colors[2];
print(third); // 'violet'
// Get values for first two colors
int firstValue = colors.indexOf(colors[0]);
int secondValue = colors.indexOf(colors[1]);
print('$firstValue$secondValue'); // '15'
}
List.indexOf Method
The indexOf() method returns the index of the first occurrence of an element in a list. If the element is not found, it returns -1.
void main() {
List<String> colors = [
'black',
'brown',
'red',
'orange',
'yellow'
];
// Find index of a color
int index = colors.indexOf('red');
print(index); // 2
// Find index of another color
int brownIndex = colors.indexOf('brown');
print(brownIndex); // 1
// The index IS the value!
// 'red' is at index 2, so its value is 2
}
String Concatenation
Strings can be concatenated using the + operator. When you concatenate two strings, they are joined together.
void main() {
String first = '1';
String second = '5';
// Concatenate strings
String combined = first + second;
print(combined); // '15'
// Convert numbers to strings first
int num1 = 1;
int num2 = 5;
String result = num1.toString() + num2.toString();
print(result); // '15'
// Direct concatenation
String direct = '1' + '5';
print(direct); // '15'
}
Integer Parsing
The int.parse() method converts a string to an integer. It’s needed when you have a string representation of a number.
void main() {
// Parse a string to integer
String numberStr = '15';
int number = int.parse(numberStr);
print(number); // 15
// Parse concatenated strings
String combined = '1' + '5';
int value = int.parse(combined);
print(value); // 15
// Two-digit number from two single digits
int digit1 = 1;
int digit2 = 5;
int twoDigit = int.parse(digit1.toString() + digit2.toString());
print(twoDigit); // 15
}
Expression Bodied Methods
Expression bodied methods use the => syntax to provide a concise way to write methods that return a single expression.
class Example {
List<String> items = ['a', 'b', 'c'];
// Regular method
int getValue(int index) {
return items.indexOf(items[index]);
}
// Expression bodied method (shorter)
int getValueShort(int index) => items.indexOf(items[index]);
// Complex expression
int combineValues(List<String> colors) =>
int.parse(colors[0] + colors[1]);
}
Variable Declaration with var
The var keyword allows Dart to infer the type of a variable based on its initial value.
void main() {
// var infers the type from the value
var colors = ['black', 'brown', 'red'];
// Dart infers: List<String>
// Equivalent to:
List<String> colors2 = ['black', 'brown', 'red'];
// Both are the same
print(colors.runtimeType); // List<String>
print(colors2.runtimeType); // List<String>
}
Introduction
If you want to build something using a Raspberry Pi, you’ll probably use resistors. For this exercise, you need to know two things about them:
- Each resistor has a resistance value.
- Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read.
To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values. Each band has a position and a numeric value.
The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number. For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15.
In this exercise you are going to create a helpful program so that you don’t have to remember the values of the bands. The program will take color names as input and output a two digit number, even if the input is more than two colors!
Color Encoding
The band colors are encoded as follows:
- black: 0
- brown: 1
- red: 2
- orange: 3
- yellow: 4
- green: 5
- blue: 6
- violet: 7
- grey: 8
- white: 9
From the example above: brown-green should return 15, and brown-green-violet should return 15 too, ignoring the third color.
What is a resistor color code?
The electronic color code is used to indicate the values or ratings of electronic components, usually for resistors, but also for capacitors, inductors, diodes and others. The first two bands of a resistor indicate the significant digits of the resistance value, with the first band being the tens digit and the second band being the ones digit.
— Wikipedia
How can we combine two colors into a number?
To combine two colors into a two-digit number:
- Get the first color’s value: Use
indexOf()to find the index (value) of the first color - Get the second color’s value: Use
indexOf()to find the index (value) of the second color - Convert to strings: Convert both values to strings
- Concatenate: Join the two strings together
- Parse to integer: Convert the concatenated string back to an integer
The key insight is that the first color represents the tens digit and the second color represents the ones digit. By concatenating their string representations, we form a two-digit number.
For example, with colors [‘brown’, ‘green’]:
- First color ‘brown’ → index 1 → value 1
- Second color ‘green’ → index 5 → value 5
- Convert to strings: ‘1’ + ‘5’ = ‘15’
- Parse to integer: 15
Solution
class ResistorColorDuo {
var colors = [
'black',
'brown',
'red',
'orange',
'yellow',
'green',
'blue',
'violet',
'grey',
'white'
];
int value(List<String> selectedColors) => int.parse(
(colors.indexOf(selectedColors[0]).toString() +
colors.indexOf(selectedColors[1]).toString())
);
}
Let’s break down the solution:
-
class ResistorColorDuo- Defines a class for resistor color duo operations:- Encapsulates the color data and value calculation functionality
-
var colors = [...]- Stores all resistor colors in order:- Uses
varfor type inference (Dart infersList<String>) - Colors are ordered from 0 to 9 (same as Resistor Color exercise):
- Index 0: ‘black’ (value 0)
- Index 1: ‘brown’ (value 1)
- Index 2: ‘red’ (value 2)
- … and so on
- The list itself can be accessed to get all colors
- Uses
-
int value(List<String> selectedColors) => ...- Calculates the two-digit value from two colors:- Uses expression-bodied method syntax (
=>) - Takes a list of color names as input (only uses the first two)
- Step 1:
selectedColors[0]- Gets the first color from the input list - Step 2:
colors.indexOf(selectedColors[0])- Finds the index (value) of the first color - Step 3:
.toString()- Converts the first value to a string - Step 4:
selectedColors[1]- Gets the second color from the input list - Step 5:
colors.indexOf(selectedColors[1])- Finds the index (value) of the second color - Step 6:
.toString()- Converts the second value to a string - Step 7:
+- Concatenates the two strings (e.g., ‘1’ + ‘5’ = ‘15’) - Step 8:
int.parse(...)- Converts the concatenated string to an integer - Returns the two-digit number
- Uses expression-bodied method syntax (
The solution efficiently combines two color values into a two-digit number by using string concatenation. The first color represents the tens digit and the second color represents the ones digit. Any additional colors in the input list are simply ignored, as only the first two are used.
You can watch this tutorial on YouTube. So don’t forget to like and subscribe. 😉
Watch on YouTube