0% found this document useful (0 votes)
972 views14 pages

Codility Python Test Solutions Guide

The code counts the number of elements in a 2D array that are located in a subarray with equal row and column sums. It calculates the row and column sums, and then iterates through each element, comparing the sums of the subarrays to the left/above and right/below to determine if they are equal, incrementing the count if they are.

Uploaded by

Ramya Rams
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
972 views14 pages

Codility Python Test Solutions Guide

The code counts the number of elements in a 2D array that are located in a subarray with equal row and column sums. It calculates the row and column sums, and then iterates through each element, comparing the sums of the subarrays to the left/above and right/below to determine if they are equal, incrementing the count if they are.

Uploaded by

Ramya Rams
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • Q1
  • Ans.1
  • Q2
  • Ans.2
  • Q3
  • Ans.3
  • Q4
  • Ans.4

class Solution {

public static int solution(int N,int[] A,int[] B){


int[] arr=new int[N];
int[] arr1=new int[[Link]+1];
int sum=0,temp=N;
for(int i=0;i<[Link];i++){
arr[A[i]-1]++;
arr[B[i]-1]++;
}
for(int i=0;i<N;i++){
arr1[arr[i]]++;
}

for(int i=[Link];i>0;i--){
sum +=((2*temp*arr1[i]-arr1[i]arr1[i]+arr1[i])/2)(i);
temp-=arr1[i];
}
return sum;
}
}
public int solution(String direction, int radius, int[] X, int[] Y) {
int count=0;
switch (direction) {
case "U":
for(int i=0;i<[Link];i++) {
double z=180*(Math.atan2(X[i],Y[i]))/([Link]);
double r=[Link]((X[i]*X[i])+(Y[i]*Y[i]));
if(r<=radius && (z<=45 && z>=-45)){
count++;
}
}
break;

case "R":
for(int i=0;i<[Link];i++) {
double z=180*(Math.atan2(X[i],Y[i]))/([Link]);
double r=[Link]((X[i]*X[i])+(Y[i]*Y[i]));
if(r<=radius && (z<=135 && z>=45)){
count++;
}
}
break;
case "D":
for(int i=0;i<[Link];i++) {
double z=180*(Math.atan2(X[i],Y[i]))/([Link]);
double r=[Link]((X[i]*X[i])+(Y[i]*Y[i]));
if(r<=radius && ((z<=180 & z>=135) || (z<=-135 &
z>=-180))){
count++;
}
}
break;

case "L":
for(int i=0;i<[Link];i++) {
double z=180*(Math.atan2(X[i],Y[i]))/([Link]);
double r=[Link]((X[i]*X[i])+(Y[i]*Y[i]));
if(r<=radius && (z<=-45 && z>=-135)){
count++;
}
}
break;

}
return count;
}
}
def solution(S,T):
if abs(len(S)-len(T))>=2:
return "IMPOSSIBLE"
if S==T:
return "EQUAL"
if abs(len(S)-len(T))==1:
if len(S)>len(T):
if S[:-1]==T:
return "REMOVE "+S[-1]
else:
return "IMPOSSIBLE"
else:
if T[1:]==S:
return "INSERT "+T[0]
else:
return "IMPOSSIBLE"
if len(S)==len(T):
flag=0
res=""
for i in range(len(S)):
if S[i]!=T[i]:
flag +=1
res +=S[i]
if flag==2:
return f"SWAP {rs[0]} {res[1]}"
else:
return "IMPOSSIBLE"
class Solution{
public static int solution(int[][] arr)
{
int N=[Link];

int M=arr[N-1].length;

int[] row=new int[N];

int[] col=new int[M];

int x=0,y=0;

int sumrow=0,colsum=0;

int rowUS=0,rowLS=0,colLS=0,colRS=0;

int a=0,b=0,count=0;

for(int i=0;i<N;i++) {

sumrow=0;
for(int j=0;j<M;j++) {

sumrow+=arr[i][j];

row[x++]=sumrow;

for(int i=0;i<M;i++) {

colsum=0;

for(int j=0;j<N;j++) {

colsum+=arr[j][i];

col[y++]=colsum;

}
//[Link]([Link](row));

//[Link]([Link](col));

for(int i=0;i<N;i++)

a=0;

//[Link]("i"+i);

rowUS=0;

while(a<i)

rowUS+=row[a++];

//[Link]("rowUS"+rowUS);

a++;

//[Link]("a"+a);
rowLS=0;

while(a<N)

rowLS+=row[a++];

//[Link]("rowLS"+rowLS);

//[Link]("aLS"+a);

if(rowUS==rowLS)

for(int j=0;j<M;j++)

//[Link]("j"+j);

b=0;

colLS=0;
while(b<j)

colLS+=col[b++];

//[Link]("colLS"+colLS);

b++;

colRS=0;

while(b<M)

colRS+=col[b++];

//[Link]("colRS"+colRS);

if(colLS==colRS) {

//[Link]("arr"+arr[i][j]);

count++;

}
}
}
}
return count;
}
}

Common questions

Powered by AI

Solution 4 employs nested loops to iterate through possible splitting lines for rows and columns. The outer loops calculate cumulative sums (rowUS, rowLS, colLS, colRS) for each row and column split scenario. By using these loops effectively, the solution checks each potential equilibrium split where cumulative sums match, allowing for a comprehensive analysis of partition combinations, identifying balanced divisions .

In Solution 4, the 'row' array stores cumulative sums of individual rows, while 'col' stores cumulative sums of columns. The solution uses these arrays to calculate upper and lower sums when potential splitting lines are considered. By comparing these cumulative sums, the solution determines if a symmetry exists when the sums above and below or to the left and right of the split are equal, indicating a balanced division .

The solution returns 'IMPOSSIBLE' if the absolute difference in lengths of S and T is 2 or more. If the lengths differ by 1, 'IMPOSSIBLE' is returned unless S without its last character equals T or T without its first character equals S. If lengths are equal, 'IMPOSSIBLE' is returned unless exactly one character differs between S and T .

The approach calculates cumulative sums for each row and column. It iterates over possible splitting rows, calculating upper and lower sums (rowUS, rowLS) and compares them. For split rows where sums are equal, it iterates over the columns to compare left and right column sums (colLS, colRS). When both row and column splits are valid, it increases the count. The solution checks splits for potential equilibrium points across rows and columns .

Solution 3 checks for a SWAP operation by counting discrepancies between strings S and T. If the strings have the same length and exactly two characters differ, it attempts to swap these differing characters in S to see if the result matches T. If this is possible, it returns the swapped characters, otherwise 'IMPOSSIBLE' .

The function calculates the angle 'z' using the arc tangent of the coordinates (X[i], Y[i]), converted to degrees. It calculates the distance 'r' from the origin to each point. Depending on the direction ('U', 'R', 'D', 'L'), it checks if the point's distance 'r' is within the given radius and if 'z' falls within specific degree ranges corresponding to each direction. If both conditions are met, the point is counted .

Solution 2 uses the atan2 function to determine angles in radians relative to the coordinate plane, which are then converted to degrees. Depending on the directional case (U, R, D, L), the solution checks if the point's angle falls within a specific angular sector (e.g., -45 to 45 degrees for 'U'). This check ensures points are within the acceptable angular range for each direction .

The Java code initializes an array 'arr' with size N and another array 'arr1' with size A.length+1. It increments indices in 'arr' based on elements in A and B. It increments indices in 'arr1' where 'arr' has specific values. Next, it calculates 'sum' by iterating from the maximum index of A backwards to 1, using a formula (2*temp*arr1[i]-arr1[i]*arr1[i]+arr1[i])/2 * i and decreases 'temp' by 'arr1[i]' after each calculation .

Solution 1 incrementally updates an array 'arr' based on input arrays A and B to accumulate counts for each index. Then, it uses array 'arr1' to tally occurrences of these counts. This dual-layer count informs the computation of 'sum', which multiplies count occurrence probabilities with a decreasing 'temp' value to iteratively build the final sum, reflecting a complex summation and weighting scheme in relation to index appearances .

In Solution 2, 'r', the radial distance from the origin to a point (X[i], Y[i]), determines if the point lies within the specified radius. The angle 'z', derived from atan2, establishes the point's angular position relative to the origin. Both 'r' and 'z' are used to confirm a point's inclusion within a sector defined by radius and direction-specific angular boundaries (e.g., -45 to 45 degrees for 'U'). Only points satisfying both conditions increment the count .

 
 
 
 
 
 
 Q.1
class Solution {     
    public static int solution(int N,int[] A,int[] B){ 
int[] arr=new int[N]; 
int[] arr1=new int
 
 
 
 
 
 
 Q.2
public int solution(String direction, int radius, int[] X, int[] Y) { 
 
 
int count=0; 
 
 
switch (direction) { 
 
 
case
case "D": 
 
 
 
for(int i=0;i<X.length;i++) { 
 
 
 
 
double z=180*(Math.atan2(X[i],Y[i]))/(Math.PI); 
 
 
 
 
double r
 
 
 
 
 
}
 
 
 
 
 
 
 
 Q.3
def solution(S,T): 
    if abs(len(S)-len(T))>=2: 
        return "IMPOSSIBLE" 
    if S==T: 
        return "EQUAL" 
    i
 Q.4
class Solution{ 
    public static int solution(int[][] arr) 
{ 
    int N=arr.length; 
 
    int M=arr[N-1].length;
for(int j=0;j<M;j++) { 
 
            sumrow+=arr[i][j]; 
 
        } 
 
    row[x++]=sumrow; 
 
    } 
 
    for(int

You might also like