Skip to content

Commit bbd73b5

Browse files
committed
added linq-aggregate
1 parent 941d533 commit bbd73b5

File tree

4 files changed

+166
-81
lines changed

4 files changed

+166
-81
lines changed

README.md

Lines changed: 75 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ The samples below mirrors the C# LINQ samples layout with the names of the top-l
7979
||`Enumerable.Repeat`|`[x] * n` *or* <br /> `itertools.repeat(x, n)`||
8080
|**Quantifiers**|`Any`|`any`||
8181
||`All`|`all`||
82-
|**Aggregate**|`Count`|`length`||
83-
||`Count(lamda)`|`where(lambda).length`||
84-
||`Sum`||Custom [sum](#dart-utils-added-4) utility added|
85-
||`Min`||Custom [min](#dart-utils-added-4) utility added|
86-
||`Max`||Custom [max](#dart-utils-added-4) utility added|
87-
||`Avg`||Custom [avg](#dart-utils-added-4) utility added|
82+
|**Aggregate**|`Count`|`length` *or* <br /> `sum(iterator)`||
83+
||`Count(lamda)`|`sum(iterator)`||
84+
||`Sum`|sum||
85+
||`Min`|min||
86+
||`Max`|max||
87+
||`Avg`|||
8888
||`Sum(lambda)`||Custom [sum](#dart-utils-added-4) utility added|
8989
||`Min(lambda)`||Custom [min](#dart-utils-added-4) utility added|
9090
||`Max(lambda)`||Custom [max](#dart-utils-added-4) utility added|
@@ -2718,15 +2718,15 @@ public void Linq73()
27182718
Console.WriteLine($"There are {uniqueFactors} unique prime factors of 300.");
27192719
}
27202720
```
2721-
```dart
2722-
//dart
2723-
linq73(){
2724-
var factorsOf300 = [ 2, 2, 3, 5, 5 ];
2725-
2726-
int uniqueFactors = factorsOf300.toSet().length;
2727-
2728-
print("There are $uniqueFactors unique factors of 300.");
2729-
}
2721+
```python
2722+
#python
2723+
def linq73():
2724+
factors_of_300 = [2, 2, 3, 5, 5]
2725+
2726+
unique_factors = len(set(factors_of_300))
2727+
2728+
print("There are %d unique factors of 300." % unique_factors)
2729+
27302730
```
27312731
#### Output
27322732

@@ -2744,15 +2744,14 @@ public void Linq74()
27442744
Console.WriteLine($"There are {oddNumbers} odd numbers in the list.");
27452745
}
27462746
```
2747-
```dart
2748-
//dart
2749-
linq74(){
2750-
var numbers = [ 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 ];
2751-
2752-
int oddNumbers = numbers.where((n) => n % 2 == 1).length;
2753-
2754-
print("There are $oddNumbers odd numbers in the list.");
2755-
}
2747+
```python
2748+
#python
2749+
def linq74():
2750+
numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0]
2751+
2752+
odd_numbers = sum(n % 2 == 1 for n in numbers)
2753+
2754+
print("There are %d odd numbers in the list." % odd_numbers)
27562755
```
27572756
#### Output
27582757

@@ -2771,16 +2770,14 @@ public void Linq76()
27712770
ObjectDumper.Write(orderCounts);
27722771
}
27732772
```
2774-
```dart
2775-
//dart
2776-
linq76(){
2777-
var customers = customersList();
2778-
2779-
var orderCounts = customers
2780-
.map((c) => { 'CustomerId': c.customerId, 'OrderCount': c.orders.length });
2773+
```python
2774+
#python
2775+
def linq75():
2776+
customers = shared.getCustomerList()
27812777

2782-
orderCounts.forEach(print);
2783-
}
2778+
order_counts = map(lambda cust: SimpleNamespace(CustomerID=cust.CustomerID, OrderCount=len(cust.Orders)), customers)
2779+
2780+
shared.print_namespace(order_counts)
27842781
```
27852782
#### Output
27862783

@@ -2807,15 +2804,18 @@ public void Linq77()
28072804
ObjectDumper.Write(categoryCounts);
28082805
}
28092806
```
2810-
```dart
2811-
//dart
2807+
```python
2808+
#python
28122809
linq77(){
2813-
var products = productsList();
2814-
2815-
var categoryCounts = group(products, by:(p) => p.category)
2816-
.map((g) => { 'Category': g.key, 'ProductCount': g.length });
2810+
def linq77():
2811+
products = shared.getProductList()
2812+
2813+
sorted_by_category = sorted(products, key=lambda p: p.Category)
2814+
grouped_by_category = groupby(sorted_by_category, key=lambda p: p.Category)
28172815

2818-
categoryCounts.forEach(print);
2816+
category_counts = map(lambda g: SimpleNamespace(Category=g[0], ProductCount=len(list(g[1]))), grouped_by_category)
2817+
2818+
shared.print_namespace(category_counts)
28192819
}
28202820
```
28212821
#### Output
@@ -2841,15 +2841,14 @@ public void Linq78()
28412841
Console.WriteLine($"The sum of the numbers is {numSum}.");
28422842
}
28432843
```
2844-
```dart
2845-
//dart
2846-
linq78(){
2847-
var numbers = [ 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 ];
2848-
2849-
var numSum = sum(numbers);
2850-
2851-
print("The sum of the numbers is $numSum.");
2852-
}
2844+
```python
2845+
#python
2846+
def linq78():
2847+
numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0]
2848+
2849+
num_sum = sum(numbers)
2850+
2851+
print("The sum of the numbers is %d." % num_sum)
28532852
```
28542853
#### Output
28552854

@@ -2867,15 +2866,14 @@ public void Linq79()
28672866
Console.WriteLine($"There are a total of {totalChars} characters in these words.");
28682867
}
28692868
```
2870-
```dart
2871-
//dart
2872-
linq79(){
2873-
var words = [ "cherry", "apple", "blueberry" ];
2874-
2875-
var totalChars = sum(words, (w) => w.length);
2876-
2877-
print("There are a total of $totalChars characters in these words.");
2878-
}
2869+
```python
2870+
#python
2871+
def linq79():
2872+
words = ["cherry", "apple", "blueberry"]
2873+
2874+
total_chars = sum(len(w) for w in words)
2875+
2876+
print("There are a total of %d characters in these words." % total_chars)
28792877
```
28802878
#### Output
28812879

@@ -2895,16 +2893,17 @@ public void Linq80()
28952893
ObjectDumper.Write(categories);
28962894
}
28972895
```
2898-
```dart
2899-
//dart
2900-
linq80(){
2901-
var products = productsList();
2902-
2903-
var categories = group(products, by:(p) => p.category)
2904-
.map((g) => { 'Category': g.key, 'TotalUnitsInStock': sum(g.values, (p) => p.unitsInStock) });
2905-
2906-
categories.forEach(print);
2907-
}
2896+
```python
2897+
#python
2898+
def linq80():
2899+
products = shared.getProductList()
2900+
2901+
sorted_by_category = sorted(products, key=lambda p: p.Category)
2902+
grouped_by_category = groupby(sorted_by_category, key=lambda p: p.Category)
2903+
2904+
category_counts = map(lambda g: SimpleNamespace(Category=g[0], TotalUnitsInStock=sum(p.UnitsInStock for p in g[1])), grouped_by_category)
2905+
2906+
shared.print_namespace(category_counts)
29082907
```
29092908
#### Output
29102909

@@ -2929,15 +2928,14 @@ public void Linq81()
29292928
Console.WriteLine($"The minimum number is {minNum}.");
29302929
}
29312930
```
2932-
```dart
2933-
//dart
2934-
linq81(){
2935-
var numbers = [ 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 ];
2936-
2937-
int minNum = min(numbers);
2938-
2939-
print("The minimum number is $minNum.");
2940-
}
2931+
```python
2932+
#python
2933+
def linq81():
2934+
numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0]
2935+
2936+
min_num = min(numbers)
2937+
2938+
print("The minimum number is %d" % min_num);
29412939
```
29422940
#### Output
29432941

src/python/linq-aggregate.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import shared
2+
from types import SimpleNamespace
3+
from itertools import groupby
4+
5+
def linq73():
6+
factors_of_300 = [2, 2, 3, 5, 5]
7+
8+
unique_factors = len(set(factors_of_300))
9+
10+
print("There are %d unique factors of 300." % unique_factors)
11+
12+
13+
def linq74():
14+
numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0]
15+
16+
odd_numbers = sum(n % 2 == 1 for n in numbers)
17+
18+
print("There are %d odd numbers in the list." % odd_numbers)
19+
20+
21+
def linq75():
22+
customers = shared.getCustomerList()
23+
24+
order_counts = map(lambda cust: SimpleNamespace(CustomerID=cust.CustomerID, OrderCount=len(cust.Orders)), customers)
25+
26+
shared.print_namespace(order_counts)
27+
28+
29+
def linq77():
30+
products = shared.getProductList()
31+
32+
sorted_by_category = sorted(products, key=lambda p: p.Category)
33+
grouped_by_category = groupby(sorted_by_category, key=lambda p: p.Category)
34+
35+
category_counts = map(lambda g: SimpleNamespace(Category=g[0], ProductCount=len(list(g[1]))), grouped_by_category)
36+
37+
shared.print_namespace(category_counts)
38+
39+
40+
def linq78():
41+
numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0]
42+
43+
num_sum = sum(numbers)
44+
45+
print("The sum of the numbers is %d." % num_sum)
46+
47+
48+
def linq79():
49+
words = ["cherry", "apple", "blueberry"]
50+
51+
total_chars = sum(len(w) for w in words)
52+
53+
print("There are a total of %d characters in these words." % total_chars)
54+
55+
56+
def linq80():
57+
products = shared.getProductList()
58+
59+
sorted_by_category = sorted(products, key=lambda p: p.Category)
60+
grouped_by_category = groupby(sorted_by_category, key=lambda p: p.Category)
61+
62+
category_counts = map(lambda g: SimpleNamespace(Category=g[0], TotalUnitsInStock=sum(p.UnitsInStock for p in g[1])), grouped_by_category)
63+
64+
shared.print_namespace(category_counts)
65+
66+
67+
def linq81():
68+
numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0]
69+
70+
min_num = min(numbers)
71+
72+
print("The minimum number is %d" % min_num)
73+
74+
75+
def linq82():
76+
words = ["cherry", "apple", "blueberry"]
77+
78+
shortest_word = min(len(w) for w in words)
79+
80+
print("The shortest word is %s characters long." % shortest_word)
81+
82+
# linq73()
83+
# linq74()
84+
# linq75()
85+
# linq77()
86+
# linq78()
87+
# linq79()
88+
# linq80()
89+
# linq81()
90+
linq82()

src/python/linq-conversion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import shared
2-
2+
from types import SimpleNamespace
33

44
def linq54():
55
doubles = [ 1.7, 2.3, 1.9, 4.1, 2.9 ]

src/python/linq-quantifiers.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import shared
2-
3-
41
def linq67():
52
words = ["believe", "relief", "receipt", "field"]
63

0 commit comments

Comments
 (0)