Skip to content

Commit 4f6c3b4

Browse files
committed
Added linq-ordering.py
1 parent 272f9d8 commit 4f6c3b4

File tree

5 files changed

+178
-38
lines changed

5 files changed

+178
-38
lines changed

README.md

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ The samples below mirrors the C# LINQ samples layout with the names of the top-l
5555
||`IEnumerable.TakeWhile(predicate)`|`itertools.takewhile(predicate, sequence)`||
5656
||`IEnumerable.Skip(n)`|`array[n:]`||
5757
||`SkipWhile`|`itertools.dropwhile(predicate, sequence)`||
58-
|**Ordering**|`OrderBy`|`sequence.sort` or</br> `sorted(sequence)`||
59-
||`OrderBy(lambda)`|`sequence.sort(key=lambda)` or </br> `sorted(sequence, key=lambda)`||
60-
||`OrderByDescending`|`sequence.sort(reverseTrue)` or </br> `sorted(sequence, reverse=true)`||
61-
||`OrderByDescending(lambda)`|`sequence.sort(key=lambda, reverseTrue)` or </br> `sorted(sequence, key=lambda, reverse=true)`||
62-
||`ThenBy`||`sequence.sort(key=lambda (key1, key2))` or </br> `sorted(sequence, key=lambda (key1, key))`||
63-
||`ThenByDescending`||Custom [order](#dart-utils-added) utility added, followed by `reversed`|
64-
||`Reverse`|`reverse`||
58+
|**Ordering**|`OrderBy`|`sequence.sort()` *or* <br/> `sorted(sequence)`||
59+
||`OrderBy(lambda)`|`sequence.sort(key=lambda)` *or* <br/> `sorted(sequence, key=lambda)`||
60+
||`OrderByDescending`|`sequence.sort(reverse=True)` *or* <br/> `sorted(sequence, reverse=True)`||
61+
||`OrderByDescending(lambda)`|`sequence.sort(key=lambda, reverse=True)` *or* <br/> `sorted(sequence, key=lambda, reverse=True)`||
62+
||`ThenBy`|`sequence.sort(key=lambda (key1, key2))` *or* <br/> `sorted(sequence, key=lambda (key1, key))`||
63+
||`ThenByDescending`|`sequence.sort(key=lambda (key1, -key2))` *or* <br/> `sorted(sequence, key=lambda (key1, -key2))` <br/> *or use a 2 pass sort, starting with least significant* <br/> `ordered = sorted(unordered, key=lambda (key2))` <br/> `ordered = sorted(ordered, key=lambda (key1))` |
64+
||`Reverse`|`sequence.reverse()` *or* `reversed(sequence)`||
6565
|**Grouping**|`GroupBy`||Custom [group](#dart-utils-added-1) utility added
6666
|**Sets**|`Distinct`|`toSet`||
6767
||`Union`|`union`||
@@ -1445,8 +1445,8 @@ public void Linq35()
14451445
def linq35():
14461446
digits = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
14471447

1448-
sorted_digits = sorted(digits, key=lambda s: (len(s), s))
1449-
1448+
sorted_digits = sorted(digits, key=lambda digit: (len(digit), digit))
1449+
14501450
print("Sorted digits:")
14511451
shared.printS(sorted_digits)
14521452
```
@@ -1478,15 +1478,14 @@ public void Linq36()
14781478
ObjectDumper.Write(sortedWords);
14791479
}
14801480
```
1481-
```dart
1482-
//dart
1483-
linq36(){
1484-
var words = [ "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" ];
1485-
1486-
var sortedWords = order(words, byAll:[(a,b) => a.length.compareTo(b.length), caseInsensitiveComparer]);
1487-
1488-
sortedWords.forEach(print);
1489-
}
1481+
```python
1482+
#python
1483+
def linq36():
1484+
words = ["aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry"]
1485+
1486+
sorted_words = sorted(words, key=lambda word: (len(word), word.casefold()))
1487+
1488+
shared.print_namespace(sorted_words)
14901489
```
14911490
#### Output
14921491

@@ -1511,15 +1510,16 @@ public void Linq37()
15111510
ObjectDumper.Write(sortedProducts);
15121511
}
15131512
```
1514-
```dart
1515-
//dart
1516-
linq37(){
1517-
var products = productsList();
1513+
```python
1514+
#python
1515+
def linq37():
1516+
products = shared.getProductList()
1517+
1518+
# negate secondary sort because its a number for reverse order
1519+
sorted_products = sorted(products, key=lambda product: (product.Category, -product.UnitPrice))
1520+
1521+
shared.print_namespace(sorted_products)
15181522

1519-
var sortedProducts = order(products, onAll:[(a) => a.category, (a) => a.unitPrice * -1]);
1520-
1521-
sortedProducts.forEach(print);
1522-
}
15231523
```
15241524
#### Output
15251525

@@ -1548,15 +1548,17 @@ public void Linq38()
15481548
ObjectDumper.Write(sortedWords);
15491549
}
15501550
```
1551-
```dart
1552-
//dart
1553-
linq38(){
1554-
var words = [ "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" ];
1555-
1556-
var sortedWords = order(words,
1557-
byAll:[(a,b) => a.length.compareTo(b.length), (a,b) => caseInsensitiveComparer(b,a)]);
1551+
```python
1552+
#python
1553+
def linq38():
1554+
words = ["aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry"]
15581555

1559-
sortedWords.forEach(print);
1556+
# two pass sort, sort by least significant first
1557+
sorted_words = sorted(words, key=lambda word: word.casefold(), reverse=True)
1558+
sorted_words = sorted(sorted_words, key=lambda word: len(word))
1559+
1560+
1561+
shared.printS(sorted_words)
15601562
}
15611563
```
15621564
#### Output
@@ -1583,8 +1585,8 @@ public void Linq39()
15831585
reversedIDigits.ForEach(Console.WriteLine);
15841586
}
15851587
```
1586-
```dart
1587-
//dart
1588+
```python
1589+
#python
15881590
linq39(){
15891591
var digits = [ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" ];
15901592

src/csharp/linq-ordering/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ static void Main(string[] args)
1313
// Linq29();
1414
// Linq30();
1515
// Linq31();
16-
Linq32();
16+
// Linq32();
1717
// Linq33();
1818
// Linq34();
1919
// Linq35();
2020
// Linq36();
2121
// Linq37();
22-
// Linq38();
22+
Linq38();
2323
// Linq39();
2424
}
2525

src/python/functions.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from types import SimpleNamespace
2+
3+
def select(item, the_list):
4+
return map(lambda b: SimpleNamespace(item_a=item, item_b=b), the_list)
5+
6+
7+
def left_outer_join(outer_list, inner_list):
8+
9+
result = []
10+
for outer_item in outer_list:
11+
result.extend(select(outer_item, inner_list))
12+
return result
13+
14+
15+
def select_many(outer_list, inner_item_list_key):
16+
result = []
17+
for outer_item in outer_list:
18+
result.extend(select(outer_item, getattr(outer_item, inner_item_list_key)))
19+
return result

src/python/linq-grouping.py

Whitespace-only changes.

src/python/linq-ordering.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import shared
2+
3+
4+
def linq28():
5+
words = ["cherry", "apple", "blueberry"]
6+
7+
sorted_words = sorted(words)
8+
9+
print("The sorted list of words:")
10+
shared.printS(sorted_words)
11+
12+
13+
def linq29():
14+
words = ["cherry", "apple", "blueberry"]
15+
16+
sorted_words = sorted(words, key=lambda x: len(x))
17+
18+
print("The sorted list of words (by length):")
19+
shared.printS(sorted_words)
20+
21+
22+
def linq30():
23+
products = shared.getProductList()
24+
25+
sorted_products = sorted(products, key=lambda p: p.ProductName)
26+
27+
shared.print_namespace(sorted_products)
28+
29+
30+
def linq31():
31+
words = ["aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry"]
32+
33+
sorted_words = sorted(words, key=lambda s: s.casefold())
34+
35+
shared.printS(sorted_words)
36+
37+
38+
def linq32():
39+
doubles = [1.7, 2.3, 1.9, 4.1, 2.9]
40+
41+
sorted_doubles = sorted(doubles, reverse=True)
42+
43+
print("The doubles from highest to lowest:")
44+
shared.printN(sorted_doubles)
45+
46+
47+
def linq33():
48+
products = shared.getProductList()
49+
50+
sorted_products = sorted(products, key=lambda p: p.UnitsInStock, reverse=True);
51+
52+
shared.print_namespace(sorted_products)
53+
54+
55+
def linq34():
56+
words = ["aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry"]
57+
58+
sorted_words = sorted(words, key=lambda s: s.casefold(), reverse=True);
59+
60+
shared.print_namespace(sorted_words)
61+
62+
63+
def linq35():
64+
digits = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
65+
66+
sorted_digits = sorted(digits, key=lambda digit: (len(digit), digit))
67+
68+
print("Sorted digits:")
69+
shared.printS(sorted_digits)
70+
71+
72+
def linq36():
73+
words = ["aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry"]
74+
75+
sorted_words = sorted(words, key=lambda word: (len(word), word.casefold()))
76+
77+
shared.printS(sorted_words)
78+
79+
80+
def linq37():
81+
products = shared.getProductList()
82+
83+
# negate secondary sort because its a number for reverse order
84+
sorted_products = sorted(products, key=lambda product: (product.Category, -product.UnitPrice))
85+
86+
shared.print_namespace(sorted_products)
87+
88+
89+
def linq38():
90+
words = ["aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry"]
91+
92+
# two pass sort, sort by least significant first
93+
sorted_words = sorted(words, key=lambda word: word.casefold(), reverse=True)
94+
sorted_words = sorted(sorted_words, key=lambda word: len(word))
95+
96+
97+
shared.printS(sorted_words)
98+
99+
100+
def linq39():
101+
digits = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
102+
103+
reversed_i_digits = reversed(list(filter(lambda digit: digit[1] == "i", digits)))
104+
105+
print("A backwards list of the digits with a second character of 'i':");
106+
shared.printS(reversed_i_digits)
107+
108+
# linq28()
109+
# linq29()
110+
# linq30()
111+
# linq31()
112+
# linq32()
113+
# linq33()
114+
# linq34()
115+
# linq35()
116+
# linq36()
117+
# linq37()
118+
# linq38()
119+
# linq39()

0 commit comments

Comments
 (0)