Skip to content

Instantly share code, notes, and snippets.

@empeje
Last active December 17, 2017 11:15
Show Gist options
  • Save empeje/74c36e1f0f6a41487aa2f582c350be02 to your computer and use it in GitHub Desktop.
Save empeje/74c36e1f0f6a41487aa2f582c350be02 to your computer and use it in GitHub Desktop.
[SNIPPET-22] HackerRank - Between Two Sets (Ugly Solution)
#!/bin/python3
import sys
from functools import reduce
n,m = input().strip().split(' ')
n,m = [int(n),int(m)]
a = [int(a_temp) for a_temp in input().strip().split(' ')]
b = [int(b_temp) for b_temp in input().strip().split(' ')]
factors_of_b = []
# TODO: learn about this algorithm
def factors(n):
return set(reduce(list.__add__,
([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))
for number in b:
factors_of_b.append(factors(number))
temp = set()
for i in range(len(factors_of_b)-1):
temp = factors_of_b[i] & factors_of_b[i+1]
if (len(b) == 1):
temp = factors(b[0])
# print(temp)
result = set()
for number in temp:
tempz = 0
for other_number in a:
if number % other_number == 0:
tempz += 1
if tempz == len(a):
result.add(number)
new_resultz = set()
for number in result:
tempz = 0
for other_number in b:
if number in factors(other_number):
tempz +=1
if tempz == len(b):
new_resultz.add(number)
print(len(new_resultz))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment