Python Forum
Trying to integrate gcd function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Trying to integrate gcd function (/thread-30614.html)



Trying to integrate gcd function - hhydration - Oct-28-2020

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?


RE: Trying to integrate gcd function - perfringo - Oct-28-2020

This is homework and probably some conditions apply. Otherwise I would hae suggested to use built-in math.gcd()


RE: Trying to integrate gcd function - hhydration - Oct-28-2020

(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?


RE: Trying to integrate gcd function - perfringo - Oct-28-2020

(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))