Sum Embedded Numbers
May 15, 2018
It’s mid-May, so at most universities the semester has ended and it is safe for me to solve some of the exercises that students sent to me over the last few months. Here’s a fun little one:
Given a string containing embedded numbers, find the sum of the embedded numbers. For instance, given the string “11aa22bb33cc44”, the desired sum is 11 + 22 + 33 + 44 = 110. You may not use regular expressions.
Although the problem statement doesn’t say so, you may assume that the numbers of interest are non-negative integers. Thus, the purpose of the exercise is for students to iterate through a string, identify the digits in the string, and manipulate them numerically.
Your task is to write a program that sums the numbers embedded in a string. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
Python
Sample output:
11aa22bb33cc44 = (20, 110)
1k23jk34jk56jk3454 = (40, 3568)
Tested the samples of Pascal and found a misplacement of a variable. Should be fixed now.
Output is:
(0, 0) =
(6, 42) = 42
(6, 42) = -42
(6, 42) = -42-
(10, 10) = 1-2-3-4
(10, 10) = 1 2, 3; 4.
(20, 110) = 11aa22bb33cc44
Here’s a one-line Erlang solution
and an attempt to explain it:
Start with a list containing the value 0 (line 6). For each character X (lines 3,4) in the string (line 7), if it’s between “0” and “9”, multiply the head of the list by 10, add X to it, and put that back at the head of the list (line 3); and if X is not numeric, tack on another 0 to the list (line 4). When lists:foldl is done, you have the list of numbers [44,0,33,0,22,0,11], which lists:sums to 110 (line 1).
Cache/Mumps version
f i=””,”foo”,”42″,”-42″,”-42-“,”1-2-3-4″,” 1 2, 3; 4. “,”11aa22bb33cc44″ w !,””””,i,””” –>”,?40,””””,$$SUMEMBNUM(i),””””
“” –> “0”
“foo” –> “0”
“42” –> “42”
“-42” –> “42”
“-42-” –> “42”
“1-2-3-4” –> “10”
” 1 2, 3; 4. ” –> “10”
“11aa22bb33cc44” –> “110”
Bash.
Test:
ClojureScript.
Test:
Re-implementation of my Python solution in D
Here’s a solution in C.
Example:
I really like the Haskell solution of Globules. The closest I could come with in ClojureScript, without using regex, is with partition-by:
Test:
= 0 OK
foo = 0 OK
42 = 42 OK
-42 = 42 OK
-42- = 42 OK
1-2-3-4 = 10 OK
1 2, 3; 4. = 10 OK
11aa22bb33cc44 = 110 OK
Here’s a solution in Python.
Output:
Here’s the same solution using double quotes, to attempt to improve the formatting.
Here’s another solution in C.
Example:
@sbocq: It’s not hard to adapt Globules’ Haskell solution to Scheme. Start with a variant of the Standard Prelude’s
string-split
function that takes a predicate instead of just comparing to a character:Then just build a pipeline of Scheme functions:
And here’s the result:
Be sure you understand the output of
string-split-by
, which is slightly different than the HaskellwordsBy
function.Oh sure, I could have reimplemented split-by myself. But for these exercises, I’m interested in how much bang you get for the bucks sticking to the language and its standard library. In my eyes, you give up a bit of that if you have to resort to custom loops or recursion to implement split-by. This is why I like the Haskell solution.
In Ruby. An imperative and a functional version.
Output:
Another Bash solution inspired from Daniel’s nice use of strtol in his C version:
Example:
Python
Uses ‘groupby’ to break the input string into groups of characters according to the function provided as the ‘key’ parameter, e.g., ‘isdigit’. If the key, k, is True, the characters in the group are concatenated and converted to an integer. The sum of the integers is returned.
Here is a link to a pastebin that lets you execute the code.
Try it online!
What ?
What ?
What ?
What ?
What ?
What ?
What ?
What ?
What ?
What ?
What ?
What ?
What ?
What ?
What ?
May 15th, 2018.c:
The solution is known to run on an Apple Power Mac G4 (AGP Graphics) (450MHz processor, 1GB memory) on both Mac OS 9.2.2 (International English) (the solution interpreted using Leonardo IDE 3.4.1) and Mac OS X 10.4.11 (the solution compiled using Xcode 2.2.1).
(I’m just trying to solve the problems posed by this ‘site whilst I try to get a job; I’m well-aware that my solutions are far from the best – but, in my defence, I don’t have any traditional qualifications in computer science :/ )