File tree Expand file tree Collapse file tree 4 files changed +69
-10
lines changed Expand file tree Collapse file tree 4 files changed +69
-10
lines changed Original file line number Diff line number Diff line change
1
+ name : Ruby Style Check
2
+
3
+ on :
4
+ push :
5
+ branches : [ main ]
6
+ paths :
7
+ - ' **/*.rb'
8
+
9
+ jobs :
10
+ style :
11
+ runs-on : ubuntu-latest
12
+ steps :
13
+ - uses : actions/checkout@v4
14
+
15
+ - name : Set up Ruby
16
+ uses : ruby/setup-ruby@v1
17
+ with :
18
+ ruby-version : ' 3.4'
19
+ bundler-cache : true
20
+
21
+ - name : Install RuboCop and extensions
22
+ run : |
23
+ gem install rubocop
24
+ gem install rubocop-rake
25
+ gem install rubocop-rspec
26
+
27
+ - name : Run RuboCop
28
+ continue-on-error : true
29
+ run : rubocop
Original file line number Diff line number Diff line change
1
+ require :
2
+ - rubocop-rake
3
+ - rubocop-rspec
4
+
5
+ AllCops :
6
+ NewCops : enable
7
+ TargetRubyVersion : 3.4
8
+
9
+ Style/Documentation :
10
+ Enabled : false
11
+
12
+ Layout/LineLength :
13
+ Max : 120
14
+
15
+ Style/StringLiterals :
16
+ EnforcedStyle : single_quotes
17
+
18
+ Style/FrozenStringLiteralComment :
19
+ Enabled : true
20
+ EnforcedStyle : always
21
+
22
+ Style/For :
23
+ Enabled : false
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
1
3
def functional_style ( input )
2
- lefts , rights = input . split ( "\n " ) . map {
4
+ lefts , rights = input . split ( "\n " ) . map do
3
5
|line | line . split ( ' ' ) . map ( &:to_i )
4
- } . transpose
6
+ end . transpose
5
7
6
8
sorted_lefts = lefts . sort
7
9
sorted_rights = rights . sort
8
10
9
11
distance = sorted_lefts . zip ( sorted_rights ) . sum { |left , right | ( left - right ) . abs }
10
12
11
- return distance
13
+ distance
12
14
end
13
15
14
16
def imperative_style ( input )
@@ -18,7 +20,7 @@ def imperative_style(input)
18
20
rights = [ ]
19
21
20
22
for line in lines
21
- left , right = line . split ( " " )
23
+ left , right = line . split ( ' ' )
22
24
lefts << left . to_i
23
25
rights << right . to_i
24
26
end
@@ -30,13 +32,15 @@ def imperative_style(input)
30
32
for i in 0 ..( lefts . length - 1 )
31
33
distance += ( sorted_lefts [ i ] - sorted_rights [ i ] ) . abs
32
34
end
33
- return distance
35
+
36
+ distance
34
37
end
35
38
36
39
def solution ( input )
37
40
functional_result = functional_style ( input )
38
41
imperative_result = imperative_style ( input )
39
42
puts "Functional result: #{ functional_result } "
40
43
puts "Imparative result: #{ imperative_result } "
41
- return functional_result
44
+
45
+ functional_result
42
46
end
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
1
3
def functional_style ( input )
2
4
lefts , rights = input . split ( "\n " ) . map { |line | line . split ( ' ' ) } . transpose
3
5
4
6
counts = rights . tally
5
7
6
8
similarity = lefts . sum { |left | counts [ left ] . to_i * left . to_i }
7
9
8
- return similarity
10
+ similarity
9
11
end
10
12
11
13
def imperative_style ( input )
@@ -15,7 +17,7 @@ def imperative_style(input)
15
17
rights = Hash . new ( 0 )
16
18
17
19
for line in lines
18
- left , right = line . split ( " " )
20
+ left , right = line . split ( ' ' )
19
21
lefts << left
20
22
rights [ right ] += 1
21
23
end
@@ -25,13 +27,14 @@ def imperative_style(input)
25
27
similarity += left . to_i * rights [ left ]
26
28
end
27
29
28
- return similarity
30
+ similarity
29
31
end
30
32
31
33
def solution ( input )
32
34
functional_result = functional_style ( input )
33
35
imperative_result = imperative_style ( input )
34
36
puts "Functional result: #{ functional_result } "
35
37
puts "Imparative result: #{ imperative_result } "
36
- return functional_result
38
+
39
+ functional_result
37
40
end
You can’t perform that action at this time.
0 commit comments