-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorting_things.py
More file actions
58 lines (44 loc) · 1.22 KB
/
Sorting_things.py
File metadata and controls
58 lines (44 loc) · 1.22 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
### Method 1 ==> ordonner un string avec ''.join + sorted
# ==> avec sorted(iterable)
# crée une liste ordonnée
# +
# ==> ''.join(sorted(iterable))
# recrée un string
letters = 'zyx'
new_string = ''.join(sorted(letters))
# <-------------->
# |--> make alist from any string, tuple
print(new_string)
# xyz
# string
print(''.join(sorted("letters")))
# eelrstt
# dictionnary
print(''.join(sorted({"z":24,"y":23,"x":22,"a":1})))
# axyz
# set
print(''.join(sorted({"z","y","x","a"})))
# axyz
# tuple
print(''.join(sorted(("z","y","x","a"))))
# axyz
# list
print(''.join(sorted(["z","y","x","a"])))
# axyz
### Method 2 ==> faire une list à partir d'un iterable + .sort() puis ''.join()
# make a list form a string, sort it then making a string with .join()
tmp = list(letters)
tmp.sort()
new_string = ''.join(tmp)
data = [10, 2, 1, 7, 5, 6, 4, 3, 9, 8]
def find_high_low(nums):
nums.sort()
print(nums)
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
return nums[-1], nums[0]
high, low = find_high_low(data)
print(
('The highest number is {} ' +
'and the lowest number is {}.').format(high, low)
)
# The highest number is 10 and the lowest number is 1.