Just One More
This CodeNewbie Challenge is about incrementing numbers. Incrementing is the process of adding one to a number, making the number one larger. Consider the following example:
x = 0 => 0
x = x + 1 => 1
The value of x starts at 0 and is incremented, making it one larger.
This week’s challenge is about incrementing numbers.
The challenge starts with a list, your job is to take the values in the list and increment them. Like past challenges, this challenge is split into three levels. Please choose your level based on your available time and comfort level.
In the first level you start with the following list of numbers:
[ 1, 0, -1, 5, 100, 37, 20, 18, 12, 0 ]
Your challenge is to write code to increment each number in the list so that your final output will be a list of numbers that has values one larger than the input list.
Given the list [ 1, 2, 3 ] When you run your code Then it would output [ 2, 3, 4 ]
The second level starts with a list as well, but this list will be a list of strings. As you can see some of the strings represent numbers and others do not.
[ ‘1’, ‘c’, ‘0’, ‘-1’, ‘5’, ‘b’, ‘100’, ‘37’, ‘a’, ‘20’, ‘18’, ‘12’, ‘0’ ]
Your challenge is to write code that increments the numbers and discards items that do not represent numbers.
Given the list [ ‘1’, ‘b’, ‘3’] When you run your code Then it would produce [ 2, 4 ]
The third and final level starts with a list that contains values that have numeric and non-numeric parts. Here is is:
[ ‘ab123’, ‘gh00’, ‘ijk8’, ‘lmn12’, ‘cd99ef11’ ]
Your challenge is to take the items from this list and increment the numeric parts and then put them back into the string.
Given the list [ ‘ab12’, ‘a5’, ‘b23a51’ ] When you run your code Then it would output [ ‘ab13’, ‘a6’, ‘b24a52’ ]
Thank You For Your Participation
The idea for this week’s challenge was sent in by Phillip Gray
If you have an idea for a challenge, we would like to know about it. Please share your idea by posting it to the discourse thread.
Sharing your Solution
If you have created a solution to a challenge and have not yet shared it to discourse I encourage you to do so. We have seen solutions in a variety of languages and styles and posted there and would love to see yours too.