How to find the best number of integers with fewer repeated values sequentially in Python
Question:
A = [7, 3, 7, 3, 1, 3, 4, 1]
I have a variable containing a list of integers. These integers show the location,
7 = USA
3 = UAE
1 = India
4 = Pakistan
If I have five location to visit in each day, how do I get the best five days in sequence for less repetition?
For example, I want the output array of [7,3,1,3,4] from 7 to 4 these 5 locations are most unique just 1 repeated from the array sequence, I just get 1 repeated location, which is 3, so dynamically, is it possible to get the output when i change the number of locations?
Answers:
So if I’m understanding the problem correctly, you’re trying to go through a table and find the capture that has the most unique numbers
eg:
You can do this pretty simply
A = [7, 3, 7, 3, 1, 3, 4, 1]
most_unique = []
for start in range(len(A)-4): # making sure not to go out of the table bounds
capture = A[start:start+5]
if len(set(capture)) > len(set(most_unique)): #using a set() which removes duplicates and checking the length of it
most_unique = capture
print(most_unique)
As per comments, the OP requested for a solution that is dynamic and determines the amount of days with the most unique numberset
A = [7, 3, 7, 3, 1, 3, 4, 1]
most_unique = []
for size in range(len(A)): # loops through all the possible day amounts
for start in range(len(A)-size+1): # making sure not to go out of the table bounds
capture = A[start:start+size]
if len(set(capture)) > len(set(most_unique)): #using a set() which removes duplicates and checking the length of it
most_unique = capture
print(most_unique)
So if you mean a slice from A of length 5 (ie days) then, since len(A) == 8
, then there are only a few possible slices.
I would just enumerate all the slices and pick one with the fewest repeats (or the largest number of different locations):
def find_best_sequence(seq, days):
num_slices = len(seq) - days + 1
trials = []
for index in range(num_slices):
s = seq[index:index+days]
trials.append((s,set(s)))
trials.sort(key=lambda x:-len(x[1]))
return trials
seq = [7, 3, 7, 3, 1, 3, 4, 1]
days = 5
trials = find_best_sequence(seq, days)
print(trials[0][0])
Output as requested
A = [7, 3, 7, 3, 1, 3, 4, 1]
I have a variable containing a list of integers. These integers show the location,
7 = USA
3 = UAE
1 = India
4 = Pakistan
If I have five location to visit in each day, how do I get the best five days in sequence for less repetition?
For example, I want the output array of [7,3,1,3,4] from 7 to 4 these 5 locations are most unique just 1 repeated from the array sequence, I just get 1 repeated location, which is 3, so dynamically, is it possible to get the output when i change the number of locations?
So if I’m understanding the problem correctly, you’re trying to go through a table and find the capture that has the most unique numbers
eg:
You can do this pretty simply
A = [7, 3, 7, 3, 1, 3, 4, 1]
most_unique = []
for start in range(len(A)-4): # making sure not to go out of the table bounds
capture = A[start:start+5]
if len(set(capture)) > len(set(most_unique)): #using a set() which removes duplicates and checking the length of it
most_unique = capture
print(most_unique)
As per comments, the OP requested for a solution that is dynamic and determines the amount of days with the most unique numberset
A = [7, 3, 7, 3, 1, 3, 4, 1]
most_unique = []
for size in range(len(A)): # loops through all the possible day amounts
for start in range(len(A)-size+1): # making sure not to go out of the table bounds
capture = A[start:start+size]
if len(set(capture)) > len(set(most_unique)): #using a set() which removes duplicates and checking the length of it
most_unique = capture
print(most_unique)
So if you mean a slice from A of length 5 (ie days) then, since len(A) == 8
, then there are only a few possible slices.
I would just enumerate all the slices and pick one with the fewest repeats (or the largest number of different locations):
def find_best_sequence(seq, days):
num_slices = len(seq) - days + 1
trials = []
for index in range(num_slices):
s = seq[index:index+days]
trials.append((s,set(s)))
trials.sort(key=lambda x:-len(x[1]))
return trials
seq = [7, 3, 7, 3, 1, 3, 4, 1]
days = 5
trials = find_best_sequence(seq, days)
print(trials[0][0])
Output as requested