New to Prolog - challenge with lists - list

First I apologize for any mistakes I may make since English is not my first language.
So I decided to learn Prolog all by myself and I came across this "challenge."
I have this database about TV Shows. It has the following predicates:
person(Person_id,Name).
show(Show_id,Name).
participates(Person_id,Show_id,Activity).
What I have to find out is the relation between 2 people... I have to write an objective like this:
network(Person1,Person2), that given the names of 2 people (Person1,Person2) gives back the name of 2 other people, Person3 and Person4 - Person1 has worked with Person3 on any show, Person2 has worked with Person4, and Person3 and Person4 have both worked together.
I made a list of all the shows Person1 has worked in and then made a list of all the shows Person2 has worked in.
My problem is how to continue from here. I thought about making a list of all the people who worked in the shows Person1 has worked in, and another list with all the people who worked in the shows Person2 has worked in, and then try to find out if, of all the people Person1 has worked with, if someone has worked with someone on the list of people Person2 has worked with.
Can anyone give me some lights on how to work this out? Thanks!!

in prolog there is no such thing as "returning value"
therefore, you actually have to write a predicate like
network(Person1,Person2,Person3,Person4).
the first step is writing the predicate worked_with(Person1,Person2)
something like:
worked_with(Person1,Person2):-
participates(Person1,X,_),
participates(Person2,X,_),
Person1 \= Person2.
after that the network predicate would be something like
network(P1,P2,P3,P4):-
worked_with(P1,P3),
worked_with(P2,P4),
worked_with(P3,P4).
however, this predicate uses as input the ID's instead of the names;
you simply need to write a wrapper that will do the decoding.
i think that you could try to write it yourself as an exercise:b
by the way, if you are just starting to learn prolog, i dont really think that there is a reason to try something complex like that; try something simpler first to grasp the way prolog behaves

Related

Count occurances in multiple filtered rows

I need a little help since im new to PowerBI. I have a data set which says what have been eaten in a specific day. At the end there are columns which show if the day before the overall feeling was better (so in this specific day it got worse). I got up to 30 Ingredients and 5 days before. The "1" in e.g. Day2 means its TRUE for condition "2 days before it got worse"
The data looks like this:
Data set example
Now, I want to retrieve and add up all ingredients, which has been eaten at "Day1", "Day2" and so on, so I can see which food is maybe causing problems because it should appear more often in the days before or at least appear in every case there. How do I achieve this?
For example I can see then, that on Day2 overy often Ingredient "Apple" appears, so there could be an assumption that Chicken meat is not good for this person.
I tried to pivot the table, as well as disconnect the "DayX" into another table and make relationship between them, but nothing adds up the things in the way I want it to.

Prolog add certain items to list

I'm unable to figure out why my code isn't working despite looking through answers to similar questions. I'm too new at Prolog to properly name things, but I hope you can see what I'm trying to get at.
I am defining a timetable roughly based on this program and am struggling with getting a list of the Classes that Mike teaches for a given result (Next step will be to declare that only results where both Mike and Phil teach 2 should be returned, but I want to work through it so that I can see and understand what's going on).
I imagine this should be simple but any combinations of the addToList(List,C) predicate never work. I know there is the append predicate but I hear it's inefficient, and would like to learn the 'raw' way. I don't know how many variations I've attempted and can't get my head around the way Prolog works in this regard and don't know on what level I'm going wrong - it's all a bit of a black box mystery working with it.
var program =
:- use_module(library(lists)).
prefers(may,a).
prefers(may,b).
prefers(may,c).
prefers(may,d).
prefers(bob,a).
prefers(bob,b).
prefers(bob,c).
prefers(pete,a).
prefers(pete,b).
prefers(pete,c).
prefers(pete,d).
prefers(tom,a).
prefers(tom,b).
prefers(tom,c).
prefers(tom,d).
teacher_pref(mike,a).
teacher_pref(mike,b).
teacher_pref(mike,c).
teacher_pref(phil,b).
teacher_pref(phil,c).
teacher_pref(phil,d).
addToList([C|List],C):- addToList(List,C).
timetable([a,[C1,S1,T1],b,[C2,S2,T2],c,[C3,S3,T3],d,[C4,S4,T4]],List1):-
teacher_pref(T1,C1),
teacher_pref(T2,C2),
teacher_pref(T3,C3),
teacher_pref(T4,C4),
prefers(S1,C1),
prefers(S2,C2),
S1\\=S2,
prefers(S3,C3),
S1\\=S3,
S2\\=S3,
prefers(S4,C4),
S1\\=S4,
S2\\=S4,
S3\\=S4,
addToList(List1,C):-
teacher_pref(mike,C).
session.consult( program );
session.query('timetable([C1,[a,S1,T1],C2,[b,S2,T2],C3,[c,S3,T3,L3],C4,[d,S4,T4]],List1).')
If I understand correctly you have:
teacher_pref(mike,a).
teacher_pref(mike,b).
teacher_pref(mike,c).
And you want to get a list of these classes, which would be:
[a, b, c]
In Prolog we have some higher-order predicates that are for occasions like this:
% (What to find, the goal to call, all the results)
?- findall(Class, teacher_pref(mike, Class), Classes).
Classes = [a, b, c].
In the Tau-Prolog docs they're under All Solutions, in SWI-Prolog there's a couple more.
To make this into a more generic predicate:
teacher_prefs(Teacher, Prefs) :-
findall(Pref, teacher_pref(Teacher, Pref), Prefs).

fstream deleting specific start to end rows

Im a noob programmer currently making a small family database using cpp but i have trouble deleting a family from the the list...
My list looks something like this
start-of-family 1
jim
joe
bob
sam
end-of-family 1
start-of-family 2
rob
max
end-of-family 2
start-of-family 3
sue
tom
kim
end-of-family 3
If i wanted to delete family 1, I would locate start-of-family 1 and end-of-family 1. Then run a loop but how do i locate it if the user only inputs an int to represent a family number. Also how do i make the succeeding family numbers deduct by 1 so that family 2 will be 1 and family 3 will be 2.
thanks a lot
If I were doing this problem I would start by making each family into a vector of names. Then, I would create a vector containing those family vectors.
The result would look something like:
{
<(jim), (joe), (bob), (sam) >
<(rob), (max) >
<(sue), (tom), (kim) >
}
Then, if the user wants to delete one of the families, you can use vector.remove(n) where n is the index of the family to be removed.
This sounds like a school or text book assignment. Have you gotten to vectors yet? Where are the names coming from? Are you hard coding them into the list? Or reading them from a .txt file? What kind of list structure are you storing them in right now?
i realized that clearing the db and updating it with what ive got is way easier than modifying the db and updating my program

EXCEL How to iterate and add iterator to double entries

I have an Excel list which does contain a certain number of double entries.
eg.
enter image description here
What I want to do is to search each doublette and add an iterator to it. So that the result looks something like this:
enter image description here
I've absolutly no idea where to start with this.
Any ideas!?
Put this in B2:
=IF(A2=A1,A2&"."&COUNTIF($A$1:A2,A2)-1,A2)
And drag down. Let me know if it helps...
Thanks a lot to Black.Jack! You were right all along!
My own stupidity was in my way. ;) I am running a German version of Libre Office, which brings two subtle, but important changes. First conditions etc. must be named German!!! (as a colleague pointed out ;))
Secondly the parameters must be seperated by ; and not by , in Libreoffice...
So the working formula for a German Libre Office version is this one:
=WENN(A2=A1;A1&"."&ZĂ„HLENWENN($A$1:A2;A2)-1;A2)
again: Thank you very much Black.Jack. Wouldn't have figured it out without your help!!!

c++ read from a file and output sorted content

I am learning to code and found interesting task, but i do not know where to start in solving it. So i have a file with some titles and comments which need to be placed under the right title. So the first line of the input contains a number N which determines the quantity of the titles. Each row starts with a unique article id (integer), followed by the title in quotation marks. After there is no more titles, comments are given. At the beginning there is Title ID and comment (one word), but comments may recur for the same ID. so here is a structure of a file:
<N>
<ID1> "<Title1>"
...
<IDN> "<TitleN>"
<ID1> <Comment1>
...
<IDK> <CommentK>
Now in the output file each Title has two lines - first for the title and second one for comments. Titles must be in ascending order. And comments should be in reverse order (newest comments in the beginning) Structure of output file:
<Title1>
<Comment11> ... < CommentK1>
...
<TitleN>
<Comment1N> ... < CommentLN>
Example:
input:
3
1 "This is some title"
3 "Another title"
2 "And one more"
1 COmment
1 Another
3 Great
2 Awesome
3 Lucky
2 Stanley
output
This is some title
Another COmment
And one more
Stanley Awesome
Another Title
Lucky Great
I do not now where to begin with.. Should I use arrays to save the data in memory and then try to sort it in the right pattern?Or is it better to load the text file into a data structure; in this case a linked list? Maybe someone can guide me in the right direction how to accomplish this task. (I do not ask to code it for me, just guide me or give some algorithm, it would be highly appreciated). Thanks!
I assume you know how to read a file in C++, if not, please look at it, for example on this tutorial.
For the sorting part, you could use a STL container
to store the ids. I would recommend a map with the id as key and the string as value.
The advantage of the map, is that it's already sorted (ascending order).
If you use another container, you should look at the sorting algorithms if you want to understand how they work. For instance bubble sort, selection sort, quick sort or merge sort for the main ones.
However if you want to sort without any implementation, have a look at this.
This doesn't provide you a specific answer for your problem, but it can help you start.
[UPDATE]
I didn't read correctly and I haven't seen that multiple lines could have the same ID. A map would not necessarily be the most suitable container.