回答編集履歴

3

関数名変更

2018/08/01 10:20

投稿

asm
asm

スコア15147

test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
 
6
6
 
7
- def find_hoge(list, a)
7
+ def find_neighbor(list, a)
8
8
 
9
9
  con2 = list.each_cons(2)
10
10
 
@@ -12,10 +12,10 @@
12
12
 
13
13
  end
14
14
 
15
- p find_hoge(list, 300) # => [200,500]
15
+ p find_neighbor(list, 300) # => [200,500]
16
16
 
17
- p find_hoge(list, 50) # => [100]
17
+ p find_neighbor(list, 50) # => [100]
18
18
 
19
- p find_hoge(list, 1000) # => [500]
19
+ p find_neighbor(list, 1000) # => [500]
20
20
 
21
21
  ```

2

修正

2018/08/01 10:20

投稿

asm
asm

スコア15147

test CHANGED
@@ -1,19 +1,21 @@
1
1
  ```ruby
2
-
3
- a = 300
4
2
 
5
3
  list = [50, 100, 200, 300, 500, 1000]
6
4
 
7
5
 
8
6
 
9
- ifnone = proc{}
7
+ def find_hoge(list, a)
10
8
 
9
+ con2 = list.each_cons(2)
10
+
11
- p list.each_cons(3).find{|x,y,z| y == a}.yield_self{|x,y,z| [x,z]}
11
+ con2.find_all{|x,y| x == a}.map{|x,y| y} + con2.find_all{|x,y| y == a}.map{|x,y| x}
12
+
13
+ end
14
+
15
+ p find_hoge(list, 300) # => [200,500]
16
+
17
+ p find_hoge(list, 50) # => [100]
18
+
19
+ p find_hoge(list, 1000) # => [500]
12
20
 
13
21
  ```
14
-
15
-
16
-
17
- **追記**
18
-
19
- 端っこの部分についてがまだ上手くいってないので、後で追記しときます。

1

追記

2018/08/01 10:19

投稿

asm
asm

スコア15147

test CHANGED
@@ -6,6 +6,14 @@
6
6
 
7
7
 
8
8
 
9
+ ifnone = proc{}
10
+
9
11
  p list.each_cons(3).find{|x,y,z| y == a}.yield_self{|x,y,z| [x,z]}
10
12
 
11
13
  ```
14
+
15
+
16
+
17
+ **追記**
18
+
19
+ 端っこの部分についてがまだ上手くいってないので、後で追記しときます。