Ctrl K

Glue the Ends Together

Easy
A row of cards with positive integers cards lies on the table. Until no cards are left, repeat: - if there are at least two cards, take the leftmost and the rightmost, write the digits of the right one after the digits of the left one (so 15 and 49 make 1549), and add that number to the score; - if a single card remains, add its number to the score. Cards that are taken leave the table. The score starts at zero. Return the final score.
Example 1
cards=[3, 40, 6, 21] → 727

First the outer cards 3 and 21 glue into 321, then 40 and 6 into 406. In total 321 + 406 = 727.

Example 2
cards=[12, 5, 9] → 134

12 and 9 give 129, and the lone card 5 is added as it is: 129 + 5 = 134.

Example 3
cards=[8] → 8
Constraints
  • 1 ≤ len(cards) ≤ 1000
  • 1 ≤ cards[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMirror and Negate a Bitmap EasyMerge Equal Neighbours EasyMatch-Three Cascade MediumTwo Gardeners and Their Refills MediumSplit Text at the Marks Medium
loading Python…
Input
cards=[3, 40, 6, 21]
Expected
727

Run checks the first 3 tests in your browser. Submit checks all 116 tests on the server, hidden ones included.