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

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

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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()

执行结果:

1
brown	46

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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)


执行结果:

1
2
3
4
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?

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

1
2
3
4
5
6
7
8
9
10
11
12
13
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)

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

1
2
3
4
5
6
7
8
9
10
11
12
13
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?

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

1
2
3
4
5
6
7
8
9
10
11
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?

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

1
2
3
4
5
6
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.)

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

1
2
3
4
5
6
7
8
9
10
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()


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!