Summing A String
June 19, 2020
In a string consisting of digits and other non-digit characters, the digits form an embedded series of positive integers. For instance, the string “123abc45def” contains the embedded integers 123 and 45, which sum to 168.
Your task is to write a program that takes a string and writes the sum of the integers embedded in the 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.
In Python.
Here are two solutions in standard Scheme (R7RS). The first is a “one
liner” that uses string-tokenize from SRFI 13. The second uses only
simple Scheme. It also reliably (based on my reading of R7RS) handles
numbers in character-sets other than ASCII. (See sample inputs.)
Output:
Here’s some Haskell. Scan the string accumulating digits, when a complete number is found, use base-10 arithmetic to add number into running total:
Here’s another Haskell version, using splitWhen from the “split” library.
Here’s a shell script solution.
sed
is combined withtr
to replace repeated non-digit characters with a+
. This could alternatively be achieved by passingsed
‘s flag for extended regex, and checking for one or more non-digit characters.Example:
Here’s a solution in C.
Example:
@Daniel – nice solutions, scanning the string in reverse is a good idea, here’s a simplified version of my “elementary” Haskell solution:
I left an extra “0” in the test data there, so the result is actually “[5,7,3]”.
Here’s a simple solution: get a list of numbers in the string, then add them up. A Commonlisp implementation:
A different approach in Python:
Actually, that’s rather like Daniel’s shell solution.
Here is a different solution:
#123abc45def
st = input(“Enter string”)
sum_val = 0
msg = ”
for i in st:
if ord(i)>47 and ord(i)< 58:
msg+=i
else:
if msg!=”:
sum_val+=int(msg)
msg=”
else:
continue
print(sum_val)
public class Exercises_2 {
public static void main(String[] args) {
class Input{
public String inputString(){
Scanner scanner = new Scanner(System.in);
return scanner.nextLine();
}
public int string_in_int_out(String s){
int sum = 0;
char [] inputValue = s.toCharArray();
char [] numbers = new char [inputValue.length + 1];
for (int i = 0; i < inputValue.length; i++) {
if (Character.isDigit(inputValue[i])){
numbers[i] = inputValue[i];
}else {
continue;
}
}
String buffer = “”;
for (int i = 0; i < numbers.length; i++) {
if (!Character.isDigit(numbers[i])){
continue;
}else if(Character.isDigit(numbers[i])){
buffer += numbers[i];
if (!Character.isDigit(numbers[i + 1])){
sum += Integer.parseInt(buffer);
buffer = “”;
}
}
}
return sum;
}
}
Input input = new Input();
System.out.println(input.string_in_int_out(input.inputString()));
}
}
Hi guys how I can post my solutions like a code?
‘code’ blocks are best: https://wordpress.com/support/wordpress-editor/blocks/syntax-highlighter-code-block/2/
Here’s another solution in C.
Example:
A bit late to the party…here is a one liner in Ruby:
Example:
solution in Clojure with transducer:
Although the regex should better be #”[^\d+]”
A naive Haskell solution
Here’s mine in Perl, using regexes