3
\$\begingroup\$

The Challenge

I always write my math assignments down in the same way. For example:

pg. 420: #25, 50-56, 90
pg. 26: #50-60 evens, 70-80 odds, 90-99 3s
pg. 69: #12, 16, 18, 37

But I need you to tell me the individual problem numbers. For example:

input: 
pg. 420: #25, 50-56, 90

output:
25, 50, 51, 52, 53, 54, 55, 56, 90

Here are the rules for telling me my problem numbers:

The assignment always starts with pg. (number): #, feel free to discard it.

Each group of problems is separated by a comma, and different patterns mean different things.

A single number (4, 6, 7438, 698236) just means that one problem is in the assignment.

Two numbers with a dash between them means those two numbers and all integers between. For example, 1-6 would become 1, 2, 3, 4, 5, 6

If two numbers have a dash between them, then there's a word, there is a special rule for the numbers.

If the word is evens, then all even numbers between the two are included (1-6 evens -> 2, 4, 6).

If the word is odds, then all odd numbers between the two are included (1-6 odds -> 1, 3, 5).

If the word is 3s, then all numbers where n mod 3 = 0 between the two are included (1-6 3s -> 3, 6);

Test Examples: (inputs then outputs)

pg. 1: #1, 2, 3
1, 2, 3

pg. 82: #76, 84-87, 91, 94-100 evens, 103-112 3s
76, 84, 85, 86, 87, 91, 94, 96, 98, 100, 103, 106, 109, 112

pg. 9001: #9000-9002 odds
9001

This is code-golf, so your score is the number of bytes.

\$\endgroup\$
6
  • \$\begingroup\$ Would stuff like "every other odd/even" be a valid input? Or "every other problem"? \$\endgroup\$ Oct 19 '15 at 22:15
  • \$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ Those wouldn't be valid input currently; do you think they should be? \$\endgroup\$
    – DanTheMan
    Oct 19 '15 at 22:18
  • \$\begingroup\$ No, just wondering. \$\endgroup\$ Oct 19 '15 at 23:18
  • 3
    \$\begingroup\$ I feel like we've had this challenge before. \$\endgroup\$
    – xnor
    Oct 19 '15 at 23:26
  • 2
    \$\begingroup\$ Closely related: Tell me how many math problems I have to do! \$\endgroup\$
    – Dennis
    Oct 19 '15 at 23:49
1
\$\begingroup\$

JavaScript (ES7), 138 bytes

F=s=>s.replace(/.*#|(\d+)-?(\d+)? ?(\d|\w*)s?/g,(_,a,b,c)=>b?[for(i of Array(b-a--+1))if(++a*c?(b-a)%c<1:!c||a%2==c>'f')a].join`, `:a||'')

F=s=>s.replace(/.*#|(\d+)-?(\d+)? ?(\d|\w*)s?/g,(_,a,b,c)=>b?[for(i of Array(b-a--+1))if(++a*c?(b-a)%c<1:!c||a%2==c>'f')a].join`, `:a||'')
button.onclick=()=>output.innerHTML=F(range.value)
button, textarea {
  display: block;
  width: 100%;
  margin-bottom: 0.5em;
  box-sizing: border-box;
}
<textarea id="range"></textarea>
<button id="button">submit</button>
<div id="output"></div>

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.