Skip to content

Instantly share code, notes, and snippets.

@wcargen
Forked from neurosaurus/Week 01 - Day 01 Homework
Created September 24, 2013 16:02
Show Gist options
  • Save wcargen/6687061 to your computer and use it in GitHub Desktop.
Save wcargen/6687061 to your computer and use it in GitHub Desktop.
# Building Ruby Familiarity
# In this exercise you will take a first look at some common commands in Ruby
# The idea here is to build familiary with Ruby syntax
# This will likely be the first time you've seen some of these commands
# Just type them in and see the displayed output
# Steps:
# 1. Open up a new terminal window
# 2. Launch irb
# 3. Paste the line of code into irb or pry
# 4. Press return
# 5. Write down the displayed output
# 6. Repeat steps 3-5 for all lines below in order
first_ans = 7 / 2
puts first_ans
print first_ans
first_ans = 7 / 2.to_f
first_ans = 7.to_f / 2
first_ans = 7 / 2.0
first_ans = 7.0 / 2
first_ans = first_ans.round * 4
def get_character(full_string, index)
full_string[index]
end
message_string = "oicdlcwhejkeenoemrstuo"
character_1 = get_character(message_string, 4)
character_2 = get_character(message_string, 7)
character_3 = get_character(message_string, 2)
message_array = [character_1, character_2]
message_array.push(character_2)
message_array.pop()
message_array.push(character_3)
message_array
puts message_array
print message_array
value_float_1 = Math.sin(Math::PI / 2)
value_float_2 = Math.cos(Math::PI)
value_float_3 = (value_float_1 + value_float_2)
value_integer_1 = (value_float_1 + value_float_2).to_i
value_float_1 = value_float_1 * 4
value_float_2 *= 5
value_float_2 = value_float_2.abs
value_integer_1 += 8
value_float_4 = value_integer_1 * 3
value_float_3 -= 1
number_array = [value_float_1, value_float_2, value_float_3, value_float_4]
number_array.push(first_ans)
number_array.unshift(value_integer_1)
number_array.push(value_integer_1)
number_array.unshift( Math.sqrt(36) )
number_array.delete_at(5)
number_array.push( [19, 21, 6, 3, 1] )
number_array.flatten!
number_array.each { |current_index| puts get_character(message_string, current_index) }
#--------------------------------------------------------------------------------------------------
#If you're feeling really comfortable with the above exercises,
#here are some more challenging ones.
#*** THE FOLLOWING QUESTIONS BELOW CAN BE COMPLETED IN SUBLIME.
#USE IRB TO PLAY AROUND WITH THE EXERCISES.
#WRITE YOUR ANSWERS BELOW ***
#1. What is the problem with the following code?
#my variable = "Mr. White"
#2. Set the variable x to be 5 and the variable y to be 7 and
#write 5 + 7 = 12 with string interpolation.
#3. Update the code, so it can run successfully:
#band = "Blink" + 182
#4. Return the number of characters in the string "The Godfather"
#5. Prime numbers
#Print out the prime numbers between 1 and 100.
#6. Write a program which asks for a person's first name, then last, then age.
#Finally, it should greet the person using their full name and age.
#7. What is the difference between if and unless
#8. Write a program which asks for a person's favorite color. Have your program
#add another color to the list, then suggest the result a better favorite color.
#String Exercise:
#9. Use a pattern and consolidate the amount of characters into a simplified string.
#input pattern = "AAASSSDDDDRDDSASSDDDSSSAD"
#output pattern = 3A3S4DR2DSA2S3D3SAD
#If a character is represented more than once,
#append the number of occurences in front of it.
#If a chracter is only represented once, just put that letter.
#Hint Use a loop to iterate over a string.
#You can reference characters in a string similar to an array.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment