Python Forum
Trying to integrate gcd function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to integrate gcd function
#1
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?
Reply
#2
This is homework and probably some conditions apply. Otherwise I would hae suggested to use built-in math.gcd()
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
(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?
Reply
#4
(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))
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Differentiate and Integrate A Given Function wildmommy666 2 1,585 May-11-2020, 04:30 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020