我的老师总是给我一套最复杂的家庭作业数学题。比如:pg. 546: 17-19, 22, 26, pg. 548: 35-67 odd, 79, 80-86 even
。我想事先知道我有多少时间用来做家庭作业,但我不想把这些都弄清楚。这就是为什么你的任务是为我编写程序。
comma-space
分隔的)79
)。17-18
形式的范围(同样,您必须处理可选的空格)odd
或even
后缀,您必须考虑到这一点。pg. 545:
中的页码加在前面,同样必须处理可选的空格。您可以安全地忽略这些问题,因为您需要处理所有页面上的问题。pg. 546: 17-19, 22, 26, pg. 548: 35-67 odd, 79, 80-86 even -> 27
pg. 34: 1 -> 1
PG. 565: 2-5,PG.345:7 -> 5
pg. 343: 5,8,13 - 56 even,pg. 345: 34 - 78,80 -> 70
pg.492: 2-4 odd,7-9 even -> 2
发布于 2015-06-03 04:22:29
USING: arrays ascii kernel math math.parser math.ranges pcre sequences ;
IN: examples.golf.homework
: c ( a -- b )
>lower "(?:[,:]|^) *(\\d+) *(?:- *(\\d+) *(e|o)?)?" findall [
rest [ second dup string>number swap or ] map
dup length 1 = [ drop 1 ] [
dup length 2 = [ first2 swap - 1 + ] [
first3 "o" = [ [a,b] [ odd? ] count ] [
[a,b] [ even? ] count
] if
] if
] if
] map sum ;
发布于 2015-06-03 12:13:04
在命令行上测试就更容易了。
for(r=/[:,] *(\d+)[- ]*(\d+)? *(o|e)?/gi,m=readline(z=0);f=r.exec(m);z+=!b||((p=b-a)%2||!c|a%2^/e/i.test(c))+p/(2-!c)|0)[,a,b,c]=f
print(z)
未高尔夫球:
// any number set after "pg:" or a comma
// \1 is FROM, \2 is TO, \3 is odd/even
r=/[:,] *(\d+)[- ]*(\d+)? *(o|e)?/gi;
m=readline();
z=0; // this is the sum.
while(f=r.exec(m)){
[,from,to,oddEven]=f;
if(!to) {
z++;
} else {
if((to-from)%2) {
// if to and from are not the same parity, add 1
z++;
} else {
// if to and from are not the same parity...
if(!oddEven) {
// and we don't have a parity marker, add one
z++;
} else if(a%2 != /e/i.test(c)) {
// if we do have a parity marker,
// AND the parity of from and to matches the
// parity of the oddEven sign, then add 1
z++;
}
}
// then add the difference between to-from and
// if oddEven exists, divide by two and round down
z+=(to-from)/(oddEven?2:1)|0;
}
}
print(z);
发布于 2015-06-03 10:39:00
在Firefox中运行代码段来测试
F=s=>s.replace(/([-,.:]) *(\d+) *(o?)(e?)/ig,(_,c,v,o,e)=>
c=='-'?(t+=1+(o?(v-(r|1))>>1:e?(v-(-~r&~1))>>1:v-r),r=0)
:c!='.'&&(t+=!!r,r=v)
,r=t=0)&&t+!!r
// Less golfed
U=s=>{
var r = 0, // value, maybe range start
t = 0; // total
s.replace(/([-,.:]) *(\d+) *(o?)(e?)/ig, // execute function for each match
(_ // full match, not used
, c // separator char, match -:,.
, v // numeric value
, o // match first letter of ODD if present
, e // match first letter of EVEN if present
)=>
{
if (c == '-') // range end
{
t++; // in general, count range values as end - start + 1
if (o) // found 'odd'
{
r = r | 1; // if range start is even, increment to next odd
t += (v-r)>>1; // end - start / 2
}
else if (e) // found 'even'
{
r = (r+1) & ~1; // if range start is odd, increment to next even
t += (v-r)>>1; // end - start / 2
}
else
{
t += v-r; // end - start
}
r = 0; // range start value was used
}
else if (c != '.') // ignore page numbers starting with '.'
{
// if a range start was already saved, then it was a single value, count it
if (r != 0) ++t;
r = v; // save value as it counld be a range start
}
}
)
if (r != 0) ++t; // unused, pending range start, was a single value
return t
}
// TEST
out=x=>O.innerHTML+=x+'\n';
test=["pg. 546: 17-19, 22, 26, pg. 548: 35-67 odd, 79, 80-86 even",
"pg. 34: 1", "PG. 565: 2-5,PG.345:7",
"pg. 343: 5,8,13 - 56 even,pg. 345: 34 - 78,80"];
test.forEach(t=>out(t + ' --> ' + F(t)))
<pre id=O></pre>
https://codegolf.stackexchange.com/questions/51189
复制相似问题