All problems
0995EasyLinked ListMath

Reading a Punch Chain as a Number

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1290Convert Binary Number in a Linked List to Integer

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def get_decimal_value(punches: list[int]) -> int:
Java
public int getDecimalValue(int[] punches)
September 7
Apply