Skip to content

Add support for sorting problems by acceptance rate #728

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

Merged
merged 2 commits into from
Aug 21, 2021
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ Thanks for [@yihong0618](https://github.com/yihong0618) provided a workaround wh
| `leetcode.showCommentDescription` | Specify whether to include the problem description in the comments | `false` |
| `leetcode.useEndpointTranslation` | Use endpoint's translation (if available) | `true` |
| `leetcode.colorizeProblems` | Add difficulty badge and colorize problems files in explorer tree | `true` |
| `leetcode.problems.sortStrategy` | Specify sorting strategy for problems list | `None` |

## Want Help?

Expand Down
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"onCommand:leetcode.testSolution",
"onCommand:leetcode.submitSolution",
"onCommand:leetcode.switchDefaultLanguage",
"onCommand:leetcode.problems.sort",
"onView:leetCodeExplorer"
],
"main": "./out/src/extension",
Expand Down Expand Up @@ -134,6 +135,12 @@
"command": "leetcode.switchDefaultLanguage",
"title": "Switch Default Language",
"category": "LeetCode"
},
{
"command": "leetcode.problems.sort",
"title": "Sort Problems",
"category": "LeetCode",
"icon": "$(sort-precedence)"
}
],
"viewsContainers": {
Expand Down Expand Up @@ -179,6 +186,11 @@
"command": "leetcode.pickOne",
"when": "view == leetCodeExplorer",
"group": "overflow@0"
},
{
"command": "leetcode.problems.sort",
"when": "view == leetCodeExplorer",
"group": "overflow@1"
}
],
"view/item/context": [
Expand Down Expand Up @@ -677,6 +689,17 @@
"default": true,
"scope": "application",
"description": "Add difficulty badge and colorize problems files in explorer tree."
},
"leetcode.problems.sortStrategy": {
"type": "string",
"default": "None",
"scope": "application",
"enum": [
"None",
"Acceptance Rate (Ascending)",
"Acceptance Rate (Descending)"
],
"description": "Sorting strategy for problems list."
}
}
}
Expand Down
36 changes: 35 additions & 1 deletion src/commands/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
// Licensed under the MIT license.

import * as vscode from "vscode";
import { leetCodeTreeDataProvider } from "../explorer/LeetCodeTreeDataProvider";
import { leetCodeExecutor } from "../leetCodeExecutor";
import { IQuickItemEx } from "../shared";
import { Endpoint } from "../shared";
import { Endpoint, SortingStrategy } from "../shared";
import { DialogType, promptForOpenOutputChannel, promptForSignIn } from "../utils/uiUtils";
import { deleteCache } from "./cache";

Expand Down Expand Up @@ -52,3 +53,36 @@ export function getLeetCodeEndpoint(): string {
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
return leetCodeConfig.get<string>("endpoint", Endpoint.LeetCode);
}

const SORT_ORDER: SortingStrategy[] = [
SortingStrategy.None,
SortingStrategy.AcceptanceRateAsc,
SortingStrategy.AcceptanceRateDesc,
];

export async function switchSortingStrategy(): Promise<void> {
const currentStrategy: SortingStrategy = getSortingStrategy();
const picks: Array<IQuickItemEx<string>> = [];
picks.push(
...SORT_ORDER.map((s: SortingStrategy) => {
return {
label: `${currentStrategy === s ? "$(check)" : " "} ${s}`,
value: s,
};
}),
);

const choice: IQuickItemEx<string> | undefined = await vscode.window.showQuickPick(picks);
if (!choice || choice.value === currentStrategy) {
return;
}

const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
await leetCodeConfig.update("problems.sortStrategy", choice.value, true);
await leetCodeTreeDataProvider.refresh();
}

export function getSortingStrategy(): SortingStrategy {
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
return leetCodeConfig.get<SortingStrategy>("problems.sortStrategy", SortingStrategy.None);
}
4 changes: 4 additions & 0 deletions src/explorer/LeetCodeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ export class LeetCodeNode {
};
}

public get acceptanceRate(): number {
return Number(this.passRate.slice(0, -1).trim());
}

public get uri(): Uri {
return Uri.from({
scheme: "leetcode",
Expand Down
20 changes: 16 additions & 4 deletions src/explorer/explorerNodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import * as _ from "lodash";
import { Disposable } from "vscode";
import * as list from "../commands/list";
import { Category, defaultProblem, ProblemState } from "../shared";
import { getSortingStrategy } from "../commands/plugin";
import { Category, defaultProblem, ProblemState, SortingStrategy } from "../shared";
import { shouldHideSolvedProblem } from "../utils/settingUtils";
import { LeetCodeNode } from "./LeetCodeNode";

Expand Down Expand Up @@ -56,7 +57,9 @@ class ExplorerNodeManager implements Disposable {
}

public getAllNodes(): LeetCodeNode[] {
return Array.from(this.explorerNodeMap.values());
return this.applySortingStrategy(
Array.from(this.explorerNodeMap.values()),
);
}

public getAllDifficultyNodes(): LeetCodeNode[] {
Expand Down Expand Up @@ -114,7 +117,7 @@ class ExplorerNodeManager implements Disposable {
res.push(node);
}
}
return res;
return this.applySortingStrategy(res);
}

public getChildrenNodesById(id: string): LeetCodeNode[] {
Expand Down Expand Up @@ -142,7 +145,7 @@ class ExplorerNodeManager implements Disposable {
break;
}
}
return res;
return this.applySortingStrategy(res);
}

public dispose(): void {
Expand Down Expand Up @@ -186,6 +189,15 @@ class ExplorerNodeManager implements Disposable {
break;
}
}

private applySortingStrategy(nodes: LeetCodeNode[]): LeetCodeNode[] {
const strategy: SortingStrategy = getSortingStrategy();
switch (strategy) {
case SortingStrategy.AcceptanceRateAsc: return nodes.sort((x: LeetCodeNode, y: LeetCodeNode) => Number(x.acceptanceRate) - Number(y.acceptanceRate));
case SortingStrategy.AcceptanceRateDesc: return nodes.sort((x: LeetCodeNode, y: LeetCodeNode) => Number(y.acceptanceRate) - Number(x.acceptanceRate));
default: return nodes;
}
}
}

export const explorerNodeManager: ExplorerNodeManager = new ExplorerNodeManager();
1 change: 1 addition & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
vscode.commands.registerCommand("leetcode.switchDefaultLanguage", () => switchDefaultLanguage()),
vscode.commands.registerCommand("leetcode.addFavorite", (node: LeetCodeNode) => star.addFavorite(node)),
vscode.commands.registerCommand("leetcode.removeFavorite", (node: LeetCodeNode) => star.removeFavorite(node)),
vscode.commands.registerCommand("leetcode.problems.sort", () => plugin.switchSortingStrategy()),
);

await leetCodeExecutor.switchEndpoint(plugin.getLeetCodeEndpoint());
Expand Down
8 changes: 8 additions & 0 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,11 @@ export enum DescriptionConfiguration {
}

export const leetcodeHasInited: string = "leetcode.hasInited";

export enum SortingStrategy {
None = "None",
AcceptanceRateAsc = "Acceptance Rate (Ascending)",
AcceptanceRateDesc = "Acceptance Rate (Descending)",
FrequencyAsc = "Frequency (Ascending)",
FrequencyDesc = "Frequency (Descending)",
}