Exercise

parents
Pipeline #1178 failed with stages
in 0 seconds
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "ruby-debug",
"request": "launch",
"name": "Launch File",
"program": "${workspaceFolder}/${command:AskForProgramName}",
"programArgs": [],
"useBundler": false
}
]
}
\ No newline at end of file
def gold_room
puts "This room is full of gold. How much do you take?"
print "> "
choice = $stdin.gets.chomp
# this line has a bug, so fix it
if choice.to_i.to_s == choice
how_much = choice.to_i
else
dead("Man, learn to type a number.")
end
if how_much < 50
puts "Nice, you're not greedy, you win!"
exit(0)
else
dead("You greedy bastard!")
end
end
def bear_room
puts "There is a bear here."
puts "The bear has a bunch of honey."
puts "The fat bear is in front of another door."
puts "How are you going to move the bear?"
bear_moved = false
while true
print "> "
choice = $stdin.gets.chomp
if choice == "take honey"
dead("The bear looks at you then slaps your face off.")
elsif choice == "taunt bear" && !bear_moved
puts "The bear has moved from the door. You can go through it now."
bear_moved = true
elsif choice == "taunt bear" && bear_moved
dead("The bear gets pissed off and chews your leg off.")
elsif choice == "open door" && bear_moved
gold_room
else
puts "I got no idea what that means."
end
end
end
def cthulhu_room
puts "Here you see the great evil Cthulhu."
puts "He, it, whatever stares at you and you go insane."
puts "Do you flee for your life or eat your head?"
print "> "
choice = $stdin.gets.chomp
if choice.include? "flee"
start
elsif choice.include? "head"
dead("Well that was tasty!")
else
cthulhu_room
end
end
def dead(why)
puts why, "Good job!"
exit(0)
end
def start
puts "You are in a dark room."
puts "There is a door to your right and left."
puts "Which one do you take?"
print "> "
choice = $stdin.gets.chomp
if choice == "left"
bear_room
elsif choice == "right"
cthulhu_room
else
dead("You stumble around the room until you starve.")
end
end
gold_room()
\ No newline at end of file
=begin #Exercise 24: More Practice
puts "let's pratice everything."
puts 'You \'d need to know \'bout escapes with \\ that do \n newlines and \t tabs. '
poem = <<MP
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none.
MP
puts "--------------"
puts poem
puts "--------------"
five = 10 - 2 + 3 - 6
puts "This should be five: #{five}"
def secret_formula(started)
jelly_beans = started * 500
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans, jars, crates
end
start_point = 10000
beans, jars, crates = secret_formula(start_point)
puts "With a starting point of: #{start_point}"
puts "We'd have #{beans} beans, #{jars} jars, and #{crates} crates."
puts "We can also do that this way:"
puts "We'd have %s beans, %d jars, and %d crates." % secret_formula(start_point)
=end
#Exercise 25: Even More Practice
=begin module Ex25
# This function will break up words for us.
def Ex25.break_words(stuff)
words = stuff.split(' ')
return words
end
# Sorts the words.
def Ex25.sort_words(words)
return words.sort
end
# Prints the first word after shifting it off.
def Ex25.print_first_word(words)
word = words.shift
puts word
end
# Prints the last word after popping it off.
def Ex25.print_last_word(words)
word = words.pop
puts word
end
# Takes in a full sentence and returns the sorted words.
def Ex25.sort_sentence(sentence)
words = Ex25.break_words(sentence)
return Ex25.sort_words(words)
end
# Prints the first and last words of the sentence.
def Ex25.print_first_and_last(sentence)
words = Ex25.break_words(sentence)
Ex25.print_first_word(words)
Ex25.print_last_word(words)
end
# Sorts the words then prints the first and last one.
def Ex25.print_first_and_last_sorted(sentence)
words = Ex25.sort_sentence(sentence)
Ex25.print_first_word(words)
Ex25.print_last_word(words)
end
end
=end
#Exercise 29: What If
people = 20
cats = 30
dogs = 15
if people < cats
puts "Too many cats! The world is doomed!"
end
if people > cats
puts "Not many cats! The world is saved!"
end
if people < dogs
puts "The world is drooled on!"
end
if people > dogs
puts "The world is dry!"
end
dogs += 5
if people >= dogs
puts "People are greater than or equal to dogs."
end
if people <= dogs
puts "People are less than or equal to dogs."
end
if people == dogs
puts "People are dogs."
end
\ No newline at end of file
#Exercise 30: Else and If
=begin
people = 30
cars = 40
trucks = 15
if cars > people
puts "We should take the cars."
elsif cars < people
puts "We should not take the cars."
else
puts "We can't decide."
end
if trucks > cars
puts "That's too many trucks."
elsif trucks < cars
puts "Maybe we could take the trucks."
else
puts "We still can't decide."
end
if people > trucks
puts "Alright, let's just take the trucks."
else
puts "Fine, let's stay home then."
end
=end
#Exercise 31: Making Decisions
=begin puts "You enter a dark room with two doors. Do you go through door #1 or door #2?"
print "> "
door = $stdin.gets.chomp
if door == "1"
puts "There's a giant bear here eating a cheese cake. What do you do?"
puts "1. Take the cake."
puts "2. Scream at the bear."
print "> "
bear = $stdin.gets.chomp
if bear == "1"
puts "The bear eats your face off. Good job!"
elsif bear == "2"
puts "The bear eats your legs off. Good job!"
else
puts "Well, doing %s is probably better. Bear runs away." % bear
end
elsif door == "2"
puts "You stare into the endless abyss at Cthulhu's retina."
puts "1. Blueberries."
puts "2. Yellow jacket clothespins."
puts "3. Understanding revolvers yelling melodies."
print "> "
insanity = $stdin.gets.chomp
if insanity == "1" || insanity == "2"
puts "Your body survives powered by a mind of jello. Good job!"
else
puts "The insanity rots your eyes into a pool of muck. Good job!"
end
else
puts "You stumble around and fall on a knife and die. Good job!"
end
=end
#Exercise 32: Loops and Arrays
=begin the_count = [1, 2, 3, 4, 5, 6 ]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']
#loop for
for number in the_count
puts "This is count #{number}"
end
#each
fruits.each do |fruit|
puts "A fruit of type: #{fruit}"
end
#change
change.each {|i| puts "I got #{i}" }
#build list
elements = [7, 8]
the_count.each do|i|
puts "Adding #{i} to the list."
elements.push(i)
end
elements.each {|i| puts "Element was: #{i}" }
=end
#Exercise 33: While Loops
=begin
numbers = []
def numbers_list(i,x):
i = 0
while i < x:
print "At the top of i is %d" % i
numbers.append(i)
i += 1
print "Numbers now: ", numbers
print "At the bottom of i is %d" % i
return numbers
numbers_list(6,1)
print "the numbers: "
for num in numbers:
print num
number_list(2,7)
=end
#Exercise 34: Accessing Elements of Arrays
animals = ['bear' , 'ruby', 'peacock' , 'kangaroo' , 'whale' , 'platypus' ]
choice = 0;
while choice < 7
puts %Q{
--------------------------------------
\t\tMenu
Choose your choice
1.bear
2.ruby
3.peacock
4.kangaroo
5.whale
6.platypus"
7.exit
--------------------------------------
}
choice = $stdin.gets.to_i
if(choice == 1)
puts "The first (1st) animal is at 0 and is a bear.\nThe animal at 0 is the 1st animal and is a bear."
elsif(choice == 2)
puts "The first (2st) animal is at 1 and is a ruby.\nThe animal at 1 is the 2st animal and is a ruby."
elsif(choice == 3)
puts "The first (3st) animal is at 2 and is a peacock.\nThe animal at 2 is the 3st animal and is a peacock."
elsif(choice == 4)
puts "The first (4st) animal is at 3 and is a kangaroo.\nThe animal at 3 is the 4st animal and is a kangaroo."
elsif(choice == 5)
puts "The first (5st) animal is at 4 and is a whale.\nThe animal at 4 is the 5st animal and is a whale."
elsif(choice == 6)
puts "The first (6st) animal is at 5 and is a platypus.\nThe animal at 5 is the 6st animal and is a platypus."
elsif(choice == 7)
puts "Hope you will comeback soon"
else
puts "Please choose 1-->7"
end
end
=begin
#Exercise 4: Variables and Names
cars = 100
space_in_a_car = 4.0
drivers =30
passengers =90
car_not_driven = cars - drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers / cars_driven
puts "There are #{cars} cars available."
puts "There are only #{drivers} drivers available."
puts "There will be #{car_not_driven} empty cars today."
puts "We can transport #{carpool_capacity} peolple today."
puts "We have #{passengers} to carpool today."
puts "We need to put about #{average_passengers_per_car} in each car"
#Exercise 5: More Variables and Printing
name = 'Zed A. Shaw'
age = 35 # not a lie in 2009
height = 74 # inches
height_cm = height * 2.54
weight = 180 # lbs
weight_kg = weight / 2.2046
eyes = 'Blue'
teeth = 'White'
hair = 'Brown'
puts "Let's talk about #{name}."
puts "He's #{height_cm} centimets tall."
puts "He's #{weight_kg} kilograms heavy."
puts "Actually that's not too heavy."
puts "He's got #{eyes} eyes and #{hair} hair."
puts "His teeth are usually #{teeth} depending on the coffee."
# this line is tricky, try to get it exactly right
puts "If I add #{age}, #{height_cm}, and #{weight_kg} I get #{age + height_cm + weight_kg}"
#Exercise 6: Strings and Text
types_of_people = 10
x = "There are #{types_of_people} types of people."
binary = 'binary'
do_not = "don't"
y = "Those who know #{binary} and those who #{do_not}."
puts x
puts y
puts "I said: #{x}."
puts "I also said: '#{y}'."
hilarious = false
joke_evaluation = "Isn't that joke so funny?! #{hilarious}"
puts joke_evaluation
w = "This is the left side of..."
e = "a string with a right side."
puts w + e
puts "Mary had a little lamb."
puts "Its fleece was white as #{'snow'}."
puts "And everywhere that Mary went."
puts "." * 10 # what'd that do?
end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"
print end1 + end2 + end3 + end4 + end5 + end6
puts end7 + end8 + end9 + end10 + end11 + end12
puts %q{
There's something going on here.
With this weird quote
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
}
print "How old are you? "
age = gets.chomp
print "Where are u from? "
country = gets.chomp
puts "So, you're #{age} old, live #{country}"
print "Give me a number: "
number = gets.chomp.to_f
bigger = number * 10
puts "A bigger number is #{bigger}"
print "GIve me another number: "
another = gets.chomp
number = another.to_i
smaller = number / 10
puts "A small number is #{smaller}."
#Exercise 13: Parameters, Unpacking, Variables
first, second, third = ARGV
puts "Your first variable is: #{first}"
puts "Your second variable is: #{second}"
puts "Your third variable is: #{third}"
user_name = ARGV.first
prompt = '> '
puts "Hi #{user_name}."
puts "I'd like to ask you a few questions."
puts "Do u like me #{user_name}? ", prompt
likes = $stdin.gets.chomp
puts "Where u live #{user_name}? ", prompt
lives = $stdin.gets.chomp
puts "What kind of computer do you have? ", prompt
computer = $stdin.gets.chomp
puts %Q{
Alright, so you said #{likes} about liking me.
You live in #{lives}. Not sure where that is.
And you have a #{computer} computer. Nice.
}
filename = ARGV.first
txt = open(filename)
puts " Here's ur file #{filename}: "
print txt.read
print "Type the filename again: "
file_again = $stdin.gets.chomp
txt_again = open(file_again)
print txt_again.read
filename = ARGV.first
puts "We're going to erase #{filename}"
puts "If u dun want that, hit CTRL-C. "
puts "If u do want that, hit RETURN."
$stdin.gets
puts "opening the file..."
target = open(filename, 'w')
puts "Truncating the file. "
target.truncate(0)
puts "Now I'm going to ask you for three lines."
print "line 1: "
line1 = $stdin.gets.chomp
print "line 2: "
line2 = $stdin.gets.chomp
print "line 3: "
line3 = $stdin.gets.chomp
puts "I'm going to write these to the file."
target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")
puts "And finally, we close it."
target.close
=end
=begin
from_file, to_file = ARGV
puts "Copying from #{from_file} to #{to_file}"
in_file = open(from_file)
indata = in_file.read
puts "The input file is #{indata.length} bytes long"
puts "Does the output file exist? #{File.exist?(to_file)}"
puts "Ready, hit RETURN to continue, CTRL-C to abort."
$stdin.gets
out_file =open(to_file,'w')
out_file.write(indata)
puts "Alright, all done."
out_file.close
in_file.close
=end
# this one is like your scripts with ARGV
=begin
input_file = ARGV.first
def print_all(f)
puts f.read
end
def rewind(f)
f.seek(0)
end
def print_a_line(line_count, f)
puts "#{line_count}, #{f.gets.chomp}"
end
current_file = open(input_file)
puts "First let's print the whole file:\n"
print_all(current_file)
puts "Now let's rewind, kind of like a tape."
rewind(current_file)
puts "Let's print three lines:"
current_line = 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)
Exercise 21: Functions Can Return Something
def add(a, b)
puts "ADDING #{a} + #{b}"
return a + b
end
def subtract(a, b)
puts "SUBTRACTING #{a} - #{b}"
return a - b
end
def multiply(a, b)
puts "MULTIPLYING #{a} * #{b}"
return a * b
end
def divide(a, b)
puts "DIVIDING #{a} / #{b}"
return a / b
end
puts "Let's do some math with just functions!"
age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)
puts "Age: #{age}, Height: #{height}, Weight: #{weight}, IQ: #{iq}"
# A puzzle for the extra credit, type it in anyway.
puts "Here is a puzzle."
what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
puts "That becomes: #{what}. Can you do it by hand?"
=end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment