哪吒杯开源SPL答题竞赛,火爆来袭
Posted 哪 吒
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了哪吒杯开源SPL答题竞赛,火爆来袭相关的知识,希望对你有一定的参考价值。
目录
一、竞赛规则
本次竞赛共有3个题目,将提供数据文件和参考的SQL解法,参赛选手要写出返回同样结果的SPL脚本。
可以假定所有数据能在内存中放下,代码中不必考虑外存计算。
最后提交SPL脚本文件(.splx)。
二、奖励机制
1、每个题目设置三个优胜奖
第一个给出正确解答的(以邮件发送时间为准);奖励现金1000元;
在正确的基础上,代码最为简洁的(可以通俗地理解为短,当然用短变量名导致代码短并没有意义);奖励现金1500元;
在正确的基础上,代码效率最高的(可以通俗地理解为快,但这里因为数据量较小,测试运行时间会有较大随机性,只能评估代码的计算量);奖励现金1500元;
对于2、3项,如果两份答案的评分相同,则最早收到的答案获胜(以邮件发送时间为准)。
2、对于所有参赛但没得奖的选手,会抽取30名幸运奖,各奖励罗技蓝牙无线鼠标一个;
三、数据说明
1、SPL集文件格式的数据
2、在集算器(即SPL运行环境)中执行该脚本可将数据写入数据库(以mysql为例)
四、例题(仅供参考学习)
找出2021年3月18日(该日期可作为参数)上涨且已经连续上涨天数达到或超过4天(该天数可作为参数)的股票和相应的连续上涨天数。
参考SQL(使用MySQL8版本及以上,以下同)
with recursive
t1 as (select *, row_number() over(partition by code order by tdate) rn
from stock),
t2 as (select *
from t1 where tdate='2021-03-18'
union all
select t1.*
from t2 join t1 on t2.code=t1.code and t2.rn=t1.rn+1
where t2.price>t1.price)
select code,count(1)-1 cnt
from t2
group by code
having cnt>=4;
参考SPL解法
A | |
---|---|
1 | 2021-03-18 |
2 | =T(“data.btx”).select(tdate<=A1).sort(code,-tdate) |
3 | =A2.group@o(code).select(tdate==A1) |
4 | =A3.new(code,~.pselect(price<=price[1])-1:cnt) |
5 | =A4.select(cnt>=4) |
五、本次竞赛试题
1、上周四周五上涨、本周无停牌且至少4天上涨、本周一涨幅超1%且有一天涨幅超3%的股票中,接下来的2周里最大涨幅超10%的概率有多大?
参考SQL:
with
#dayofweek('1970-01-04')=1, 周日
t1 as (select *, datediff(tdate,'1970-01-04') div 7 as w,
price/lag(price) over(partition by code order by tdate) rise
from stock),
t2 as (select t1.code, t1.w, count(1) z0, sum(if(t1.rise>1,1,0)) z1,
sum(if(dayofweek(t1.tdate)=2 and t1.rise>1.01,1,0)) z2,
sum(if(dayofweek(t1.tdate)<>2 and t1.rise>1.03,1,0)) z3
from t1 join t1 e1 on t1.code=e1.code and t1.w=e1.w+1
join t1 e2 on t1.code=e2.code and t1.w=e2.w+1
where dayofweek(e1.tdate)=5 and e1.rise>1
and dayofweek(e2.tdate)=6 and e2.rise>1
group by code, w
having z0=5 and z1>=4 and z2=1 and z3>=1),
t4 as (select code, w, max(if(dayofweek(tdate)=6,price,null)) price5, max(price) h
from t1 group by code, w),
t5 as (select code, w, price5, lead(h) over(partition by code order by w) h1,
lead(h,2) over(partition by code order by w) h2
from t4),
t6 as (select count(1) n
from t5 join t2 using (code,w)
where t5.h1/t5.price5>1.1 or t5.h2/t5.price5>1.1)
select n/(select count(1) from t2) p from t6;
2、设有涨>5%、跌>5%、涨>5%、跌>5%、涨>5%五个事件,请计算在2021年1季度(时间段可作为参数)中某20天(天数可作为参数)内依次发生前1、2、3、4、5个事件的股票数分别是多少?
参考SQL:
with
q as (select *, row_number() over(partition by code order by tdate) n,
price/lag(price) over(partition by code order by tdate) rise
from stock),
q0 as (select *
from q where tdate between '2021-01-01' and '2021-03-31'),
q1 as (select code, n, 20-1 rg #range是mysql关键字
from q0 where rise>1.05),
q2 as (select q0.code, q0.n, q1.rg+q1.n-q0.n rg
from q0 join q1 on q0.code=q1.code and q0.n between q1.n+1 and q1.n+q1.rg
where q0.rise<0.95),
q3 as (select q0.code, q0.n, q2.rg+q2.n-q0.n rg
from q0 join q2 on q0.code=q2.code and q0.n between q2.n+1 and q2.n+q2.rg
where q0.rise>1.05),
q4 as (select q0.code, q0.n, q3.rg+q3.n-q0.n rg
from q0 join q3 on q0.code=q3.code and q0.n between q3.n+1 and q3.n+q3.rg
where q0.rise<0.95),
q5 as (select q0.code, q0.n, q4.rg+q4.n-q0.n rg
from q0 join q4 on q0.code=q4.code and q0.n between q4.n+1 and q4.n+q4.rg
where q0.rise>1.05)
select (select count(distinct code) from q0) n0,
(select count(distinct code) from q1) n1,
(select count(distinct code) from q2) n2,
(select count(distinct code) from q3) n3,
(select count(distinct code) from q4) n4,
(select count(distinct code) from q5) n5;
3、在2021年第一季度(时间段可作为参数)中,每天选出最低价的涨停股和最高价的跌停股各一支,且前者价格低于后者的,找不到合适的则当天不选出。为避免精度导致的歧义,这里认为涨跌幅度超过9.5%即是涨停跌停。
参考SQL:
with
q as (select *, price/lag(price) over(partition by code order by tdate) rise
from stock),
q1 as (select tdate, first_value(code) over(w1) code1, first_value(price) over(w1) price1
from q
where tdate between '2021-01-01' and '2021-03-31' and rise>1.095
window w1 as (partition by tdate order by price)),
q2 as (select * from q1 group by tdate),
q3 as (select tdate, first_value(code) over(w2) code2, first_value(price) over(w2) price2
from q
where tdate between '2021-01-01' and '2021-03-31' and rise<0.905
window w2 as (partition by tdate order by price desc)),
q4 as (select * from q3 group by tdate)
select q2.tdate, q2.code1, q2.price1, q4.code2, q4.price2
from q2 join q4 using (tdate)
where q2.price1<q4.price2
order by tdate;
六、参考资料
七、参赛时间及方式
活动时间:4月15日~5月12日
参赛方式:按照答题要求将答案发至邮箱:785713526@qq.com
榜单公布:会根据答题情况及时在官网公布得奖情况,将会在5月13日前公布完所有获奖信息。
奖励发放:5月20日前
八、报名方式
以上是关于哪吒杯开源SPL答题竞赛,火爆来袭的主要内容,如果未能解决你的问题,请参考以下文章