#C14840. Digit Sum Transformation

    ID: 44534 Type: Default 1000ms 256MiB

Digit Sum Transformation

Digit Sum Transformation

You are given a list of tokens (separated by spaces) which are intended to represent integers. Your task is to implement several functions that operate on the list as follows:

  • sum_of_digits(n): Given an integer \(n\), compute the sum of its digits. For example, \(\text{sum_of_digits}(123)=1+2+3=6\).
  • create_sum_list(int_list): Given a list of integers, return a new list where every integer is replaced by the sum of its digits.
  • create_digit_sum_dict(int_list): Given a list of integers, return a mapping (dictionary) where each key is the original integer and its value is the sum of its digits. When printing the dictionary, keys should be printed in increasing order.
  • highest_digit_sum_in_list(int_list): Given a list that may contain integers (or non-integer tokens), return the integer or integers which have the highest digit sum. In case of a tie, output all such integers in the order they appear in the input. If the input list is empty, output the error message "Error: Empty list". If any element is not a valid integer, output "Error: List contains non-integer elements".

The program should read input from stdin and print the outputs to stdout as follows (if no error occurs):

  1. First line: The output of create_sum_list — each number printed and separated by a single space.
  2. Second line: The output of create_digit_sum_dict — each key:value pair printed as key:value separated by a single space, with keys sorted in increasing order.
  3. Third line: The output of highest_digit_sum_in_list — the integer(s) with the highest digit sum printed and separated by a single space.

If an error is encountered, only the error message should be printed.

inputFormat

The input consists of a single line containing tokens separated by spaces. The tokens are intended to be integers. For example:

7 21 123

The list may be empty or contain invalid (non-integer) tokens. In these cases, output the corresponding error message.

outputFormat

If the input is valid, output three lines:

  1. The first line is the list obtained by replacing each integer with the sum of its digits.
  2. The second line is a series of key:value pairs corresponding to the original integer and its digit sum, with keys sorted in increasing order.
  3. The third line contains the integer(s) with the highest digit sum (if there is a tie, print all such integers in the order they appear in the original list).

If an error condition is encountered, output the corresponding error message:

  • If the input list is empty: Error: Empty list
  • If any token is not a valid integer: Error: List contains non-integer elements
## sample
7 21 123
7 3 6

7:7 21:3 123:6 7

</p>