Numeric literal formats in Swift - Time & Space Complexity
Let's see how the time it takes to read and use different numeric literals changes as the numbers get bigger.
We want to know how the program's work grows when using various numeric formats.
Analyze the time complexity of the following code snippet.
let decimal = 12345
let binary = 0b11000000111001
let octal = 0o30071
let hexadecimal = 0x3039
print(decimal)
print(binary)
print(octal)
print(hexadecimal)
This code defines numbers in different formats and prints them.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Converting each numeric literal from its format to a usable number.
- How many times: Once per literal, four times total here.
As the number of digits in the literal grows, the work to convert it grows too.
| Input Size (digits) | Approx. Operations |
|---|---|
| 5 | 5 steps to read digits |
| 10 | 10 steps to read digits |
| 20 | 20 steps to read digits |
Pattern observation: The work grows directly with the number of digits in the number.
Time Complexity: O(n)
This means the time to process a numeric literal grows in a straight line with the number of digits it has.
[X] Wrong: "All numeric literals take the same time to process no matter their size or format."
[OK] Correct: Larger numbers or different formats need more steps to read each digit, so they take more time.
Understanding how numeric literals are processed helps you explain how programs handle data efficiently, a useful skill in many coding situations.
"What if we changed the numeric literals to include underscores for readability? How would the time complexity change?"
Practice
Solution
Step 1: Understand binary literal format in Swift
Binary literals start with0bfollowed by 0s and 1s.Step 2: Check each option
0b1010uses0bprefix correctly.0x1010is hexadecimal, C is invalid, A is octal.Final Answer:
0b1010 -> Option AQuick Check:
Binary literals start with 0b = D [OK]
- Confusing binary (0b) with hexadecimal (0x)
- Placing the b suffix after the number
- Using octal prefix 0o for binary
Solution
Step 1: Understand underscore usage in numeric literals
Underscores can be placed anywhere between digits to improve readability.Step 2: Check each option for correct grouping
1_000_000groups digits in thousands correctly. Others have irregular grouping which is allowed but uncommon and confusing.Final Answer:
1_000_000 -> Option DQuick Check:
Use underscores to separate thousands = A [OK]
- Placing underscores at the start or end of number
- Using inconsistent grouping that confuses readers
- Thinking underscores change the value
let number = 1.5e2 print(number)
Solution
Step 1: Understand scientific notation in Swift
1.5e2 means 1.5 x 10², which equals 150.0.Step 2: Evaluate the print output
Printing the number shows the decimal value 150.0, not the literal string.Final Answer:
150.0 -> Option AQuick Check:
1.5e2 = 150.0 [OK]
- Thinking print shows the literal string '1.5e2'
- Confusing e2 with e-2 (which divides)
- Assuming integer output instead of floating-point
let value = 0b1021
Solution
Step 1: Check digits allowed in binary literals
Binary literals only allow digits 0 and 1 after the 0b prefix.Step 2: Analyze the given literal
The literal contains digit 2, which is invalid for binary (only 0s and 1s allowed).Final Answer:
Binary literals cannot contain digits other than 0 or 1 -> Option BQuick Check:
Binary digits only 0 or 1 = C [OK]
- Thinking underscores are mandatory
- Confusing binary prefix 0b with hexadecimal 0x
- Assuming floating-point literals can be binary
Solution
Step 1: Convert decimal 255 to hexadecimal
255 decimal equals FF in hexadecimal, so0xFFis correct.Step 2: Convert decimal 255 to octal
255 decimal equals 377 in octal, so0o377is correct.Final Answer:
0xFF and 0o377 -> Option CQuick Check:
255 decimal = 0xFF hex & 0o377 oct [OK]
- Confusing octal digits with decimal digits
- Using decimal digits after 0x prefix
- Mixing up hexadecimal and octal prefixes
Ready to prove it?
Pass the test and this pattern moves unknown
