CodeQL官方教程中幾道QL練習(xí)題

跟著CodeQL官方的QL tutorials做練習(xí)鸥昏,在Crown the rightful heir這一章節(jié)中有五個(gè)問(wèn)題,來(lái)做一做。在看這篇文章之前需先跟著官方的QL tutorial做一遍蜂大。

下面的CodeQL代碼踩蔚,直接放到這里運(yùn)行即可

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

第一個(gè)問(wèn)題:村里發(fā)色最多的一種是哪種顏色

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()

執(zhí)行結(jié)果:

brown   46

第二個(gè)問(wèn)題:每個(gè)地區(qū)發(fā)色最多的一種是哪種顏色

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)


執(zhí)行結(jié)果:

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

說(shuō)明下:各地區(qū)擁有brown發(fā)色的人的個(gè)數(shù)分別是:

  • 西部:7
  • 東部:11
  • 北部:3
  • 南部:10
  • 無(wú)地區(qū):15 (也就是Person.getLocation()為空的人)

總數(shù)就是46聂薪,符合第一個(gè)問(wèn)題的查詢結(jié)果部逮。

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

第一個(gè)問(wèn)題:哪位村民擁有最多小孩

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)

第二個(gè)問(wèn)題:哪位村民擁有最多后代

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?

各地區(qū)分別有多少人居准纤 :

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)

(查詢結(jié)果不包含20位無(wú)地區(qū)的人員)

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

找出跟他家長(zhǎng)不住在同一地區(qū)的人:

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.)

這個(gè)問(wèn)題不太具體醋虏,我理解為:找出比他后代的年紀(jì)還小的人寻咒。

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()

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市颈嚼,隨后出現(xiàn)的幾起案子毛秘,更是在濱河造成了極大的恐慌,老刑警劉巖阻课,帶你破解...
    沈念sama閱讀 221,198評(píng)論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件叫挟,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡限煞,警方通過(guò)查閱死者的電腦和手機(jī)抹恳,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)署驻,“玉大人奋献,你說(shuō)我怎么就攤上這事⊥希” “怎么了瓶蚂?”我有些...
    開封第一講書人閱讀 167,643評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)宣吱。 經(jīng)常有香客問(wèn)我窃这,道長(zhǎng),這世上最難降的妖魔是什么征候? 我笑而不...
    開封第一講書人閱讀 59,495評(píng)論 1 296
  • 正文 為了忘掉前任钦听,我火速辦了婚禮,結(jié)果婚禮上倍奢,老公的妹妹穿的比我還像新娘朴上。我一直安慰自己,他們只是感情好卒煞,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,502評(píng)論 6 397
  • 文/花漫 我一把揭開白布痪宰。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪衣撬。 梳的紋絲不亂的頭發(fā)上乖订,一...
    開封第一講書人閱讀 52,156評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音具练,去河邊找鬼乍构。 笑死,一個(gè)胖子當(dāng)著我的面吹牛扛点,可吹牛的內(nèi)容都是我干的哥遮。 我是一名探鬼主播,決...
    沈念sama閱讀 40,743評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼陵究,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼眠饮!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起铜邮,我...
    開封第一講書人閱讀 39,659評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤仪召,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后松蒜,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體扔茅,經(jīng)...
    沈念sama閱讀 46,200評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,282評(píng)論 3 340
  • 正文 我和宋清朗相戀三年秸苗,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了咖摹。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,424評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡难述,死狀恐怖萤晴,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情胁后,我是刑警寧澤店读,帶...
    沈念sama閱讀 36,107評(píng)論 5 349
  • 正文 年R本政府宣布,位于F島的核電站攀芯,受9級(jí)特大地震影響屯断,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜侣诺,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,789評(píng)論 3 333
  • 文/蒙蒙 一殖演、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧年鸳,春花似錦趴久、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)灭忠。三九已至,卻和暖如春座硕,著一層夾襖步出監(jiān)牢的瞬間弛作,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工华匾, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留映琳,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,798評(píng)論 3 376
  • 正文 我出身青樓蜘拉,卻偏偏與公主長(zhǎng)得像萨西,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子诸尽,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,435評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容