Python Forum

Full Version: Trying to integrate gcd function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have this function, it prints a list of values.
def kasinski(text):
    trigraphs=[]
    distances=[]
    for trigraph in range (len(text)-2):
        newtrigraph= text[trigraph:trigraph+3]
        if newtrigraph in trigraphs:
            distances.append(trigraph-(text.index(newtrigraph)))
        else:
            trigraphs.append(newtrigraph)
            trigraphs.append(trigraph)
            return distances

I also have functions that will find the greatest common divisor of all numbers in a list
def gcd(x,y):
    if y==0:
        return x
    else:
        return gcd(y, x% y)
def gcd_list_helper(list, index:
    if index==len((list)-1):
        return list[index]
    else:
        return gcd(list[index], gcd_list_helper(list, index+1))
def gcd_of_list(list):
    return gcd_list_helper(list, 0)
I am having trouble integrating the gcd function into my first function so that the kasiski function returns the GCD of the list rather than the list. Any pointers?
This is homework and probably some conditions apply. Otherwise I would hae suggested to use built-in math.gcd()
(Oct-28-2020, 01:34 PM)perfringo Wrote: [ -> ]This is homework and probably some conditions apply. Otherwise I would hae suggested to use built-in math.gcd()

Do you have an idea of how I would integrate my gcd function?
(Oct-28-2020, 01:58 PM)hhydration Wrote: [ -> ]I am having trouble integrating the gcd function into my first function so that the kasiski function returns the GCD of the list rather than the list. Any pointers?
Do you have an idea of how I would integrate my gcd function?

If function kasinksi returns list of integers then it's should be simple just wrap the resulting list with gcd:

import math

result = math.gcd(kasinski(text))