Prolog,检查特定列表参数

发布于 2025-01-21 03:18:30 字数 1951 浏览 0 评论 0原文

% adds two lists together
add_pairs(X, [], X).
add_pairs([I1, I2, I3, I4, I5], [I7, I8, I9, I10, I11], [O1, O2, O3, O4, O5]) :-
  O1 is I1 + I7,
  O2 is I2 + I8,
  O3 is I3 + I9,
  O4 is I4 + I10,
  O5 is I5 + I11.

% Checks to see if the meal is satisfactory
check_sat([_, E2, E3, E4, E5], Len) :-
  E2 >= 3,
  E3 =< Len - 3,
  E4 =< Len - 3,
  E5 >= 3.

% Adds all lists in L together.
add_lists([L|[]], L).
add_lists([X, Y|T], Out) :-
  add_pairs(X, Y, Sum),
  append([Sum], T, TOut),
  add_lists(TOut, Out).

% Calls to the functions, determining if a meal is satisfactory
satisfactory_meal(L) :-
  add_lists(L, Output),
  length(L, Len),
  check_sat(Output, Len).

因此,我目前正在研究一个程序,其中我将一些数据作为输入。如果满足某些条件,则返回true,否则为错误。 基本上,列表代表一餐的属性。

[dish_number,有机,has_dairy,has_meat,locally_sourced]

盘数是任何数字,如果发现1,则表示以下属性如下​​,如果0在其位置,则不存在。

例如:

satisfactory_meal([
     [8,  0, 0, 0, 1],
     [9,  1, 1, 0, 1],
     [23, 1, 0, 0, 1],
     [2,  1, 0, 1, 0],
     [6,  0, 0, 1, 1]
]).

返回true。因为我需要3个或更多的有机和局部! 而且列表的长度小于或等于肉或乳制品的3个。

satisfactory_meal([
     [4,  1, 0, 0, 0],
     [7,  0, 1, 0, 1],
     [90, 0, 0, 0, 0],
     [3,  0, 0, 1, 1]
]).

返回false,因为有机具有一个实例,并且局部_sourced有两个。少于需要的。

我当前的程序设置都很好,但是,有一个测试案例我无法正确返回。如果所有人都不喜欢一顿饭,该程序应返回假。

看起来这样:

% No one can eat dish 3 
satisfactory_meal([
     [1, 1, 0, 0, 1],
     [3, 0, 1, 1, 1],
     [5, 0, 0, 0, 1],
     [7, 1, 0, 1, 0],
     [9, 1, 0, 0, 1]
]).
% No one can eat dish 9
satisfactory_meal([
     [1, 1, 0, 0, 0],
     [3, 1, 0, 0, 1],
     [5, 0, 0, 0, 1],
     [7, 1, 0, 0, 1],
     [9, 0, 1, 1, 1]
]).

如果列表看起来像[#,0、1、1、0]或[#,0、1、1、1、1],所有人都不喜欢一顿饭。

我的问题是我目前如何处理列表。我将它们添加在一起,非常容易寻找少于,大于和等于值。 我无法算术找出如何找到所有人不喜欢菜肴的实例。 aka [#,0,1,1,0]或[#,0,1,1,1]。将列表汇总在一起后,有没有办法弄清楚这一点?还是我必须创建一个新功能并在将列表添加在一起之前查找这些实例?随着我接近使该程序起作用,任何帮助将不胜感激!谢谢。

% adds two lists together
add_pairs(X, [], X).
add_pairs([I1, I2, I3, I4, I5], [I7, I8, I9, I10, I11], [O1, O2, O3, O4, O5]) :-
  O1 is I1 + I7,
  O2 is I2 + I8,
  O3 is I3 + I9,
  O4 is I4 + I10,
  O5 is I5 + I11.

% Checks to see if the meal is satisfactory
check_sat([_, E2, E3, E4, E5], Len) :-
  E2 >= 3,
  E3 =< Len - 3,
  E4 =< Len - 3,
  E5 >= 3.

% Adds all lists in L together.
add_lists([L|[]], L).
add_lists([X, Y|T], Out) :-
  add_pairs(X, Y, Sum),
  append([Sum], T, TOut),
  add_lists(TOut, Out).

% Calls to the functions, determining if a meal is satisfactory
satisfactory_meal(L) :-
  add_lists(L, Output),
  length(L, Len),
  check_sat(Output, Len).

So, I'm currently working on a program where I'm given some data as input. It returns true if certain conditions are met and false otherwise.
Basically, the list represents the attributes of a meal.

[Dish_number, Organic, Has_dairy, Has_meat, Locally_sourced]

The dish number is any number and the following attributes are represented as present if 1 is found and absent if 0 is in its place.

For example:

satisfactory_meal([
     [8,  0, 0, 0, 1],
     [9,  1, 1, 0, 1],
     [23, 1, 0, 0, 1],
     [2,  1, 0, 1, 0],
     [6,  0, 0, 1, 1]
]).

Returns true. Because I need 3 or more instances of Organic and Locally_sourced.
And there are less than or equal to the length of the List - 3 of Meat or Dairy.

satisfactory_meal([
     [4,  1, 0, 0, 0],
     [7,  0, 1, 0, 1],
     [90, 0, 0, 0, 0],
     [3,  0, 0, 1, 1]
]).

returns false because organic has a single instance and locally_sourced has two. Less than what's needed.

All's good and well with my current program setup... However, there's a test case I cannot return properly. The program should return false if a single meal is disliked by all.

Which looks like this:

% No one can eat dish 3 
satisfactory_meal([
     [1, 1, 0, 0, 1],
     [3, 0, 1, 1, 1],
     [5, 0, 0, 0, 1],
     [7, 1, 0, 1, 0],
     [9, 1, 0, 0, 1]
]).
% No one can eat dish 9
satisfactory_meal([
     [1, 1, 0, 0, 0],
     [3, 1, 0, 0, 1],
     [5, 0, 0, 0, 1],
     [7, 1, 0, 0, 1],
     [9, 0, 1, 1, 1]
]).

A meal is disliked by all if the list looks like [#, 0, 1, 1, 0] or [#, 0, 1, 1, 1].

My issue is with how I'm currently handling my lists. I'm adding them all together, making it very easy to look for less than, greater than, and equal to values.
I can't arithmetically figure out how to find the instance where a dish is disliked by all. AKA [#, 0, 1, 1, 0] or [#, 0, 1, 1, 1]. Is there a way to figure it out after summing the lists together? Or do I have to create a new function and look for those instances before adding the lists together? Any help would be most appreciated as I'm close to making this program work! Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

厌倦 2025-01-28 03:18:30

我发现了。在将数据添加在一起之前,我查看了数据,创建了一个函数,该函数查看缺乏[#,0、1、1、0]或[#,0、1、1、1、1]的实例。

nonmember(Arg,[Arg|_]) :-
    !,
        fail.
nonmember(Arg,[_|Tail]) :-
        !,
        nonmember(Arg,Tail).
nonmember(_,[]).

  nonmember([_,0,1,1,0], L),
  nonmember([_,0,1,1,1], L),

I figured it out. I looked through the data before adding it together, creating a function that looks for the lack of instances of [#, 0, 1, 1, 0] or [#, 0, 1, 1, 1].

nonmember(Arg,[Arg|_]) :-
    !,
        fail.
nonmember(Arg,[_|Tail]) :-
        !,
        nonmember(Arg,Tail).
nonmember(_,[]).

  nonmember([_,0,1,1,0], L),
  nonmember([_,0,1,1,1], L),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文