start_time: 2024-04-24 09:01:30 +0800

CodeQL官方教程中几道QL练习题

96
Ovie
IP属地: 上海
2021.03.04 15:29 字数 397

跟着CodeQL官方的QL tutorials做练习,在Crown the rightful heir这一章节中有五个问题,来做一做。在看这篇文章之前需先跟着官方的QL tutorial做一遍。

下面的CodeQL代码,直接放到这里运行即可

What is the most common hair color in the village? And in each region?

第一个问题:村里发色最多的一种是哪种颜色

import tutorial

class HairColor extends string{
  HairColor(){
    exists(Person p | this = p.getHairColor())
  }
  
  int ct(){
    result = count(Person p | p.getHairColor() = this | p)
  }
  
}


from HairColor c
where not exists(HairColor c1 | c1.ct() > c.ct())
select c, c.ct()

执行结果:

brown   46

第二个问题:每个地区发色最多的一种是哪种颜色

import tutorial

class HairColor extends string{
  HairColor(){
    exists(Person p | this = p.getHairColor())
  }
  
  // int ct(){
  //   result = count(Person p | p.getHairColor() = this | p)
  // }
  
  int ctOfLocation(Location loc){ 
    result = count(Person p | p.getLocation() = loc and p.getHairColor() = this | p)
  }
}

class Location extends string{
  Location() {
    this in ["east", "west", "south", "north"]
  }
}

from Location location, HairColor c
where c.ctOfLocation(location) = max(HairColor c1 | | c1.ctOfLocation(location) )
select location, c, c.ctOfLocation(location)


执行结果:

west    brown   7
east    brown   11
north   black   8
south   brown   10

说明下:各地区拥有brown发色的人的个数分别是:

  • 西部:7
  • 东部:11
  • 北部:3
  • 南部:10
  • 无地区:15 (也就是Person.getLocation()为空的人)

总数就是46,符合第一个问题的查询结果。

Which villager has the most children? Who has the most descendants?

第一个问题:哪位村民拥有最多小孩

import tutorial

Person childOf(Person p) {
  p = parentOf(result)
}

int childrenNumber(Person p) {
  result = count(Person c | c = childOf(p) | c)
}

from Person p
where childrenNumber(p) = max(Person q | | childrenNumber(q))
select p, childrenNumber(p)

第二个问题:哪位村民拥有最多后代

import tutorial

Person descendantOf(Person p) {
  p = parentOf+(result)
}

int descendantNumber(Person p) {
  result = count(Person c | c = descendantOf(p) | c)
}

from Person p
where descendantNumber(p) = max(Person q | | descendantNumber(q))
select p, descendantNumber(p)

How many people live in each region of the village?

各地区分别有多少人居住:

import tutorial


class Location extends string{
  Location() {
    this in ["east", "west", "south", "north"]
  }
}

from Location location
select location, count(Person p |  p.getLocation() = location | p)

(查询结果不包含20位无地区的人员)

Do all villagers live in the same region of the village as their parents?

找出跟他家长不住在同一地区的人:

import tutorial


from Person p, Person c
where p = parentOf(c) and p.getLocation() != c.getLocation()
select c

Find out whether there are any time travelers in the village! (Hint: Look for “impossible” family relations.)

这个问题不太具体,我理解为:找出比他后代的年纪还小的人。

import tutorial

Person descendantOf(Person p) {
  p = parentOf+(result)
}

from Person p
where exists(Person c | c = descendantOf(p) and p.getAge() < c.getAge())
select p, p.getAge()

最后编辑于 :2021-03-04 15:32:21
©著作权归作者所有,转载或内容合作请联系作者
序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
783c64db 45e5 48d7 82e4 95736f50533e 沈念sama 阅读 158233 评论 4 赞 360
序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
783c64db 45e5 48d7 82e4 95736f50533e 沈念sama 阅读 67013 评论 1 赞 291
文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
8 开封第一讲书人 阅读 108030 评论 0 赞 241
文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
8 开封第一讲书人 阅读 43827 评论 0 赞 204
正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
388e473c fe2f 40e0 9301 e357ae8f1b41 茶点故事 阅读 52221 评论 3 赞 286
文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
8 开封第一讲书人 阅读 40542 评论 1 赞 216
那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
783c64db 45e5 48d7 82e4 95736f50533e 沈念sama 阅读 31814 评论 2 赞 312
文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
8 开封第一讲书人 阅读 30513 评论 0 赞 198
序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
783c64db 45e5 48d7 82e4 95736f50533e 沈念sama 阅读 34225 评论 1 赞 241
正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
388e473c fe2f 40e0 9301 e357ae8f1b41 茶点故事 阅读 30497 评论 2 赞 244
正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
388e473c fe2f 40e0 9301 e357ae8f1b41 茶点故事 阅读 31998 评论 1 赞 258
序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
783c64db 45e5 48d7 82e4 95736f50533e 沈念sama 阅读 28342 评论 2 赞 253
正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
388e473c fe2f 40e0 9301 e357ae8f1b41 茶点故事 阅读 32986 评论 3 赞 235
文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
8 开封第一讲书人 阅读 26055 评论 0 赞 8
文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
8 开封第一讲书人 阅读 26812 评论 0 赞 194
我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
783c64db 45e5 48d7 82e4 95736f50533e 沈念sama 阅读 35560 评论 2 赞 271
正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
388e473c fe2f 40e0 9301 e357ae8f1b41 茶点故事 阅读 35461 评论 2 赞 266
end_time: 2024-04-24 09:01:30 +0800 Completed in 166.18007ms