From 3b153a9f1f5fa1f88f8fd651df0dec7762978075 Mon Sep 17 00:00:00 2001 From: Divlo Date: Mon, 4 Oct 2021 10:06:05 +0200 Subject: [PATCH] feat(solutions): add `offset-arrays/python/function` --- .gitpod.yml | 4 +- CONTRIBUTING.md | 2 +- .../solutions/python/function/README.md | 3 + .../solutions/python/function/solution.py | 64 ++++++++++++++ .../solutions/typescript/function/README.md | 3 - .../solutions/typescript/function/solution.ts | 88 ------------------- 6 files changed, 70 insertions(+), 94 deletions(-) create mode 100644 challenges/offset-arrays/solutions/python/function/README.md create mode 100644 challenges/offset-arrays/solutions/python/function/solution.py delete mode 100644 challenges/offset-arrays/solutions/typescript/function/README.md delete mode 100644 challenges/offset-arrays/solutions/typescript/function/solution.ts diff --git a/.gitpod.yml b/.gitpod.yml index c7d97ac..ee503e3 100644 --- a/.gitpod.yml +++ b/.gitpod.yml @@ -3,8 +3,8 @@ image: 'gitpod/workspace-full' tasks: - name: 'programming-challenges' before: 'npm install' - init: 'npm run build && npm install --global' - command: 'programming-challenges' + init: 'npm run build' + command: 'npm install --global && programming-challenges' github: prebuilds: diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ce354ba..c7650aa 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -36,7 +36,7 @@ You can add support for a new language, so you can solve the challenges with eve - Create a new template inside `templates/solution` folder with the file extension of the new programming language, with the basic boilerplate to read from `stdin` and print to `stdout` the result - Every programming language should have at least one working solution for `challenges/hello-world`. -To do so, run the command `programming-challenges generate solution --challenge="hello-world" --github-user="" --language="" --solution="function` + To generate the solution for the `hello-world` challenge with your new language, run the command `programming-challenges generate solution --challenge="hello-world" --github-user="" --language="" --solution="function"` Before submitting the new programming language, make sure it passes all the tests by running `programming-challenges run test --affected` diff --git a/challenges/offset-arrays/solutions/python/function/README.md b/challenges/offset-arrays/solutions/python/function/README.md new file mode 100644 index 0000000..f9a1645 --- /dev/null +++ b/challenges/offset-arrays/solutions/python/function/README.md @@ -0,0 +1,3 @@ +# offset-arrays/python/function + +Created by [@Divlo](https://github.com/Divlo) on 4 October 2021. diff --git a/challenges/offset-arrays/solutions/python/function/solution.py b/challenges/offset-arrays/solutions/python/function/solution.py new file mode 100644 index 0000000..913b724 --- /dev/null +++ b/challenges/offset-arrays/solutions/python/function/solution.py @@ -0,0 +1,64 @@ +from typing import List +import sys + +input_values: List[str] = [] +for value in sys.stdin: + input_values.append(value.rstrip('\n')) + + +class AssignmentArray: + def __init__(self, assignment: str) -> None: + identifier = assignment.split('[')[0] + rest = assignment.split('[')[1] + indexes = rest.split(']')[0] + values_string = rest.split(']')[1] + first_index_string = indexes.split('..')[0] + last_index_string = indexes.split('..')[1] + first_index = int(first_index_string) + last_index = int(last_index_string) + numbers_string = values_string.split('=')[1] + numbers_string_array = numbers_string.strip().split(' ') + numbers = [int(number) for number in numbers_string_array] + values = {} + index = 0 + for virtual_index in range(first_index, last_index + 1): + values[virtual_index] = numbers[index] + index += 1 + + self.identifier = identifier + self.first_index = first_index + self.last_index = last_index + self.values = values + + def get_by_index(self, index: int) -> int: + return self.values[index] + + +assignments = {} +assignments_length = int(input_values[0]) +assignment_index = 0 +for assignment_index in range(0, assignments_length): + assignment = input_values[assignment_index + 1] + assignment_array = AssignmentArray(assignment) + assignments[assignment_array.identifier] = assignment_array +assignment_index += 1 + +operation = input_values[assignment_index + 1] +simplified_operation = operation.split(']')[0] +nested = simplified_operation.split('[') +last_operation_index = len(nested) - 1 +operation_identifiers = nested[0:last_operation_index] +operation_index = 0 +operations_index = int(nested[last_operation_index]) + +current_value = None +for index in range(len(operation_identifiers) - 1, -1, -1): + current_identifier = operation_identifiers[index] + if current_value == None: + current_value = assignments[current_identifier].get_by_index( + operations_index) + else: + current_value = assignments[current_identifier].get_by_index( + current_value) + +print(current_value) diff --git a/challenges/offset-arrays/solutions/typescript/function/README.md b/challenges/offset-arrays/solutions/typescript/function/README.md deleted file mode 100644 index 997a53f..0000000 --- a/challenges/offset-arrays/solutions/typescript/function/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# offset-arrays/typescript/function - -Created by [@Divlo](https://github.com/Divlo) on 29 June 2021. diff --git a/challenges/offset-arrays/solutions/typescript/function/solution.ts b/challenges/offset-arrays/solutions/typescript/function/solution.ts deleted file mode 100644 index 8884131..0000000 --- a/challenges/offset-arrays/solutions/typescript/function/solution.ts +++ /dev/null @@ -1,88 +0,0 @@ -import readline from 'node:readline' - -const input: string[] = [] -const readlineInterface = readline.createInterface({ - input: process.stdin, - output: process.stdout -}) -readlineInterface.on('line', (value) => { - input.push(value) -}) -readlineInterface.on('close', solution) - -interface AssignmentValues { - [key: string]: number -} - -interface Assignments { - [key: string]: AssignmentArray -} - -class AssignmentArray { - public identifier: string - public firstIndex: number - public lastIndex: number - public values: AssignmentValues - - constructor(assignment: string) { - const [identifier, rest] = assignment.split('[') - const [indexes, valuesString] = rest.split(']') - const [firstIndexString, lastIndexString] = indexes.split('..') - const firstIndex = parseInt(firstIndexString) - const lastIndex = parseInt(lastIndexString) - - const [, numbersString] = valuesString.split('=') - const numbersStringArray = numbersString.trim().split(' ') - const numbers = numbersStringArray.map((number) => Number(number)) - const values: AssignmentValues = {} - let index = 0 - for ( - let virtualIndex = firstIndex; - virtualIndex <= lastIndex; - virtualIndex++ - ) { - values[virtualIndex] = numbers[index] - index++ - } - - this.identifier = identifier - this.firstIndex = firstIndex - this.lastIndex = lastIndex - this.values = values - } - - public getByIndex(index: number): number { - return this.values[index] - } -} - -function solution(): void { - const assignments: Assignments = {} - const assignmentsLength = parseInt(input[0]) - let assignmentIndex = 0 - for ( - assignmentIndex = 0; - assignmentIndex < assignmentsLength; - assignmentIndex++ - ) { - const assignment = input[assignmentIndex + 1] - const assignmentArray = new AssignmentArray(assignment) - assignments[assignmentArray.identifier] = assignmentArray - } - const operation = input[assignmentIndex + 1] - const [simplifiedOperation] = operation.split(']') - const nested = simplifiedOperation.split('[') - const lastOperationIndex = nested.length - 1 - const operationIdentifiers = nested.slice(0, lastOperationIndex) - const operationIndex = parseInt(nested[lastOperationIndex]) - let currentValue: number | null = null - for (let index = operationIdentifiers.length - 1; index >= 0; index--) { - const currentIdentifier = operationIdentifiers[index] - if (currentValue == null) { - currentValue = assignments[currentIdentifier].getByIndex(operationIndex) - } else { - currentValue = assignments[currentIdentifier].getByIndex(currentValue) - } - } - console.log(currentValue) -}