Skip to content

Instantly share code, notes, and snippets.

@abutler3
Created July 25, 2019 14:13
Show Gist options
  • Save abutler3/225d15563d461e9f2ea157fed506624f to your computer and use it in GitHub Desktop.
Save abutler3/225d15563d461e9f2ea157fed506624f to your computer and use it in GitHub Desktop.
Given an array of integers, can you find the sum of its elements?
Input Format
The first line contains an integer, , denoting the size of the array.
The second line contains space-separated integers representing the array's elements.
Output Format
Print the sum of the array's elements as a single integer.
// If variable is an array
// If array has all numbers
// Get all elements of array
// Add them together
// Print total
def get_array_sum(array)
array.is_a?(Array)
array.all? { |i| i.is_a?(Integer) }
sum = 0
array.each { |a| sum+=a }
print sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment