回答編集履歴

2

10

2017/06/16 05:53

投稿

fuzzball
fuzzball

スコア16733

test CHANGED
@@ -72,7 +72,7 @@
72
72
 
73
73
  ```swift
74
74
 
75
- let integerArray = [(Int, UInt32)](repeating: (0, 0), count: Length)
75
+ let integerArray = [(Int, UInt32)](repeating: (0, 0), count: 10)
76
76
 
77
77
  .enumerated().map {($0.0+1, arc4random_uniform(UInt32.max))}
78
78
 

1

まじめ

2017/06/16 05:53

投稿

fuzzball
fuzzball

スコア16733

test CHANGED
@@ -19,3 +19,71 @@
19
19
 
20
20
 
21
21
  質問文中に「乱数」って書かれていないので。
22
+
23
+
24
+
25
+ # 真面目に
26
+
27
+
28
+
29
+ ```swift
30
+
31
+ let Length = 10
32
+
33
+ let array = [(Int, UInt32)](repeating: (0, 0), count: Length)
34
+
35
+
36
+
37
+ //1~10に乱数で重みを付ける
38
+
39
+ let weighted = array.enumerated().map {($0.0+1, arc4random_uniform(UInt32.max))}
40
+
41
+ print(weighted)
42
+
43
+ //=> [(1, 2394627293), (2, 3231339695), (3, 1429293380), (4, 2040053551), (5, 1055190483), (6, 2379829461), (7, 1949144040), (8, 522764666), (9, 1567954026), (10, 1966320976)]
44
+
45
+
46
+
47
+ //重みでソートする
48
+
49
+ let sorted = weighted.sorted {$0.1 > $1.1}
50
+
51
+ print(sorted)
52
+
53
+ //=> [(2, 3231339695), (1, 2394627293), (6, 2379829461), (4, 2040053551), (10, 1966320976), (7, 1949144040), (9, 1567954026), (3, 1429293380), (5, 1055190483), (8, 522764666)]
54
+
55
+
56
+
57
+ //ソート結果から1~10を取り出す
58
+
59
+ let integerArray = sorted.map {$0.0}
60
+
61
+ print(integerArray)
62
+
63
+ //=> [2, 1, 6, 4, 10, 7, 9, 3, 5, 8]
64
+
65
+ ```
66
+
67
+
68
+
69
+ まとめると、
70
+
71
+
72
+
73
+ ```swift
74
+
75
+ let integerArray = [(Int, UInt32)](repeating: (0, 0), count: Length)
76
+
77
+ .enumerated().map {($0.0+1, arc4random_uniform(UInt32.max))}
78
+
79
+ .sorted {$0.1 > $1.1}
80
+
81
+ .map {$0.0}
82
+
83
+ print(integerArray)
84
+
85
+ //=> [6, 3, 9, 8, 5, 10, 7, 1, 2, 4]
86
+
87
+ ```
88
+
89
+