回答編集履歴

1

コード追加

2015/10/22 07:21

投稿

K_T_T_K
K_T_T_K

スコア231

test CHANGED
@@ -17,3 +17,97 @@
17
17
  根本的な解決策ではなくなりますが、teratailにあげた、質問者様のコードを再度コピーして
18
18
 
19
19
  coloud9に新たに別名で作って実行してみてはいかがでしょう。
20
+
21
+
22
+
23
+ 追記:
24
+
25
+ こちらのコードを試してみてください。
26
+
27
+
28
+
29
+ #配列のなかに3文字以上の数字が入っている時、三番目に大きい数字を返すメソッドを作れ。
30
+
31
+ ```
32
+
33
+ def third_greatest(nums)
34
+
35
+ first = nil
36
+
37
+ second = nil
38
+
39
+ third = nil
40
+
41
+
42
+
43
+ idx = 0
44
+
45
+ while idx < nums.length
46
+
47
+ value = nums[idx]
48
+
49
+ if first == nil || value > first then
50
+
51
+ third = second
52
+
53
+ second = first
54
+
55
+ first = value
56
+
57
+ elsif second == nil || value > second then
58
+
59
+ third = second
60
+
61
+ second = value
62
+
63
+ elsif third == nil || value > third then
64
+
65
+ third = value
66
+
67
+ end
68
+
69
+
70
+
71
+ idx += 1
72
+
73
+ end
74
+
75
+
76
+
77
+ return third
78
+
79
+ end
80
+
81
+
82
+
83
+ # These are tests to check that your code is working. After writing
84
+
85
+ # your solution, they should all print true.
86
+
87
+
88
+
89
+ puts(
90
+
91
+ 'third_greatest([5, 3, 7]) == 3: ' +
92
+
93
+ (third_greatest([5, 3, 7]) == 3).to_s
94
+
95
+ )
96
+
97
+ puts(
98
+
99
+ 'third_greatest([5, 3, 7, 4]) == 4: ' +
100
+
101
+ (third_greatest([5, 3, 7, 4]) == 4).to_s
102
+
103
+ )
104
+
105
+ puts(
106
+
107
+ 'third_greatest([2, 3, 7, 4]) == 3: ' +
108
+
109
+ (third_greatest([2, 3, 7, 4]) == 3).to_s
110
+
111
+ )
112
+
113
+ ```