Skip to content

Commit 4fc9eba

Browse files
committed
Fix lint
1 parent d244507 commit 4fc9eba

File tree

5 files changed

+64
-71
lines changed

5 files changed

+64
-71
lines changed

ciphers/caesar.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,3 @@ def self.decrypt(ciphertext, shift)
3030
end.join
3131
end
3232
end
33-

ciphers/caesar_test.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,3 @@ def run_tests(plaintext, expected_cipher, shift)
1919
assert_equal decrypted, plaintext
2020
end
2121
end
22-

ciphers/rsa.rb

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require "prime"
1+
require 'prime'
22

33
def initialize(keys = {})
44
@e ||= keys[:e]
@@ -7,16 +7,16 @@ def initialize(keys = {})
77

88
def cipher(message)
99
message.bytes.map do |byte|
10-
cbyte = ((byte.to_i ** e) % n).to_s
10+
cbyte = ((byte.to_i**e) % n).to_s
1111
missing_chars = n.to_s.size - cbyte.size
12-
"0" * missing_chars + cbyte
12+
'0' * missing_chars + cbyte
1313
end.join
1414
end
1515

1616
def decipher(ciphed_message)
1717
ciphed_message.chars.each_slice(n.to_s.size).map do |arr|
18-
(arr.join.to_i ** d) % n
19-
end.pack("c*")
18+
(arr.join.to_i**d) % n
19+
end.pack('c*')
2020
end
2121

2222
def public_keys
@@ -52,43 +52,47 @@ def d
5252
end
5353

5454
def extended_gcd(a, b)
55-
last_remainder, remainder = a.abs, b.abs
56-
x, last_x, y, last_y = 0, 1, 1, 0
55+
last_remainder = a.abs
56+
remainder = b.abs
57+
x = 0
58+
last_x = 1
59+
y = 1
60+
last_y = 0
5761
while remainder != 0
58-
last_remainder, (quotient, remainder) = remainder, last_remainder.divmod(remainder)
62+
(quotient, remainder) = last_remainder.divmod(remainder)
63+
last_remainder = remainder
5964
x, last_x = last_x - quotient * x, x
6065
y, last_y = last_y - quotient * y, y
6166
end
6267

63-
return last_remainder, last_x * (a < 0 ? -1 : 1)
68+
[last_remainder, last_x * (a < 0 ? -1 : 1)]
6469
end
6570

6671
def invmod(e, et)
6772
g, x = extended_gcd(e, et)
68-
raise "The maths are broken!" if g != 1
73+
raise 'The maths are broken!' if g != 1
74+
6975
x % et
7076
end
7177

7278
def random_prime_number
7379
number = Random.rand(1..1000)
74-
until Prime.prime?(number) || number == p || number == q
75-
number = Random.rand(1..1000)
76-
end
80+
number = Random.rand(1..1000) until Prime.prime?(number) || number == p || number == q
7781
number
7882
end
7983

80-
def main()
81-
puts "Enter the message you want to encrypt and decrypt with RSA algorithm: "
82-
message = gets.chomp().to_s
83-
puts "Encoded Text:"
84+
def main
85+
puts 'Enter the message you want to encrypt and decrypt with RSA algorithm: '
86+
message = gets.chomp.to_s
87+
puts 'Encoded Text:'
8488
puts cipher(message)
85-
puts "Decoded Text:"
89+
puts 'Decoded Text:'
8690
puts decipher(cipher(message))
87-
puts "p: #{p()}"
88-
puts "q: #{q()}"
89-
puts "e: #{e()}"
90-
puts "d: #{d()}"
91-
puts "totient: #{totient()}"
91+
puts "p: #{p}"
92+
puts "q: #{q}"
93+
puts "e: #{e}"
94+
puts "d: #{d}"
95+
puts "totient: #{totient}"
9296
end
9397

94-
main()
98+
main
Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,39 @@
11
class Node
2-
attr_accessor :data, :parent, :rank
3-
def initialize(data)
4-
@data = data
5-
@parent = self
6-
@rank = 0
7-
end
8-
def parent
9-
@parent
10-
end
11-
def parent=(parent)
12-
@parent = parent;
13-
end
14-
def rank
15-
@rank
16-
end
17-
def rank=(rank)
18-
@rank = rank
19-
end
2+
attr_accessor :data, :parent, :rank, :parent, :rank
3+
4+
def initialize(data)
5+
@data = data
6+
@parent = self
7+
@rank = 0
8+
end
209
end
2110

2211
class DisjointSets
23-
def make_set(d)
24-
Node.new(d)
25-
end
12+
def make_set(d)
13+
Node.new(d)
14+
end
2615

27-
def find_set(x)
28-
raise ArgumentError unless x.class <= Node
29-
x.parent=(find_set(x.parent)) unless x.parent == x
30-
x.parent
31-
end
16+
def find_set(x)
17+
raise ArgumentError unless x.class <= Node
3218

33-
def union_set(x, y)
34-
px = find_set(x)
35-
py = find_set(y)
36-
return if px == py
37-
if px.rank > py.rank
38-
py.parent = px
39-
elsif py.rank > px.rank
40-
px.parent = py
41-
else
42-
px.parent = py
43-
py.rank += 1
44-
end
45-
end
19+
x.parent = (find_set(x.parent)) unless x.parent == x
20+
x.parent
21+
end
22+
23+
def union_set(x, y)
24+
px = find_set(x)
25+
py = find_set(y)
26+
return if px == py
27+
28+
if px.rank > py.rank
29+
py.parent = px
30+
elsif py.rank > px.rank
31+
px.parent = py
32+
else
33+
px.parent = py
34+
py.rank += 1
35+
end
36+
end
4637
end
4738

4839
ds = DisjointSets.new
@@ -53,4 +44,4 @@ def union_set(x, y)
5344
puts ds.find_set(one) == ds.find_set(two) # should be true
5445
ds.union_set(one, three)
5546
puts ds.find_set(two) == ds.find_set(three) # should be true
56-
puts one.rank + two.rank + three.rank == 1 # should be true
47+
puts one.rank + two.rank + three.rank == 1 # should be true

electronics/ohms_law.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
# Reference: https://en.wikipedia.org/wiki/Ohm's_law
44

55
def ohms_law(i, r)
6-
if(i > 0 && r > 0)
6+
if i > 0 && r > 0
77
"The voltage for given #{i} ampheres current and #{r} ohms resistance is #{r * i} volts."
88
else
99
raise
1010
end
11-
rescue
12-
"Error: Please provide valid inputs only!"
11+
rescue StandardError
12+
'Error: Please provide valid inputs only!'
1313
end
1414

1515
# Valid inputs
@@ -25,7 +25,7 @@ def ohms_law(i, r)
2525
# Error: Please provide valid inputs only!
2626
puts(ohms_law(-5, -10))
2727
# Error: Please provide valid inputs only!
28-
puts(ohms_law(5, "10"))
28+
puts(ohms_law(5, '10'))
2929
# Error: Please provide valid inputs only!
30-
puts(ohms_law("a", 10))
30+
puts(ohms_law('a', 10))
3131
# Error: Please provide valid inputs only!

0 commit comments

Comments
 (0)