This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
first_num = 2 | |
last_num = 20 | |
def prime? num | |
return false if num <=1 | |
2.upto(num/2).each { |y| return false if num % y == 0} | |
true | |
end | |
first_num.upto(last_num) do |temp| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
arr = ["apple", "bananna", "pineapple", "orange"] | |
puts arr.sort_by {|x| x.length} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s = "I am Ravi Suraneni" | |
def count_word_characters(s) | |
h = Hash.new(0) | |
s.downcase.each_char do |char| | |
next unless char =~/\w/ | |
h[char] += 1 | |
end | |
h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
student = {"First Name:" => "John", | |
"Last Nmae:" => "Doe", | |
"Subjects:" => "Physics, Chemistry, Maths", | |
"Marks:" => "Physics: 75, Maths: 85, Chemistry: 98" | |
} | |
student.each {|key, value| puts key, student[key]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
num = 1000 | |
def prime? num | |
return false if num <=1 | |
2.upto(num/2).each { |y| return false if num % y == 0} | |
true | |
end | |
temp = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print "Enter your string: " | |
string = gets.chomp | |
if string == string.reverse | |
puts "#{string} is a pallindrome" | |
else | |
puts "#{string} is not a pallindrome" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Factorial: n! = n*(n-1)*(n-2)*...1 | |
print "Enter a number: " | |
num = gets.chomp.to_i | |
fact = 1 | |
puts "Factorial for #{num} is: " | |
num.downto(1) {|x| fact *= x} | |
puts fact |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print "Enter first number here: " | |
first_num = gets.chomp.to_i | |
print "Enter second number here: " | |
second_num = gets.chomp.to_i | |
puts "GCD of #{first_num} and #{second_num} is: " | |
temp=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print "Enter a number here: " | |
num = gets.chomp.to_i | |
prime = true | |
2.upto(math.sqrt(num)).each do |x| | |
if num % x == 0 | |
not_prime = false | |
break | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x=10 | |
x.downto(1) {|n| print n, " "} | |
x=8 | |
y=12 | |
puts x.gcd(y) | |
x=8 | |
puts x.next |
NewerOlder