mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-11-16 19:50:00 +01:00
Fix lint
This commit is contained in:
parent
d2445070fb
commit
4fc9eba58a
5 changed files with 63 additions and 70 deletions
|
@ -30,4 +30,3 @@ class CaesarCipher
|
||||||
end.join
|
end.join
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -19,4 +19,3 @@ class CaesarCipherTest < Minitest::Test
|
||||||
assert_equal decrypted, plaintext
|
assert_equal decrypted, plaintext
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
require "prime"
|
require 'prime'
|
||||||
|
|
||||||
def initialize(keys = {})
|
def initialize(keys = {})
|
||||||
@e ||= keys[:e]
|
@e ||= keys[:e]
|
||||||
|
@ -7,16 +7,16 @@ end
|
||||||
|
|
||||||
def cipher(message)
|
def cipher(message)
|
||||||
message.bytes.map do |byte|
|
message.bytes.map do |byte|
|
||||||
cbyte = ((byte.to_i ** e) % n).to_s
|
cbyte = ((byte.to_i**e) % n).to_s
|
||||||
missing_chars = n.to_s.size - cbyte.size
|
missing_chars = n.to_s.size - cbyte.size
|
||||||
"0" * missing_chars + cbyte
|
'0' * missing_chars + cbyte
|
||||||
end.join
|
end.join
|
||||||
end
|
end
|
||||||
|
|
||||||
def decipher(ciphed_message)
|
def decipher(ciphed_message)
|
||||||
ciphed_message.chars.each_slice(n.to_s.size).map do |arr|
|
ciphed_message.chars.each_slice(n.to_s.size).map do |arr|
|
||||||
(arr.join.to_i ** d) % n
|
(arr.join.to_i**d) % n
|
||||||
end.pack("c*")
|
end.pack('c*')
|
||||||
end
|
end
|
||||||
|
|
||||||
def public_keys
|
def public_keys
|
||||||
|
@ -52,43 +52,47 @@ def d
|
||||||
end
|
end
|
||||||
|
|
||||||
def extended_gcd(a, b)
|
def extended_gcd(a, b)
|
||||||
last_remainder, remainder = a.abs, b.abs
|
last_remainder = a.abs
|
||||||
x, last_x, y, last_y = 0, 1, 1, 0
|
remainder = b.abs
|
||||||
|
x = 0
|
||||||
|
last_x = 1
|
||||||
|
y = 1
|
||||||
|
last_y = 0
|
||||||
while remainder != 0
|
while remainder != 0
|
||||||
last_remainder, (quotient, remainder) = remainder, last_remainder.divmod(remainder)
|
(quotient, remainder) = last_remainder.divmod(remainder)
|
||||||
|
last_remainder = remainder
|
||||||
x, last_x = last_x - quotient * x, x
|
x, last_x = last_x - quotient * x, x
|
||||||
y, last_y = last_y - quotient * y, y
|
y, last_y = last_y - quotient * y, y
|
||||||
end
|
end
|
||||||
|
|
||||||
return last_remainder, last_x * (a < 0 ? -1 : 1)
|
[last_remainder, last_x * (a < 0 ? -1 : 1)]
|
||||||
end
|
end
|
||||||
|
|
||||||
def invmod(e, et)
|
def invmod(e, et)
|
||||||
g, x = extended_gcd(e, et)
|
g, x = extended_gcd(e, et)
|
||||||
raise "The maths are broken!" if g != 1
|
raise 'The maths are broken!' if g != 1
|
||||||
|
|
||||||
x % et
|
x % et
|
||||||
end
|
end
|
||||||
|
|
||||||
def random_prime_number
|
def random_prime_number
|
||||||
number = Random.rand(1..1000)
|
number = Random.rand(1..1000)
|
||||||
until Prime.prime?(number) || number == p || number == q
|
number = Random.rand(1..1000) until Prime.prime?(number) || number == p || number == q
|
||||||
number = Random.rand(1..1000)
|
|
||||||
end
|
|
||||||
number
|
number
|
||||||
end
|
end
|
||||||
|
|
||||||
def main()
|
def main
|
||||||
puts "Enter the message you want to encrypt and decrypt with RSA algorithm: "
|
puts 'Enter the message you want to encrypt and decrypt with RSA algorithm: '
|
||||||
message = gets.chomp().to_s
|
message = gets.chomp.to_s
|
||||||
puts "Encoded Text:"
|
puts 'Encoded Text:'
|
||||||
puts cipher(message)
|
puts cipher(message)
|
||||||
puts "Decoded Text:"
|
puts 'Decoded Text:'
|
||||||
puts decipher(cipher(message))
|
puts decipher(cipher(message))
|
||||||
puts "p: #{p()}"
|
puts "p: #{p}"
|
||||||
puts "q: #{q()}"
|
puts "q: #{q}"
|
||||||
puts "e: #{e()}"
|
puts "e: #{e}"
|
||||||
puts "d: #{d()}"
|
puts "d: #{d}"
|
||||||
puts "totient: #{totient()}"
|
puts "totient: #{totient}"
|
||||||
end
|
end
|
||||||
|
|
||||||
main()
|
main
|
||||||
|
|
|
@ -1,48 +1,39 @@
|
||||||
class Node
|
class Node
|
||||||
attr_accessor :data, :parent, :rank
|
attr_accessor :data, :parent, :rank, :parent, :rank
|
||||||
def initialize(data)
|
|
||||||
@data = data
|
def initialize(data)
|
||||||
@parent = self
|
@data = data
|
||||||
@rank = 0
|
@parent = self
|
||||||
end
|
@rank = 0
|
||||||
def parent
|
end
|
||||||
@parent
|
|
||||||
end
|
|
||||||
def parent=(parent)
|
|
||||||
@parent = parent;
|
|
||||||
end
|
|
||||||
def rank
|
|
||||||
@rank
|
|
||||||
end
|
|
||||||
def rank=(rank)
|
|
||||||
@rank = rank
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class DisjointSets
|
class DisjointSets
|
||||||
def make_set(d)
|
def make_set(d)
|
||||||
Node.new(d)
|
Node.new(d)
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_set(x)
|
def find_set(x)
|
||||||
raise ArgumentError unless x.class <= Node
|
raise ArgumentError unless x.class <= Node
|
||||||
x.parent=(find_set(x.parent)) unless x.parent == x
|
|
||||||
x.parent
|
|
||||||
end
|
|
||||||
|
|
||||||
def union_set(x, y)
|
x.parent = (find_set(x.parent)) unless x.parent == x
|
||||||
px = find_set(x)
|
x.parent
|
||||||
py = find_set(y)
|
end
|
||||||
return if px == py
|
|
||||||
if px.rank > py.rank
|
def union_set(x, y)
|
||||||
py.parent = px
|
px = find_set(x)
|
||||||
elsif py.rank > px.rank
|
py = find_set(y)
|
||||||
px.parent = py
|
return if px == py
|
||||||
else
|
|
||||||
px.parent = py
|
if px.rank > py.rank
|
||||||
py.rank += 1
|
py.parent = px
|
||||||
end
|
elsif py.rank > px.rank
|
||||||
|
px.parent = py
|
||||||
|
else
|
||||||
|
px.parent = py
|
||||||
|
py.rank += 1
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
ds = DisjointSets.new
|
ds = DisjointSets.new
|
||||||
|
@ -53,4 +44,4 @@ ds.union_set(one, two)
|
||||||
puts ds.find_set(one) == ds.find_set(two) # should be true
|
puts ds.find_set(one) == ds.find_set(two) # should be true
|
||||||
ds.union_set(one, three)
|
ds.union_set(one, three)
|
||||||
puts ds.find_set(two) == ds.find_set(three) # should be true
|
puts ds.find_set(two) == ds.find_set(three) # should be true
|
||||||
puts one.rank + two.rank + three.rank == 1 # should be true
|
puts one.rank + two.rank + three.rank == 1 # should be true
|
||||||
|
|
|
@ -3,13 +3,13 @@
|
||||||
# Reference: https://en.wikipedia.org/wiki/Ohm's_law
|
# Reference: https://en.wikipedia.org/wiki/Ohm's_law
|
||||||
|
|
||||||
def ohms_law(i, r)
|
def ohms_law(i, r)
|
||||||
if(i > 0 && r > 0)
|
if i > 0 && r > 0
|
||||||
"The voltage for given #{i} ampheres current and #{r} ohms resistance is #{r * i} volts."
|
"The voltage for given #{i} ampheres current and #{r} ohms resistance is #{r * i} volts."
|
||||||
else
|
else
|
||||||
raise
|
raise
|
||||||
end
|
end
|
||||||
rescue
|
rescue StandardError
|
||||||
"Error: Please provide valid inputs only!"
|
'Error: Please provide valid inputs only!'
|
||||||
end
|
end
|
||||||
|
|
||||||
# Valid inputs
|
# Valid inputs
|
||||||
|
@ -25,7 +25,7 @@ puts(ohms_law(5, -10))
|
||||||
# Error: Please provide valid inputs only!
|
# Error: Please provide valid inputs only!
|
||||||
puts(ohms_law(-5, -10))
|
puts(ohms_law(-5, -10))
|
||||||
# Error: Please provide valid inputs only!
|
# Error: Please provide valid inputs only!
|
||||||
puts(ohms_law(5, "10"))
|
puts(ohms_law(5, '10'))
|
||||||
# Error: Please provide valid inputs only!
|
# Error: Please provide valid inputs only!
|
||||||
puts(ohms_law("a", 10))
|
puts(ohms_law('a', 10))
|
||||||
# Error: Please provide valid inputs only!
|
# Error: Please provide valid inputs only!
|
||||||
|
|
Loading…
Reference in a new issue