Below is a list of exercises for experimenting with building algorithms focusing on loop construction.
The topic is the manipulation of strings.
Similar to lists/arrays, strings can be iterated over, and can be seen as a collection of characters.
Once your algorithms designed, it is recommended to code them in Python to be able to test them.
However, if you do not feel confident with Python so far, you can simply write them on a sheet of paper and test them by hand.
When testing your algorithms, try to think of cases that cover all possible results.
For instance, if your algorithm should output true or false, try it for inputs that lead to both cases.
Constraint – There are many ways to write algorithms to solve a problem, and many ways to program an algorithm.
In this practical activity, you do not have the right to use the break keyword.
Also, think carefully on whether you need a bounded loop (for) or an unbounded one (while).
Important
The aim of this session is to help you master important notions in computer science.
An intelligent programming assistant such as GitHub Copilot, that you may have installed already, will be able to provide you with a solution to these exercises based only on a wisely chosen file name.
For the sake of training, we advise you to disable such tools first.
At the end of the practical activity, we suggest you to work on the exercise again with these tools activated.
Following these two steps will improve your skills both fundamentally and practically.
Also, we provide you the solutions to the exercises.
Make sure to check them only after you have a solution to the exercises, for comparison purpose!
Even if you are sure your solution is correct, please have a look at them, as they sometimes provide additional elements you may have missed.
Activity contents
1 — Last occurrence
Start with the following program:
# The string to manipulates ="IMT Atlantique"print(s)
# Write your codes here# ...
/**
* To run this code, you need to have Java installed on your computer, then:
* - Create a file named `Main.java` in a directory of your choice.
* - Copy this code in the file.
* - Open a terminal in the directory where the file is located.
* - Run the command `javac Main.java` to compile the code.
* - Run the command `java -ea Main` to execute the compiled code.
* Note: '-ea' is an option to enable assertions in Java.
*/publicclassMain {
/**
* This is the entry point of your program.
* It contains the first codes that are going to be executed.
*
* @param args Command line arguments received.
*/publicstaticvoidmain(String[] args) {
// The string to manipulate String s ="IMT Atlantique";
System.out.println(s);
// Write your codes here// ... }
}
Conceive an algorithm that determines the position (between 0 and the length of s minus 1) of the last occurrence of a symbol in the string s.
The searched symbol is stored in a variable named v.
The algorithm displays the position found or indicates if the symbol is not present in s.
The algorithm can be evaluated with the following values:
s = "IMT Atlantique" and v = 'l'
s = "IMT Atlantique" and v = 'z'
s = "I love Computer Science and Algorithms" and v = 'o'
Correction
# The string to manipulates ="IMT Atlantique"print(s)
# The thing to searchv ='t'# Last occurrence of v in sindex = len(s) -1while index >=0and s[index] != v:
index -=1# Print resultif index >=0:
print("Found at index", index)
else:
print("Not found")
# The string to manipulates ="IMT Atlantique"print(s)
# The thing to searchv ='l'# Use string method to find last occurrenceresult = s.rfind(v)
# Print resultif result !=-1:
print("Found at index", result)
else:
print("Not found")
/**
* To run this code, you need to have Java installed on your computer, then:
* - Create a file named `Main.java` in a directory of your choice.
* - Copy this code in the file.
* - Open a terminal in the directory where the file is located.
* - Run the command `javac Main.java` to compile the code.
* - Run the command `java -ea Main` to execute the compiled code.
* Note: '-ea' is an option to enable assertions in Java.
*/publicclassMain {
/**
* This is the entry point of your program.
* It contains the first codes that are going to be executed.
*
* @param args Command line arguments received.
*/publicstaticvoidmain(String[] args) {
// The string to manipulate String s ="IMT Atlantique";
System.out.println(s);
// The thing to searchchar v ='t';
// Last occurrence of v in sint index = s.length() - 1;
while (index >= 0 && s.charAt(index) != v) {
index--;
}
// Print resultif (index >= 0) {
System.out.println("Found at index "+ index);
} else {
System.out.println("Not found");
}
}
}
/**
* To run this code, you need to have Java installed on your computer, then:
* - Create a file named `Main.java` in a directory of your choice.
* - Copy this code in the file.
* - Open a terminal in the directory where the file is located.
* - Run the command `javac Main.java` to compile the code.
* - Run the command `java -ea Main` to execute the compiled code.
* Note: '-ea' is an option to enable assertions in Java.
*/publicclassMain {
/**
* This is the entry point of your program.
* It contains the first codes that are going to be executed.
*
* @param args Command line arguments received.
*/publicstaticvoidmain(String[] args) {
// The string to manipulate String s ="IMT Atlantique";
System.out.println(s);
// The thing to searchchar v ='l';
// Use string method to find last occurrenceint result = s.lastIndexOf(v);
// Print resultif (result !=-1) {
System.out.println("Found at index "+ result);
} else {
System.out.println("Not found");
}
}
}
2 — First occurrence
Start again with the same base code as in exercise 1.
Conceive an algorithm that determines the position (between 0 and the length of s minus 1) of the first occurrence of a symbol in the string s.
The searched symbol is stored in a variable named v.
The algorithm displays the position found or indicates if the symbol is not present in s.
The algorithm can be evaluated with the following values:
s = "IMT Atlantique" and v = 'l'
s = "IMT Atlantique" and v = 'z'
s = "I love Computer Science and Algorithms" and v = 'o'
Correction
# The string to manipulates ="IMT Atlantique"print(s)
# The thing to searchv ='t'# Initialize variable for the while loopindex =0# Iterate from the start to the endwhile index < len(s) and s[index] != v:
index +=1# Print the resultif index < len(s):
print("Found at index", index)
else:
print("Not found")
# The string to manipulates ="IMT Atlantique"print(s)
# The thing to searchv ='l'# Use string method to find first occurrenceresult = s.find(v)
# Print resultif result !=-1:
print("Found at index", result)
else:
print("Not found")
/**
* To run this code, you need to have Java installed on your computer, then:
* - Create a file named `Main.java` in a directory of your choice.
* - Copy this code in the file.
* - Open a terminal in the directory where the file is located.
* - Run the command `javac Main.java` to compile the code.
* - Run the command `java -ea Main` to execute the compiled code.
* Note: '-ea' is an option to enable assertions in Java.
*/publicclassMain {
/**
* This is the entry point of your program.
* It contains the first codes that are going to be executed.
*
* @param args Command line arguments received.
*/publicstaticvoidmain(String[] args) {
// The string to manipulate String s ="IMT Atlantique";
System.out.println(s);
// The thing to searchchar v ='l';
// Initialize variable for the while loopint index = 0;
// Iterate from the start to the endwhile (index < s.length() && s.charAt(index) != v) {
index++;
}
// Print the resultif (index < s.length()) {
System.out.println("Found at index "+ index);
} else {
System.out.println("Not found");
}
}
}
/**
* To run this code, you need to have Java installed on your computer, then:
* - Create a file named `Main.java` in a directory of your choice.
* - Copy this code in the file.
* - Open a terminal in the directory where the file is located.
* - Run the command `javac Main.java` to compile the code.
* - Run the command `java -ea Main` to execute the compiled code.
* Note: '-ea' is an option to enable assertions in Java.
*/publicclassMain {
/**
* This is the entry point of your program.
* It contains the first codes that are going to be executed.
*
* @param args Command line arguments received.
*/publicstaticvoidmain(String[] args) {
// The string to manipulate String s ="IMT Atlantique";
System.out.println(s);
// The thing to searchchar v ='l';
// Use string method to find first occurrenceint result = s.indexOf(v);
// Print resultif (result !=-1) {
System.out.println("Found at index "+ result);
} else {
System.out.println("Not found");
}
}
}
3 — Prefix
Start again with the same base code as in exercise 1.
Considering a second array of characters, named pref for instance, determine if pref is a prefix of s.
Carefully think about the different possible scenarios related to the lengths of s and pref.
The algorithm prints if pref is a prefix of s.
The algorithm can be evaluated with the following values:
s = "IMT Atlantique" and pref = "IMT"
s = "IMT Atlantique" and pref = "IMT Atlantique is a great school"
s = "I love Computer Science and Algorithms" and pref = "I love"
Correction
# The string to manipulates ="IMT Atlantique"print(s)
# The thing to searchpref ="IMT"# Iterate over the string util a different character is foundi =0while i < len(s) and i < len(pref) and s[i] == pref[i]:
i +=1# Print resultif i == len(pref):
print(pref, "is a prefix of", s)
else:
print(pref, "is not a prefix of", s)
# The string to manipulates ="IMT Atlantique"print(s)
# The thing to searchpref ="IMT"# Use string method for thatif s.startswith(pref):
print(pref, "is a prefix of", s)
else:
print(pref, "is not a prefix of", s)
/**
* To run this code, you need to have Java installed on your computer, then:
* - Create a file named `Main.java` in a directory of your choice.
* - Copy this code in the file.
* - Open a terminal in the directory where the file is located.
* - Run the command `javac Main.java` to compile the code.
* - Run the command `java -ea Main` to execute the compiled code.
* Note: '-ea' is an option to enable assertions in Java.
*/publicclassMain {
/**
* This is the entry point of your program.
* It contains the first codes that are going to be executed.
*
* @param args Command line arguments received.
*/publicstaticvoidmain(String[] args) {
// The string to manipulate String s ="IMT Atlantique";
System.out.println(s);
// The prefix to search String pref ="IMT";
// Iterate over the string until a different character is foundint i = 0;
while (i < s.length() && i < pref.length() && s.charAt(i) == pref.charAt(i)) {
i++;
}
// Print resultif (i == pref.length()) {
System.out.println(pref +" is a prefix of "+ s);
} else {
System.out.println(pref +" is not a prefix of "+ s);
}
}
}
/**
* To run this code, you need to have Java installed on your computer, then:
* - Create a file named `Main.java` in a directory of your choice.
* - Copy this code in the file.
* - Open a terminal in the directory where the file is located.
* - Run the command `javac Main.java` to compile the code.
* - Run the command `java -ea Main` to execute the compiled code.
* Note: '-ea' is an option to enable assertions in Java.
*/publicclassMain {
/**
* This is the entry point of your program.
* It contains the first codes that are going to be executed.
*
* @param args Command line arguments received.
*/publicstaticvoidmain(String[] args) {
// The string to manipulate String s ="IMT Atlantique";
System.out.println(s);
// The thing to search String pref ="IMT";
// Use string method for thatif (s.startsWith(pref)) {
System.out.println(pref +" is a prefix of "+ s);
} else {
System.out.println(pref +" is not a prefix of "+ s);
}
}
}
4 — Contains
Start again with the same base code as in exercise 1.
Considering a second array of characters, named substr for instance, determine if substr is present in s.
The algorithm indicates the position of the first occurrence of substr in s or whether it is not present.
The algorithm can be evaluated with the following values:
s = "IMT Atlantique" and substr = "Atlan"
s = "IMT Atlantique" and substr = "Pyhton"
s = "I love Computer Science and Algorithms" and substr = "and"
Correction
# The string to manipulates ="IMT Atlantique"print(s)
# The thing to searchsubstr ="Atlan"# Initialize variablesindex_start =0index_sub =0found =False# Outer loop: iterate through each position in the string where the substring could startwhile index_start <= len(s) - len(substr) and index_sub < len(substr):
# Inner loop: check if the substring matches at this position index_sub =0while index_sub < len(substr) and s[index_start + index_sub] == substr[index_sub]:
index_sub +=1 index_start +=1# Print a message if not foundif index_sub >= len(substr):
print("Substring found at position", index_start -1)
else:
print("Substring not found")
# The string to manipulates ="IMT Atlantique"print(s)
# The thing to searchsubstr ="Atlan"# You can directly check with 'in' keywordif substr in s:
print("Substring found at index", s.index(substr))
else:
print("Substring not found")
/**
* To run this code, you need to have Java installed on your computer, then:
* - Create a file named `Main.java` in a directory of your choice.
* - Copy this code in the file.
* - Open a terminal in the directory where the file is located.
* - Run the command `javac Main.java` to compile the code.
* - Run the command `java -ea Main` to execute the compiled code.
* Note: '-ea' is an option to enable assertions in Java.
*/publicclassMain {
/**
* This is the entry point of your program.
* It contains the first codes that are going to be executed.
*
* @param args Command line arguments received.
*/publicstaticvoidmain(String[] args) {
// The string to manipulate String s ="IMT Atlantique";
System.out.println(s);
// The thing to search String substr ="Atlan";
// Initialize variablesint indexStart = 0;
int indexSub = 0;
boolean found =false;
// Outer loop: iterate through each position in the string where the substring could startwhile (indexStart <= s.length() - substr.length() && indexSub < substr.length()) {
// Inner loop: check if the substring matches at this position indexSub = 0;
while (indexSub < substr.length() && s.charAt(indexStart + indexSub) == substr.charAt(indexSub)) {
indexSub++;
}
indexStart++;
}
// Print a message if not foundif (indexSub >= substr.length()) {
System.out.println("Substring found at position "+ (indexStart - 1));
} else {
System.out.println("Substring not found");
}
}
}
/**
* To run this code, you need to have Java installed on your computer, then:
* - Create a file named `Main.java` in a directory of your choice.
* - Copy this code in the file.
* - Open a terminal in the directory where the file is located.
* - Run the command `javac Main.java` to compile the code.
* - Run the command `java -ea Main` to execute the compiled code.
* Note: '-ea' is an option to enable assertions in Java.
*/publicclassMain {
/**
* This is the entry point of your program.
* It contains the first codes that are going to be executed.
*
* @param args Command line arguments received.
*/publicstaticvoidmain(String[] args) {
// The string to manipulate String s ="IMT Atlantique";
System.out.println(s);
// The thing to search String substr ="Atlan";
// Use string method for thatif (s.contains(substr)) {
System.out.println("Substring found at index "+ s.indexOf(substr));
} else {
System.out.println("Substring not found");
}
}
}
5 — Reverse
Start again with the same base code as in exercise 1.
Determine an algorithm that reverses the content of string s.
The algorithm can be evaluated with the following values:
s = "IMT Atlantique"
s = "euqitnaltA TMI"
s = "I love Computer Science and Algorithms"
Correction
# The string to manipulates ="IMT Atlantique"print(s)
# Loop to append to a new stringresult =""for character in s:
result = character + result
# Print the resultprint("Reversed string:", result)
# The string to manipulates ="IMT Atlantique"print(s)
# Using operator []result = s[::-1]
# Print the resultprint("Reversed string:", result)
/**
* To run this code, you need to have Java installed on your computer, then:
* - Create a file named `Main.java` in a directory of your choice.
* - Copy this code in the file.
* - Open a terminal in the directory where the file is located.
* - Run the command `javac Main.java` to compile the code.
* - Run the command `java -ea Main` to execute the compiled code.
* Note: '-ea' is an option to enable assertions in Java.
*/publicclassMain {
/**
* This is the entry point of your program.
* It contains the first codes that are going to be executed.
*
* @param args Command line arguments received.
*/publicstaticvoidmain(String[] args) {
// The string to manipulate String s ="IMT Atlantique";
System.out.println(s);
// Loop to append to a new string String result ="";
for (int i = 0; i < s.length(); i++) {
result = s.charAt(i) + result;
}
// Print the result System.out.println("Reversed string: "+ result);
}
}
/**
* To run this code, you need to have Java installed on your computer, then:
* - Create a file named `Main.java` in a directory of your choice.
* - Copy this code in the file.
* - Open a terminal in the directory where the file is located.
* - Run the command `javac Main.java` to compile the code.
* - Run the command `java -ea Main` to execute the compiled code.
* Note: '-ea' is an option to enable assertions in Java.
*/publicclassMain {
/**
* This is the entry point of your program.
* It contains the first codes that are going to be executed.
*
* @param args Command line arguments received.
*/publicstaticvoidmain(String[] args) {
// The string to manipulate String s ="IMT Atlantique";
System.out.println(s);
// Using StringBuilder for reversing a string String result =new StringBuilder(s).reverse().toString();
// Print the result System.out.println("Reversed string: "+ result);
}
}
6 — Palindrome
A palindrome is a word that can be read from both sides, as for instance “radar” or “laval”.
Determine a first naive strategy to check if a string is a palindrome based on the reverse algorithm devised earlier.
The algorithm can be evaluated with the following values:
s = "laval"
s = "rennes"
s = "detartrated"
Correction
# Initialize variabless ="LAVAL"i =0# Iterate over the stringwhile i < len(s) /2and s[i] == s[len(s) -1- i]:
i +=1# Show result# Show resultif i >= len(s) /2:
print(s, "is a palindrome")
else:
print(s, "is not a palindrome")
# The string to manipulates ="laval"print(s)
# Check if s is a palindromeif s == s[::-1]:
print(s, "is a palindrome")
else:
print(s, "is not a palindrome")
/**
* To run this code, you need to have Java installed on your computer, then:
* - Create a file named `Main.java` in a directory of your choice.
* - Copy this code in the file.
* - Open a terminal in the directory where the file is located.
* - Run the command `javac Main.java` to compile the code.
* - Run the command `java -ea Main` to execute the compiled code.
* Note: '-ea' is an option to enable assertions in Java.
*/publicclassMain {
/**
* This is the entry point of your program.
* It contains the first codes that are going to be executed.
*
* @param args Command line arguments received.
*/publicstaticvoidmain(String[] args) {
// The string to manipulate String s ="laval";
System.out.println(s);
// Initialize variablesint i = 0;
// Loop to check characters from start and endwhile (i < s.length() / 2 && s.charAt(i) == s.charAt(s.length() - i - 1)) {
i++;
}
// Print a message based on the resultif (i >= s.length() / 2) {
System.out.println(s +" is a palindrome");
} else {
System.out.println(s +" is not a palindrome");
}
}
}
/**
* To run this code, you need to have Java installed on your computer, then:
* - Create a file named `Main.java` in a directory of your choice.
* - Copy this code in the file.
* - Open a terminal in the directory where the file is located.
* - Run the command `javac Main.java` to compile the code.
* - Run the command `java -ea Main` to execute the compiled code.
* Note: '-ea' is an option to enable assertions in Java.
*/publicclassMain {
/**
* This is the entry point of your program.
* It contains the first codes that are going to be executed.
*
* @param args Command line arguments received.
*/publicstaticvoidmain(String[] args) {
// The string to manipulate String s ="laval";
System.out.println(s);
// Check if s is a palindrome using StringBuilderif (s.equals(new StringBuilder(s).reverse().toString())) {
System.out.println(s +" is a palindrome");
} else {
System.out.println(s +" is not a palindrome");
}
}
}
6.1 — Additional challenge (optional)
A palindrome can also be a sentence, such as "A man, a plan, a canal: Panama".
Adapt your algorithm to ignore spaces, punctuation marks and to convert to lowercase before checking if the string is a palindrome.
Python provides built-in methods for such operations : isalnum(), lower().
Correction
# The string to manipulates ="A man, a plan, a canal: Panama"print(s)
# Remove spaces, punctuation marks and convert to lowercasesimplified_s =""for character in s:
if character.isalnum():
simplified_s += character.lower()
# Iterate over the stringi =0while i < len(simplified_s) /2and simplified_s[i] == simplified_s[len(simplified_s) -1- i]:
i +=1# Show resultif i >= len(simplified_s) /2:
print(s, "is a palindrome")
else:
print(s, "is not a palindrome")
# The string to manipulates ="A man, a plan, a canal: Panama"print(s)
# Remove spaces, punctuation marks and convert to lowercasesimplified_s =''.join(character for character in s if character.isalnum()).lower()
# Check if s is a palindromeif simplified_s == simplified_s[::-1]:
print(s, "is a palindrome")
else:
print(s, "is not a palindrome")
/**
* To run this code, you need to have Java installed on your computer, then:
* - Create a file named `Main.java` in a directory of your choice.
* - Copy this code in the file.
* - Open a terminal in the directory where the file is located.
* - Run the command `javac Main.java` to compile the code.
* - Run the command `java -ea Main` to execute the compiled code.
* Note: '-ea' is an option to enable assertions in Java.
*/publicclassMain {
/**
* This is the entry point of your program.
* It contains the first codes that are going to be executed.
*
* @param args Command line arguments received.
*/publicstaticvoidmain(String[] args) {
// The string to manipulate String s ="A man, a plan, a canal: Panama";
System.out.println(s);
// Remove spaces, punctuation marks and convert to lowercase StringBuilder builder =new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char character = s.charAt(i);
if (Character.isLetterOrDigit(character)) {
builder.append(Character.toLowerCase(character));
}
}
String simplifiedS = builder.toString();
// Iterate over the stringint i = 0;
while (i < simplifiedS.length() / 2 && simplifiedS.charAt(i) == simplifiedS.charAt(simplifiedS.length() - 1 - i)) {
i++;
}
// Show resultif (i >= simplifiedS.length() / 2) {
System.out.println(s +" is a palindrome");
} else {
System.out.println(s +" is not a palindrome");
}
}
}
/**
* To run this code, you need to have Java installed on your computer, then:
* - Create a file named `Main.java` in a directory of your choice.
* - Copy this code in the file.
* - Open a terminal in the directory where the file is located.
* - Run the command `javac Main.java` to compile the code.
* - Run the command `java -ea Main` to execute the compiled code.
* Note: '-ea' is an option to enable assertions in Java.
*/publicclassMain {
/**
* This is the entry point of your program.
* It contains the first codes that are going to be executed.
*
* @param args Command line arguments received.
*/publicstaticvoidmain(String[] args) {
// The string to manipulate String s ="A man, a plan, a canal: Panama";
System.out.println(s);
// Remove spaces, punctuation marks and convert to lowercase String simplifiedS = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
// Check if s is a palindrome using StringBuilderif (simplifiedS.contentEquals(new StringBuilder(simplifiedS).reverse())) {
System.out.println(s +" is a palindrome");
} else {
System.out.println(s +" is not a palindrome");
}
}
}
7 — Optimize your solutions
What you can do now is to use AI tools such as GitHub Copilot or ChatGPT, either to generate the solution, or to improve the first solution you came up with!
Try to do this for all exercises above, to see the differences with your solutions.
To go further
8 — Hexadecimal
Consider a string representing a number in an hexadecimal format.
Calculate its decimal value.
As a recall, an hexadecimal number may contain the following digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F.
‘A’ is equal to 10, and so on.
For instance:
A built-in solution exists in Python (and in other languages) to solve this problem but try to work with loops to familiarize with their use.
It is assumed that every symbol in the string to be converted is an acceptable digit in hexadecimal format.
Correction
# Initialize variablesto_convert ="8EA4"result =0# Iterate over the stringfor index in range(len(to_convert)):
# Convert to decimal value =Nonematch to_convert[index]:
case'A': value =10case'B': value =11case'C': value =12case'D': value =13case'E': value =14case'F': value =15case _ : value = int(to_convert[index])
# Aggregate to solution power = len(to_convert) - index -1 result +=16** power * value
# Print resultprint("Decimal version of", to_convert, "is", result)
# Value to convertto_convert ="8EA4"# Convert into decimalresult = int(to_convert, 16)
print("Decimal version of", to_convert, "is", result)
/**
* To run this code, you need to have Java installed on your computer, then:
* - Create a file named `Main.java` in a directory of your choice.
* - Copy this code in the file.
* - Open a terminal in the directory where the file is located.
* - Run the command `javac Main.java` to compile the code.
* - Run the command `java -ea Main` to execute the compiled code.
* Note: '-ea' is an option to enable assertions in Java.
*/publicclassMain {
/**
* This is the entry point of your program.
* It contains the first codes that are going to be executed.
*
* @param args Command line arguments received.
*/publicstaticvoidmain(String[] args) {
// Initialize variables String toConvert ="8EA4";
int result = 0;
// Iterate over the stringfor (int index = 0; index < toConvert.length(); index++) {
// Convert to decimalint value;
char currentChar = toConvert.charAt(index);
switch (currentChar) {
case'A': value = 10; break;
case'B': value = 11; break;
case'C': value = 12; break;
case'D': value = 13; break;
case'E': value = 14; break;
case'F': value = 15; break;
default: value = Character.getNumericValue(currentChar); break;
}
// Aggregate to solutionint power = toConvert.length() - index - 1;
result += Math.pow(16, power) * value;
}
// Print result System.out.println("Decimal version of "+ toConvert +" is "+ result);
}
}
/**
* To run this code, you need to have Java installed on your computer, then:
* - Create a file named `Main.java` in a directory of your choice.
* - Copy this code in the file.
* - Open a terminal in the directory where the file is located.
* - Run the command `javac Main.java` to compile the code.
* - Run the command `java -ea Main` to execute the compiled code.
* Note: '-ea' is an option to enable assertions in Java.
*/publicclassMain {
/**
* This is the entry point of your program.
* It contains the first codes that are going to be executed.
*
* @param args Command line arguments received.
*/publicstaticvoidmain(String[] args) {
// Value to convert String toConvert ="8EA4";
// Convert into decimal using Java's built-in methodint result = Integer.parseInt(toConvert, 16);
System.out.println("Decimal version of "+ toConvert +" is "+ result);
}
}
To go beyond
9 — Check invariants
Coding and testing loops with a few, even wisely selected, cases is not enough to ensure that your algorithm will always returns the expected result.
Using mathematical induction, check the correctness of the iterations you have conceived during this activity.