Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions 01-power.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# -----------------------------------------------------------------------------------
# 1.Power
# Write a method power which takes two integers (base and exponent) and returns the
# base raised to the power of exponent. Do not use Ruby’s **operator for this!
#
# This code works with Integers and Negative values. unfortunately I need more time
# to make it to work with decimals, but coming soon.
# -----------------------------------------------------------------------------------

# Method Power

def power()

# Tells user what this program does.
puts "This program calculate the exponents"
puts "You can use Positive & Negative numbers"
puts

# Request input from user and save it on variables.
puts "Please provide Base number"
base_num = gets.chomp.to_i
puts
puts "Please provide the Exponent number"
expo_num = gets.chomp.to_i
puts

# Nested method with the calculations
def power(bn, en)

# Calculations with Negative values
if en < 0
holder = 1
en.abs.times do
holder *= bn
end
holder = 1.0 / holder

# Calculations with positive numbers
else
holder = 1
en.times do
holder *= bn
end
end

# return value to main method
return holder
end

# Prints results of calculations.
puts "#{base_num} to the #{expo_num} power is: #{power(base_num, expo_num)}"

end

# Calling the method.
power()

# ---------------------------------------------
# Test of program run
#
# This program calculate the exponents
# You can use Positive & Negative numbers
#
# Please provide Base number
# 5
#
# Please provide the Exponent number
# 5
#
# 5 to the 5 power is: 3125
# --------------------------------------------
46 changes: 46 additions & 0 deletions 02-factorial.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# --------------------------------------------------------------------------------
# 2.Factorial
# Write a method factorial which takes a number and returns the product of every
# number up to the current number multiplied together.
# --------------------------------------------------------------------------------


# Factorial

def factorial

# Provide information to user about what this sofware does.
puts "This software calculates the factorial of any integer."
puts "Note: if you enter a negative value it will convert it to positive."
puts

# Requesting input from user
puts "Please provide number to factor:"
number = gets.chomp.to_i.abs
puts

# Calculates the factoria of given input
total = 1
for x in 1..number
total *= x
end

# Prints results of factorial to user.
puts "The Factorial value of #{number} is: #{total}"

end

factorial()


# ---------------------------------------------------------------------------
# Test of Program run

# This software calculates the factorial of any integer
# Note: if you enter a negative value it will work with the absolute value

# Please provide number to factor:
# 7

# The Factorial value of 7 is: 5040
# ---------------------------------------------------------------------------
102 changes: 102 additions & 0 deletions 03-uniques.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# ------------------------------------------------------------------------------------
# 3. Uniques
# Write a method uniques which takes an array of items and returns the array without
# any duplicates. Don’t use Ruby’s uniq method!
#
# The following solution is Dynamic and Interactive, you will be prompted to provide:
# size of the array, maximum value, and minimum value, whit this information the
# software will create and populate an array with the provided specifications, then
# it will print it so you can see it. after that it will delete all duplicates and
# presented only the UNIQUE values in an organize order.
#
# Because things happen, in case that you mix the Maximun and Minimum values it will
# catch it and fix it.
# ------------------------------------------------------------------------------------

# Method Uniques

def uniques ()

# Inform user on what this program do.
puts "This program will create a list of random numbers"
puts "using information provided by you."
puts

# Requesting user for input and save it on variables
puts "Please provide how many elements on the list:"
places = gets.chomp.to_i
puts
puts "Please provide Minimu value:"
min = gets.chomp.to_i
puts
puts "Please provide Maximum value:"
max = gets.chomp.to_i
puts

# Fail-Safe: check user input for mistakes and fix them.
# also inform user about the error and the changes made.
if min > max
puts "It seems that you mix the Maximum and Minimu values"
puts "don't worry we got you cover, we just swtich them"
holder = max
max = min
min = holder
puts "now the values are Maximum: #{max}, Minimu: #{min}"
puts
end


# -------- Ligic sequenses -------------

# Create and pupulate a random array with user inputs.
# Also shows the new created array to user
numbers = Array.new(places) { rand(min..max) }
puts "The random Array generated:"
p numbers
puts

# Rearrange the array in numerical sequence
numbers.sort!

# Creates a new array to storage non duplicate values
singles = []

# This loop will push non duplicated values to the new array.
places.times do |x|
next if numbers[x] == singles.last
singles.push(numbers[x])
end

# Inform and show user unique and organized values
puts "Unique and Organize values:"
p singles

end


# calling the method
uniques()


# ----------------------------------------------------------------------------------------
# Example of software run
#
# Please provide how many elements on the array:
# 25
#
# Please provide Minimu value:
# 12
#
# Please provide Maximum value:
# 4
#
# It seems that you mix the Maximum and Minimu values
# don't worry we got you cover, we just swtich them
# now the values are Maximum: 12, Minimu: 4
#
# The random Array generated:
# [6, 4, 11, 12, 4, 5, 10, 10, 10, 9, 7, 8, 12, 12, 5, 9, 11, 11, 5, 8, 5, 6, 10, 9, 12]
#
# Unique and Organize values:
# [4, 5, 6, 7, 8, 9, 10, 11, 12]
# ----------------------------------------------------------------------------------------
59 changes: 59 additions & 0 deletions 04-combinations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# -----------------------------------------------------------------
# 4. Combinations
# Write a method combinations which takes two arrays of strings
# and returns an array with all of the combinations of the items
# in them, listing the first items first.
# -----------------------------------------------------------------


# Method Combinations
def combinations

# Creating arrays ans variables
arr_1 = ["on", "in"]
arr_2 = ["to", "rope"]
arr_3 = []
arr_4 = []
count = 0

# Printing arrays before combining
puts "This program will create all combinations of the following arrays:"
p arr_1
p arr_2
puts


# Pupulating array with prefixes
arr_1.length.times do |x|
arr_2.length.times do |y|
arr_3[count] = arr_1[x]
count += 1
end
end

# Duplicating values of array 2
arr_2 = arr_2 + arr_2


# Combining arrays
arr_3.length.times do |x|
arr_4[x] = arr_3[x] + arr_2[x]
end
puts "All the combinations are:"
p arr_4

end

combinations()


# --------------------------------------
# Test of program run
#
# This program will create all combinations of the following arrays:
# ["on", "in"]
# ["to", "rope"]
#
# All the combinations are:
# ["onto", "onrope", "into", "inrope"]
# --------------------------------------
42 changes: 42 additions & 0 deletions 05-primes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# -------------------------------------------------------------------------------------------------
# 5. Primes
# Write a method is_prime? which takes in a number and returns true if it is a prime number.
#
# In this case, “Huston we got a problem” basically there is NOT a test or formula that would find
# Prime Numbers with a 100% exactitude, no matter how fancy the formula you choose. for this
# reason I create a very basic way to find out if a number is prime, using a library call prime.
# -------------------------------------------------------------------------------------------------


# Method Prime?
def prime()

# calling an external library
require 'prime'

# Requesting user for input
puts "Please provide a number to see if is PRIME:"
number = gets.chomp.to_i
puts

# Logical test to find out if is prime.
if Prime.prime?(number) == true
puts "'YES' the number #{number} is PRIME"
else
puts "'NO' the number #{number} is NOT PRIME"
end
end


# Calling the method "prime()"
prime()


# -------------------------------------------------
# Program test run
#
# Please provide a number to see if is PRIME:
# 1997
#
# 'YES' the number 1997 is PRIME
# -------------------------------------------------
Loading