mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-12-08 00:45:29 +01:00
feat(solutions): add offset-arrays/python/function
This commit is contained in:
parent
a9287a87fe
commit
3b153a9f1f
@ -3,8 +3,8 @@ image: 'gitpod/workspace-full'
|
|||||||
tasks:
|
tasks:
|
||||||
- name: 'programming-challenges'
|
- name: 'programming-challenges'
|
||||||
before: 'npm install'
|
before: 'npm install'
|
||||||
init: 'npm run build && npm install --global'
|
init: 'npm run build'
|
||||||
command: 'programming-challenges'
|
command: 'npm install --global && programming-challenges'
|
||||||
|
|
||||||
github:
|
github:
|
||||||
prebuilds:
|
prebuilds:
|
||||||
|
@ -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
|
- 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`.
|
- 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="<your-github-user>" --language="<your-new-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="<your-github-user>" --language="<your-new-language>" --solution="function"`
|
||||||
|
|
||||||
Before submitting the new programming language, make sure it passes all the tests by running `programming-challenges run test --affected`
|
Before submitting the new programming language, make sure it passes all the tests by running `programming-challenges run test --affected`
|
||||||
|
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
# offset-arrays/python/function
|
||||||
|
|
||||||
|
Created by [@Divlo](https://github.com/Divlo) on 4 October 2021.
|
@ -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)
|
@ -1,3 +0,0 @@
|
|||||||
# offset-arrays/typescript/function
|
|
||||||
|
|
||||||
Created by [@Divlo](https://github.com/Divlo) on 29 June 2021.
|
|
@ -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)
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user