I'm new to XSB prolog and I'm trying to solve this problem.
I've got prices of products and some orders. It looks like this:
price(cola,3).
price(juice,1).
price(sprite,4).
// product for ex. is cola with a price of 3 (something, it doesn't matter which currency)
order(1, [cola,cola,sprite]).
order(2, [sprite,sprite,juice]).
order(3, [juice,cola]). // the number here is the number of the order
// and the list represents the products that
// belong to that order
Now, my task is to write a new function called bill/2. This function should take the number of the order and then sum up all the prices for the products in the same order(list).
Something like:
|?-bill(1,R).
R= 10 ==> ((cola)3 + (cola)3 + (sprite)4 = 10)
|?-bill(2,R).
R= 9 ==> ((sprite)4 + (sprite4 + (juice)1 = 9)
and so on... I know how to get to the number of the order but I don't know how to get each product from the list inside that order to get to it's price, so I can sum it up.
Thanks in advance.
In plain Prolog, first get all numbers in a list, then sum the list:
bill(Ord, Tot) :-
order(Ord, Items),
findall(Price, (member(I, Items), price(I, Price)), Prices),
sum_list(Prices, Tot).
but since XSB has tabling available, there could be a better way, using some aggregation function.
Related
I have the following list:
val bought = List(("ta",5.5,"1/09/21"),("ta",50.5,"2/09/20"),("xa",60.38,"1/09/21"),...)
I want to implement a function in Scala that returns a list of tuples, type List[(String,Double)], being String the date and Double the sum of money spent on that day.
My idea was to obtain the set of dates
val x = bought.map(_._3).toSet
and then use a for-comprehension structure to sum the value of each tuple if they have the same date, but my tries have been in vain.
Please, it should be scala like, not imperative style.
Any hint?
I think a 'Scala' way to solve your problem is:
from your list, create a Map where the key is the date and the value is the list of bought things
sum all the money for each day
In Scala, it can be done with groupBy and mapValues:
bought.groupBy(_._3)
.view
.mapValues(values => values.map(_._2).sum)
.toList
I have a little formula problem that I would really appreciate some help with.
The list has columns with Student names that repeat, Course names that repeat, and course status that can be passed, not passed, or not started.
I would like to count the number of unique students that passed all 10 courses that are available.
I tried different variations of Calculate and COUNTROWS.
This is the formula I have at the moment that doesn't work
PassedAll =CALCULATE(DISTINCTCOUNT(Progress[Student]),Progress[Mark]="Passed",Progress[Course]="Course1"&&Progress[Course]="Course2")
I understand that && doesn't work in this scenario because in a single row it cannot be both courses. And I don't want to replace it with an OR, || operator because I want to count students that have Passed marks on each of these courses.
Can someone please recommend how to somehow replace the course section of the filter with something that will include all 10 courses?
If you want only number to show in "Card Visualization" then:
StudentPassed = countrows(filter(GENERATE(VALUES(Sheet1[Student]), ROW("CoursCompleted", CALCULATE( DISTINCTCOUNT(Sheet1[Course]), Sheet1[Mark] ="Passed"))), [CoursCompleted]= 10))
in my sample data 1 Student Passed all, 1 Student Passed 9courses, 1 Student Pass 8 (and no record for 2 of course).
i'am new in Prolog ,
i tried to write 5 lists and get the intersection between them ,
how i can achieve that, ** lists will be defined in file so it's not input from the user .
i see many resources they implement it with two lists and its work fine if i make list as query from user ...
but when i try to pre-defined lists in file it's not work.
simple description of part of my project to more clarify...
menus will display and user will select one from each of the season , weather condition , occasion...
lists will be about what clothes are appropriate
so for example user select "winter" season, "rainy" weather condition and "wedding"occasion
lists for each of them
rainy([take_umbrella, jacket,coat]).
winter([jacket,sweater,coat,take_umbrella]).
wedding ([take_umbrella,dress,jacket,coat]).
so the result form intersection will be take_umbrella ,jacket,coat
i hope my idea is clear, and thank you in advance:)
I try to predefined lists in file and it's work. Y your lists in file it's not work? I do not know. I fix your errors because your paste is not error free then it's work.
?- winter(Winter),
rainy(Rainy),
wedding(Wedding),
intersection(Winter, Rainy, Winter_and_Rainy),
intersection(Winter_and_Rainy, Wedding, Winter_and_Rainy_and_Wedding).
Winter = [jacket, sweater, coat, take_umbrella],
Rainy = [take_umbrella, jacket, coat],
Wedding = [take_umbrella, dress, jacket, coat],
Winter_and_Rainy = Winter_and_Rainy_and_Wedding, Winter_and_Rainy_and_Wedding = [jacket, coat, take_umbrella].
But if it's not know how many list maybe you make list and reduce.
?- % make some lists L1, L2, ..., Ln,
foldl(intersection, [L1, L2, ..., Ln-1], Ln, Intersection).
When is winter on a rainy wedding you reduce like:
?- winter(Winter), rainy(Rainy), wedding(Wedding),
foldl(intersection, [Winter, Rainy], Wedding, Intersection).
Winter = [jacket, sweater, coat, take_umbrella],
Rainy = Intersection, Intersection = [take_umbrella, jacket, coat],
Wedding = [take_umbrella, dress, jacket, coat].
You see order of element is change but is it problem? For me no problem.
Problem =====>
Basically there are three .rrd which are generated for three departments.
From that we fetch three values (MIN, MAX, CURRENT) and print ins 3x3 format. There is a python script which does that.
eg -
Dept1: Min=10 Max=20 Cur=15
Dept2: Min=0 Max=10 Cur=5
Dept3: Min=10 Max=30 Cur=25
Now I want to add the values together (Min, Max, Cur) and print in one line.
eg -
Dept: Min=20 Max=60 Cur=45
Issue I am facing =====>
No matter what CDEF i write, I am breaking the graph. :(
This is the part I hate as i do not get any error message.
As far as I understand(please correct me if i am wrong) I definitely cannot store the value anywhere in my program as a graph is returned.
What would be a proper way to add the values in this condition.
Please let me know if my describing the problem is lacking more detail.
You can do this with a VDEF over a CDEF'd sum.
DEF:a=dept1.rrd:ds0:AVERAGE
DEF:b=dept2.rrd:ds0:AVERAGE
DEF:maxa=dept1.rrd:ds0:MAXIMUM
DEF:maxb=dept2.rrd:ds0:MAXIMUM
CDEF:maxall=maxa,maxb,+
CDEF:all=a,b,+
VDEF:maxalltime=maxall,MAXIMUM
VDEF:alltimeavg=all,AVERAGE
PRINT:maxalltime:Max=%f
PRINT:alltimeavg:Avg=%f
LINE:all#ff0000:AllDepartments
However, you should note that, apart form at the highest granularity, the Min and Max totals will be wrong! This is because max(a+b) != max(a) + max(b). If you dont calculate the min/max aggregate at time of storage, the granularity will be gone at time of display.
For example, if a = (1, 2, 3) and b = (3, 2, 1), then max(a) + max(b) = 6; however the maximum at any point in time is in fact 4. The same issue applies to using min(a) + min(b).
I am new to python. I am trying to print sum of all duplicates nos and products of non-duplicates nos from the python list. for examples
list = [2,2,4,4,5,7,8,9,9]. what i want is sum= 2+2+4+4+9+9 and product=5*7*8.
There are pythonic one liners that can do this but here is an explicit way you might find easier to understand.
num_list = [2,2,4,4,5,7,8,9,9]
sum_dup = 0
product = 1
for n in num_list:
if num_list.count(n) == 1:
product *= n
else:
sum_dup += n
Also side note, don't call your list the name "list", it interferes with the builtin name of the list type.
count is useful for this. Sum is built in, but there is no built in "product", so using reduce is the easiest way to do this.
from functools import reduce
import operator
the_sum = sum([x for x in list if list.count(x)>1])
the_product = reduce(operator.mul, [x for x in lst if lst.count(x)==1])
Use a for loop to read a number from the list. create a variable and assign the number to it, read another number and compare them using an if statement. If they are the same sum them like sameNumSum+=sameNumSum else multiply them. Before for loop create these two variables and initialize them. I just gave you the algorithm to it, you can change it into your code. Hope that help though.