Skip to content

Instantly share code, notes, and snippets.

View haritak's full-sized avatar

Charitakis Yannis haritak

View GitHub Profile
#!/usr/bin/env ruby
# https://www.theodinproject.com/lessons/ruby-linked-lists
class Node
attr_accessor :value
attr_accessor :next_node
def initialize( *options )
case options
in [Object=>value, Node=>next_node]
def factorial( n )
return 1 if n <= 1
return n*factorial( n-1 )
end
puts factorial(2)
puts factorial(3)
puts factorial(4)
module Enumerable
# Your code goes here
def my_each_with_index
counter = 0
self.each do |element|
yield element, counter
counter += 1
end
end
require 'json'
class Hangman
def Hangman.restore_game
hg = Hangman.new
saved_games = Dir.glob "*.hangman"
if saved_games.length>0
puts "Would you like to restore a game ?"
if gets.strip.downcase.include? "y"
puts saved_games
puts "Enter filename to restore: "
# https://www.theodinproject.com/lessons/ruby-event-manager
require 'csv'
require 'google/apis/civicinfo_v2'
require 'erb'
civic_info = Google::Apis::CivicinfoV2::CivicInfoService.new
civic_info.key = 'AIzaSyClRzDqDh5MsXwnCWi0kOiiBivP6JsSyBw'
template_letter = File.read 'form_letter.html.erb'
template = ERB.new template_letter
#!/usr/bin/env ruby
# https://www.theodinproject.com/lessons/ruby-mastermind
module Human
def get_name
puts "Hello #{self.class}. How should I call you ?"
gets.strip
end
#!/usr/bin/env ruby
# https://www.theodinproject.com/lessons/ruby-tic-tac-toe
class Board
def initialize
@cells = [
"...",
"...",
"...",
#!/usr/bin/env ruby
# https://www.theodinproject.com/lessons/ruby-bubble-sort
def bubble_sort!( data )
not_sorted_part = data.length-1
not_sorted_part.downto( 1 ) do | last_index |
last_index.times do | index |
data[ index ], data[ index+1] = data[index+1], data[index] if data[ index ] > data[ index+1]
end
#!/usr/bin/env ruby
# https://www.theodinproject.com/lessons/ruby-stock-picker
def stock_picker( prices )
profits_all = []
prices.each_with_index do | buy_price, buy_index |
profits_when_bying_at_index = prices[buy_index..].each_with_index.map do | sell_price, sell_index |
{buy: buy_index, sell: buy_index + sell_index, profit: sell_price - buy_price}
end
#!/usr/bin/env ruby
# https://www.theodinproject.com/lessons/ruby-sub-strings
def substrings( sentence, dictionary )
result = Hash.new(0)
sentence.downcase.split(" ").each do |word|
dictionary.each do |d|
if word.include? d
result[ d ] += 1