Last active
April 20, 2022 19:47
-
-
Save ZakriaJanjua/8ced550889ba3da9eba02922a7d52b55 to your computer and use it in GitHub Desktop.
Coding challenge: find the greatest profit of the given stocks and intersperse the result with the given challenge_token
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
arr = [44, 30, 24, 32, 35, 30, 40, 38, 15] #16 | |
arr2 = [10, 9, 8, 2] #-1 | |
arr3 = [10, 12, 4, 5, 9] #5 | |
arr4 = [14, 20, 4, 12, 5, 11] #8 | |
def possibility(array): | |
for i in range(0, len(array)): | |
try: | |
if (array[i] < array[i+1]): | |
return True | |
else: | |
pass | |
except: | |
return False | |
return False | |
def interperse(array1, array2): | |
array1_index = 0 | |
for i in range(0, len(array2)): | |
array1.insert(array1_index, array2[i]) | |
array1_index += 2 | |
return array1 | |
def ArrayChallenge(array): | |
challenge_token = 'qf5bt07k6' | |
if (possibility(array) == True): | |
max_val = 0 | |
for i in range(0, len(array)): | |
temp_val = array[i] | |
temp_arr = array[i:] | |
temp_arr_max = max(temp_arr) | |
temp_max = temp_arr_max - temp_val | |
if (temp_max > max_val): | |
max_val = temp_max | |
res_arr = interperse(list(challenge_token), list(str(max_val))) | |
return ''.join(res_arr) | |
else: | |
res_arr = interperse(list(challenge_token), list(str(-1))) | |
return ''.join(res_arr) | |
print(ArrayChallenge(arr)) | |
print(ArrayChallenge(arr2)) | |
print(ArrayChallenge(arr3)) | |
print(ArrayChallenge(arr4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment