Sum Embedded Numbers
May 15, 2018
We break the problem into two parts: function next-number
gets the next number from the input string while resetting the string for the next call, and function sum-embedded-numbers
iterates through the numbers returned by next-number
, returning the sum when the input string is exhausted:
(define (next-number xs) (let loop ((xs xs)) (if (null? xs) (values #f #f) (if (not (char-numeric? (car xs))) (loop (cdr xs)) (let loop ((xs xs) (n 0)) (if (null? xs) (values n xs) (if (char-numeric? (car xs)) (loop (cdr xs) (+ (* n 10) (- (char->integer (car xs)) (char->integer #\0)))) (values n xs))))))))
(define (sum-embedded-numbers str) (let loop ((xs (string->list str)) (sum 0)) (call-with-values (lambda () (next-number xs)) (lambda (n xs) (if n (loop xs (+ sum n)) sum)))))
The outer loop
in next-number
advances past any initial non-digits, and the inner loop
collects the values of the number n. Here’s the example:
> (sum-embedded-numbers "11aa22bb33cc44") 110
You can run the program at https://ideone.com/GZ5Ltn.
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 :/ )