codility-3-2-FrogJmp

FrogJmp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29


A small frog wants to get to the other side of the road. The frog is currently located at position X and wants to get to a position greater than or equal to Y. The small frog always jumps a fixed distance, D.

Count the minimal number of jumps that the small frog must perform to reach its target.

Write a function:

function solution(X, Y, D);

that, given three integers X, Y and D, returns the minimal number of jumps from position X to a position equal to or greater than Y.

For example, given:
X = 10
Y = 85
D = 30

the function should return 3, because the frog will be positioned as follows:

after the first jump, at position 10 + 30 = 40
after the second jump, at position 10 + 30 + 30 = 70
after the third jump, at position 10 + 30 + 30 + 30 = 100

Write an efficient algorithm for the following assumptions:

X, Y and D are integers within the range [1..1,000,000,000];
X ≤ Y.

Copyright 2009–2019 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

설명

X : 시작 위치
Y : 도착 위치
D : 증가량

X+nD > Y의 n을 구하기
도착위치에서 시작위치를 뺀 후 증가량 D로 나누기
다른 언어의 경우 기본적으로 정수형 연산이기 때문에 나머지가 있으면 몫+1 이지만
javascript는 기본적으로 모든 숫자의 연산이 정수가 아닌 실수형이기 때문에 올림으로 계산하면 됨

코드

1
2
3
4
function solution(X, Y, D) {
// write your code in JavaScript (Node.js 8.9.4)
return Math.ceil((Y - X) / D);
}

결과

image

codility Lesson3 TapeEquilibrium

TapeEquilibrium

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45


A non-empty array A consisting of N integers is given. Array A represents numbers on a tape.

Any integer P, such that 0 < P < N, splits this tape into two non-empty parts: A[0], A[1], ..., A[P − 1] and A[P], A[P + 1], ..., A[N − 1].

The difference between the two parts is the value of: |(A[0] + A[1] + ... + A[P − 1]) − (A[P] + A[P + 1] + ... + A[N − 1])|

In other words, it is the absolute difference between the sum of the first part and the sum of the second part.

For example, consider array A such that:
A[0] = 3
A[1] = 1
A[2] = 2
A[3] = 4
A[4] = 3

We can split this tape in four places:

P = 1, difference = |3 − 10| = 7
P = 2, difference = |4 − 9| = 5
P = 3, difference = |6 − 7| = 1
P = 4, difference = |10 − 3| = 7

Write a function:

function solution(A);

that, given a non-empty array A of N integers, returns the minimal difference that can be achieved.

For example, given:
A[0] = 3
A[1] = 1
A[2] = 2
A[3] = 4
A[4] = 3

the function should return 1, as explained above.

Write an efficient algorithm for the following assumptions:

N is an integer within the range [2..100,000];
each element of array A is an integer within the range [−1,000..1,000].

Copyright 2009–2019 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

방법

배열의 요소를 둘로 나누어 좌측 합과 우측합의 차를 절대값으로 계산하여 가장 작은 값을 찾는다.
좌측은 첫 요소부터 마지막요소를 제외한 나머지 요소의 합까지이며
우측은 첫 요소를 제외한 나머지의 합부터 마지막 요소까지
A[0] = 3
A[1] = 1
A[2] = 2
A[3] = 4
A[4] = 3
일 때,
3+1+2+4+5 = 13 이며,
|3 - (1+2+4+3)| 부터 |(3+1+2+4)-3|까지 이므로
마지막 요소는 좌측의 합이 아니기 때문에 반복문은 A.length-1

코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function solution(A) {
const getSum = (accumulator, currentValue) => accumulator + currentValue;
let right = A.reduce(getSum);
let left = 0;
let min = 10001;
for (let i = 0; i < A.length - 1; i++) {
left += A[i];
right -= A[i];

min = Math.min(Math.abs(left - right), min);
}

return min;
}

결과

image

call_by_Value, call_by_Reference

Call by Value 와 Call by Reference

Call by Value

1
2
3
4
5
6
7
8
9
10
void change_value(int x, int val) {
x = val;
printf("x : %d in change_value \n", x);
}

int main(void) {
int x = 10;
change_value(x, 20);
printf("x : %d in main \n", x);
}

위 소스는 main에서 선언된 변수 x에 값 10을 주고 변수 x와 정수 val 매개변수로 하여 change_value를 호출한 후 change_value에서 x의 값을 val로 변경하는 소스이다.

image

실행을 하면 change_value에서 x의 값은 20으로 변경되었지만 main에서는 여전히 10이다.

이는 main에서 넘긴 x는 x의 값 10을 의미하기 때문에 이것을 바꾸어도 main의 x는 영향을 받지 않는다.

그래서 Call by Value이다.


codility Lesson2 CyclicRotation

CyclicRotation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7] (elements are shifted right by one index and 6 is moved to the first place).

The goal is to rotate array A K times; that is, each element of A will be shifted to the right K times.

Write a function:

def solution(A, K)

that, given an array A consisting of N integers and an integer K, returns the array A rotated K times.

For example, given
A = [3, 8, 9, 7, 6]
K = 3

the function should return [9, 7, 6, 3, 8]. Three rotations were made:
[3, 8, 9, 7, 6] -[6, 3, 8, 9, 7]
[6, 3, 8, 9, 7] -[7, 6, 3, 8, 9]
[7, 6, 3, 8, 9] -[9, 7, 6, 3, 8]

For another example, given
A = [0, 0, 0]
K = 1

the function should return [0, 0, 0]

Given
A = [1, 2, 3, 4]
K = 4

the function should return [1, 2, 3, 4]

Assume that:

N and K are integers within the range [0..100];
each element of array A is an integer within the range [−1,000..1,000].

In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.
Copyright 2009–2019 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

codility Lesson2 OddOccurrencesInArray

OddOccurrencesInArray

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
A non-empty array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired.

For example, in array A such that:
A[0] = 9 A[1] = 3 A[2] = 9
A[3] = 3 A[4] = 9 A[5] = 7
A[6] = 9

the elements at indexes 0 and 2 have value 9,
the elements at indexes 1 and 3 have value 3,
the elements at indexes 4 and 6 have value 9,
the element at index 5 has value 7 and is unpaired.

Write a function:

function solution(A);

that, given an array A consisting of N integers fulfilling the above conditions, returns the value of the unpaired element.

For example, given array A such that:
A[0] = 9 A[1] = 3 A[2] = 9
A[3] = 3 A[4] = 9 A[5] = 7
A[6] = 9

the function should return 7, as explained in the example above.

Write an efficient algorithm for the following assumptions:

N is an odd integer within the range [1..1,000,000];
each element of array A is an integer within the range [1..1,000,000,000];
all but one of the values in A occur an even number of times.

Copyright 2009–2019 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

box-sizing

box-sizing과 box model

box의 크기를 어떤 기준으로 계산 할 것인가?

HTML은 Box로 이루어져 있으며 문서의 레이아웃을 계산 할 때 박스 모델에 따라 박스의 사이즈가 달라지게 됨


box의 크기는 어떤 요소들로 이루어 지는가?

box의 크기에 영향을 주는 요소는 box의 width, height, border, padding, margin이 됨


각 요소의 값이 동일 할 때 box의 크기는 달라지는가?

1
2
<div class="div content">content-box</div>
<div class="div border">border-box</div>

codility Lesson1 BinaryGap

BinaryGap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.

For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. The number 32 has binary representation 100000 and has no binary gaps.

Write a function:

function solution(N);

that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.

For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5. Given N = 32 the function should return 0, because N has binary representation '100000' and thus no binary gaps.

Write an efficient algorithm for the following assumptions:

N is an integer within the range [1..2,147,483,647].

Copyright 2009–2019 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

TypeError-is-not-a-function

증상

JQuery의 stop, animate 를 사용 할 때 TypeError발생
TypeError: $(…).stop is not a function

TypeError

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×