Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions recursions/...
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

57 changes: 57 additions & 0 deletions recursions/recursive_digit_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""

Check failure on line 1 in recursions/recursive_digit_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (INP001)

recursions/recursive_digit_sum.py:1:1: INP001 File `recursions/recursive_digit_sum.py` is part of an implicit namespace package. Add an `__init__.py`.
The super digit problem is defined as follows:
Given an integer represented as a string and an integer k,

Check failure on line 3 in recursions/recursive_digit_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

recursions/recursive_digit_sum.py:3:59: W291 Trailing whitespace
the goal is to find the super digit of the number formed by concatenating

Check failure on line 4 in recursions/recursive_digit_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

recursions/recursive_digit_sum.py:4:74: W291 Trailing whitespace
the integer n k times.

The super digit of a number is defined recursively:
- If the number has only one digit, that digit is the super digit.
- Otherwise, the super digit is the super digit of the sum of its digits.

For example, for n = "9875" and k = 4, the concatenated number is:
super_digit(9875987598759875), which can be reduced by summing its digits.
"""

from __future__ import annotations

def superDigit(n: str, k: int) -> int:

Check failure on line 17 in recursions/recursive_digit_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

recursions/recursive_digit_sum.py:15:1: I001 Import block is un-sorted or un-formatted

Check failure on line 17 in recursions/recursive_digit_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N802)

recursions/recursive_digit_sum.py:17:5: N802 Function name `superDigit` should be lowercase
"""
Computes the super digit of a number formed by concatenating n k times.

Parameters:
n (str): The string representation of the integer.
k (int): The number of times to concatenate n.

Returns:
int: The super digit of the concatenated number.

>>> superDigit("148", 3)
3
>>> superDigit("9875", 4)
8
>>> superDigit("123", 3)
9
"""

# Calculate the initial sum of the digits in n
digit_sum = sum(int(digit) for digit in n)

Check failure on line 38 in recursions/recursive_digit_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

recursions/recursive_digit_sum.py:38:1: W293 Blank line contains whitespace
# Multiply the sum by k
total_sum = digit_sum * k

Check failure on line 41 in recursions/recursive_digit_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

recursions/recursive_digit_sum.py:41:1: W293 Blank line contains whitespace
# Recursive function to find the super digit
while total_sum >= 10:
total_sum = sum(int(digit) for digit in str(total_sum))

Check failure on line 45 in recursions/recursive_digit_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

recursions/recursive_digit_sum.py:45:1: W293 Blank line contains whitespace
return total_sum

if __name__ == '__main__':
# Read input and split it into n and k
first_multiple_input = input().rstrip().split()

Check failure on line 51 in recursions/recursive_digit_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

recursions/recursive_digit_sum.py:51:1: W293 Blank line contains whitespace
n = first_multiple_input[0] # n as a string
k = int(first_multiple_input[1]) # k as an integer

Check failure on line 54 in recursions/recursive_digit_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

recursions/recursive_digit_sum.py:54:1: W293 Blank line contains whitespace
# Call the superDigit function and print the result
result = superDigit(n, k)
print(result)