johnmee / codility Goto Github PK
View Code? Open in Web Editor NEWPython solutions to exercises and tests at http://codility.com/
Python solutions to exercises and tests at http://codility.com/
from math import ceil
import random
import unittest
MAXLENGHT = 100000
MINLENGHT = 3
RANGEVALUE = (-2147483648,2147483648)
def solution(A):
if not isinstance(A, list):
raise TypeError("Input A must be a List.")
if not len(A):
return 0
if len(A) < MINLENGHT:
return 0
if len(A) > MAXLENGHT:
raise ValueError(f"Input A should not be greater than {MAXLENGHT}")
A.sort()
for i in range(len(A) - 2):
if A[i] > 0 and A[i] <= A[i+1] and (A[i]+A[i+1]) > A[i+2]:
return 1
return 0
class TestExercise(unittest.TestCase):
def test_simple(self):
self.assertEqual(solution([-100, 2, 4, 5]), 1)
self.assertEqual(solution([2,3,5]), 0)
self.assertEqual(solution([3,3,5]), 1)
self.assertEqual(solution([2,2,2]), 1)
self.assertNotEqual(solution([1,2,3]), 1)
self.assertEqual(solution([10,20,30]), 0)
self.assertEqual(solution([10,11,20]), 1)
def test_type(self):
self.assertRaises(TypeError, solution, {})
self.assertRaises(TypeError, solution, "5,5,5")
def test_value(self):
self.assertRaises(ValueError, solution, [1,1]*MAXLENGHT)
def test_extreme(self):
print(solution([random.randint(RANGEVALUE[0], RANGEVALUE[1]) for _ in range(MAXLENGHT)]))
print(solution([random.randint(RANGEVALUE[0], -1) for _ in range(MAXLENGHT)]))
print(solution([random.randint(-10, -1) for _ in range(MAXLENGHT)]))
if __name__ == '__main__':
unittest.main()
Unfortunately only screenshots
https://github.com/larytet-py/codility
I wonder how to solve the following
N integers in array of [1..N] In one move you inc/dec the value by 1
After every move all number should remain within [1..N]
What is the smallest number of steps to make all values distinct?
[1,2,1] can be changed in 2 steps [1,2,2], [1,2,3]
[6,2,3,5,6,3] 4 steps [6,2,1,5,3]
My idea was https://github.com/larytet-py/codility/blob/main/distinct-integers.py
If I enter the input like: solution(3, [3, 3, 1, 1, 4]), its showing 4 output. It should show -1, because 2 index does not exists in list and frog can only cross if all (1 to X) positions are filled.
Explain the algorithms in the comment sections.
solution(1, [1,2,2]), [2]),
you wrote the answer [2]. but the answer should be [1]
in Test_Singles, place 3.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.