This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# What is a data type? | |
# - Data types are the specified types of data stored by variables | |
# What are variables? | |
# - Variables are named objects created to store data in a location in the computers memory | |
# What do variables do? | |
# - Variables make it easier to work with data that we may not know yet, or may need to perform multiple operations | |
# - Generally it is best practice to name variables in a way that makes sense, and usually relates to the variables data type or the data itself | |
# Let's take a look at some examples | |
# In ruby, we output data by using the puts command: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Your challenge should you choose to accept it: | |
#**************** | |
#* Preface ** | |
#************************************************************************************************** | |
# Build a cipher to decode the message: "J\\wgdm\x1Ehq\x1D>mlhne-\x1EP`nsdm`dp\x1D,1sg!\x1E9'" ** | |
# This message is passed as an argument(input) to the encrypt method along with a key ** | |
# pp is better for testing than puts xD ** ** | |
# program inherently outputs the return of the encrypt funcion by calling it in a pp statement ;)** | |
#************************************************************************************************** | |
#**************************** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def encode(message, key) | |
c = key.to_s.split('').map { |e| e.to_i }.cycle #setup key cycle | |
letter_hash = Hash[('a'..'z').zip(1..26)] #hash containing 'a' => 1, 'b' => 2, and so on... | |
message.split('').map { |e| letter_hash.fetch(e,e) + c.next} #encode vlaues | |
end | |
def decode(code, key) | |
c = key.to_s.split('').map { |e| e.to_i }.cycle #setup key cycle | |
h = Hash[(1..26).zip('a'..'z')] #hash containing 1 => 'a', 2=> 'b', and so on... | |
code.map { |e| h.fetch((e - c.next),e) }.join('') # decode and fetch values from hash and join |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def make_stars(height) | |
star = '*' | |
space = ' ' | |
arr = [] | |
# Sets up non-formatted stars for given height | |
for i in 1..height | |
arr << [i == 1 ? star : star * ((i * 2) - 2)] | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This Program creates and outputs a formatted and colorized chirstmas tree with "Merry Christmas!" below it | |
# This code handles colorization of console output | |
class String | |
# colorization | |
def colorize(color_code) | |
"\e[#{color_code}m#{self}\e[0m" | |
end | |
def red |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# readFile sets up a new file right away by calling writeFile | |
# then stores the file as readfile and returns the formatFile method on the readfile | |
def readFile | |
writeFile | |
file = File.open(File.expand_path File.dirname(__FILE__) + "/output.txt") | |
readfile = file.read | |
file.close | |
readfile.formatFile | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# use the .to_dollars method on any string and get appropriate | |
# dollar amount returned with two decimal places rounded to the hundreths | |
# format is for us dollar | |
class String | |
def to_dollars | |
d = self.decimals_present | |
return "$" + self + "00" if d == 0 | |
return "$" + self + "0" if d == 1 | |
return "$" + self if d == 2 | |
return "$" + self.decimals[1] + '.' + self.round_decimals if d > 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sort(a, a_is_unsorted = true) | |
# (a_is_unsorted = false; (a.size - 1).times{ |i| j = i + 1; a[i], a[j], a_is_unsorted = a[j], a[i], true if a[i] > a[j]}) while a_is_unsorted | |
while a_is_unsorted do | |
a_is_unsorted = false | |
(a.length - 1).times{ |i| | |
a[i], a[i + 1], a_is_unsorted = a[i + 1], a[i], true if a[i] > a[i + 1] | |
} | |
end | |
a | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def self.output_to_file(item) | |
fname = "program-output.txt" | |
time = Time.new.strftime("%a, %b %d @ %I:%M:%S %p") | |
file = File.open(fname, "a") # a = append, w = wipe | |
file.puts "#{time}: #{item} \n\n" | |
file.close | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def count_words(s) | |
return nil if s.nil? || s.split.empty? | |
s.downcase.gsub(/[^a-z0-9'!\s-]/i, '').split(" ").each_with_object(Hash.new(0)){ |word,counts| counts[word] += 1 } | |
end |
NewerOlder