回答編集履歴
2
10
answer
CHANGED
@@ -35,7 +35,7 @@
|
|
35
35
|
まとめると、
|
36
36
|
|
37
37
|
```swift
|
38
|
-
let integerArray = [(Int, UInt32)](repeating: (0, 0), count:
|
38
|
+
let integerArray = [(Int, UInt32)](repeating: (0, 0), count: 10)
|
39
39
|
.enumerated().map {($0.0+1, arc4random_uniform(UInt32.max))}
|
40
40
|
.sorted {$0.1 > $1.1}
|
41
41
|
.map {$0.0}
|
1
まじめ
answer
CHANGED
@@ -8,4 +8,37 @@
|
|
8
8
|
//=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
9
9
|
```
|
10
10
|
|
11
|
-
質問文中に「乱数」って書かれていないので。
|
11
|
+
質問文中に「乱数」って書かれていないので。
|
12
|
+
|
13
|
+
# 真面目に
|
14
|
+
|
15
|
+
```swift
|
16
|
+
let Length = 10
|
17
|
+
let array = [(Int, UInt32)](repeating: (0, 0), count: Length)
|
18
|
+
|
19
|
+
//1~10に乱数で重みを付ける
|
20
|
+
let weighted = array.enumerated().map {($0.0+1, arc4random_uniform(UInt32.max))}
|
21
|
+
print(weighted)
|
22
|
+
//=> [(1, 2394627293), (2, 3231339695), (3, 1429293380), (4, 2040053551), (5, 1055190483), (6, 2379829461), (7, 1949144040), (8, 522764666), (9, 1567954026), (10, 1966320976)]
|
23
|
+
|
24
|
+
//重みでソートする
|
25
|
+
let sorted = weighted.sorted {$0.1 > $1.1}
|
26
|
+
print(sorted)
|
27
|
+
//=> [(2, 3231339695), (1, 2394627293), (6, 2379829461), (4, 2040053551), (10, 1966320976), (7, 1949144040), (9, 1567954026), (3, 1429293380), (5, 1055190483), (8, 522764666)]
|
28
|
+
|
29
|
+
//ソート結果から1~10を取り出す
|
30
|
+
let integerArray = sorted.map {$0.0}
|
31
|
+
print(integerArray)
|
32
|
+
//=> [2, 1, 6, 4, 10, 7, 9, 3, 5, 8]
|
33
|
+
```
|
34
|
+
|
35
|
+
まとめると、
|
36
|
+
|
37
|
+
```swift
|
38
|
+
let integerArray = [(Int, UInt32)](repeating: (0, 0), count: Length)
|
39
|
+
.enumerated().map {($0.0+1, arc4random_uniform(UInt32.max))}
|
40
|
+
.sorted {$0.1 > $1.1}
|
41
|
+
.map {$0.0}
|
42
|
+
print(integerArray)
|
43
|
+
//=> [6, 3, 9, 8, 5, 10, 7, 1, 2, 4]
|
44
|
+
```
|