From 34644bd333c8718fefe60d42208e4301bff5aef5 Mon Sep 17 00:00:00 2001 From: Divlo Date: Sun, 24 Apr 2022 20:27:51 +0200 Subject: [PATCH] refactor: usage of built-in type hinting in Python solutions --- .../solutions/python/function/solution.py | 3 +- .../solutions/python/function/solution.py | 8 +-- .../solutions/python/function/solution.py | 1 - .../solutions/python/function/solution.py | 11 ++-- .../solutions/python/function/solution.py | 7 +-- .../solutions/python/function/solution.py | 3 +- .../solutions/python/function/solution.py | 3 +- .../solutions/python/function/solution.py | 3 +- .../solutions/python/function/solution.py | 7 +-- .../solutions/python/function/solution.py | 5 +- .../solutions/python/function/solution.py | 3 +- .../solutions/python/function/solution.py | 12 +++- .../solutions/python/bubble-sort/solution.py | 5 +- .../solutions/python/function/solution.py | 3 +- .../python/insertion-sort/solution.py | 5 +- .../solutions/python/merge-sort/solution.py | 16 +++--- .../sudoku/solutions/python/function/Grid.py | 8 +-- .../solutions/python/function/solution.py | 5 +- cli/commands/generate/challenge.ts | 2 +- cli/commands/generate/solution.ts | 2 +- cli/commands/run/test.ts | 6 +- cli/services/Challenge.ts | 8 ++- cli/services/Docker.ts | 12 ++-- cli/services/GitAffected.ts | 56 ++++++++++++------- cli/services/Solution.ts | 43 ++++++++------ cli/services/Test.ts | 18 +++--- templates/solution/python/solution.py | 3 +- 27 files changed, 142 insertions(+), 116 deletions(-) diff --git a/challenges/acronyms/solutions/python/function/solution.py b/challenges/acronyms/solutions/python/function/solution.py index 49eb226..4642f90 100644 --- a/challenges/acronyms/solutions/python/function/solution.py +++ b/challenges/acronyms/solutions/python/function/solution.py @@ -1,7 +1,6 @@ -from typing import List import sys -input_values: List[str] = [] +input_values: list[str] = [] for value in sys.stdin: input_values.append(value.rstrip('\n')) diff --git a/challenges/caesar-cipher/solutions/python/function/solution.py b/challenges/caesar-cipher/solutions/python/function/solution.py index 5153ac2..cbfe966 100644 --- a/challenges/caesar-cipher/solutions/python/function/solution.py +++ b/challenges/caesar-cipher/solutions/python/function/solution.py @@ -1,7 +1,7 @@ -from typing import List, TypedDict +from typing import TypedDict import sys -input_values: List[str] = [] +input_values: list[str] = [] for value in sys.stdin: input_values.append(value.rstrip('\n')) @@ -11,8 +11,8 @@ class ShiftedLetter(TypedDict): shifted: str -def shift_alphabet(shift: int) -> List[ShiftedLetter]: - result: List[ShiftedLetter] = [] +def shift_alphabet(shift: int) -> list[ShiftedLetter]: + result: list[ShiftedLetter] = [] alphabet = 'A B C D E F G H I J K L M N O P Q R S T U V W X Y Z'.split(' ') is_negative_shift = shift < 0 if is_negative_shift: diff --git a/challenges/cakes-swerc-2020-2021/solutions/python/function/solution.py b/challenges/cakes-swerc-2020-2021/solutions/python/function/solution.py index c78a93b..049ccca 100644 --- a/challenges/cakes-swerc-2020-2021/solutions/python/function/solution.py +++ b/challenges/cakes-swerc-2020-2021/solutions/python/function/solution.py @@ -1,4 +1,3 @@ -from typing import List import sys maximum_number_of_cake_possible = None diff --git a/challenges/consecutive-numbers/solutions/python/function/solution.py b/challenges/consecutive-numbers/solutions/python/function/solution.py index d869ecc..0d78f60 100644 --- a/challenges/consecutive-numbers/solutions/python/function/solution.py +++ b/challenges/consecutive-numbers/solutions/python/function/solution.py @@ -1,16 +1,15 @@ -from typing import List import sys -input_values: List[str] = [] +input_values: list[str] = [] for value in sys.stdin: input_values.append(value.rstrip('\n')) -def consecutive_numbers(numbers: List[int], couple_length: int) -> List[List[int]]: - result: List[List[int]] = [] +def consecutive_numbers(numbers: list[int], couple_length: int) -> list[list[int]]: + result: list[list[int]] = [] numbers_length = len(numbers) for index in range(numbers_length): - consecutive: List[int] = [numbers[index]] + consecutive: list[int] = [numbers[index]] for couple_index in range(1, couple_length, 1): is_last_number = index + couple_index == numbers_length if is_last_number: @@ -23,7 +22,7 @@ def consecutive_numbers(numbers: List[int], couple_length: int) -> List[List[int return result -numbers: List[int] = [] +numbers: list[int] = [] for value in input_values[1].split(' ; '): numbers.append(int(value)) diff --git a/challenges/defibrillators/solutions/python/function/solution.py b/challenges/defibrillators/solutions/python/function/solution.py index 189cc58..44d67e7 100644 --- a/challenges/defibrillators/solutions/python/function/solution.py +++ b/challenges/defibrillators/solutions/python/function/solution.py @@ -1,8 +1,7 @@ -from typing import List import sys import math -input_values: List[str] = [] +input_values: list[str] = [] for value in sys.stdin: input_values.append(value.rstrip('\n')) @@ -28,7 +27,7 @@ class Position: class Defibrillator: - def __init__(self, strings: List[str], user_position: Position) -> None: + def __init__(self, strings: list[str], user_position: Position) -> None: self.id = strings[0] self.name = strings[1] self.address = strings[2] @@ -41,7 +40,7 @@ class Defibrillator: longitude = convert_string_to_float(input_values[0]) latitude = convert_string_to_float(input_values[1]) user_position = Position(longitude, latitude) -defibrillators: List[Defibrillator] = [] +defibrillators: list[Defibrillator] = [] for index in range(3, len(input_values), 1): line = input_values[index].split(';') diff --git a/challenges/fibonacci/solutions/python/function/solution.py b/challenges/fibonacci/solutions/python/function/solution.py index 183e2a8..83fdbcf 100644 --- a/challenges/fibonacci/solutions/python/function/solution.py +++ b/challenges/fibonacci/solutions/python/function/solution.py @@ -1,7 +1,6 @@ -from typing import List import sys -input_values: List[str] = [] +input_values: list[str] = [] for value in sys.stdin: input_values.append(value.rstrip('\n')) diff --git a/challenges/fizzbuzz/solutions/python/function/solution.py b/challenges/fizzbuzz/solutions/python/function/solution.py index 8bbdaa1..f158efc 100644 --- a/challenges/fizzbuzz/solutions/python/function/solution.py +++ b/challenges/fizzbuzz/solutions/python/function/solution.py @@ -1,7 +1,6 @@ -from typing import List import sys -input_values: List[str] = [] +input_values: list[str] = [] for value in sys.stdin: input_values.append(value.rstrip('\n')) diff --git a/challenges/hello-world/solutions/python/function/solution.py b/challenges/hello-world/solutions/python/function/solution.py index 18a11a4..bfee34b 100644 --- a/challenges/hello-world/solutions/python/function/solution.py +++ b/challenges/hello-world/solutions/python/function/solution.py @@ -1,7 +1,6 @@ -from typing import List import sys -input_values: List[str] = [] +input_values: list[str] = [] for value in sys.stdin: input_values.append(value.rstrip('\n')) diff --git a/challenges/is-prime-number/solutions/python/function/solution.py b/challenges/is-prime-number/solutions/python/function/solution.py index d972bea..6af866c 100644 --- a/challenges/is-prime-number/solutions/python/function/solution.py +++ b/challenges/is-prime-number/solutions/python/function/solution.py @@ -1,13 +1,12 @@ -from typing import List import sys -input_values: List[str] = [] +input_values: list[str] = [] for value in sys.stdin: input_values.append(value.rstrip('\n')) -def divider_list(number: int) -> List[int]: - number_list: List[int] = [] +def divider_list(number: int) -> list[int]: + number_list: list[int] = [] for index in range(1, number + 1): if number % index == 0: number_list.append(index) diff --git a/challenges/is-valid-array-subsequence/solutions/python/function/solution.py b/challenges/is-valid-array-subsequence/solutions/python/function/solution.py index 312fe68..052f5a0 100644 --- a/challenges/is-valid-array-subsequence/solutions/python/function/solution.py +++ b/challenges/is-valid-array-subsequence/solutions/python/function/solution.py @@ -1,12 +1,11 @@ -from typing import List import sys -input_values: List[str] = [] +input_values: list[str] = [] for value in sys.stdin: input_values.append(value.rstrip('\n')) -def get_is_valid_subsequence(array: List, sequence: List): +def get_is_valid_subsequence(array: list, sequence: list): index_to_check = 0 for index in range(len(array)): if index_to_check < len(sequence) and array[index] == sequence[index_to_check]: diff --git a/challenges/offset-arrays/solutions/python/function/solution.py b/challenges/offset-arrays/solutions/python/function/solution.py index 913b724..fa9f6d9 100644 --- a/challenges/offset-arrays/solutions/python/function/solution.py +++ b/challenges/offset-arrays/solutions/python/function/solution.py @@ -1,7 +1,6 @@ -from typing import List import sys -input_values: List[str] = [] +input_values: list[str] = [] for value in sys.stdin: input_values.append(value.rstrip('\n')) diff --git a/challenges/roman-numerals/solutions/python/function/solution.py b/challenges/roman-numerals/solutions/python/function/solution.py index ee1700b..2d3cf16 100644 --- a/challenges/roman-numerals/solutions/python/function/solution.py +++ b/challenges/roman-numerals/solutions/python/function/solution.py @@ -1,7 +1,13 @@ -from typing import List +from typing import TypedDict import sys -matches_roman_arabic = [ + +class RomanArabicMatch(TypedDict): + arabic: int + roman: str + + +matches_roman_arabic: list[RomanArabicMatch] = [ {'arabic': 1000, 'roman': 'M'}, {'arabic': 900, 'roman': 'CM'}, {'arabic': 500, 'roman': 'D'}, @@ -42,7 +48,7 @@ def convert_roman_to_arabic(roman_number: str) -> int: return arabic_number -input_values: List[str] = [] +input_values: list[str] = [] for value in sys.stdin: input_values.append(value.rstrip('\n')) diff --git a/challenges/sorting-algorithms/solutions/python/bubble-sort/solution.py b/challenges/sorting-algorithms/solutions/python/bubble-sort/solution.py index a66b74d..fe72641 100644 --- a/challenges/sorting-algorithms/solutions/python/bubble-sort/solution.py +++ b/challenges/sorting-algorithms/solutions/python/bubble-sort/solution.py @@ -1,8 +1,7 @@ -from typing import List import sys -def bubble_sort(numbersInput: List[int]) -> List[int]: +def bubble_sort(numbersInput: list[int]) -> list[int]: numbers = list(numbersInput) length = len(numbers) for index_1 in range(length): @@ -14,7 +13,7 @@ def bubble_sort(numbersInput: List[int]) -> List[int]: return numbers -numbers: List[int] = [] +numbers: list[int] = [] for value in sys.stdin: numbers.append(int(value.rstrip('\n'))) diff --git a/challenges/sorting-algorithms/solutions/python/function/solution.py b/challenges/sorting-algorithms/solutions/python/function/solution.py index f5bfc25..68c5fe1 100644 --- a/challenges/sorting-algorithms/solutions/python/function/solution.py +++ b/challenges/sorting-algorithms/solutions/python/function/solution.py @@ -1,7 +1,6 @@ -from typing import List import sys -numbers: List[int] = [] +numbers: list[int] = [] for value in sys.stdin: numbers.append(int(value.rstrip('\n'))) diff --git a/challenges/sorting-algorithms/solutions/python/insertion-sort/solution.py b/challenges/sorting-algorithms/solutions/python/insertion-sort/solution.py index 757cede..b7b456b 100644 --- a/challenges/sorting-algorithms/solutions/python/insertion-sort/solution.py +++ b/challenges/sorting-algorithms/solutions/python/insertion-sort/solution.py @@ -1,8 +1,7 @@ -from typing import List import sys -def insertion_sort(numbersInput: List[int]) -> List[int]: +def insertion_sort(numbersInput: list[int]) -> list[int]: numbers = list(numbersInput) for index_1 in range(1, len(numbers)): current = numbers[index_1] @@ -14,7 +13,7 @@ def insertion_sort(numbersInput: List[int]) -> List[int]: return numbers -numbers: List[int] = [] +numbers: list[int] = [] for value in sys.stdin: numbers.append(int(value.rstrip('\n'))) diff --git a/challenges/sorting-algorithms/solutions/python/merge-sort/solution.py b/challenges/sorting-algorithms/solutions/python/merge-sort/solution.py index e3f3fec..3fa9235 100644 --- a/challenges/sorting-algorithms/solutions/python/merge-sort/solution.py +++ b/challenges/sorting-algorithms/solutions/python/merge-sort/solution.py @@ -1,20 +1,22 @@ -from typing import List, Any +from typing import TypeVar import sys +T = TypeVar('T') -def divide_list(values: List[Any]) -> List[Any]: + +def divide_list(values: list[T]) -> tuple[list[T], list[T]]: middle = len(values) // 2 left = values[middle:] right = values[:middle] - return [left, right] + return (left, right) -def merge(numbers_1: List[int], numbers_2: List[int]) -> List[int]: +def merge(numbers_1: list[int], numbers_2: list[int]) -> list[int]: length_numbers_1 = len(numbers_1) length_numbers_2 = len(numbers_2) index_numbers_1 = 0 index_numbers_2 = 0 - result: List[int] = [] + result: list[int] = [] while index_numbers_1 < length_numbers_1 and index_numbers_2 < length_numbers_2: if numbers_1[index_numbers_1] < numbers_2[index_numbers_2]: result.append(numbers_1[index_numbers_1]) @@ -29,7 +31,7 @@ def merge(numbers_1: List[int], numbers_2: List[int]) -> List[int]: return result -def merge_sort(numbers: List[int]) -> List[int]: +def merge_sort(numbers: list[int]) -> list[int]: if len(numbers) <= 1: return numbers left, right = divide_list(numbers) @@ -37,7 +39,7 @@ def merge_sort(numbers: List[int]) -> List[int]: return merge(left, right) -numbers: List[int] = [] +numbers: list[int] = [] for value in sys.stdin: numbers.append(int(value.rstrip('\n'))) diff --git a/challenges/sudoku/solutions/python/function/Grid.py b/challenges/sudoku/solutions/python/function/Grid.py index 11e3e73..c2df4db 100644 --- a/challenges/sudoku/solutions/python/function/Grid.py +++ b/challenges/sudoku/solutions/python/function/Grid.py @@ -1,13 +1,11 @@ -from typing import List - from Cell import Cell class Grid: - def __init__(self, grid: List[List[int]]) -> None: - data: List[List[Cell]] = [] + def __init__(self, grid: list[list[int]]) -> None: + data: list[list[Cell]] = [] for x in range(len(grid)): - column: List[Cell] = [] + column: list[Cell] = [] for y in range(len(grid[x])): column.append(Cell(grid[x][y], y, x)) data.append(column) diff --git a/challenges/sudoku/solutions/python/function/solution.py b/challenges/sudoku/solutions/python/function/solution.py index 6000521..f8fa53f 100644 --- a/challenges/sudoku/solutions/python/function/solution.py +++ b/challenges/sudoku/solutions/python/function/solution.py @@ -1,13 +1,12 @@ -from typing import List import sys from Sudoku import Sudoku from Grid import Grid -grid_values: List[List[int]] = [] +grid_values: list[list[int]] = [] for value in sys.stdin: row_values = value.rstrip('\n').split(' ') - current_row: List[int] = [] + current_row: list[int] = [] for row_value in row_values: current_row.append(int(row_value)) grid_values.append(current_row) diff --git a/cli/commands/generate/challenge.ts b/cli/commands/generate/challenge.ts index f502c92..a16bd70 100644 --- a/cli/commands/generate/challenge.ts +++ b/cli/commands/generate/challenge.ts @@ -23,7 +23,7 @@ export class GenerateChallengeCommand extends Command { validator: typanion.isString() }) - async execute (): Promise { + async execute(): Promise { try { const challenge = await Challenge.generate({ name: this.challenge, diff --git a/cli/commands/generate/solution.ts b/cli/commands/generate/solution.ts index 6a835e4..a12fdb2 100644 --- a/cli/commands/generate/solution.ts +++ b/cli/commands/generate/solution.ts @@ -35,7 +35,7 @@ export class GenerateSolutionCommand extends Command { validator: typanion.isString() }) - async execute (): Promise { + async execute(): Promise { try { const solution = await Solution.generate({ name: this.solutionName, diff --git a/cli/commands/run/test.ts b/cli/commands/run/test.ts index 088eb21..fb6a52b 100644 --- a/cli/commands/run/test.ts +++ b/cli/commands/run/test.ts @@ -46,11 +46,13 @@ export class RunTestCommand extends Command { description: 'Base of the current branch (usually master)' }) - async execute (): Promise { + async execute(): Promise { console.log() try { if (this.programmingLanguage != null) { - await template.verifySupportedProgrammingLanguage(this.programmingLanguage) + await template.verifySupportedProgrammingLanguage( + this.programmingLanguage + ) } if (this.all) { return await Test.runAllTests(this.programmingLanguage) diff --git a/cli/services/Challenge.ts b/cli/services/Challenge.ts index e626bc4..6a8b3c0 100644 --- a/cli/services/Challenge.ts +++ b/cli/services/Challenge.ts @@ -18,13 +18,15 @@ export class Challenge implements ChallengeOptions { public name: string public path: string - constructor (options: ChallengeOptions) { + constructor(options: ChallengeOptions) { const { name } = options this.name = name - this.path = fileURLToPath(new URL(`../../challenges/${name}`, import.meta.url)) + this.path = fileURLToPath( + new URL(`../../challenges/${name}`, import.meta.url) + ) } - static async generate (options: GenerateChallengeOptions): Promise { + static async generate(options: GenerateChallengeOptions): Promise { const { name, githubUser } = options const challenge = new Challenge({ name }) if (await isExistingPath(challenge.path)) { diff --git a/cli/services/Docker.ts b/cli/services/Docker.ts index 2c7dca1..e1c1e78 100644 --- a/cli/services/Docker.ts +++ b/cli/services/Docker.ts @@ -8,7 +8,7 @@ export class Docker { static MAXIMUM_TIMEOUT = '1 minute' static MAXIMUM_TIMEOUT_MILLISECONDS = ms(Docker.MAXIMUM_TIMEOUT) - public async build (): Promise { + public async build(): Promise { const loader = ora('Building the Docker image').start() try { await execaCommand(`docker build --tag=${Docker.CONTAINER_TAG} ./`) @@ -19,7 +19,7 @@ export class Docker { } } - public async run (input: string): Promise { + public async run(input: string): Promise { const subprocess = execaCommand( `docker run --interactive --rm ${Docker.CONTAINER_TAG}`, { @@ -40,10 +40,14 @@ export class Docker { return stdout } catch (error: any) { if (!isValid) { - throw new Error(`Timeout: time limit exceeded (${Docker.MAXIMUM_TIMEOUT}), try to optimize your solution.`) + throw new Error( + `Timeout: time limit exceeded (${Docker.MAXIMUM_TIMEOUT}), try to optimize your solution.` + ) } if (error.exitCode === Docker.SIGSEGV_EXIT_CODE) { - throw new Error('Docker run failed: SIGSEGV indicates a segmentation fault (attempts to access a memory location that it\'s not allowed to access).') + throw new Error( + "Docker run failed: SIGSEGV indicates a segmentation fault (attempts to access a memory location that it's not allowed to access)." + ) } throw new Error(`Docker run failed: ${error.message as string}`) } diff --git a/cli/services/GitAffected.ts b/cli/services/GitAffected.ts index 9d2da6d..752d687 100644 --- a/cli/services/GitAffected.ts +++ b/cli/services/GitAffected.ts @@ -3,11 +3,14 @@ import { execaCommand } from 'execa' import { Challenge } from './Challenge.js' import { Solution } from './Solution.js' -const solutionsRegex = /challenges\/[\S\s]*\/solutions\/(c|cpp|cs|dart|java|javascript|python|rust|typescript)\/[\S\s]*\/(.*).(c|cpp|cs|dart|java|js|py|rs|ts)/ +const solutionsRegex = + /challenges\/[\S\s]*\/solutions\/(c|cpp|cs|dart|java|javascript|python|rust|typescript)\/[\S\s]*\/(.*).(c|cpp|cs|dart|java|js|py|rs|ts)/ -const dockerRegex = /templates\/docker\/(c|cpp|cs|dart|java|javascript|python|rust|typescript)\/(.*)/ +const dockerRegex = + /templates\/docker\/(c|cpp|cs|dart|java|javascript|python|rust|typescript)\/(.*)/ -const inputOutputRegex = /challenges\/[\S\s]*\/test\/(.*)\/(input.txt|output.txt)/ +const inputOutputRegex = + /challenges\/[\S\s]*\/test\/(.*)\/(input.txt|output.txt)/ export interface GitAffectedOptions { isContinuousIntegration: boolean @@ -18,19 +21,19 @@ export class GitAffected implements GitAffectedOptions { public isContinuousIntegration: boolean public base?: string - constructor (options: GitAffectedOptions) { + constructor(options: GitAffectedOptions) { this.isContinuousIntegration = options.isContinuousIntegration this.base = options.base } - public parseGitOutput (output: string): string[] { + public parseGitOutput(output: string): string[] { return output .split('\n') .map((line) => line.trim()) .filter((line) => line.length > 0) } - public async getFilesUsingBaseAndHead ( + public async getFilesUsingBaseAndHead( base: string, head: string ): Promise { @@ -44,24 +47,29 @@ export class GitAffected implements GitAffectedOptions { } } - public async getUncommittedFiles (): Promise { + public async getUncommittedFiles(): Promise { return await this.getFilesUsingBaseAndHead('HEAD', '.') } - public async getLatestPushedCommit (): Promise { - const latestCommit = this.isContinuousIntegration ? '~1' : '' - const { stdout } = await execaCommand(`git rev-parse origin/master${latestCommit}`) + public async getLatestPushedCommit(): Promise { + const latestCommit = + this.isContinuousIntegration || this.base != null ? '~1' : '' + const { stdout } = await execaCommand( + `git rev-parse origin/master${latestCommit}` + ) return stdout } - public async getUnpushedFiles (): Promise { + public async getUnpushedFiles(): Promise { return await this.getFilesUsingBaseAndHead( await this.getLatestPushedCommit(), '.' ) } - public async getAffectedSolutionsFromFiles (files: string[]): Promise { + public async getAffectedSolutionsFromFiles( + files: string[] + ): Promise { const affectedSolutionsPaths = files.filter((filePath) => { return solutionsRegex.test(filePath) }) @@ -69,18 +77,26 @@ export class GitAffected implements GitAffectedOptions { return dockerRegex.test(filePath) }) const affectedLanguages = affectedDockerPaths.map((filePath) => { - const [,, programmingLanguageName] = filePath.replaceAll('\\', '/').split('/') + const [, , programmingLanguageName] = filePath + .replaceAll('\\', '/') + .split('/') return programmingLanguageName }) const affectedInputOutput = files.filter((filePath) => { return inputOutputRegex.test(filePath) }) - const affectedChallengesFromInputOutput = affectedInputOutput.map((filePath) => { - const [, challengeName] = filePath.replaceAll('\\', '/').split('/') - return new Challenge({ name: challengeName }) - }) - const solutionsChallenges = await Solution.getManyByPaths(affectedSolutionsPaths) - const solutionsDocker = await Solution.getManyByProgrammingLanguages(affectedLanguages) + const affectedChallengesFromInputOutput = affectedInputOutput.map( + (filePath) => { + const [, challengeName] = filePath.replaceAll('\\', '/').split('/') + return new Challenge({ name: challengeName }) + } + ) + const solutionsChallenges = await Solution.getManyByPaths( + affectedSolutionsPaths + ) + const solutionsDocker = await Solution.getManyByProgrammingLanguages( + affectedLanguages + ) const solutions: Solution[] = [...solutionsDocker, ...solutionsChallenges] for (const challenge of affectedChallengesFromInputOutput) { const solutionsByChallenge = await Solution.getManyByChallenge(challenge) @@ -98,7 +114,7 @@ export class GitAffected implements GitAffectedOptions { return solutionsUnique } - public async getAffectedSolutionsFromGit (): Promise { + public async getAffectedSolutionsFromGit(): Promise { let files = [ ...(await this.getUnpushedFiles()), ...(await this.getUncommittedFiles()) diff --git a/cli/services/Solution.ts b/cli/services/Solution.ts index dac5a72..80c99b6 100644 --- a/cli/services/Solution.ts +++ b/cli/services/Solution.ts @@ -35,7 +35,7 @@ export class Solution implements SolutionOptions { public name: string public path: string - constructor (options: SolutionOptions) { + constructor(options: SolutionOptions) { const { programmingLanguageName, challenge, name } = options this.programmingLanguageName = programmingLanguageName this.challenge = challenge @@ -48,7 +48,7 @@ export class Solution implements SolutionOptions { ) } - private async prepareTemporaryFolder (): Promise { + private async prepareTemporaryFolder(): Promise { await createTemporaryEmptyFolder() await copyDirectory(this.path, TEMPORARY_PATH) await template.docker({ @@ -58,13 +58,13 @@ export class Solution implements SolutionOptions { process.chdir(TEMPORARY_PATH) } - public async test (): Promise { + public async test(): Promise { await this.prepareTemporaryFolder() await docker.build() await Test.runAll(this) } - static async generate (options: GenerateSolutionOptions): Promise { + static async generate(options: GenerateSolutionOptions): Promise { const { name, challengeName, programmingLanguageName, githubUser } = options const challenge = new Challenge({ name: challengeName }) if (!(await isExistingPath(challenge.path))) { @@ -88,7 +88,7 @@ export class Solution implements SolutionOptions { return solution } - static async get (options: GetSolutionOptions): Promise { + static async get(options: GetSolutionOptions): Promise { const { name, challengeName, programmingLanguageName } = options const challenge = new Challenge({ name: challengeName @@ -104,7 +104,7 @@ export class Solution implements SolutionOptions { return solution } - static async getManyByChallenge (challenge: Challenge): Promise { + static async getManyByChallenge(challenge: Challenge): Promise { const solutionsPath = path.join(challenge.path, 'solutions') const languagesSolution = (await fs.promises.readdir(solutionsPath)).filter( (name) => { @@ -113,7 +113,9 @@ export class Solution implements SolutionOptions { ) const paths: string[] = [] for (const language of languagesSolution) { - const solutionPath = (await fs.promises.readdir(path.join(solutionsPath, language))).map((solutionName) => { + const solutionPath = ( + await fs.promises.readdir(path.join(solutionsPath, language)) + ).map((solutionName) => { return `challenges/${challenge.name}/solutions/${language}/${solutionName}` }) paths.push(...solutionPath) @@ -121,20 +123,27 @@ export class Solution implements SolutionOptions { return await Solution.getManyByPaths(paths) } - static async getManyByProgrammingLanguages (programmingLanguages?: string[]): Promise { - const languages = programmingLanguages ?? await template.getProgrammingLanguages() - const challengesPath = fileURLToPath(new URL('../../challenges', import.meta.url)) + static async getManyByProgrammingLanguages( + programmingLanguages?: string[] + ): Promise { + const languages = + programmingLanguages ?? (await template.getProgrammingLanguages()) + const challengesPath = fileURLToPath( + new URL('../../challenges', import.meta.url) + ) const challenges = await fs.promises.readdir(challengesPath) const paths: string[] = [] for (const challenge of challenges) { const solutionsPath = path.join(challengesPath, challenge, 'solutions') - const languagesSolution = (await fs.promises.readdir(solutionsPath)).filter( - (name) => { - return name !== '.gitkeep' && languages.includes(name) - } - ) + const languagesSolution = ( + await fs.promises.readdir(solutionsPath) + ).filter((name) => { + return name !== '.gitkeep' && languages.includes(name) + }) for (const language of languagesSolution) { - const solutionPath = (await fs.promises.readdir(path.join(solutionsPath, language))).map((solutionName) => { + const solutionPath = ( + await fs.promises.readdir(path.join(solutionsPath, language)) + ).map((solutionName) => { return `challenges/${challenge}/solutions/${language}/${solutionName}` }) paths.push(...solutionPath) @@ -148,7 +157,7 @@ export class Solution implements SolutionOptions { * @param paths relative to `challenges` (e.g: `challenges/hello-world/solutions/c/function`) * @returns */ - static async getManyByPaths (paths: string[]): Promise { + static async getManyByPaths(paths: string[]): Promise { const solutions: string[] = [] for (const path of paths) { if (await isExistingPath(path)) { diff --git a/cli/services/Test.ts b/cli/services/Test.ts index c6094f7..610a773 100644 --- a/cli/services/Test.ts +++ b/cli/services/Test.ts @@ -39,7 +39,7 @@ export class Test implements TestOptions { public elapsedTimeMilliseconds: number static SUCCESS_MESSAGE = `${chalk.bold.green('Success:')} Tests passed! 🎉` - constructor (options: TestOptions) { + constructor(options: TestOptions) { this.index = options.index this.path = options.path this.isSuccess = options.isSuccess @@ -49,7 +49,7 @@ export class Test implements TestOptions { this.elapsedTimeMilliseconds = options.elapsedTimeMilliseconds } - static printResult (tests: Test[]): void { + static printResult(tests: Test[]): void { const tableResult = [ [ chalk.bold('N°'), @@ -95,11 +95,13 @@ export class Test implements TestOptions { } } - static async runAll (solution: Solution): Promise { + static async runAll(solution: Solution): Promise { const name = `${solution.challenge.name}/${solution.programmingLanguageName}/${solution.name}` const testsPath = path.join(solution.challenge.path, 'test') const testsFolders = await fs.promises.readdir(testsPath) - const testsNumbers = testsFolders.map((test) => Number(test)).sort((a, b) => a - b) + const testsNumbers = testsFolders + .map((test) => Number(test)) + .sort((a, b) => a - b) const tests: Test[] = [] console.log(`${chalk.bold('Name:')} ${name}\n`) for (const testNumber of testsNumbers) { @@ -123,7 +125,7 @@ export class Test implements TestOptions { Test.printResult(tests) } - static async getInputOutput (testPath: string): Promise { + static async getInputOutput(testPath: string): Promise { const inputPath = path.join(testPath, 'input.txt') const outputPath = path.join(testPath, 'output.txt') const input = await fs.promises.readFile(inputPath, { encoding: 'utf-8' }) @@ -133,7 +135,7 @@ export class Test implements TestOptions { return { input, output } } - static async runManyWithSolutions (solutions: Solution[]): Promise { + static async runManyWithSolutions(solutions: Solution[]): Promise { for (const solution of solutions) { await solution.test() console.log('\n------------------------------\n') @@ -142,7 +144,7 @@ export class Test implements TestOptions { return 0 } - static async runAllTests (programmingLanguage?: string): Promise { + static async runAllTests(programmingLanguage?: string): Promise { const solutions = await Solution.getManyByProgrammingLanguages( programmingLanguage != null ? [programmingLanguage] : undefined ) @@ -150,7 +152,7 @@ export class Test implements TestOptions { return 0 } - static async run (options: TestRunOptions): Promise { + static async run(options: TestRunOptions): Promise { const { input, output } = await Test.getInputOutput(options.path) const start = performance.now() const stdout = await docker.run(input) diff --git a/templates/solution/python/solution.py b/templates/solution/python/solution.py index 18a11a4..bfee34b 100644 --- a/templates/solution/python/solution.py +++ b/templates/solution/python/solution.py @@ -1,7 +1,6 @@ -from typing import List import sys -input_values: List[str] = [] +input_values: list[str] = [] for value in sys.stdin: input_values.append(value.rstrip('\n'))