some solutions for projects on odin project
This commit is contained in:
5
Gemfile
Normal file
5
Gemfile
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
source "https://rubygems.org"
|
||||||
|
|
||||||
|
# gem "rails"
|
||||||
17
bubble_sort.rb
Normal file
17
bubble_sort.rb
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
### BUBBLE SORT
|
||||||
|
def bubble_sort(input_array)
|
||||||
|
left = 0
|
||||||
|
|
||||||
|
until left + 1 == input_array.length
|
||||||
|
right = left + 1
|
||||||
|
if input_array[left] > input_array[right]
|
||||||
|
input_array[left], input_array[right] = input_array[right], input_array[left]
|
||||||
|
bubble_sort(input_array)
|
||||||
|
end
|
||||||
|
left += 1
|
||||||
|
end
|
||||||
|
|
||||||
|
input_array
|
||||||
|
end
|
||||||
|
|
||||||
|
print bubble_sort([4,3,78,2,0,2])
|
||||||
22
caesar_cipher.rb
Normal file
22
caesar_cipher.rb
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
### CAESAR CIPHER
|
||||||
|
Alphabet = 'abcdefghijklmnopqrstuvwxyz'
|
||||||
|
|
||||||
|
def caesar_cipher(input, shift)
|
||||||
|
unless shift.is_a? Numeric and input.is_a? String
|
||||||
|
raise TypeError("Receive invalid input")
|
||||||
|
end
|
||||||
|
|
||||||
|
input.downcase!
|
||||||
|
|
||||||
|
array_from_input = Array.new(input.length)
|
||||||
|
result = ''
|
||||||
|
for i in 0..(input.length - 1)
|
||||||
|
start = Alphabet.index(input[i])
|
||||||
|
after_shift = start ? Alphabet[(start + shift % 26)] : -1
|
||||||
|
result += after_shift == -1 ? input[i] : Alphabet[after_shift]
|
||||||
|
end
|
||||||
|
|
||||||
|
result
|
||||||
|
end
|
||||||
|
|
||||||
|
print caesar_cipher("Doing stuff with Ruby", 5)
|
||||||
15
fizzbuzz.rb
Normal file
15
fizzbuzz.rb
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
### FIZZBUZZ
|
||||||
|
def fizzbuzz(limit)
|
||||||
|
i = 0
|
||||||
|
until i == limit do
|
||||||
|
i += 1
|
||||||
|
puts "#{i.to_s} #{i % 3 == 0 ? "Fizz" : ''}#{i % 5 == 0 ? "Buzz" : ''}\n"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def fizzbuzz_one_line(limit)
|
||||||
|
(1..limit).each { |i| puts "#{i.to_s} #{i % 3 == 0 ? "Fizz" : ''}#{i % 5 == 0 ? "Buzz" : ''}\n" }
|
||||||
|
end
|
||||||
|
|
||||||
|
# fizzbuzz(20)
|
||||||
|
fizzbuzz_one_line(20)
|
||||||
26
stock_picker.rb
Normal file
26
stock_picker.rb
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
### STOCK PICKER
|
||||||
|
def stock_picker(stock_prices)
|
||||||
|
left = 0
|
||||||
|
right = 1
|
||||||
|
highest = 0
|
||||||
|
solution = [nil, nil]
|
||||||
|
|
||||||
|
until left == stock_prices.length - 1
|
||||||
|
until right == stock_prices.length - 1
|
||||||
|
right += 1
|
||||||
|
temp = stock_prices[right] - stock_prices[left]
|
||||||
|
if temp > highest
|
||||||
|
highest = temp
|
||||||
|
solution[0] = left
|
||||||
|
solution[1] = right
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
left += 1
|
||||||
|
right = left + 1 unless left + 1 == stock_prices.length
|
||||||
|
end
|
||||||
|
|
||||||
|
solution
|
||||||
|
end
|
||||||
|
|
||||||
|
print stock_picker([17,3,6,9,15,8,6,1,10])
|
||||||
24
substrings.rb
Normal file
24
substrings.rb
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
def substrings(str_input, dictionary)
|
||||||
|
unless str_input.is_a?(String) and dictionary.is_a?(Array)
|
||||||
|
raise TypeError("Invalid type provided")
|
||||||
|
end
|
||||||
|
|
||||||
|
result = {}
|
||||||
|
|
||||||
|
all_words = str_input.downcase.gsub(/[^0-9a-z ]/i, '').split(" ")
|
||||||
|
|
||||||
|
dictionary.each do |key|
|
||||||
|
all_words.each do |word|
|
||||||
|
if word.include?(key.downcase)
|
||||||
|
result[key] = result.has_key?(key) ? result[key] + 1 : 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
result
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
dictionary = ["below","down","go","going","horn","how","howdy","it","i","low","own","part","partner","sit"]
|
||||||
|
|
||||||
|
print substrings("Howdy partner, sit down! How's it going?", dictionary)
|
||||||
78
tic_tac_toe/Board.rb
Normal file
78
tic_tac_toe/Board.rb
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
class Board
|
||||||
|
attr_accessor :turn, :squares, :update_square
|
||||||
|
|
||||||
|
def initialize(turn = 1)
|
||||||
|
@turn = turn
|
||||||
|
@squares = [
|
||||||
|
[" ", " ", " "],
|
||||||
|
[" ", " ", " "],
|
||||||
|
[" ", " ", " "]
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
print "\n
|
||||||
|
1: #{@squares[0][0]} | 2: #{@squares[0][1]} | 3: #{@squares[0][2]} \n
|
||||||
|
---------------------\n
|
||||||
|
4: #{@squares[1][0]} | 5: #{@squares[1][1]} | 6: #{@squares[1][2]} \n
|
||||||
|
---------------------\n
|
||||||
|
7: #{@squares[2][0]} | 8: #{@squares[2][1]} | 9: #{@squares[2][2]} \n
|
||||||
|
"
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_square(square, symbol)
|
||||||
|
# translate a numbered square onto a matrix
|
||||||
|
row = ((square - 1) / 3).ceil()
|
||||||
|
column = (square - 1) % 3
|
||||||
|
success = false
|
||||||
|
|
||||||
|
if @squares[row][column] != " "
|
||||||
|
puts "Square is already taken."
|
||||||
|
puts "Please try again."
|
||||||
|
else
|
||||||
|
@squares[row][column] = symbol
|
||||||
|
success = true
|
||||||
|
end
|
||||||
|
|
||||||
|
success
|
||||||
|
end
|
||||||
|
|
||||||
|
def check_for_winner
|
||||||
|
winner = nil
|
||||||
|
|
||||||
|
# check whether all squares in a row match
|
||||||
|
for row in @squares
|
||||||
|
if row.all? { |square| square == "X" }
|
||||||
|
if !winner
|
||||||
|
winner = "X"
|
||||||
|
end
|
||||||
|
elsif row.all? { |square| square == "O" }
|
||||||
|
if !winner
|
||||||
|
winner = "O"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# check whether all columns match
|
||||||
|
if not winner
|
||||||
|
for column in 0..2
|
||||||
|
if @squares[column].all? { |square| square == "X" }
|
||||||
|
winner = "X"
|
||||||
|
elsif @squares[column].all? { |square| square == "O" }
|
||||||
|
winner = "O"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# check for diagonal matches
|
||||||
|
if not winner
|
||||||
|
if (@squares[0][0] == "X" or @squares[0][0] == "O") and @squares[0][0] == @squares[1][1] and @squares[1][1] == @squares[2][2]
|
||||||
|
winner = @squares[0][0]
|
||||||
|
elsif (@squares[0][2] == "X" or @squares[0][2] == "O") and @squares[0][2] == @squares[1][1] and @squares[1][1] == @squares[2][0]
|
||||||
|
winner = @squares[0][2]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
winner
|
||||||
|
end
|
||||||
|
end
|
||||||
70
tic_tac_toe/Game.rb
Normal file
70
tic_tac_toe/Game.rb
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
class Game
|
||||||
|
def initialize
|
||||||
|
puts "Welcome to Tic Tac Toe!"
|
||||||
|
puts "Player 1, what is your name?"
|
||||||
|
player_one_name = gets
|
||||||
|
|
||||||
|
puts "Player 2, what is your name?"
|
||||||
|
player_two_name = gets
|
||||||
|
|
||||||
|
@playerOne = Player.new(player_one_name, "X")
|
||||||
|
@playerTwo = Player.new(player_two_name, "O")
|
||||||
|
@statusMessage = ""
|
||||||
|
@board = Board.new()
|
||||||
|
@winner = nil
|
||||||
|
@turn = 1
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
print "
|
||||||
|
Player one: #{@playerOne.name}\n
|
||||||
|
Player two: #{@playerTwo.name}\n
|
||||||
|
Current turn: #{@turn}\n
|
||||||
|
#{@statusMessage}
|
||||||
|
#{@board.to_s}
|
||||||
|
"
|
||||||
|
end
|
||||||
|
|
||||||
|
def play
|
||||||
|
until @winner
|
||||||
|
self.turn()
|
||||||
|
end
|
||||||
|
|
||||||
|
@statusMessage = "#{@statusMessage + "\nPress 'x' to exit"}"
|
||||||
|
print self.to_s
|
||||||
|
|
||||||
|
until gets.chomp == "x"
|
||||||
|
print self.to_s
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def turn()
|
||||||
|
# clear the terminal on each turn
|
||||||
|
puts "\e[H\e[2J"
|
||||||
|
print self.to_s
|
||||||
|
|
||||||
|
puts "Player #{@turn % 2 == 0 ? @playerTwo.name : @playerOne.name}, please select a square. (1-9)"
|
||||||
|
player_input = gets.chomp.to_i
|
||||||
|
|
||||||
|
unless player_input.is_a?(Numeric) and player_input.between?(1, 9)
|
||||||
|
@statusMessage = "Invalid input. Expected a number between 1 and 9."
|
||||||
|
self.turn()
|
||||||
|
end
|
||||||
|
|
||||||
|
move_success = @board.update_square(player_input, @turn % 2 == 0 ? "O" : "X")
|
||||||
|
|
||||||
|
if not move_success
|
||||||
|
@statusMessage = "Invalid input. This square is already taken."
|
||||||
|
self.turn()
|
||||||
|
end
|
||||||
|
|
||||||
|
winner_this_turn = @board.check_for_winner
|
||||||
|
|
||||||
|
if winner_this_turn
|
||||||
|
@winner = (winner_this_turn == "X" ? @playerOne.name : @playerTwo.name)
|
||||||
|
@statusMessage = "#{@winner} wins!"
|
||||||
|
else
|
||||||
|
@turn += 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
10
tic_tac_toe/Player.rb
Normal file
10
tic_tac_toe/Player.rb
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
class Player
|
||||||
|
attr_accessor :turn_active
|
||||||
|
attr_reader :name, :symbol, :turn_active
|
||||||
|
|
||||||
|
def initialize(name, symbol)
|
||||||
|
@name = name
|
||||||
|
@symbol = symbol
|
||||||
|
@turn_active = false
|
||||||
|
end
|
||||||
|
end
|
||||||
6
tic_tac_toe/main.rb
Normal file
6
tic_tac_toe/main.rb
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
require_relative "Board.rb"
|
||||||
|
require_relative "Player.rb"
|
||||||
|
require_relative "Game.rb"
|
||||||
|
|
||||||
|
game = Game.new
|
||||||
|
game.play
|
||||||
Reference in New Issue
Block a user