A

#-------------------------------------------------------------------------------
# Name:        digitalRoots
# Purpose:    Find digital root of number of string
#
# Author:      jlai
#
# Created:     26/11/2020
# Copyright:   (c) jlai 2020
# Licence:     <your licence>
#-------------------------------------------------------------------------------

list1 = [
    5,
    123,
    "the quick brown fox",
    7823438,
    '"640 K ought to be enough for anybody". Bill Gates, 1981.',
    "Progcomp"
]
# Get Digital Root
def getDr(source):
    dr = 0
    # keep adding up digits until total is < 10
    while True:
        for d in source:
            dr += int(d)
        if dr <10:
            break
        source = str(dr)
        total =0
    return str(dr)

for orig in list1:
    if type(orig) == int:
        source = str(orig)
        dr = getDr(source)
        print(f"{dr:8} {source:8}")
    else:
        source = 0
        for c in orig:
            c=c.lower()
            if c.isalpha():
                source += ord(c.lower())-96

        dr = getDr(str(source))
        print(f"{dr:8} {str(source):8} {orig}")


#-------------------------------------------------------------------------------
# Name:        Digital Roots
# Purpose:
#
# Author:      Joseph.Lai
#
# Created:     22/05/2014
# Copyright:   (c) Joseph.Lai 2014
# Licence:     <your licence>
#-------------------------------------------------------------------------------
# List here instead of File Read
list1 = [
5,
123,
"the quick brown fox",
7823438,
'"640 K ought to be enough for anybody". Bill Gates, 1981.',
"Progcomp"
]

list2 = [
14,
99999,
10000,
7,
12345678,
98765432,
"June",
"UNSW Progcomp Twenty-thirteen",
"the second figure thrice",
'"Tragedy is when I cut my finger,...',
'Comedy is when you walk into an open sewer and die" (attributed to Mel Brooks).',
"the thirty-sixth triangular number, declared the number of the beast",
"A Clockwork Orange, Stanley Kubrick: 3*114+1=343",
"(114 was Kubrick's signature code, it appears in four films as CRM114 or serum 114 or C-rm114.)",
"Nineteen associates attacked the World Trade Center in New York and the Pentagon in Washington"
]

# Function to Calculate Digital Roots from a Number
# ==================================================
def calcDR(number):
    while True:
        dr=0
        numstring = str(number)                     #convert number to string
        for numletter in numstring:                 #parsing each letter/digit
            digit = int(numletter)                  #convert back to an integer
            #print('digit is {}'.format(numletter))
            dr += digit                             #add up to digital root
            #print('dr is now {}'.format(dr))
        if dr < 10:                                 #if dr has 1 digit
            break                                   #then stop processing it
        else:
            number = dr                             #otherwise reduce it further
    return dr                                       #return single digit Digital Root

# Function to Convert Text to a Number
# =====================================
def convertText(sentence):
    sentence = sentence.lower()
    number = 0
    valid = "abcdefghijklmnopqrstuvwxyz"
    valod = "123456789012345"
    for letter in sentence:
        pos = valid.find(letter)+1
        #print("{} {}".format(letter, pos))
        if pos > 0:
            number += pos
    return number

# ==================================================================================
first=True
count=0
maxNum = 1000000000  #maximum number
maxLen = 100         #maximum num of letters in sentence
list = list2  # Choose the list to process - list1 is the test, list2 is actual data

# Process the List
# ==================================================================================
for sentence in list:
    if count < 20:
        try: # first assume it is a number
            number = int(sentence)  #if this errors, then sentence is words not a number
            if first :  #first number is a count
                maxcount = number
                first = False
            else:   # Get DR of a proper number
                count +=1
                if number < maxNum:
                    dr = calcDR(number)
                    print("{}     {}".format(dr, number))
                else:
                    print("{} exceeds threshold of {}".format(number,maxNum))

        except : #Process the Sentence and Convert it to a Number to get the Digital Root
            count +=1
            if len(sentence) < maxLen:
                number = convertText(sentence)
                dr = calcDR(number)
                print("{}     {}     {}".format(dr, number, sentence))
            else:
                print("Len {} exceeds Max Len of {}".format(len(sentence),maxLen))
    else:
        print("Maximum Records Reached")
        break

print("Records Read: {} , Max: {}".format(count,maxcount))

Comments