-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathleague_analysis.pl
More file actions
145 lines (105 loc) · 4.7 KB
/
Copy pathleague_analysis.pl
File metadata and controls
145 lines (105 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
my_member(X, [X|_]).
my_member(X, [_|T]):-
my_member(X, T).
list_length([],0).
list_length([_|Tail],N):-
list_length(Tail,Tmp),
N is Tmp+1.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% TASK 1 // players_in_team(barcelona, L).
% Get a list of all players in a specific team
players_in_team(Team, Players) :-
players_in_team(Team, [], Players).
% helper function to make sure all team players are in accumulator
player_team_not_in_acc(Team, Acc):-
player(X, Team, _),
\+ my_member(X, Acc).
% Base case: when all team players are found in accumulator
players_in_team(Team, Acc, Acc) :-
\+ player_team_not_in_acc(Team, Acc).
% Recursive case: Find a team player thats not in accumulator ,add them to the accumulator, continue
players_in_team(Team, Acc, Players) :-
player(Player, Team, _),
\+ my_member(Player, Acc),
players_in_team(Team, [Player | Acc], Players),
!.
% ====================================================================================
% TASK 2 // team_count_by_country(spain, N).
% Count all teams are from a specific country
team_count_by_country(Country, Count) :-
collect_team(Country,[],Teams),
list_length(Teams,Count).
% Recursive case
collect_team(Country,List,Teams):-
team(Team, Country, _),
\+ my_member(Team,List), % ensure that the team with specific country not found in the final list so i will add it
!,
collect_team(Country,[Team|List],Teams).
% Base case: No more teams left so the accumlator list will be the final list
collect_team(_,Teams,Teams).
% ====================================================================================
% TASK 3 //most_successful_team(T).
% Find the team with the most championship titles
most_successful_team(T):-
team(T, _, _),
\+ not_sucessful(T),
!. % first Team found with maximum score
% helper function to make sure no other team has higher score
not_sucessful(T):-
team(T, _, N),
team(_, _, M),
M > N.
% ====================================================================================
% TASK 4 // matches_of_team(barcelona, L).
% List all matches where a specific team participated
matches_of_team(Team, Matches) :-
list_of_matches(Team, [], Matches). % an empty accumulator [], and Matches (which will store the result)
list_of_matches(Team, Acc, Matches) :-
match(T1, T2, S1, S2),
(T1 = Team ; T2 = Team),
\+ my_member((T1, T2, S1, S2), Acc), % Ensures the match is not already added to the accumulator
!,
list_of_matches(Team, [(T1, T2, S1, S2) | Acc], Matches).
list_of_matches(_, Matches, Matches). % Base case: No more matches left so the accumulator list will be the final list
% ====================================================================================
% TASK 5 (I used task 4 to implement task 5) //num_matches_of_team(barcelona, N).
% count all matches where a specific team participated
num_matches_of_team(Team, N):-
list_of_matches(Team, [], Matches),
list_length(Matches,N).
% ====================================================================================
% TASK 6 //top_scorer(P).
% Find the top goal scorer in the tournament
max_goals(G) :-
goals(_, G),
\+ (goals(_, G2), G2 > G). % Ensure no G2 is greater than G
top_scorer(P) :-
goals(P, G), % Get players goal
max_goals(G), % Ensure its the max goal
!.
% ====================================================================================
% TASK 7 //most_common_position_in_team(barcelona, Pos).
% Find the Most Common Position in a Specific Team
most_common_position_in_team(Team, Pos) :-
player(_, Team, Pos),
count_pos(Team, Pos, N),
% ensure no other player in the same team with different position has more count
\+ (player(_, Team, OtherPos), OtherPos \= Pos, count_pos(Team, OtherPos, M), M > N),
!.
% helper function to make sure all team players in specified position are in the accumulator
player_pos_not_in_acc(Team, Acc, Pos):-
player(X, Team, Pos),
\+ my_member(X, Acc).
count_pos(Team, Pos, Count) :-
count_pos(Team, Pos, 0, [], Count).
% base case
count_pos(Team, Pos, Count, Acc, Count) :-
\+ player_pos_not_in_acc(Team, Acc, Pos).
% recursive case
count_pos(Team, Pos, TempCount, Acc, Count) :-
player(Player, Team, Pos),
\+ my_member(Player, Acc),
NewCount is TempCount + 1,
count_pos(Team, Pos, NewCount, [Player | Acc], Count),
!.