Skip to content

Instantly share code, notes, and snippets.

@antoniosmgatto
Last active January 2, 2021 16:03
Show Gist options
  • Save antoniosmgatto/747360c21b6d48d4496326b7ab1b3bae to your computer and use it in GitHub Desktop.
Save antoniosmgatto/747360c21b6d48d4496326b7ab1b3bae to your computer and use it in GitHub Desktop.

String

In this introductory challenge, your task is to use each of the above three methods to return the text Hello World and others!

def single_quote
  # single quote string here
    'Hello World and others!'
end

def double_quote
  # Double quote string here
    "Hello World and others!"
end

def here_doc
  # Here doc string here
    <<-HERE
    Hello World and others!
    HERE
end

In this challenge, we practice setting the encoding information for some string of text using Ruby's Encoding methods. Write a function named transcode which takes a ISO-8859-1 encoded string as a parameter, converts it to an UTF8 encoded string, and returns the result.

# Enter your code here.
def transcode(str)
    str.force_encoding("UTF-8")
end

In this challenge, your task is to code a serial_average method which is described below:

  • It takes a fixed width string in format: SSS-XX.XX-YY.YY. SSS is a three digit serial number, XX.XX and YY.YY are two digit numbers including up to two decimal digits.
  • It returns a string containing the answer in format SSS-ZZ.ZZ where SSS is the serial number of that input string, and ZZ.ZZ is the average of XX.XX and YY.YY.
  • All numbers are rounded off to two decimal places.
def serial_average(fixed)
    sss = fixed[0,3]
    xx = fixed[4,5].to_f
    yy = fixed[10,5].to_f
    number = ((xx+yy)/2).round(2)
    return "#{sss}-#{number}"
end

Write the method count_multibyte_char which takes a string as input and returns the number of multibyte characters (byte size > 1) in it.

def count_multibyte_char(str)
    count = 0
    str.each_char { |char| count+=1 if char.bytes.size > 1 }
    count
end

In this challenge, your task is to code a process_text method, which takes an array of strings as input and returns a single joined string with all flanking whitespace and new lines removed. Each string has to be separated by a single space.

def process_text(strings)
    strings.map(&:strip).join(" ")
end

Array

Array collections offer various ways to access their elements.

def element_at(arr, index)
    arr[index]
end

def inclusive_range(arr, start_pos, end_pos)
    arr[start_pos..end_pos]
end

def non_inclusive_range(arr, start_pos, end_pos)
    arr[start_pos...end_pos]
end

def start_and_length(arr, start_pos, length)
    arr[start_pos, length]
end

Here are some other ways to access array objects in Ruby.

def neg_pos(arr, index)
    # return the element of the array at the position `index` from the end of the list
    arr[-index]
end

def first_element(arr)
    # return the first element of the array
    arr.first
end

def last_element(arr)
    # return the last element of the array
    arr.last
end

def first_n(arr, n)
    # return the first n elements of the array
    arr.take(n)
end

def drop_n(arr, n)
    # drop the first n elements of the array and return the rest
    arr.drop(n)
end

Arrays provide a variety of methods that allow to add elements to them.

def end_arr_add(arr, element)
    # Add `element` to the end of the Array variable `arr` and return `arr`
    arr.push(element)  
end

def begin_arr_add(arr, element)
    # Add `element` to the beginning of the Array variable `arr` and return `arr`
    arr.unshift(element)
end

def index_arr_add(arr, index, element)
    # Add `element` at position `index` to the Array variable `arr` and return `arr`
    arr.insert(index, element)
end

def index_arr_multiple_add(arr, index)
    # add any two elements to the arr at the index
    arr.insert(index, 1, 2)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment