|
1 | 1 | """
|
2 | 2 | The super digit problem is defined as follows:
|
3 |
| -Given an integer represented as a string and an integer k, |
4 |
| -the goal is to find the super digit of the number formed by concatenating |
| 3 | +Given an integer n represented as a string and an integer k, |
| 4 | +the goal is to find the super digit of the number formed by concatenating |
5 | 5 | the integer n k times.
|
6 | 6 |
|
7 | 7 | The super digit of a number is defined recursively:
|
|
14 | 14 |
|
15 | 15 | from __future__ import annotations
|
16 | 16 |
|
17 |
| - |
18 |
| -def superDigit(n: str, k: int) -> int: |
| 17 | +def super_digit(n_str: str, repetitions: int) -> int: |
19 | 18 | """
|
20 |
| - Computes the super digit of a number formed by concatenating n k times. |
| 19 | + Computes the super digit of a number formed by concatenating n_str repetitions times. |
21 | 20 |
|
22 | 21 | Parameters:
|
23 |
| - n (str): The string representation of the integer. |
24 |
| - k (int): The number of times to concatenate n. |
| 22 | + n_str (str): The string representation of the integer. |
| 23 | + repetitions (int): The number of times to concatenate n_str. |
25 | 24 |
|
26 | 25 | Returns:
|
27 | 26 | int: The super digit of the concatenated number.
|
28 | 27 |
|
29 |
| - >>> superDigit("148", 3) |
| 28 | + >>> super_digit("148", 3) |
30 | 29 | 3
|
31 |
| - >>> superDigit("9875", 4) |
| 30 | + >>> super_digit("9875", 4) |
32 | 31 | 8
|
33 |
| - >>> superDigit("123", 3) |
| 32 | + >>> super_digit("123", 3) |
34 | 33 | 9
|
35 | 34 | """
|
36 | 35 |
|
37 |
| - # Calculate the initial sum of the digits in n |
38 |
| - digit_sum = sum(int(digit) for digit in n) |
39 |
| - |
40 |
| - # Multiply the sum by k |
41 |
| - total_sum = digit_sum * k |
42 |
| - |
| 36 | + # Calculate the initial sum of the digits in n_str |
| 37 | + digit_sum = sum(int(digit) for digit in n_str) |
| 38 | + |
| 39 | + # Multiply the sum by repetitions |
| 40 | + total_sum = digit_sum * repetitions |
| 41 | + |
43 | 42 | # Recursive function to find the super digit
|
44 | 43 | while total_sum >= 10:
|
45 | 44 | total_sum = sum(int(digit) for digit in str(total_sum))
|
46 |
| - |
| 45 | + |
47 | 46 | return total_sum
|
48 | 47 |
|
49 |
| - |
50 |
| -if __name__ == "__main__": |
51 |
| - # Read input and split it into n and k |
| 48 | +if __name__ == '__main__': |
| 49 | + # Read input and split it into n_str and repetitions |
52 | 50 | first_multiple_input = input().rstrip().split()
|
53 |
| - |
54 |
| - n = first_multiple_input[0] # n as a string |
55 |
| - k = int(first_multiple_input[1]) # k as an integer |
56 |
| - |
57 |
| - # Call the superDigit function and print the result |
58 |
| - result = superDigit(n, k) |
| 51 | + |
| 52 | + n_str = first_multiple_input[0] # n as a string |
| 53 | + repetitions = int(first_multiple_input[1]) # repetitions as an integer |
| 54 | + |
| 55 | + # Call the super_digit function and print the result |
| 56 | + result = super_digit(n_str, repetitions) |
59 | 57 | print(result)
|
0 commit comments