Convert Binary Number in a Linked List to Integer
Real-world version: Reading a Punch Chain as a NumberThe statement here is our own: the same inputs, outputs and technique.
A tally machine punches cards and clips them one behind another. Every card carries a single hole state, either 0 or 1, plus a clip fastened to the card behind it, and the card at the end has an empty clip. Nothing in the machine records how many cards a chain holds.
Because the harness passes plain JSON, the chain reaches you as punches, listing the hole states in clip order starting at the front card. There is always at least one card.
Read the chain as a number written in base two, with the front card carrying the largest place value and the card at the end carrying the ones. Return that number.
Examples
Example 1
- Input
- punches = [1, 1, 0]
- Output
- 6
The front card is worth four, the middle one two, and the card at the end nothing, so the chain reads six.
Example 2
- Input
- punches = [1, 0, 0, 1]
- Output
- 9
Only the front card and the card at the end are punched, worth eight and one, which adds to nine.
Example 3
- Input
- punches = [0, 0, 1]
- Output
- 1
The leading unpunched cards carry no weight, so only the ones place counts.
Constraints
- 1 <= punches.length <= 30
- 0 <= punches[i] <= 1
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def get_decimal_value(punches: list[int]) -> int:public int getDecimalValue(int[] punches)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 14 test cases: 14 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's