Last active
April 21, 2016 21:11
-
-
Save tgaff/18cc95a9d77a191571c2f56f148c7121 to your computer and use it in GitHub Desktop.
sample questions
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
# create a function first that returns the first item in an array | |
# Name: first | |
# Input: [-2, 3, 74, 'math', 'popcorn', 17] | |
# Output: -2 | |
def first(arr) | |
arr.first | |
end | |
# Create a function final that returns the final element in an array | |
# Name: final | |
# Input: [-2, 3, 74, 'math', 'popcorn', 17] | |
# Output: 17 | |
def final(arr) | |
arr.last | |
end | |
# Create a function that returns 'give me cookie!' or nil depending on whether it received a cookie | |
# Name: cookie_monster | |
# Input: 'donut' | |
# Output: 'give me cookie!' | |
# Input: 'cookie' | |
# Output: nil | |
def cookie_monster(food) | |
'give me cookie!' unless food == 'cookie' | |
end | |
# create a function count_chars | |
# given a string, e.g. "cat" | |
# return an integer, e.g. 3 | |
# given nothing, return 0 | |
def count_chars(str="") | |
str.length | |
end | |
#create a function char_counts, that, given a list of strings | |
# returns a list of character counts | |
# input: ["cat", "hi", "hello"] | |
# output: [3, 2, 5] | |
# inputs = ["cat", "hi", "hello"] | |
def char_counts(list) | |
list.map { |str| str.length } | |
end | |
# create a function `punctuate` | |
# that takes a string | |
# and adds a period if none exists | |
# input: "hello world" | |
# output: "hello world." | |
#0 | |
def punctuate(str) | |
str[-1] == “.” ? str : str + “.” | |
end | |
# `terminal_punctuation?` | |
# create a function `terminal_punk?` | |
# that takes a string | |
# and detects if the last character is a period | |
# input: "hello" | |
# output: false | |
def punctuation?(string) | |
string[-1] == "." | |
end | |
# create a function `full_stop` | |
# that converts a terminal punctuation mark into a period only when one exists | |
# "hello?" --> "hello." | |
# "hello!" --> "hello." | |
# "hello." --> "hello." | |
# "hello" --> "hello" | |
# BONUS: don't hardcode the punctuation, maybe I /do/ want ";"" | |
# if doint the bonus maybe I should be able to tell the method what punctuations I want to change | |
def full_stop(str, punk=["?", "!"]) | |
if punk.include?(str[-1]) | |
str[0..-2] + "." | |
else | |
str | |
end | |
end | |
# create a function `char_freqs` | |
# that counts the frequency of characters within a string | |
# input: "Once Upon A Time" | |
# output: { | |
# o: 2, n: 2, c: 1, e: 2, u: 1, | |
# p: 1, a: 1, t: 1, i: 1, m: 1 | |
# } | |
def char_freqs(string, output={}) | |
string.downcase.gsub(' ', '').each_char{|x| output[x] ? output[x] += 1 : output[x] = 1} | |
output | |
end | |
################################################################ | |
# Write a function that takes an integer and returns the next integer | |
# Name: plus_one | |
# Input: 9 | |
# Output: 10 | |
def plus_one(num) | |
num + 1 | |
end | |
# Write a function that takes a character and returns the next character. | |
# Name: next_letter | |
# Input: 'u' | |
# Output: 'v' | |
def next_letter(letter) | |
letter.next | |
end | |
# Write a function that takes an age. Return a string based upon the age. | |
# For 0-1: 'baby', for 2-4: 'toddler' for 5-12: child, for 13-19: teenager, | |
# 20: "smoking and voting but no drinking", 21-66: 'adult', 67+: 'senior citizen' | |
# Name: age_bracket | |
# Input: 13 | |
# Output: 'teenager' | |
def age_bracket(age) | |
case age | |
when 0..1 | |
'baby' | |
when 2..4 | |
'toddler' | |
when 5..12 | |
'child' | |
when 13..19 | |
'teenager' | |
when 20 | |
'smoking and voting but no drinking' | |
when 21..66 | |
'adult' | |
when 67..Float::INFINITY | |
'senior citizen' | |
end | |
end | |
# Create a function that returns the cube of a number | |
# Name: to_the_third_power | |
# Input: 4 | |
# Output: 64 | |
def to_the_third_power(x) | |
x**3 | |
end | |
# Given a list of goats (an array of hashes), return an array of goat names. | |
# name: call_by_name | |
# Input: (see goat_list) | |
# Output: ["Jorge", "Maude", "Patrick", "Denise", "Regina George"] | |
goat_list = [ | |
{ | |
name: 'Jorge', | |
charisma: 3, | |
color: 'white', | |
}, | |
{ | |
name: 'Maude', | |
charisma: 4, | |
color: 'mauve', | |
}, | |
{ | |
name: 'Jared', | |
charisma: 8, | |
color: 'grey', | |
}, | |
{ | |
name: 'Jessica', | |
charisma: 8, | |
color: 'brown mottled', | |
}, | |
{ | |
name: 'Regina George', | |
charisma: 8, | |
color: 'white-grey', | |
}, | |
] | |
def call_by_name(goat_list) | |
goat_list.map { |g| g[:id] } | |
end | |
# Given a list of names, return an array of names that includes only the names that start with J. | |
# name: call_by_j | |
# Input: ["Jorge", "Maude", "Jared", "Jessica", "Regina George"] | |
# Output: ["Jorge", "Jared", "Jessica"] | |
def call_by_j(names) | |
names.select { |n| n[0] == 'J' } | |
end | |
# Now worry about case too |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment