Skip to content

added ability to choose minimum and maximum difficulty in randproblem #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pe_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,7 @@ def project_euler_username(discord_id, connection=None):
return data[0]["username"]


# Returns a double array of the form [problem_1, problem_2, problem_3, ...]
# Returns a 2d array of the form [problem_1, problem_2, problem_3, ...]
# with problem_i of the form ['problem_nb', 'problem_title', 'unix timestamp of publish', 'solved by', '0']
# careful again, only strings in the arrays
def unsolved_problems(username):
Expand Down
26 changes: 23 additions & 3 deletions pe_discord_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,9 @@ async def command_list_threads(ctx):

@bot.slash_command(name="randproblem", description="Give a random problem the user has not solved")
@option("member", description="The targetted member", default=None)
async def command_randproblem(ctx, member: discord.User):
@option("minimum difficulty", description="minimum project euler difficulty rating", default=5, min=5, max=100)
@option("maximum difficulty", description="maximum project euler difficulty rating", default=100, min=5, max=100)
async def command_randproblem(ctx, member: discord.User, min_difficulty: int, max_difficulty: int):

await ctx.defer()

Expand All @@ -603,11 +605,29 @@ async def command_randproblem(ctx, member: discord.User):
if not m.is_discord_linked():
return await ctx.respond("This user does not have a project euler account linked! Please link with /link first")

if m.solve_count() == len(m.solve_array()):
if min_difficulty > max_difficulty:
return await ctx.respond("Minimum difficulty must be smaller than maximum difficulty")

if m.solve_count() == len(m.solve_array()) or random.uniform(0,1) < 0.001:
return await ctx.respond(f"I *randomly* selected problem #1729 for user: `{m.username()}`: <https://teyzer.github.io/problem1729/>")

problems = pe_api.unsolved_problems(m.username())
choice = random.choice(problems)
if min_difficulty == 5 and max_difficulty == 100:
choice = random.choice(problems)
else:
difficulties = [*map(lambda problem: (problem.problem_id,problem.difficulty_rating),pe_api.PE_Problem.complete_list())]
unsolved_difficulties = []
unsolved_ids_map = {}
for problem in problems:
unsolved_ids_map[int(problem[0])] = problem

for problem,difficulty in difficulties:
if problem in unsolved_ids_map and min_difficulty <= difficulty <= max_difficulty:
unsolved_difficulties.append(unsolved_ids_map[problem])

if len(unsolved_difficulties) == 0:
return await ctx.respond(f"{m.username()} has no unsolved problems in difficulty range [{min_difficulty},{max_difficulty}]")
choice = random.choice(unsolved_difficulties)

text_message = "I randomly selected problem #{0} for user `{1}`: \"{2}\". <https://projecteuler.net/problem={0}>"
text_message = text_message.format(choice[0], m.username(), choice[1])
Expand Down