SpoonTune

Untitled

Jul 17th, 2023
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. def gcf(a, b):
  2. i = 1
  3.  
  4. if a > b:
  5. big = a
  6. small = b
  7. else:
  8. big = b
  9. small = a
  10.  
  11. while i < big:
  12. if small % i == 0 and big % i == 0:
  13. result = i
  14. i+=1
  15. return result
  16.  
  17. def doesDivide(str, div):
  18. trial = ''
  19.  
  20. i = 1
  21. while i < 1001:
  22. trial = div * i
  23. if trial == str:
  24. return True
  25. elif len(trial) > len(str):
  26. return False
  27. i+=1
  28. return False
  29.  
  30.  
  31. def toString(listy):
  32. result = ""
  33.  
  34. i = 0
  35. while i < len(listy):
  36. result += listy[i]
  37. i+=1
  38. return result
  39.  
  40.  
  41. def getPrefixes(input, other, max):
  42. print("Input: " + input)
  43. print("Max:" + str(max))
  44. diff = abs(len(other) - len(input))
  45. prefix = [] # Store all valid prefixes for a given input
  46. letters = []
  47.  
  48. print("Diff:" + str(diff))
  49.  
  50. i = 0
  51. while i < diff and i < len(input):
  52. letters.append(input[i])
  53. i+=1
  54.  
  55.  
  56. # letters = list(input) # Store the letters that make up the prefix
  57.  
  58. i = len(input)
  59. while i > 0: # Loop through every letter in input
  60.  
  61.  
  62. if doesDivide(input, toString(letters)) and doesDivide(other, toString(letters)): # If the letters divides the input, it is a valid prefix
  63. prefix.append(toString(letters))
  64. return prefix
  65.  
  66. if len(letters) > 0:
  67. del letters[len(letters) - 1] # Remove a letter
  68.  
  69. i-=1
  70.  
  71. #if len(letters) == len(other):
  72. # print("Error 1")
  73. # return prefix
  74.  
  75. #if len(letters) * 2 > len(input) and input * 2 > other:
  76. # print("Error 2")
  77. # return prefix
  78.  
  79. if len(prefix) == max and max != 0:
  80. print("Error 3")
  81. return prefix
  82.  
  83. return prefix
  84.  
  85.  
  86. class Solution(object):
  87. def gcdOfStrings(self, str1, str2):
  88. """
  89. :type str1: str
  90. :type str2: str
  91. :rtype: str
  92. """
  93.  
  94. if str1 == str2:
  95. return str1
  96.  
  97. if doesDivide(str1, str2):
  98. return str2
  99.  
  100. if doesDivide(str2, str1):
  101. return str1
  102.  
  103. if doesDivide(str1, str1[0]):
  104. print("fdsafdsaaaa")
  105. if doesDivide(str2, str1[0]):
  106. print("FGDSAGFDSAG")
  107. return str1[0] * gcf(len(str1), len(str2))
  108.  
  109. if len(str1) < len(str2):
  110. smallerString = str1
  111. other = str2
  112. else:
  113. smallerString = str2
  114. other = str1
  115.  
  116. prefixesSmaller = getPrefixes(smallerString, other, 0)
  117.  
  118. maxSize = len(prefixesSmaller)
  119.  
  120. prefixesBigger = getPrefixes(other, smallerString, maxSize)
  121.  
  122. prefixes1 = prefixesSmaller
  123. prefixes2 = prefixesBigger
  124.  
  125. biggy = ""
  126.  
  127. i = 0
  128. while i < len(prefixes1):
  129. j = 0
  130. while j < len(prefixes2):
  131. if prefixes1[i] == prefixes2[j] and len(prefixes1[i]) > len(biggy):
  132. biggy = prefixes1[i]
  133. j+=1
  134. i+=1
  135. print(prefixes1)
  136. print(prefixes2)
  137.  
  138. return biggy
  139.  
  140. # We need to fix the code so that it gets the biggest prefix, checks if it works, and if it does, then closes
Add Comment
Please, Sign In to add comment