Last active
February 14, 2019 19:43
-
-
Save dev-elle-up/81f8e10baccb11e69d3183eee4b4457e to your computer and use it in GitHub Desktop.
Exercises from Data Transformations activity
This file contains hidden or 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
# ITERATING THROUGH ARRAYS | |
# For each of the following problems, write the code to solve the problem. Each solution should be structured as a method that takes an array as a parameter and returns the result. You should use the Array#each method to solve each problem. | |
# 1. You are given an array of String objects, stored in a variable called my_strings. You want to use this array to create a new array of Integer objects, which represents the length of each string. We want to store the resulting array in a variable called string_lengths. | |
my_strings = ["hey there", "this is the second string", "tacos", "good music", "study-group-pairs"] | |
# your_strings = ["Shamira", "Elle", "Grace"] | |
# input: string_array <- array of strings | |
# output: array of lengths of each string from inputs | |
def make_array_of_string_lengths(string_array) | |
this_array_of_lengths = [] | |
string_array.each do |string| | |
this_array_of_lengths << string.length | |
end | |
return this_array_of_lengths | |
end | |
string_lengths = make_array_of_string_lengths(my_strings) | |
puts "Array of lengths is: #{string_lengths}" | |
# your_string_lengths = make_array_of_string_lengths(your_strings) | |
# puts "Yours string lengths are: #{your_string_lengths}" | |
# 2. You are given an array of Integer objects, stored in a variable called my_nums. You want to use this array to create a new array which contains String objects, which say "even" or "odd" depending on the Integer in the original array. | |
my_nums = [42, 3, 3974, 6, 19, 789, 222] | |
def make_array_of_even_odd_strings(integer_array) | |
this_array_of_odd_or_even_strings = [] | |
integer_array.each do |integer| | |
if integer.odd? | |
this_array_of_odd_or_even_strings << "odd" | |
else | |
this_array_of_odd_or_even_strings << "even" | |
end | |
end | |
return this_array_of_odd_or_even_strings | |
end | |
array_of_strings_from_ints = make_array_of_even_odd_strings(my_nums) | |
puts "Array of even/odd strings is: #{array_of_strings_from_ints}" | |
# 3. You are given an array of String objects, stored in a variable called my_strings. You want to create a new array which only contains the strings from the original array that have a length that is smaller than 4. We want to store the resulting array in the original my_strings variable, overwriting the original value. | |
my_strings = ["the", "catapult", "as", "if", "has", "she", "felines and canines", "he", "them", "xe", "xir", "watermelon", "we"] | |
def make_array_of_strings_under_four_chars(string_array) | |
this_array_small_words = [] | |
string_array.each do |string| | |
if string.length < 4 | |
this_array_small_words << string | |
end | |
end | |
return this_array_small_words | |
end | |
my_strings = make_array_of_strings_under_four_chars(my_strings) | |
puts "Array of strings with less than four characters is: #{my_strings}" | |
# 4. You are given an array of String objects, stored in a variable called my_strings. You want to use this array to return the String that has the longest length. (In a tie, it should return the first String with the longest length) | |
my_strings = ["study", "homework", "keyboard", "Tense shoulders?? Try yoga!!!", "dual monitors rock", "Tense shoulders? Try a break.", "go", "you got this"] | |
def find_longest_string(array_of_strings) | |
this_longest_string = "" | |
array_of_strings.each do |string| | |
if string.length > this_longest_string.length | |
this_longest_string = string | |
end | |
end | |
return this_longest_string | |
end | |
longest_string = find_longest_string(my_strings) | |
puts "The string from this array with the longest length is: #{longest_string}" | |
# TRANSFORMING HASHES INTO ARRAYS | |
# 1. Given this array of String names, return an array of Hashes. Each Hash should have the keys name and id, which will represent their unique identifier in the form of an integer. The ids can start at 1 and then go up by one. | |
names = ["Grumpy", "Happy", "Sleepy", "Bashful", "Sneezy", "Dopey", "Doc"] | |
def make_array_of_hashes(an_array_item) | |
array_of_hashes = [] | |
i = 1 | |
an_array_item.each do |string| | |
this_hash = { | |
name: string, | |
id: i, | |
} | |
array_of_hashes << this_hash | |
i += 1 | |
end | |
return array_of_hashes | |
end | |
dwarf_names_and_ids = make_array_of_hashes(names) | |
puts "The hashes in the dwarfs array are:" | |
puts dwarf_names_and_ids | |
# 2. Given this array of String names and this array of String specialties, return an array of Hashes. Each Hash should have the keys name, specialty, and id, which will represent their unique identifier in the form of an integer. The ids can start at 1 and then go up by one. | |
names = ["Grumpy", "Happy", "Sleepy", "Bashful", "Sneezy", "Dopey", "Doc"] | |
specialties = ["grump", "smile", "nap", "blush", "sneeze", "goof", "contemplate"] | |
def make_array_of_hashes2(strings1, strings2) | |
array_of_hashes2 = [] | |
id = 1 | |
strings1.each do |array_item| | |
this_hash = { | |
name: array_item, | |
specialty: strings2[id - 1], | |
id: id, | |
} | |
id += 1 | |
array_of_hashes2 << this_hash | |
end | |
return array_of_hashes2 | |
end | |
names_and_specialties = make_array_of_hashes2(names, specialties) | |
puts "The hashes in the array of names and specialties are:" | |
puts names_and_specialties | |
# 3. Given these two arrays of strings, write code that returns an array of arrays. Each element in the larger array should have two elements: the first element is the name, and the second element is the specialty. | |
names = ["Grumpy", "Happy", "Sleepy", "Bashful", "Sneezy", "Dopey", "Doc"] | |
specialties = ["grump", "smile", "nap", "blush", "sneeze", "goof", "contemplate"] | |
def make_array_of_arrays(arr1, arr2) | |
array_of_arrays = [] | |
i = 0 | |
arr1.each do |element| | |
array_of_arrays << [element, arr2[i]] | |
i += 1 | |
end | |
return array_of_arrays | |
end | |
name_specialty_arrays = make_array_of_arrays(names, specialties) | |
puts "Here are the elements (arrays) inside of the larger \"name_specialty_arrays\" array: #{name_specialty_arrays}" | |
# MORE TRANSITIONS | |
# Given this array of hashes, iterate through the array and return an array of strings. Each string should be in the following format: Grumpy's specialty is to GRUMP, where Grumpy is replaced with each name and GRUMP is the specialty in capital letters. | |
characters = [{:name => "Grumpy", :specialty => "grump", :id => 1}, | |
{:name => "Happy", :specialty => "smile", :id => 2}, | |
{:name => "Sleepy", :specialty => "nap", :id => 3}, | |
{:name => "Bashful", :specialty => "blush", :id => 4}, | |
{:name => "Sneezy", :specialty => "sneeze", :id => 5}, | |
{:name => "Dopey", :specialty => "goof", :id => 6}, | |
{:name => "Doc", :specialty => "contemplate", :id => 7}] | |
def make_array_of_strings(array_of_hashes) | |
array_of_strings = [] | |
# i = 1 | |
array_of_hashes.each do |this_hash| | |
# until this_hash[:id] == i | |
# if this_hash[:id] == i | |
array_of_strings << "#{this_hash[:name]}'s specialty is to #{this_hash[:specialty].upcase}" | |
# i += 1 | |
# end | |
# end | |
end | |
return array_of_strings | |
end | |
array_of_dwarf_strings = make_array_of_strings(characters) | |
puts "The dwarfs specialties are as follows:" | |
puts array_of_dwarf_strings | |
# The question I have about this problem is: | |
# If the hash were not in order, how would we get the array to be in order by id number? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment