Range operators (.. and ...) in Ruby - Time & Space Complexity
We want to understand how the time it takes to work with ranges changes as the size of the range grows.
How does using the two types of range operators affect the number of steps the program takes?
Analyze the time complexity of the following code snippet.
n = 10
range_inclusive = (1..n).to_a
range_exclusive = (1...n).to_a
range_sum = 0
range_inclusive.each do |num|
range_sum += num
end
This code creates two ranges: one including the last number, one excluding it, then sums all numbers in the inclusive range.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each number in the inclusive range.
- How many times: The loop runs once for each number from 1 up to n.
As the range size grows, the number of steps to sum all numbers grows at the same rate.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 steps |
| 100 | 100 steps |
| 1000 | 1000 steps |
Pattern observation: Doubling the input roughly doubles the work done.
Time Complexity: O(n)
This means the time to complete the task grows in direct proportion to the size of the range.
[X] Wrong: "Using the exclusive range operator (...) makes the loop run fewer times in a way that changes the overall time complexity."
[OK] Correct: The exclusive range just stops one number earlier, so the number of steps changes slightly but still grows linearly with input size.
Understanding how range operators affect loops helps you explain how your code scales with input size, a key skill in many programming tasks.
"What if we used a nested loop inside the range loop? How would the time complexity change?"
Practice
.. and ...?Solution
Step 1: Understand the
The..operator..operator creates a range that includes the last value in the sequence.Step 2: Understand the
The...operator...operator creates a range that excludes the last value, stopping just before it.Final Answer:
..includes the end value,...excludes the end value -> Option AQuick Check:
Range inclusion =.., exclusion =...[OK]
- Confusing which operator includes or excludes the end
- Thinking both operators behave the same
- Assuming ranges create arrays automatically
- Mixing up usage with different data types
Solution
Step 1: Identify the operator for excluding the end
The...operator excludes the end value, so1...5means 1, 2, 3, 4.Step 2: Check other options
1..5includes 5,(1..5).exclude_end?is a method call, not range creation, and1-5is subtraction.Final Answer:
range = 1...5 -> Option BQuick Check:
Exclude end uses three dots...[OK]
- Using two dots when exclusion is needed
- Confusing method calls with range syntax
- Using subtraction instead of range
- Forgetting parentheses for method calls
puts (1..3).to_a.inspect
puts (1...3).to_a.inspect
Solution
Step 1: Convert (1..3) to array
The range1..3includes 3, so(1..3).to_ais[1, 2, 3].Step 2: Convert (1...3) to array
The range1...3excludes 3, so(1...3).to_ais[1, 2].Final Answer:
[1, 2, 3] [1, 2] -> Option DQuick Check:
Two dots include end, three dots exclude end [OK]
- Assuming both ranges include the end
- Mixing up which operator excludes the end
- Forgetting to convert range to array for output
- Misreading the output format
for i in 1..5
puts i
end
Solution
Step 1: Analyze the range used in the loop
The range1..5includes 5, so the loop prints 1 through 5.Step 2: Identify how to exclude 5
To exclude 5, use the...operator:1...5which stops before 5.Final Answer:
The range should use...to exclude 5 -> Option CQuick Check:
Exclude end with three dots...[OK]
- Using two dots when exclusion is needed
- Thinking loop variable needs declaration
- Misunderstanding for loop syntax in Ruby
- Placing puts outside the loop incorrectly
n. Which range operator should you use inside the method to achieve this?Solution
Step 1: Understand the requirement
The method should return numbers from 1 up to but not includingn.Step 2: Choose the correct range operator
Using1...ncreates a range that excludesn, exactly what is needed.Step 3: Evaluate other options
1..nincludesn,1...(n+1)includesn,1...n-1excludesn-1too, which is incorrect.Final Answer:
Use1...nto excluden-> Option AQuick Check:
Three dots exclude the end value [OK]
- Using two dots and including the end value
- Subtracting 1 manually instead of using three dots
- Misplacing parentheses causing wrong range
- Using three dots with arithmetic causing off-by-one
Ready to prove it?
Pass the test and this pattern moves unknown
