Skip to content

Instantly share code, notes, and snippets.

@kangkyu
kangkyu / ruby-warrior-beginner.rb
Last active September 27, 2015 01:15
Ruby Warrior Level 6
class Player
def play_turn(warrior)
if warrior.feel.empty?
if warrior.health < 20
if warrior.health < @health
if warrior.look(:backward).any?(&:enemy?)
warrior.shoot!(:backward)
else
if warrior.health < 10 && warrior.feel(:backward).empty?
warrior.walk!(:backward)
@kangkyu
kangkyu / clock.rb
Last active September 27, 2015 01:33
Warm-Up 09-21-15
# http://exercism.io/exercises/ruby/clock/readme
class Clock
def self.at(hours=0, minutes=0)
new(hours, minutes)
end
def initialize(hours=0, minutes=0)
array = minutes.divmod 60
def fizz_buzz(limit)
1.upto(limit).reduce("") do |string, n|
string << (n % 3 == 0 && n % 5 == 0 ? "FizzBuzz" : n % 3 == 0 ? "Fizz" : n % 5 == 0 ? "Buzz" : n.to_s)
string << "\n"
end
end
# today's problem: print this page out as plain text. No images, no html tags, no javascript
# write a function that prints that https://www.yahoo.com/ as plain text
# hint: pipe that page through linx
# exec('lynx -dump yahoo.com')
File.open("yahoo_screen.txt", "w") do |f|
f.write %x[lynx -dump yahoo.com]
end
# write a function called "x" that takes a string, and, if it has an odd number of characters, prints it out diagonally twice -- lower left to upper right, and upper left to lower right -- and those two strings share their middle character
def x(string)
if string.length.odd?
array = []
string.length.times do |row|
array << []
string.length.times do |col|
if col == row || col == (string.length - row - 1)
array[row][col] = string[col]
def proper(phrase)
words = phrase.split(" ")
words.map {|word| cap_if_selected(word)}.join(" ")
end
def cap_if_selected(word)
word = word.each_char.map {|char| down_char(char)}.join
word[0] = up_char(word[0]) if selected(word)
word
end
# Write a program that processes the string s = "Welcome to the forum.\nHere you can learn Ruby.\nAlong with other members.\n" a line at a time, using all that we have learned so far.
s = "Welcome to the forum.\nHere you can learn Ruby.\nAlong with other members.\n"
s.each_line.with_index 1 do |line, line_num|
puts "Line #{line_num}: #{line}"
end
# Write a method leap_year?. It should accept a year value from the user, check whether it's a leap year, and then return true or false. Note: a century year, like 1900 and 2000, is a leap year only if it is divisible by 400.
def leap_year?(year)
year % 400 == 0 || year % 100 != 0 && year % 4 == 0
end
[1998, 1996, 2000, 2100].each do |year|
minutes = if leap_year?(year)
366 * 24 * 60
else
# write a function called find_primes, where you pass in an integer (like 100),
# and your function will calculate all the prime numbers from 1 - 100
# https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
def find_primes(integer)
current_ary = (2..integer).to_a
current_idx = 0
while current_idx < current_ary.length
def fizz(number)
number % 3 == 0 ? "fizz" : ""
end
def buzz(number)
number % 5 == 0 ? "buzz" : ""
end
def fizz_buzz(number)
(number.to_s + " " + fizz(number) + buzz(number))