我正在编写一个规则来搜索表格中的事实数据库:
overground(Station1, Station2, DurationOfTravel).
Run Code Online (Sandbox Code Playgroud)
并允许您搜索所有旅行时间相同的旅程.
我写了这两条规则:
timesearch(Duration) :-
overground(Station1, Station2, Duration),
print([Station1, Station2]).
timesearch(Duration, [Station1,Station2]) :-
overground(Station1, Station2, Duration).
Run Code Online (Sandbox Code Playgroud)
哪个基本上是一样的.我不确定哪种是最佳做法?或者他们是两个同样好的解决方案?
我正在定义一个字典来将日期数字映射到它们各自的单词.由于某种原因,以下代码引发"SyntaxError:invalid token"并突出显示"08"
days = {01:"first", 02:"second", 03:"third", 04:"fourth", 05:"fifth", 06:"sixth", 07:"seventh", 08:"eighth", 09:"nineth", 10:"tenth",
11:"eleventh", 12:"twelvth", 13:"thirteenth", 14:"fourteenth", 15:"fifteenth", 16:"sixteenth", 17:"seventeenth", 18:"eighteenth",
19:"nineteenth", 20:"twentieth", 21:"twenty-first", 22:"twenty-second", 23:"twenty-third", 24:"twenty-fourth", 25:"twenty-fifth",
26:"twenty-sixth", 27:"twenty-seventh", 28:"twenty-eighth", 29:"twenty-nineth", 30:"thirtieth", 31:"thirty-first"}
Run Code Online (Sandbox Code Playgroud)
修改代码,使08和09成为98和99停止任何错误,如下面的代码:
days = {01:"first", 02:"second", 03:"third", 04:"fourth", 05:"fifth", 06:"sixth", 07:"seventh", 98:"eighth", 99:"nineth", 10:"tenth",
11:"eleventh", 12:"twelvth", 13:"thirteenth", 14:"fourteenth", 15:"fifteenth", 16:"sixteenth", 17:"seventeenth", 18:"eighteenth",
19:"nineteenth", 20:"twentieth", 21:"twenty-first", 22:"twenty-second", 23:"twenty-third", 24:"twenty-fourth", 25:"twenty-fifth",
26:"twenty-sixth", 27:"twenty-seventh", 28:"twenty-eighth", 29:"twenty-nineth", 30:"thirtieth", 31:"thirty-first"}
Run Code Online (Sandbox Code Playgroud)
并且输出变为:
{1: 'first', 2: 'second', 3: 'third', …
Run Code Online (Sandbox Code Playgroud)