1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-12-08 00:45:29 +01:00

fix(challenges): update consecutive-numbers

A little bit harder by having as input consecutive numbers needed to consider
This commit is contained in:
Divlo 2021-06-29 17:35:54 +02:00
parent 5b2b72df16
commit 9a700a0220
No known key found for this signature in database
GPG Key ID: 185ED2F15F104E52
13 changed files with 71 additions and 53 deletions

1
.gitignore vendored
View File

@ -22,3 +22,4 @@ coverage
# misc # misc
.DS_Store .DS_Store
temp temp
tmp

View File

@ -4,26 +4,42 @@ Created by [@Divlo](https://github.com/Divlo) on 28 June 2021.
## Instructions ## Instructions
Write a function which takes a list of integers, and which returns the (possibly empty) list of pairs of successive consecutive integers that there may be in the list. Write a function which takes a list of integers, and which returns the list of of successive consecutive integers that there may be in the list.
### Input First input, is the number of consecutive numbers needed to consider it as "consecutive", the second input is the list of integers.
```txt
7
1
2
5
3
4
```
### Output
```txt
1, 2
3, 4
```
## Examples ## Examples
See the `test` folder for examples of input/output. See the `test` folder for examples of input/output.
### Example 1
#### Input
```txt
2
5 ; 1 ; 2 ; 3 ; 8 ; -5 ; -4 ; 7
```
#### Output
```txt
1 ; 2
2 ; 3
-5 ; -4
```
### Example 2
#### Input
```txt
3
5 ; 1 ; 2 ; 3 ; 8 ; -5 ; -4 ; 7
```
#### Output
```txt
1 ; 2 ; 3
```

View File

@ -6,21 +6,28 @@ for value in sys.stdin:
input_values.append(value.rstrip('\n')) input_values.append(value.rstrip('\n'))
def consecutive_numbers(numbers: List[int]) -> List[List[int]]: def consecutive_numbers(numbers: List[int], couple_length: int) -> List[List[int]]:
consecutive_numbers_pairs: List[List[int]] = [] result: List[List[int]] = []
numbers_length = len(numbers) numbers_length = len(numbers)
for index in range(numbers_length): for index in range(numbers_length):
number = numbers[index] consecutive: List[int] = [numbers[index]]
is_last_number = index == numbers_length - 1 for couple_index in range(1, couple_length, 1):
if not is_last_number and number + 1 == numbers[index + 1]: is_last_number = index + couple_index == numbers_length
consecutive_numbers_pairs.append([number, number + 1]) if is_last_number:
return consecutive_numbers_pairs break
if (numbers[index] + couple_index == numbers[index + couple_index]):
consecutive.append(numbers[index] + couple_index)
is_consecutive = len(consecutive) == couple_length
if is_consecutive:
result.append(consecutive)
return result
numbers: List[int] = [] numbers: List[int] = []
for value in input_values: for value in input_values[1].split(' ; '):
numbers.append(int(value)) numbers.append(int(value))
consecutive_numbers_pairs = consecutive_numbers(numbers) result = consecutive_numbers(numbers, int(input_values[0]))
for pairs in consecutive_numbers_pairs: for consecutive in result:
print(f"{pairs[0]} ; {pairs[1]}") consecutive = [str(number) for number in consecutive]
print(' ; '.join(consecutive))

View File

@ -1,6 +1,2 @@
7
1
2 2
5 7 ; 1 ; 2 ; 5 ; 3 ; 4
3
4

View File

@ -0,0 +1,2 @@
2
1 ; 4 ; 3 ; 5

View File

@ -1,4 +1,2 @@
1 2
4 1 ; 4 ; 5 ; 3
3
5

View File

@ -0,0 +1 @@
4 ; 5

View File

@ -1,4 +1,2 @@
1 2
4 5 ; 1 ; 2 ; 3 ; 8 ; -5 ; -4 ; 7
5
3

View File

@ -1 +1,3 @@
4 ; 5 1 ; 2
2 ; 3
-5 ; -4

View File

@ -1,8 +1,2 @@
5
1
2
3 3
8 5 ; 1 ; 2 ; 3 ; 8 ; -5 ; -4 ; 7
-5
-4
7

View File

@ -1,3 +1 @@
1 ; 2 1 ; 2 ; 3
2 ; 3
-5 ; -4

View File

@ -0,0 +1,2 @@
3
5 ; 1 ; 2 ; 3 ; 4 ; 5 ; -4 ; 7

View File

@ -0,0 +1,3 @@
1 ; 2 ; 3
2 ; 3 ; 4
3 ; 4 ; 5