ruby strings exercise
I finished the strings exercise from App Academy's JumpStart preparation curriculum.
I want to focus on the "hard" set of questions. The solutions were pretty straight forward for each problem but I got hung up on a few simple but crucial points in the last two prompts.
# Write a method that returns an array of the digits of a non-negative integer in descending order and as strings, e.g., descending_digits(4291) #=> ["9", "4", "2", "1"]
def descending_digits(int)
int.to_s.split("").sort.reverse
# your code goes here
end
The prompt here tells us that we're given an integer and that we want to (1) convert the integer into a string, (2) split the string into an array, (3) sort it, and (4) reverse the order so it's values are listed from low to high.
# Write a method that converts an array of ten integers into a phone number in the format "(123) 456-7890".
def to_phone_number(arr)
num_str = arr.sort.join("")
phone_number = "(" + num_str[1..3] + ") " + num_str[4..6] + "-" + num_str[7..9] + num_str[0]
# your code goes here
end
This one was a little more tough since I didn't account for a few simple things that were happening. We're asked to produce a phone number in the format (123) 456-7890 given an array that has those numbers. So I started off thinking we can take the array and sort then join together it's elements to get a string. This was actually the right thought process but here's how I defined my phone_number variable initially:
phone_number = "(" + num_str[0..2] + ") " + num_str[3..5] + "-" + num_str[6..9]
This, however, would give me the output:
(012) 345-6789
The reason being is that our string, after being sorted, it is actually "012346789"; I hadn't accounted for the 0 at the beginning of the string.
Edit:
I realized the problem actually gave us an array whose numbers were already sorted in the format [1,2,3,4,5,6,7,8,9,0]. This means we could've also solved it by simply joining the elements in the proper format:
chunk_one = arr[0..2].join
chunk_two = arr[3..5].join
chunk_three = arr[6..9].join
phone_number = "(" + chunk_one + ")" + " " + chunk two + "-" + chunk_three
# Write a method that returns the range of a string of comma-separated integers, e.g., str_range("4,1,8") #=> 7
def str_range(str)
array_ordered = str.split(",").sort
range = array_ordered[-1].to_i - array_ordered[0].to_i
# your code goes here
end
My mistake here was also a simple one; I forgot that when we split a string into an array, the elements in the array are all in there as strings. Since all the string values in the array are numerical (as opposed to numerical and alphabetical), we can sort the array without any issues.
My problem, however, came when we had to access elements from the array and use them in a mathematical operation to obtain the range. I tried to to set the range equal to array_ordered[-1] - array_ordered[0]. Unfortunately, we can't do that since the element being accessed is a string; we have to convert it into an integer, which I then did, with the to_i method.