Find Cloned Soldiers

PythonBeginner
Practice Now

Introduction

In this challenge, we will be tasked with finding all the clone soldiers in a clone army parade formation. The clone soldiers are uniquely identified by a number within a specific range, and they can be represented by a square matrix. Our goal is to count the number of clones for each ID and return the statistical result in a dictionary format. The solution should be implemented in the count_clone_soldier(matrix: List[List[str]]) method in the count_clone_soldier.py file.

Find Cloned Soldiers

Challenge Description

In the distant galaxy, there is a galactic empire that possesses a powerful clone army. The challenge is to find all the clone soldiers in the clone army parade formation of the galactic empire.

Challenge Details

Each clone soldier in the galactic empire is uniquely identified by a number from 10000000 to 1000ffff. Clones and their originals share the same ID.

The clone army parade formation can be represented by a square matrix, for example:

[['10000000', '10000012', '1000000d', '1000000d', '10000002'],
['10000004', '10000011', '10000017', '1000000b', '1000000f'],
['10000016', '1000000d', '10000018', '10000012', '10000011'],
['10000001', '1000000c', '10000008', '10000013', '10000000'],
['10000019', '10000000', '1000000e', '10000003', '10000004']]

The challenge is to find all the clone soldiers from the matrix and count the number of actual clones for each ID. Since clones and their originals share the same ID, an ID that appears N times in the matrix consists of one original and N-1 clones. Therefore, the clone count for an ID is its total occurrences minus one. Finally, return the statistical result in the dictionary format { 'ID': clone_count }, where the keys in the dictionary should be ordered in ascending order based on the ID. For example, for the matrix above, the statistical result of clone soldiers is as follows:

{
'10000000': 2,
'10000004': 1,
'1000000d': 2,
'10000011': 1,
'10000012': 1,
}

This result indicates that for ID 10000000 (which appears 3 times in the matrix), there are 2 clone soldiers (3 total - 1 original). Similarly, for ID 10000004 (which appears 2 times), there is 1 clone soldier (2 total - 1 original). IDs that appear only once (e.g., 10000002) have zero clones and should not be included in the final dictionary.

Challenge Requirements

  1. Make sure the count_clone_soldier.py file exists in the ~/project directory.
  2. Implement the challenge in the count_clone_soldier(matrix: List[List[str]]) method in the count_clone_soldier.py file.
  3. Do not modify the specified file name, class name, method definition, and method name.
  4. Do not modify the inheritance relationship of the class, and the file path.
  5. Code can only be written in the designated area, i.e., the count_clone_soldier.py file.

Summary

Congratulations! You have completed the Find Cloned Soldiers challenge. You can practice more labs in LabEx to improve your skills.

✨ Check Solution and Practice