commit 793e2c2c5fe311090d82862cf149b5e6f5661b46 Author: Mikayla Dobson Date: Wed May 24 15:34:43 2023 -0500 some solutions for projects on odin project diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..d2403f1 --- /dev/null +++ b/Gemfile @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +# gem "rails" diff --git a/bubble_sort.rb b/bubble_sort.rb new file mode 100644 index 0000000..5c53a9e --- /dev/null +++ b/bubble_sort.rb @@ -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]) diff --git a/caesar_cipher.rb b/caesar_cipher.rb new file mode 100644 index 0000000..f1608e7 --- /dev/null +++ b/caesar_cipher.rb @@ -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) diff --git a/fizzbuzz.rb b/fizzbuzz.rb new file mode 100644 index 0000000..74a5040 --- /dev/null +++ b/fizzbuzz.rb @@ -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) diff --git a/stock_picker.rb b/stock_picker.rb new file mode 100644 index 0000000..2e79896 --- /dev/null +++ b/stock_picker.rb @@ -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]) diff --git a/substrings.rb b/substrings.rb new file mode 100644 index 0000000..db1fa3d --- /dev/null +++ b/substrings.rb @@ -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) diff --git a/tic_tac_toe/Board.rb b/tic_tac_toe/Board.rb new file mode 100644 index 0000000..19004f5 --- /dev/null +++ b/tic_tac_toe/Board.rb @@ -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 diff --git a/tic_tac_toe/Game.rb b/tic_tac_toe/Game.rb new file mode 100644 index 0000000..8293393 --- /dev/null +++ b/tic_tac_toe/Game.rb @@ -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 diff --git a/tic_tac_toe/Player.rb b/tic_tac_toe/Player.rb new file mode 100644 index 0000000..4154ef3 --- /dev/null +++ b/tic_tac_toe/Player.rb @@ -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 diff --git a/tic_tac_toe/main.rb b/tic_tac_toe/main.rb new file mode 100644 index 0000000..bc82ac2 --- /dev/null +++ b/tic_tac_toe/main.rb @@ -0,0 +1,6 @@ +require_relative "Board.rb" +require_relative "Player.rb" +require_relative "Game.rb" + +game = Game.new +game.play