0

I was being disqualified from an interview test from a "top" freelance website because they found similarities in my answers from online. But I also would like to share my own solutions to this question in python. Definitely, if anyone can improve the answers for future test cases, it will be helpful!

winter = T[0:int(num_of_days)]
spring = T[int(num_of_days):int(num_of_days*2)]
summer = T[int(num_of_days*2):int(num_of_days*3)]
autumn = T[int(num_of_days*3):int(num_of_days*4)]
winter_diff = abs(max(winter) -min(winter))
spring_diff = abs(max(spring) -min(spring))
summer_diff = abs(max(summer) -min(summer))
autumn_diff = abs(max(autumn) -min(autumn))
season_list = [ "WINTER", "SPRING", "SUMMER", "AUTUMN" ]
season_amplitude = [winter_diff,spring_diff,summer_diff,autumn_diff]
print(season_amplitude)
max_value = max(season_amplitude)
max_index = season_amplitude.index(max_value)
print(season_list[max_index])

1 Answers1

0

The question gives an "example" for T. If you study the question closely, there is an implication that the length of T will be a multiple of 4 - i.e., not necessarily just 8 but maybe 12, 16 etc. So you need a more generic solution. Something like this:

class Solution:
    SEASONS = ['WINTER', 'SPRING', 'SUMMER', 'AUTUMN']
    def __init__(self, T):
        assert len(T) % len(Solution.SEASONS) == 0
        self._T = T
        self._sv = len(self._T) // len(Solution.SEASONS)
    def get_season(self):
        max_amp, result = -1, None
        for i, season in zip(range(0, len(self._T), self._sv), Solution.SEASONS):
            sub = self._T[i:i+self._sv]
            if (abs_ := abs(min(sub)-max(sub))) > max_amp:
                max_amp = abs_
                result = season
        return result

T = [-3, -14, -5, 7, 8, 42, 8, 3]

print(Solution(T).get_season())

Output:

SUMMER
DarkKnight
  • 19,739
  • 3
  • 6
  • 22