Skip to content

Instantly share code, notes, and snippets.

@carolineartz
carolineartz / challenge1_1_array_total.rb
Last active January 2, 2016 13:29 — forked from dbc-challenges/phase0_template_gist.rb
Exercise: Calculating the array total Write a method total which takes an Array of numbers as its input and returns their total (sum).
# PSEUDOCODE
# INPUT: an array of numbers
# OUPUT: sum of all numbers in array
# STEPS: start with a total of zero
# iterate over each element in the array
# for each element, add its value to the total
# return the total after there are no more elements
# INITIAL CODE:
@carolineartz
carolineartz / challenge1_1_letter_grade.rb
Last active January 2, 2016 23:49 — forked from dbc-challenges/phase0_template_gist.rb
Exercise: Calculate the letter grade of a series of grades Create a method get_grade that accepts an Array of test scores. Each score in the array should be between 0 and 100, where 100 is the max score. Compute the average score and return the letter grade as a String, i.e., 'A', 'B', 'C', 'D', 'E', or 'F'.
# PSEUDOCODE
# INPUT: array of grades
# OUPUT: a string representation of the letter grade that corresponds with
# the average score of the array of grades
# STEPS: Compute the average value of the values in teh input array
# divide the average by the length of input array => computes average score
# define letter grade distribution
# convert the average score to its corresponding letter grade
# return letter grade, as string
@carolineartz
carolineartz / challenge1_1_array_median.rb
Last active January 3, 2016 00:09 — forked from dbc-challenges/phase0_template_gist.rb
Exercise: Calculating the median of an array of numbers Write a method median which takes an Array of numbers as its input and returns the median value.
# CALCULATE THE MEDIAN OF AN ARRAY OF NUMBERS
# PSEUDOCODE
# INPUT: array of numbers
# OUPUT: the median number of the elements of the input array
# STEPS: sort the array
# determine if the array has one or two middle values by checking
# if the length is even or odd
# if there is an odd length, return the value of the middle element
# otherwise return the average of the two middle elements' values
@carolineartz
carolineartz / challenge1_1_pad_array1.rb
Last active January 3, 2016 00:09 — forked from dbc-challenges/phase0_template_gist.rb
Implement Array#pad and Array#pad!. Each method accepts a minimum size (non-negative integer) and an optional pad value as arguments. If the array's length is less than the minimum size, Array#pad should return a new array padded with the pad value up to the minimum size. If the minimum size is less than or equal to the length of the array, it s…
# CALCULATE THE MEDIAN OF AN ARRAY OF NUMBERS
# PSEUDOCODE
# INPUT: array object takes a non-negative integer minimum array size,
# optional value to pad which defaults to nil
# OUPUT: array object (#pad! returnning the same object while #pad
# will return a copy) which is padded conditional on its existing
# attributes and the passed parameters
# STEPS: for #pad! [#pad will do the same, but make a copy prior to executing
# anything with the potential to modify it] if the array is smaller than the
@carolineartz
carolineartz / challenge1_1_pad_array1.rb
Last active January 3, 2016 02:18 — forked from dbc-challenges/phase0_template_gist.rb
Implement Array#pad and Array#pad!. Each method accepts a minimum size (non-negative integer) and an optional pad value as arguments. If the array's length is less than the minimum size, Array#pad should return a new array padded with the pad value up to the minimum size. If the minimum size is less than or equal to the length of the array, it s…
# PSEUDOCODE
# INPUT: array object takes a non-negative integer minimum array size,
# optional value to pad which defaults to nil
# OUPUT: array object (#pad! returnning the same object while #pad
# will return a copy) which is padded conditional on its existing
# attributes and the passed parameters
# STEPS: for #pad! [#pad will do the same, but make a copy prior to executing
# anything with the potential to modify it] if the array is smaller than the
@carolineartz
carolineartz / Challenge1_1_BONUS_array_mode.rb
Last active January 3, 2016 02:19 — forked from dbc-challenges/phase0_template_gist.rb
Write a method mode which takes an Array of numbers as its input and returns an Array of the most frequent values. If there's only one most-frequent value, it returns a single-element Array.
def mode(array)
array_of_modes = []
#NOTE:
#Hash.new(0) differs from defining an empty hash by {} in that:
# 1. Hash.new(0) defines an empty hash with a default value of 0 for new keys with undefined values.
# 2. {} defines an empty hash that will default to nil for new keys with undefined values.
#Here, executing the block on nil will result in NoMethodError as + is undefined for nil, so must Hash.new(0)
##############CREATE OUR FREQUENCIES HASH#########
@carolineartz
carolineartz / Chellenge1_2_Regex.rb
Last active January 3, 2016 07:29 — forked from dbc-challenges/phase0_template_gist.rb
Ruby's String class has over 100 public methods that give Ruby programmers a remarkable power to process, manipulate, and transform textual data. About a dozen of those String methods use Regular Expressions in order to allow for high-powered string matching. (Try searching for "regular expression" on the ruby docs String page.)
#######################################PSEUDOCODE###################################
# INPUT: a string
# OUPUT & PSEUDOCODE
# Contains SSN => OUTPUT: Return boolean true/false
# Test string for chars in SSN format
# Return SSN => OUTPUT: Return SSN extracted from input, as string
# Call Contains SSN method-test if a SSN in string
# If contains SSN, extract and return.
@carolineartz
carolineartz / Challenge1_2_numbers_commas.rb
Last active January 3, 2016 07:29 — forked from dbc-challenges/phase0_week2_challenge_template.rb
Write a method separate_comma which takes an integer as its input and returns a comma-separated integer as a string
#######################################PSEUDOCODE###################################
# INPUT: a positive integer number formatted without commas
# OUPUT: the input number formatted with commas as a string
# [refactored solution]
# Convert the input number to string
# Reverse the string
# put a comma after each complete group of 3 characters
# reverse and return the string
@carolineartz
carolineartz / Challenge1_2_Fibonacci.rb
Last active January 3, 2016 08:39 — forked from dbc-challenges/phase0_week2_challenge_template.rb
Check, if a number i is part of the Fibonacci sequence.
#######################################PSEUDOCODE###################################
# INPUT: a positive integer
# OUPUT: boolean true/false if it is a fibonacci number
# generate fib numbers less than or equal to input
# test whether input one of the generated fib numbers
# return boolean true if is fib number
# return boolean false if not a fib number
@carolineartz
carolineartz / Challenge1_2_Refactoring_Cipher.rb
Last active January 3, 2016 19:59 — forked from dbc-challenges/phase0_week2_refactoring_challenge.rb
The N.S.A. just broke Kim Jong Un's cipher that he's been using to give instructions to his military commanders! We wrote the following program to decipher the messages. As the N.S.A.'s best programmer on staff, your job is to refactor the code.
########################IDENTIFY WHAT EACH LINE OF CODE IS DOING####################
def north_korean_cipher(coded_message)
input = coded_message.downcase.split("") #any uppercase chars to lowercase;
#splits the string at each char and
#returns an array of chars.
decoded_sentence = [] #creates empty array to push our deciphered message chars into
cipher = {"e" => "a", #deciphers the alphabetical chars into NK key => EN value
"f" => "b",
"g" => "c",