-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagical_prime.py
More file actions
102 lines (60 loc) · 1.92 KB
/
magical_prime.py
File metadata and controls
102 lines (60 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Write a function that will return the index of the first magical prime from a list.
# A magical prime is something we consider that the addition of two inputs from the array equals a
# magical prime present in the array.
# For instance, [1, 2, 15, 17, 38, 42], 2 + 15 = 17, so 17 would be our magical prime.
# In this case the function should return 3 because 17 is at the 3rd position.
# In the case you can't find a magical prime, return -1.
# [1, 2, 4, 9, 16, 20, 23], 4 + 16 = 20, index is 5
# [2, 4, 5] , index -1, no magical prime can be found
"""
# trouver edge cases
pas d'ordre inverse, pas de self addition, dernier chiffre inutile,
pas d'ordre reverse,
le chiffre le plus élévé n'est jamais pris en compte dans la somme,
1er count le nombre de chiffres dans la liste,
minimum 3 chiffres dans la liste
#Pseudo code
def func
For loop internal
list_test
reduce(apply add() => add the 2 firts items dans une liste
si sum = any(element in list)
print index
elif
for
x=list_test[0]
y=2
sum =1+2
si sum = any(element in list), return index.sum
x=1
y=4
sum=1+4
si sum = any(element in list), return index.sum
...
x=2
y=4
sum=2+4
test_list = [1, 2, 4, 9, 16, 20, 23]
"""
# funct that finds magical prime
def find_magical_prime_index(list_magical_prime):
index = 0
for x in list_magical_prime:
index = index+1
copy_list_magical_prime = list_magical_prime[index:len(list_magical_prime)-1]
for y in copy_list_magical_prime:
#print(f'test list {x} {y}')
magical_sum = x + y
if magical_sum in list_magical_prime:
return list_magical_prime.index(magical_sum)
return -1
# Tests cases
result = find_magical_prime_index([1, 2, 4, 9, 16, 20, 23])
# 5
print(result)
result = find_magical_prime_index([2, 4, 5])
# -1
print(result)
result = find_magical_prime_index([1, 2, 15, 17, 38, 42])
# 3
print(result)