kandi background
Explore Kits

99-problems | NinetyNine Prolog Problems written by Werner Hett | Learning library

 by   shekhargulati Java Version: Current License: MIT

 by   shekhargulati Java Version: Current License: MIT

Download this library from

kandi X-RAY | 99-problems Summary

99-problems is a Java library typically used in Tutorial, Learning, Example Codes applications. 99-problems has no bugs, it has no vulnerabilities, it has a Permissive License and it has medium support. However 99-problems build file is not available. You can download it from GitHub.
This is an adaptation of the Ninety-Nine Prolog Problems written by Werner Hett.
Support
Support
Quality
Quality
Security
Security
License
License
Reuse
Reuse

kandi-support Support

  • 99-problems has a medium active ecosystem.
  • It has 3289 star(s) with 943 fork(s). There are 283 watchers for this library.
  • It had no major release in the last 12 months.
  • There are 5 open issues and 1 have been closed. On average issues are closed in 304 days. There are 4 open pull requests and 0 closed requests.
  • It has a neutral sentiment in the developer community.
  • The latest version of 99-problems is current.
99-problems Support
Best in #Learning
Average in #Learning
99-problems Support
Best in #Learning
Average in #Learning

quality kandi Quality

  • 99-problems has 0 bugs and 0 code smells.
99-problems Quality
Best in #Learning
Average in #Learning
99-problems Quality
Best in #Learning
Average in #Learning

securitySecurity

  • 99-problems has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
  • 99-problems code analysis shows 0 unresolved vulnerabilities.
  • There are 0 security hotspots that need review.
99-problems Security
Best in #Learning
Average in #Learning
99-problems Security
Best in #Learning
Average in #Learning

license License

  • 99-problems is licensed under the MIT License. This license is Permissive.
  • Permissive licenses have the least restrictions, and you can use them in most projects.
99-problems License
Best in #Learning
Average in #Learning
99-problems License
Best in #Learning
Average in #Learning

buildReuse

  • 99-problems releases are not available. You will need to build from source code and install.
  • 99-problems has no build file. You will be need to create the build yourself to build the component from source.
99-problems Reuse
Best in #Learning
Average in #Learning
99-problems Reuse
Best in #Learning
Average in #Learning
Top functions reviewed by kandi - BETA

kandi has reviewed 99-problems and discovered the below as its top functions. This is intended to give you an instant insight into 99-problems implemented functionality, and help decide if they suit your requirements.

  • Returns the longest palindrome in the input string .
  • 2 - sum function of numbers
  • Remove an element from the list .
  • Finds the number of bins in a given array of weights
  • Returns a substring of the input string
  • Matrix multiplication .
  • Inserts an element into the tree .
  • Computes the mode value for the given array of numbers .
  • Returns the combinations of a list
  • Checks if a pattern exists in the text .

99-problems Key Features

This is an adaptation of the Ninety-Nine Prolog Problems written by Werner Hett.

Prolog :Make my predicate return all possible solutions

copy iconCopydownload iconDownload
choose(X, L1, L2) :-
    append(A, [X|B], L1),
    append(A, B, L2).
?- choose(X, [a,b,c], Rest).
X = a,
Rest = [b, c] ;
X = b,
Rest = [a, c] ;
X = c,
Rest = [a, b] ;
false.
group234(G, [A,B], [C,D,E], G4):-
    choose(A, G,  G0),
    choose(B, G0, G1), A @< B,
    choose(C, G1, G2), 
    choose(D, G2, G3), C @< D,
    choose(E, G3, G4), D @< E.
?- group234([a,b,c,d,e,f,g,h,i], G2, G3, G4).
G2 = [a, b],
G3 = [c, d, e],
G4 = [f, g, h, i] ;
G2 = [a, b],
G3 = [c, d, f],
G4 = [e, g, h, i] ;
G2 = [a, b],
G3 = [c, d, g],
G4 = [e, f, h, i] ;
G2 = [a, b],
G3 = [c, d, h],
G4 = [e, f, g, i] 
...
-----------------------
choose(X, L1, L2) :-
    append(A, [X|B], L1),
    append(A, B, L2).
?- choose(X, [a,b,c], Rest).
X = a,
Rest = [b, c] ;
X = b,
Rest = [a, c] ;
X = c,
Rest = [a, b] ;
false.
group234(G, [A,B], [C,D,E], G4):-
    choose(A, G,  G0),
    choose(B, G0, G1), A @< B,
    choose(C, G1, G2), 
    choose(D, G2, G3), C @< D,
    choose(E, G3, G4), D @< E.
?- group234([a,b,c,d,e,f,g,h,i], G2, G3, G4).
G2 = [a, b],
G3 = [c, d, e],
G4 = [f, g, h, i] ;
G2 = [a, b],
G3 = [c, d, f],
G4 = [e, g, h, i] ;
G2 = [a, b],
G3 = [c, d, g],
G4 = [e, f, h, i] ;
G2 = [a, b],
G3 = [c, d, h],
G4 = [e, f, g, i] 
...
-----------------------
choose(X, L1, L2) :-
    append(A, [X|B], L1),
    append(A, B, L2).
?- choose(X, [a,b,c], Rest).
X = a,
Rest = [b, c] ;
X = b,
Rest = [a, c] ;
X = c,
Rest = [a, b] ;
false.
group234(G, [A,B], [C,D,E], G4):-
    choose(A, G,  G0),
    choose(B, G0, G1), A @< B,
    choose(C, G1, G2), 
    choose(D, G2, G3), C @< D,
    choose(E, G3, G4), D @< E.
?- group234([a,b,c,d,e,f,g,h,i], G2, G3, G4).
G2 = [a, b],
G3 = [c, d, e],
G4 = [f, g, h, i] ;
G2 = [a, b],
G3 = [c, d, f],
G4 = [e, g, h, i] ;
G2 = [a, b],
G3 = [c, d, g],
G4 = [e, f, h, i] ;
G2 = [a, b],
G3 = [c, d, h],
G4 = [e, f, g, i] 
...
-----------------------
choose(X, L1, L2) :-
    append(A, [X|B], L1),
    append(A, B, L2).
?- choose(X, [a,b,c], Rest).
X = a,
Rest = [b, c] ;
X = b,
Rest = [a, c] ;
X = c,
Rest = [a, b] ;
false.
group234(G, [A,B], [C,D,E], G4):-
    choose(A, G,  G0),
    choose(B, G0, G1), A @< B,
    choose(C, G1, G2), 
    choose(D, G2, G3), C @< D,
    choose(E, G3, G4), D @< E.
?- group234([a,b,c,d,e,f,g,h,i], G2, G3, G4).
G2 = [a, b],
G3 = [c, d, e],
G4 = [f, g, h, i] ;
G2 = [a, b],
G3 = [c, d, f],
G4 = [e, g, h, i] ;
G2 = [a, b],
G3 = [c, d, g],
G4 = [e, f, h, i] ;
G2 = [a, b],
G3 = [c, d, h],
G4 = [e, f, g, i] 
...
-----------------------
%!  %%%%

group234(G, [A,B], [C,D,E], G4):-
    select(A, G,  G0),
    select(B, G0, G1), A @< B,
    select(C, G1, G2),
    select(D, G2, G3), C @< D,
    select(E, G3, G4), D @< E.

n_group234_slago(N) :-
    numlist(1,9,L),
    aggregate_all(count,group234(L,_,_,_),N).
take_ordered(L,[X],R) :-
    select(X,L,R).
take_ordered(L,[X|Xs],R) :-
    append(H,[X|T],L),
    take_ordered(T,Xs,J),
    append(H,J,R).

group234_cc(L,[A1,A2],[B1,B2,B3],[C1,C2,C3,C4]) :-
    take_ordered(L,[A1,A2],U),
    take_ordered(U,[B1,B2,B3],[C1,C2,C3,C4]).

n_group234_cc(N) :-
    numlist(1,9,L),
    aggregate_all(count,group234_cc(L,_A,_B,_C),N).
take_ordered([],[]) --> [].
take_ordered([X|Xs],Ys) --> [X],
    take_ordered(Xs,Ys).
take_ordered(Xs,[Y|Ys]) --> [Y],
    take_ordered(Xs,Ys).

take_ordered(L,O,R) :-
    phrase(take_ordered(O,R),L).
?- numlist(1,9,L),time(aggregate_all(count,group3(L,A,B,C),N)).
% 122,373 inferences, ...

?- time(n_group234_cc(N)).
% 7,549 inferences, ...
-----------------------
%!  %%%%

group234(G, [A,B], [C,D,E], G4):-
    select(A, G,  G0),
    select(B, G0, G1), A @< B,
    select(C, G1, G2),
    select(D, G2, G3), C @< D,
    select(E, G3, G4), D @< E.

n_group234_slago(N) :-
    numlist(1,9,L),
    aggregate_all(count,group234(L,_,_,_),N).
take_ordered(L,[X],R) :-
    select(X,L,R).
take_ordered(L,[X|Xs],R) :-
    append(H,[X|T],L),
    take_ordered(T,Xs,J),
    append(H,J,R).

group234_cc(L,[A1,A2],[B1,B2,B3],[C1,C2,C3,C4]) :-
    take_ordered(L,[A1,A2],U),
    take_ordered(U,[B1,B2,B3],[C1,C2,C3,C4]).

n_group234_cc(N) :-
    numlist(1,9,L),
    aggregate_all(count,group234_cc(L,_A,_B,_C),N).
take_ordered([],[]) --> [].
take_ordered([X|Xs],Ys) --> [X],
    take_ordered(Xs,Ys).
take_ordered(Xs,[Y|Ys]) --> [Y],
    take_ordered(Xs,Ys).

take_ordered(L,O,R) :-
    phrase(take_ordered(O,R),L).
?- numlist(1,9,L),time(aggregate_all(count,group3(L,A,B,C),N)).
% 122,373 inferences, ...

?- time(n_group234_cc(N)).
% 7,549 inferences, ...
-----------------------
%!  %%%%

group234(G, [A,B], [C,D,E], G4):-
    select(A, G,  G0),
    select(B, G0, G1), A @< B,
    select(C, G1, G2),
    select(D, G2, G3), C @< D,
    select(E, G3, G4), D @< E.

n_group234_slago(N) :-
    numlist(1,9,L),
    aggregate_all(count,group234(L,_,_,_),N).
take_ordered(L,[X],R) :-
    select(X,L,R).
take_ordered(L,[X|Xs],R) :-
    append(H,[X|T],L),
    take_ordered(T,Xs,J),
    append(H,J,R).

group234_cc(L,[A1,A2],[B1,B2,B3],[C1,C2,C3,C4]) :-
    take_ordered(L,[A1,A2],U),
    take_ordered(U,[B1,B2,B3],[C1,C2,C3,C4]).

n_group234_cc(N) :-
    numlist(1,9,L),
    aggregate_all(count,group234_cc(L,_A,_B,_C),N).
take_ordered([],[]) --> [].
take_ordered([X|Xs],Ys) --> [X],
    take_ordered(Xs,Ys).
take_ordered(Xs,[Y|Ys]) --> [Y],
    take_ordered(Xs,Ys).

take_ordered(L,O,R) :-
    phrase(take_ordered(O,R),L).
?- numlist(1,9,L),time(aggregate_all(count,group3(L,A,B,C),N)).
% 122,373 inferences, ...

?- time(n_group234_cc(N)).
% 7,549 inferences, ...
-----------------------
%!  %%%%

group234(G, [A,B], [C,D,E], G4):-
    select(A, G,  G0),
    select(B, G0, G1), A @< B,
    select(C, G1, G2),
    select(D, G2, G3), C @< D,
    select(E, G3, G4), D @< E.

n_group234_slago(N) :-
    numlist(1,9,L),
    aggregate_all(count,group234(L,_,_,_),N).
take_ordered(L,[X],R) :-
    select(X,L,R).
take_ordered(L,[X|Xs],R) :-
    append(H,[X|T],L),
    take_ordered(T,Xs,J),
    append(H,J,R).

group234_cc(L,[A1,A2],[B1,B2,B3],[C1,C2,C3,C4]) :-
    take_ordered(L,[A1,A2],U),
    take_ordered(U,[B1,B2,B3],[C1,C2,C3,C4]).

n_group234_cc(N) :-
    numlist(1,9,L),
    aggregate_all(count,group234_cc(L,_A,_B,_C),N).
take_ordered([],[]) --> [].
take_ordered([X|Xs],Ys) --> [X],
    take_ordered(Xs,Ys).
take_ordered(Xs,[Y|Ys]) --> [Y],
    take_ordered(Xs,Ys).

take_ordered(L,O,R) :-
    phrase(take_ordered(O,R),L).
?- numlist(1,9,L),time(aggregate_all(count,group3(L,A,B,C),N)).
% 122,373 inferences, ...

?- time(n_group234_cc(N)).
% 7,549 inferences, ...

Community Discussions

Trending Discussions on 99-problems
  • Prolog :Make my predicate return all possible solutions
Trending Discussions on 99-problems

QUESTION

Prolog :Make my predicate return all possible solutions

Asked 2021-Jun-19 at 14:44

I found this problem in 99-problems in prolog online. There is a solution (has nothing to do with mine) and I was wondering why mine won't work. Or to be precise: it works but it finds only 1 solution instead of all of them. The problem is stated as such: a) In how many ways can a group of 9 people work in 3 disjoint subgroups of 2, 3 and 4 persons?

member(X,[X]).
member(X,[X|_]).
member(X,[_|R]):- member(X,R).

append(X,[],X).
append([],X,X).
append([H|R], [A|B], [H|W]):- append(R,[A|B],W).

group234(G,G2,G3,G4):- length(G2,2),
                       length(G3,3),
                       length(G4,4),
                       member(X,G),
                       member(Y,G),
                       member(Z,G),
                       member(X,G2),
                       member(Y,G2),
                       member(Z,G3),
                       append(G2,G3,I),append(I,G4,G).

q1: Is there a way to use length and append and member as I did and sole this succesfully or do I need to completely rewrite this?
q2: Why does this code produce only 1 solution? Prolog should search for many possible members, shouldn't it? (Obviously , it should not , because the language knows better than I do . But to my understanding it should so why it does not?)

ANSWER

Answered 2021-Jun-19 at 01:41

You need only append/3 to define a predicate to choose and remove one person of a group:

choose(X, L1, L2) :-
    append(A, [X|B], L1),
    append(A, B, L2).

For example:

?- choose(X, [a,b,c], Rest).
X = a,
Rest = [b, c] ;
X = b,
Rest = [a, c] ;
X = c,
Rest = [a, b] ;
false.

Then, using this predicate, you can define group234/4 as:

group234(G, [A,B], [C,D,E], G4):-
    choose(A, G,  G0),
    choose(B, G0, G1), A @< B,
    choose(C, G1, G2), 
    choose(D, G2, G3), C @< D,
    choose(E, G3, G4), D @< E.

Notice that you need condition A @< B, to avoid permutations (since both lists [A,B] and [B,A] represent the same group). Analogously, conditions C @< D and D @< E avoid permutations of the list [C,D,E].

Example:

?- group234([a,b,c,d,e,f,g,h,i], G2, G3, G4).
G2 = [a, b],
G3 = [c, d, e],
G4 = [f, g, h, i] ;
G2 = [a, b],
G3 = [c, d, f],
G4 = [e, g, h, i] ;
G2 = [a, b],
G3 = [c, d, g],
G4 = [e, f, h, i] ;
G2 = [a, b],
G3 = [c, d, h],
G4 = [e, f, g, i] 
...

Source https://stackoverflow.com/questions/68040463

Community Discussions, Code Snippets contain sources that include Stack Exchange Network

Vulnerabilities

No vulnerabilities reported

Install 99-problems

You can download it from GitHub.
You can use 99-problems like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the 99-problems component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .

Support

For any new features, suggestions and bugs create an issue on GitHub. If you have any questions check and ask questions on community page Stack Overflow .

DOWNLOAD this Library from

Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from
over 430 million Knowledge Items
Find more libraries
Reuse Solution Kits and Libraries Curated by Popular Use Cases

Save this library and start creating your kit

Explore Related Topics

Share this Page

share link
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from
over 430 million Knowledge Items
Find more libraries
Reuse Solution Kits and Libraries Curated by Popular Use Cases

Save this library and start creating your kit

  • © 2022 Open Weaver Inc.