Last active
February 13, 2023 20:57
-
-
Save deangrant/b8972a32080ae3ac5f9d6eb8aa357998 to your computer and use it in GitHub Desktop.
The code used the optimal stopping problem using secretary problem algorithm.
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
package main | |
import ( | |
"fmt" | |
"math/rand" | |
) | |
func secretaryProblem(n, cutoff int) int { | |
// Generate a slice of candidates from 1 to n. | |
candidates := make([]int, n) | |
for i := 0; i < n; i++ { | |
candidates[i] = i + 1 | |
} | |
// Shuffle the order of the candidates randomly. | |
for i := range candidates { | |
j := rand.Intn(i + 1) | |
candidates[i], candidates[j] = candidates[j], candidates[i] | |
} | |
// Initialize the best candidate seen so far as the first candidate. | |
bestCandidate := candidates[0] | |
// Go through the first cutoff candidates and update the best candidate if a better one is found. | |
for i, candidate := range candidates { | |
if i >= cutoff { | |
// Stop if the cutoff point is reached. | |
break | |
} | |
if candidate > bestCandidate { | |
// Update the best candidate if the current candidate is better. | |
bestCandidate = candidate | |
} | |
} | |
// Return the best candidate found. | |
return bestCandidate | |
} | |
func main() { | |
n := 10 // Total number of candidates | |
cutoff := |
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
import random | |
def secretary_problem( | |
n: int, | |
e: int | |
) -> int: | |
""" | |
Defines a function that solves the secretary problem using a cutoff strategy. | |
Parameters: | |
n (int): the total number of candidates | |
e (int): the number of candidates that can be observed before | |
making a decision. | |
Returns: | |
int: the best candidate found. | |
Example: | |
>>> n = 10 | |
>>> cutoff = 2.71828 | |
>>> best_candidate = secretary_problem(n, cutoff) | |
>>> print(f"The best candidate is {best_candidate}.") | |
The best candidate is 9. | |
""" | |
# Generate a list of candidates. | |
candidates = list( | |
range( | |
1, | |
n+1 | |
) | |
) | |
# Shuffle the order of the candidates randomly. | |
random.shuffle( | |
candidates | |
) | |
# Initialize the best candidate seen so far as the first candidate. | |
best_candidate = candidates[0] | |
# Go through the first cutoff candidates and update the best candidate | |
# if a better one is found. | |
for i, candidate in enumerate( | |
candidates | |
): | |
if i >= cutoff: | |
# Stop if the cutoff point is reached. | |
break | |
if candidate > best_candidate: | |
# Update the best candidate if the current candidate is better. | |
best_candidate = candidate | |
# Return the best candidate found. | |
return best_candidate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment