codility | Ruby solutions to Codility tasks : https : //codility | Learning library
kandi X-RAY | codility Summary
kandi X-RAY | codility Summary
Ruby solutions to Codility tasks: The MIT License (MIT). Copyright (c) 2014 Jan Suchal.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of codility
codility Key Features
codility Examples and Code Snippets
Community Discussions
Trending Discussions on codility
QUESTION
There's this demo task on Codility that I'm getting low-end score for, even though it's really simple:
Given the array $a
, which contains (non-unique) integers in range -1,000,000
to 1,000,000
, find lowest integer that does not exist in array.
I've tried first using array_filter on original array to filter out only positive integers. Another idea to speed it up is only extract unique values. From there only a basic for loop that stops at $i
that's not in array.
ANSWER
Answered 2022-Apr-11 at 21:02There are a lot of unnecessary and expensive steps (full-array iterations) in what you do.
- Instead of
array_unique
, you canarray_flip
for an array with unique keys. - If you have a large array,
in_array
at each iteration is expensive. - However, checking
isset
on an array key is a cheap operation. - No need to filter negative integers, or sort any of it:
- ... just loop from 1 up++ and check matches; first "no-match" is the lowest missing integer.
QUESTION
I'm trying to understand how the XOR operation managed to solve the problem PermMissingElem
I know one of the properties of XOR is that:
...ANSWER
Answered 2022-Mar-15 at 14:01The XOR operation is commutative, the order of application does not matter.
Two XORs with the same value cancel each other.
So, starting from 0 and applying any sequence of XORs, followed by the same sequence that includes an extra element, only the effect of the extra element remains.
E.g. 0 ⊕2⊕3⊕1⊕5 ⊕1⊕2⊕3⊕4⊕5 has the same effect as 0⊕1⊕1⊕2⊕2⊕3⊕3⊕4⊕5⊕5, or 0⊕4.
QUESTION
I am trying to use recursion to solve the OddOccurrencesInArray Problem in Codility, in which
- we are given an array with N elements, N is always odd
- all of the elements of the array except for one has a total even number of occurrences
- we need to write code that returns the one unpaired value
For example, if the array given is [9, 3, 9, 3, 7, 9, 9], the code must return 7, because that is the only element in the array which is unpaired.
My solution pseudocode/thought process was:
- sort the array
- if the first two elements are equal to each other, remove them and run the solution algorithm again recursively on the array minus the first two elements (after sorting) i.e. if the unpaired element is not found, we keep reducing the size of the array
- if the first two elements are NOT equal to each other, the first element of the array must be the unpaired item
My implementation was:
...ANSWER
Answered 2022-Jan-05 at 18:47You don't return anything from your recursive call, which means you are returning None
.
QUESTION
I'm solving the MinPerimeterRectangle problem from Codility:
An integer N is given, representing the area of some rectangle.
The area of a rectangle whose sides are of length A and B is A * B, and the perimeter is 2 * (A + B).
The goal is to find the minimal perimeter of any rectangle whose area equals N. The sides of this rectangle should be only integers.
For example, given integer N = 30, rectangles of area 30 are:
(1, 30), with a perimeter of 62, (2, 15), with a perimeter of 34, (3, 10), with a perimeter of 26, (5, 6), with a perimeter of 22. Write a function:
def solution(N)
that, given an integer N, returns the minimal perimeter of any rectangle whose area is exactly equal to N.
For example, given an integer N = 30, the function should return 22, as explained above.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [1..1,000,000,000].
I initially used this code:
''' import math
...ANSWER
Answered 2022-Jan-10 at 15:17The answer to your question is pretty simple. I assume that you are aware that the minimum of perimeter occurs when the length is equal to breadth meaning that if there was no constraint that l and b have to be natural numbers, you could have twice the square root of your area as your perimeter, but because you have the constraint, both the length and breadth should be as close to each other as possible.
ex: For an area of 30, the minimum perimeter is at (5,6) and for 20, it is at (4,5) and for 12, it is at (3,4).......so on.
Translating this to nerdy computer language would mean that you should get the pair of dimensions in square root of are number of iterations i.e O(n^0.5) which you very well implemented. Now the problem with prime numbers is that they have no other factors apart from one and themselves......meaning that even you started from the square root of the area, you have to iterate all the way up until n or all the way down until 1. For large numbers, iterating back to 1 is easier than iterating up to n (remember that this is n^0.5 and not n/2 and hence the twist). To better explain this you can think of it this way, iterating from 5 to 1 is easier than 5 to 25 and iterating from 8 to 1 is faster than 8 to 64 :).
Hence you were able to find the minimal pair sooner when you iterated down the ladder rather than up. Hope this answers your question.
Happy Coding!
QUESTION
I have already read through the answers on this question - Codility NailingPlanks. This is not a duplicate, as I'm trying to solve this problem using a different approach - instead of running a binary search on the planks that a given nail can cover, I'm trying to run it on the total number of nails required to cover all the planks.
This is my code:
...ANSWER
Answered 2022-Jan-02 at 15:26The problem with your if
condition can be seen with this sample input:
QUESTION
I'm trying to solve the Codility FibFrog problem and I came up with the following solution:
...ANSWER
Answered 2021-Dec-31 at 18:49Please refer to the first point in my previous answer.
If len(A) = 100000, you are calculating 100003 fibonacci numbers, while we only need fibonacci numbers which are less than 100k, which would be <30 of them.
The current fibonacci
function is still returning N
fibonacci numbers instead of just returning fibonacci numbers which are less than N
. For N=100k
, it should be just 25 numbers instead of over 100k.
Please update your fibonacci
function to this -
QUESTION
I'm trying to solve the FibFrog Codility problem and I came up with the following approach:
- If
len(A)
is 0 we know we can reach the other side in one jump. - If
len(A) + 1
is a fibonacci number, we can also reach it in one jump. - Else, we loop through A, and for the positions we can reach, we check if we can either reach them directly from -1 using a fibonacci number
(idx + 1 in fibonaccis)
or if we can reach them by first jumping to another position (reachables
) and then jumping to the current position. In either case, we also check if we can go from the current position to the end of the river - if we can, then we can return because we found the minimum number of steps required. - Finally, if
unreachable
isTrue
once this loop completes, this means we can't reach any position using a Fibonacci number, so we return -1.
I'm getting 83% correctness and 0% performance with this approach.
I understand the solution is O(n^2)
, assuming the array consists of only 1
, the nested loop for v in reachables:
would run n
times - however I'm not sure how else I can compute this, since for each of the positions I need to check whether we can reach it from the start of the array, or from any previous positions using a fibonacci number.
ANSWER
Answered 2021-Dec-31 at 10:09Some suggestions for improving performance of your algorithm -
- If
len(A) = 100000
, you are calculating 100003 fibonacci numbers, while we only need fibonacci numbers which are less than 100k, which would be <30 of them. - Your solution is
O(n^4)
, since eachX in reachables
orY in fibonaccis
operation isO(N)
whereN
islen(A)
. (and length offibonaccis
beingN
because of above issue) - Since you are doing a lot of
item in list
operations onfibonaccis
andreachables
, consider making it aset
or adictionary
for faster(O(1)
instead ofO(n)
) lookup. - Even with the above changes, the algorithm would be
O(N^2)
because of nested looping acrossA
andreachables
, so you need to come up with a better approach.
With your existing implementation, you need to traverse through all the paths and then in the end you will get the smallest number of jumps.
Instead of this approach, if you start at 0
, and then keep a count of the number of jumps you have taken so far, and maintain how far(and to which numbers) you can reach after each jump then you can easily find the minimum jumps required to reach the end. (this will also save on redundant work you would have to do in case you have all 1
s in A.
e.g. for
QUESTION
Recently, I'm practicing code exercises in Codility. Here you can find the problem, it is in the Exercises 6 - SQL section. Just start a test to see the problem description! SqlEventsDelta
Problem Define:
I wrote this solution to the SqlEventDelta Question in SQLite. It works fine in local tool But, It was not working in web tool.
Can anyone give any advice on how can I solve this problem?
※ I searched this problem in Stackoverflow and I know a better code then my own way. But, If possible, I wanna use my own SQLite code logic and function.
...ANSWER
Answered 2021-Oct-25 at 13:58I tried to use a somehow naive approach. I'm aware that it is very bad for performance due to many subqueries but the catch here is the "DISTINCT ON" of PostgreSQL, however I got 100% 😃
Hope you like it!
QUESTION
I've written the below algorithm as a solution to Codility Flags. This passes the correctness checks, however it times out on most of the performance checks.
The complexity of this should be O(m**2)
where m
is the number of peaks in A
and n
is the length of A
. However, the while potentialK > maxFlags
loop should only execute until a suitable number of flags is found which satisfies the criteria. I'm not sure how to further optimise this for time complexity.
ANSWER
Answered 2021-Dec-28 at 23:28I managed to optimize it as follows:
Since the distance between the individual flags has to be >= the number of flags, we know the maximum number of flags will be the root of the last element of peaks - the first element of peaks: sqrt(peaks[-1] - peaks[0])
I was then able to update the initial value of potentialK to
QUESTION
I'm practicing on Codility. There is an Easy Level question, yet I'm stuck on performance. The test result analysis marks this code as O(N**2), but obviously there are not any nested loops. Can anyone tell me why my code is O(N**2)?
...ANSWER
Answered 2021-Dec-17 at 16:37The other loop is hidden in the remove method of ArrayList
. The remove method of ArrayList
is O(N) because it has to shift the elements to fill the gap.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install codility
On a UNIX-like operating system, using your system’s package manager is easiest. However, the packaged Ruby version may not be the newest one. There is also an installer for Windows. Managers help you to switch between multiple Ruby versions on your system. Installers can be used to install a specific or multiple Ruby versions. Please refer ruby-lang.org for more information.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page