Skip to content

Instantly share code, notes, and snippets.

@aydinemre
Created February 24, 2022 08:06
Show Gist options
  • Save aydinemre/7ca4e4d7a39cf63d0639221bf4572d03 to your computer and use it in GitHub Desktop.
Save aydinemre/7ca4e4d7a39cf63d0639221bf4572d03 to your computer and use it in GitHub Desktop.
Codility Test Case
def solution(A: list) -> int:
"""
Get list of integers and return the number of trees with aesthetics if remove one element.
:param A: List of integers
:return: Number of trees with aesthetics if remove one element from the list
"""
cutting_way_count = 0
if is_tree_aesthetics(A):
# if the original list is a tree with aesthetics return 0
return 0
for index, item in enumerate(A):
# get all possible combinations of the list without the index element
sub_list = A[:index] + A[index + 1:]
if is_tree_aesthetics(sub_list):
cutting_way_count += 1
if cutting_way_count == 0:
return -1
return cutting_way_count
def is_tree_aesthetics(l: list) -> bool:
"""
Get list of integers and return True if it is a tree with aesthetics.
:param l: list of integers
:return: True if it is a tree with aesthetics
"""
for i, item in enumerate(l[1:-1]):
if not ((l[i] < item and item > l[i + 2]) or (l[i] > item and item < l[i + 2])):
return False
return True
assert is_tree_aesthetics([1, 3, 1, 2]) == True
assert is_tree_aesthetics([5, 3, 6, 2]) == True
assert is_tree_aesthetics([5, 3, 8, 7, 9, 5]) == True
assert is_tree_aesthetics([5, 3, 8, 7, 9, 5, 8]) == True
assert is_tree_aesthetics([5, 3, 8, 7, 9, 5, 3]) == False
assert is_tree_aesthetics([1, 2, 3, 4]) == False
assert solution([3, 4, 5, 3, 7]) == 3
assert solution([1, 3, 1, 2]) == 0
assert solution([1, 2, 3, 4]) == -1
assert solution([3, 1, 5, 3, 5, 3]) == 0
assert solution([1, 5, 3, 5, 3]) == 0
"""
Result:
Task 1
Python
Correctness
100%
Performance
Task score
100%
Example test cases
Passed 3 out of 3
Correctness test cases
Passed 8 out of 8
"""
from operator import itemgetter
from typing import Generator
def solution(A: list, X: int, Y: int) -> int:
return min(map(sum, find_possible_sessions(A, X, Y)))
def find_possible_sessions(A: list, X: int, Y: int) -> Generator:
for i in range(len(A)):
possible_indexes = [j for j in range(i, len(A), Y)]
if len(possible_indexes) == X:
yield itemgetter(*possible_indexes)(A)
assert solution([4, 2, 3, 7], 2, 2) == 7
assert solution([10, 3, 4, 7], 2, 3) == 17
assert solution([4, 2, 5, 4, 3, 5, 1, 4, 2, 7], 3, 2) == 6
assert solution([5, 2, 1, 3], 2, 2) == 5
"""
Task 2
Python
Correctness
60%
Performance
0%
Task score
27%
Example test cases
Passed 3 out of 3
Correctness test cases
Passed 3 out of 5
Performance test cases
Passed 0 out of 4
Submission date
2022-02-23 23:31 +03
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment