I’m solving yet another problem in HackerRank (https://www.hackerrank.com/challenges/determining-dna-health/problem). In short: you are given 2 arrays (
health), one of which have a ‘gene’ name, and the other – ‘gene’ weight (aka health). You then given a bunch of strings, each containing values
n, which denote the start and end of the slice to be applied to the
health arrays, and the ‘gene’-string, for which we need to determine healthiness. Then we need to return health-values for the most and the least healthy strings.
My solution is below, and it works, but it’s not scalable, i.e. it fails testcases with a lot of values.
import re if __name__ == '__main__': n = int(input()) genes = input().rstrip().split() health = list(map(int, input().rstrip().split())) s = int(input()) weights = () for s_itr in range(s): m,n,gn = input().split() weight = 0 for i in range(int(m),int(n)+1): if genes(i) in gn: compilt = "r'(?=("+genes(i)+"))'" matches = len(re.findall(eval(compilt), gn)) weight += health(i)*matches weights.append(weight) print(min(weights), max(weights))
Can you advise on how to apply generators here? I suspect that the solution fails because of the very big list that’s being assembled. Is there a way to get min and max values here without collecting them all?
genes = ('a', 'b', 'c', 'aa', 'd', 'b') health = (1, 2, 3, 4, 5, 6) gene1 = "1 5 caaab" (result = 19 = max) gene2 = "0 4 xyz" (result = 0 = min) gene3 = "2 4 bcdybc" (result = 11)
This case returns