Merge remote-tracking branch 'origin/master' into jk-intersection

This commit is contained in:
Jessica Kwok 2021-04-16 16:35:08 -07:00
commit f1c72d43ba
3 changed files with 121 additions and 0 deletions

View file

@ -5,6 +5,9 @@
## Ciphers
* [Merkle Hellman Cryptosystem](https://github.com/TheAlgorithms/Ruby/blob/master/ciphers/merkle_hellman_cryptosystem.rb)
## Conversions
* [Temperature Conversions](https://github.com/TheAlgorithms/Ruby/blob/master/conversions/temperature_conversions.rb)
## Data Structures
* Arrays
* [Add Digits](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/add_digits.rb)
@ -19,6 +22,7 @@
* [Single Number](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/single_number.rb)
* [Sort Squares Of An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/sort_squares_of_an_array.rb)
* Strings
* [Anagram Checker](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/anagram_checker.rb)
* [Jewels And Stones](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/jewels_and_stones.rb)
* [Remove Vowels](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/remove_vowels.rb)
* [Two Sum](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/two_sum.rb)

View file

@ -0,0 +1,72 @@
# A ruby program for temperature conversions
module TemperatureConversion
# celsius -> kelvin = value of celsius + 273.15 => K
def self.celsius_to_kelvin(celsius_input)
kelvin_output = (celsius_input + 273.15).round(2)
puts "#{celsius_input}°C = #{kelvin_output}K"
rescue
puts "Error: Please provide number only!"
end
# kelvin -> celsius = vale of kelvin - 273.15 => °C
def self.kelvin_to_celsius(kelvin_input)
celsius_output = (kelvin_input - 273.15).round(2)
puts "#{kelvin_input}K = #{celsius_output}°C"
rescue
puts "Error: Please provide number only!"
end
# celsius -> fahrenheit = (value of celsius * 9 / 5) + 32 => °F
def self.celsius_to_fahrenheit(celsius_input)
fahrenheit_output = ((celsius_input * 9 / 5) + 32).round(2)
puts "#{celsius_input}°C = #{fahrenheit_output}°F"
rescue
puts "Error: Please provide number only!"
end
# fahrenheit -> celsius = (value of fahrenheit - 32) * 5 / 9 => °C
def self.fahrenheit_to_celsius(fahrenheit_input)
celsius_output = ((fahrenheit_input - 32) * 5 / 9).round(2)
puts "#{fahrenheit_input}°F = #{celsius_output}°C"
rescue
puts "Error: Please provide number only!"
end
# fahrenheit -> kelvin = [(value of fahrenheit - 32) * 5 / 9] + 273.15 => K
def self.fahrenheit_to_kelvin(fahrenheit_input)
kelvin_output = ((fahrenheit_input - 32) * 5 / 9).round(2).round(2)
puts "#{fahrenheit_input}°F = #{kelvin_output}K"
rescue
puts "Error: Please provide number only!"
end
# kelvin -> fahrenheit = [(value of kelvin - 32) * 5 / 9] + 273.15 => K
def self.kelvin_to_fahrenheit(kelvin_input)
fahrenheit_output = (((kelvin_input - 273.15) * 9 / 5) + 32).round(2).round(2)
puts "#{kelvin_input}K = #{fahrenheit_output}°F"
rescue
puts "Error: Please provide number only!"
end
end
# celsius <-> kelvin
TemperatureConversion.celsius_to_kelvin(20)
TemperatureConversion.kelvin_to_celsius(20)
# Invalid input
TemperatureConversion.kelvin_to_celsius("a")
# celsius <-> fahrenheit
TemperatureConversion.celsius_to_fahrenheit(-20)
TemperatureConversion.fahrenheit_to_celsius(68)
# Invalid input
TemperatureConversion.celsius_to_fahrenheit("abc")
# fahrenheit <-> kelvin
TemperatureConversion.fahrenheit_to_kelvin(60)
TemperatureConversion.kelvin_to_fahrenheit(-60)
# Invalid input
TemperatureConversion.fahrenheit_to_kelvin("60")

View file

@ -0,0 +1,45 @@
# Challenge name: Is anagram
#
# Given two strings s and t , write a function to determine
# if t is an anagram of s.
#
# Note:
# You may assume the string contains only lowercase alphabets.
#
# Follow up:
# What if the inputs contain unicode characters?
# How would you adapt your solution to such case?
#
# @param {String} s
# @param {String} t
# @return {Boolean}
#
# Approach 1: Sort and Compare
#
# Complexity analysis:
#
# Time Complexity: O(n log n). Assume that n is the length of s, sorting costs O(n log n), and comparing two strings costs O(n). Sorting time dominates and the overall time complexity is O(n log n).
# Space Complexity: O(1). Space depends on the sorting implementation which, usually, costs O(1) auxiliary space if heapsort is used.
#
def is_anagram(s, t)
return false if s.length != t.length
arr1 = s.split('').sort
arr2 = t.split('').sort
arr1 == arr2
end
s = 'anagram'
t = 'nagaram'
puts(is_anagram(s, t))
# => true
s = 'rat'
t = 'car'
puts(is_anagram(s, t))
# => false
s = 'a'
t = 'ab'
puts(is_anagram(s, t))
# => false