How to count the specific item in dynamic list? - swiftui

#Published var storedMedicines: [Medicine] = [
Medicine(Title: "Medicine 1", Description: "Lower blood sugar", date: .init(timeIntervalSince1970: 1664779248), isTaken: false),
Medicine(Title: "Medicine 1", Description: "Lower blood sugar", date: .init(timeIntervalSince1970: 1664880248), isTaken: false),
Medicine(Title: "Medicine 1", Description: "Lower blood sugar", date: .init(timeIntervalSince1970: 1664720990), isTaken: false),
Medicine(Title: "Medicine 1", Description: "Lower blood sugar", date: .init(timeIntervalSince1970: 1664720990), isTaken: false),
]
I want to know how can I count the "isTaken" attribute in here. I want to specifically make it said that "2 isTaken true" and 2 isTaken false"

There are two many ways to do this.
Using filter.
let trueCount = storedMedicines.filter(\.isTaken).count
let falseCount = storedMedicines.count - trueCount
Using reduce.
let trueCount = storedMedicines.reduce(0, { $0 + ($1.isTaken ? 1 : 0) })
let falseCount = storedMedicines.count - trueCount

Related

Left Join C++ MYSQL library

I'm trying to access "category" table with LEFT JOIN. I need to retrieve the field "name" in this table.
This is my code:
void Product::read(MYSQL *connection)
{
MYSQL_RES *result;
MYSQL_ROW row;
if(mysql_query(connection, "SELECT * FROM product LEFT JOIN category ON product.category=category.category_id"))
std::cout<<"Query failed!!"<<mysql_error(connection)<<std::endl;
else
result=mysql_store_result(connection);
if(result->row_count>0)
{
while(row=mysql_fetch_row(result))
{
std::cout<<"Name: "<<row[1]<<" Brand: "<<row[3]<<" Price: "<<row[4]<<" Category: "<<row[5]<<" Amount: "<<row[6]<<std::endl;
}
}
mysql_free_result(result);
}
And this is the result of the query:
Name: Oneplus Nord 2 5G Brand: Oneplus Price: 299.99 Category: 1 Amount: 3
Name: Acer Swift 3 Brand: Acer Price: 899.99 Category: 2 Amount: 5
Name: Bose SoundLink Revolve Brand: Bose Price: 100.23 Category: 1 Amount: 3
How can I show the name of the category?
You can be more specific about what columns are you selecting and their order. Rather than * you can specify the table_name.column_name (or just column_name if you have no overlaps, or alias_name.column_name if you want to use aliases), so you could try something like:
SELECT product.name, product.brand, product.price, category.name, product.amount FROM product LEFT JOIN category ON product.category=category.category_id
I am assuming how columns are named.
This way the following code will be more predictable, because indices would be based on what you've written in select.
std::cout<<"Name: "<<row[1]<<" Brand: "<<row[3]<<" Price: "<<row[4]<<" Category: "<<row[5]<<" Amount: "<<row[6]<<std::endl;

I got the from date and to date from the user. I want to return the Sold Product's price and its count in the basis of Daily sold order. how?

models.Order.objects.filter(purchasedate__range=[from_date, to_date])
today_total = 0
quant = 0
for values in daterangeorder:
today_total += values.price
quant += values.quantity
return response.Response({
"from_date": from_date,
"to_date": to_date,
"Amount Sold": str(today_total),
"Count": str(quant)
})
This is the output:
{
"from_date": "2021-11-19",
"to_date": "2021-11-23",
"Amount Sold": "27000",
"Count": "9"
}
I want like this (day by day sold products count):-
{
date: “'2021-10-20”,
count: 20,
total_price_sold: 5000.00
},
{
date: “'2021-10-21”,
count: 4,
total_price_sold: 300.00
}
Instead of solving your problem with Python code, you can let the database handle it for you using aggregation, see the docs: https://docs.djangoproject.com/en/3.2/topics/db/aggregation/#values
from django.db.models import Sum
result = models.Order.objects.values('purchasedate')
.order_by('purchasedate')
.annotate(total_price_sold=Sum('price'), count=Sum('quantity'))
return response.Response(result)

RegEx fetch a value data by sentence

Hello community,
I'm trying to figure out how I can get only the value of a specific key. Let's say:
Item Number: 121225734541
transaction:: 1205737904002
Price: C $4.73
Shipping price: Free
I need to get only the value after the ":" char.
Key: Item Number, Value: 121225734541
Any ideas?
Check this out: https://jsfiddle.net/5buspznn/1/
var raw_data = 'Item Number: 121225734541\ntransaction: 1205737904002\nPrice: C $4.73\nShipping price: Free'
var data_object = {}
raw_data.split('\n').map(function (data_line) {
var parsed = data_line.match(/(.*?):(.*)/)
data_object[parsed[1]] = parsed[2].trim()
})
console.log(data_object)
returns this:
Object {
Item Number: "121225734541",
transaction: "1205737904002",
Price: "C $4.73",
Shipping price: "Free"
}
Here you go:
([^:]+):\s*([^\n]+)
# anything not :
# followed by : and whitespaces, eventually
# anything not a newline
The first capture group holds your key, the second your value.
See a demo on regex101.com.

Couchbase View _count Reduce For Given Keys

I am trying to write a view in Couchbase using a reduce such as _count which will give me a count of the products at an address.
I have some documents in the database in the following format;
Document 1
{
id: 1,
address: {
street: 'W Churchill St'
city: 'Chicago',
state: 'IL',
},
product: 'Cable'
}
Document 2
{
id: 2,
address: {
street: 'W Churchill St'
city: 'Chicago',
state: 'IL',
},
product: 'Cable'
}
Document 3
{
id: 3,
address: {
street: 'W Churchill St'
city: 'Chicago',
state: 'IL',
},
product: 'Satellite'
}
Document 4
{
id: 4,
address: {
street: 'E Foster Rd'
city: 'New York',
state: 'NY',
},
product: 'Free To Air'
}
I already have a view which gives me all the products at an address which uses a composite key such as;
emit([doc.address.street, doc.address.city, doc.address.state], null)
Now this leads me on to the actual problem, I want to be able to get a count of products at a address or addresses.
I want to be able to see for an array of "keys"
['W Churchill St','Chicago','IL']
['E Foster Rd','New York','NY']
which products and a count of them. So i would expect to see in my results.
'Cable' : 2,
'Satellite': 1,
'Free To Air': 1
however if I specified only this "key",
['W Churchill St','Chicago','IL']
I would expect to see
'Cable' : 2,
'Satellite': 1
How to write my view to accommodate this?
The solution to this was to append my product to the key like so;
emit([doc.address.street, doc.address.city, doc.address.state, doc.product], null)
Then using;
?start_key=[street,city,state]&end_key=[street,city,state,{}]&group_level=4
Result:
{"rows":[
{"key":['W Churchill St','Chicago','IL','Cable'], "value":2},
{"key":['W Churchill St','Chicago','IL','Satellite'], "value":1}
]}
I would then need to repeat this query for each of the addresses and sum the results.

Sencha Touch Filter List for values greater than a given one

I have a list of meeting rooms with the following capacity of people they support. The data is something like:
{
"name" : "Room 1",
"city" : "Mumbai",
"capacity" : 4
},
{
"name" : "Room 2",
"city" : "Mumbai",
"capacity" : 10
},
{
"name" : "Room 3",
"city" : "Mumbai",
"capacity" : 6
},
{
"name" : "Room 6",
"city" : "Mumbai",
"capacity" : 3
},
{
"name" : "Room 4",
"city" : "Mumbai",
"capacity" : 2
}
// etc etc
Now I want to add a filter so that when I choose the number of ppl that need to meet the app shows me all rooms having a capacity GREATER THAN this number.
For example: If I say I'm looking for a room for 4 ppl it should show me room 1,2,3. If I search for a room for 2 ppl it shows all the rooms. Etc.
As of now if I use a filter this is what I do now:
store.filter("capacity",2)
and it only shows me rooms with the capacity of 2. Not rooms with a greater capacity.
What should I do???
You need to create a filter function like this:
var roomFilter = new Ext.util.Filter({
filterFn: function(item) {
return item.capacity > 2;
}
});
then:
var largeRooms = store.filter(roomFilter);
P/S: If Ext.util.Filter doesn't work with your Store, you should create a clone of its data which is of type Ext.util.MixedCollection.