dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
98 lines
2.3 KiB
Markdown
98 lines
2.3 KiB
Markdown
---
|
|
id: 69bc6cb30c1d112a2e110a06
|
|
title: "Challenge 249: String Math"
|
|
challengeType: 29
|
|
dashedName: challenge-249
|
|
---
|
|
|
|
# --description--
|
|
|
|
Given a string with numbers and other characters, perform math on the numbers based on the count of non-digit characters between the numbers.
|
|
|
|
- If the count of characters separating two numbers is even, use addition.
|
|
- If it's odd, use subtraction.
|
|
- Consecutive digits form a single number.
|
|
- Operations are applied left to right.
|
|
- Ignore leading and trailing characters that aren't digits.
|
|
|
|
For example, given `"3ab10c8"`, return `5`. Add 3 and 10 to get 13 because there's an even number of characters between them. Then subtract 8 from 13 because there's an odd number of characters between the result and 8.
|
|
|
|
# --hints--
|
|
|
|
`do_math("3ab10c8")` should return `5`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(do_math("3ab10c8"), 5)`)
|
|
}})
|
|
```
|
|
|
|
`do_math("6MINUS4")` should return `2`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(do_math("6MINUS4"), 2)`)
|
|
}})
|
|
```
|
|
|
|
`do_math("9plus3")` should return `12`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(do_math("9plus3"), 12)`)
|
|
}})
|
|
```
|
|
|
|
`do_math("5fkwo#10i#%.<>15P=@20!#B/25")` should return `15`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(do_math("5fkwo#10i#%.<>15P=@20!#B/25"), 15)`)
|
|
}})
|
|
```
|
|
|
|
`do_math("a.67,1$lk6ldf34@#LD@]2d32d2'2l3,@l3L#@2gh35s09if=df#$t9sm49t0df3$^%[vc;:0:4mt")` should return `67`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(do_math("a.67,1$lk6ldf34@#LD@]2d32d2'2l3,@l3L#@2gh35s09if=df#$t9sm49t0df3$^%[vc;:0:4mt"), 67)`)
|
|
}})
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```py
|
|
def do_math(s):
|
|
|
|
return s
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```py
|
|
import re
|
|
|
|
def do_math(s):
|
|
tokens = re.findall(r'\d+|\D+', s)
|
|
result = None
|
|
pending_op = None
|
|
for token in tokens:
|
|
if re.match(r'^\d+$', token):
|
|
if result is None:
|
|
result = int(token)
|
|
elif pending_op == 'add':
|
|
result += int(token)
|
|
else:
|
|
result -= int(token)
|
|
else:
|
|
pending_op = 'add' if len(token) % 2 == 0 else 'subtract'
|
|
return result
|
|
```
|