You are given an implementation of a function:
def solution(A, K)
This function, given a non-empty array A of N integers (sorted in non-decreasing order) and integer K, checks whether A contains numbers 1, 2, ..., K (every number from 1 to K at least once) and no other numbers.
For example, given the following array A, and K = 3:
A[0] = 1 A[1] = 1 A[2] = 2 A[3] = 3 A[4] = 3The function should return True.
For the following array A, and K = 2:
A[0] = 1 A[1] = 1 A[2] = 3the function should return False.
The attached code is still incorrect for some inputs. Despite the error(s), the code may produce a correct answer for the example test cases. The goal of the exercise is to find and fix the bug(s) in the implementation. You can modify at most two lines.
Assume that:
- N and K are integers within the range [1..300,000];
- each element of array A is an integer within the range [0..1,000,000,000];
- array A is sorted in non-decreasing order.
In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.
No lines have been modified, therefore the final score is 0.
[[1, 1, 1], 2]
[[1, 1, 1], 2]
The following issues have been detected: wrong answers.
For example, for the input ([1, 1, 3], 2) the solution returned a wrong answer (got True expected False).
check if the first condition is still correct
got True expected False
Build a "like button" component using React 16. The component should be the default export (use export default).
Requirements:
1. There should be a like button:
- The content of the like button should be in the following format: "Like | 100", where 100 is the total number of likes.
- It should have a "like-button" class.
- Wrap the number of likes in a span with a "likes-counter" class.
- The initial number of likes in the counter should be 100.
2. Users can add a like. By clicking the button:
- The number of likes should increase by one.
- Like button should have "liked" class in addition to the "like-button" class (you can use the classnames tool for that).
3. Users can undo their like by clicking again on the button:
- The counter should decrease by one.
- "liked" class should be removed.
Assessment/Tools:
- Only two imports are allowed: react (v16.8.6) and classnames (v2.2.5). Both are at the top of the starting code.
- Use the animation below as a reference for your solution.
- Design/styling is not assessed and will not affect the score. You should focus only on implementing the requirements.
- The "Preview" tab will display your component. You can use it for testing purposes.
import cx from 'classnames';
import { Component } from 'react';
export default class LikeButton extends Component {
render() {
return (
<>
<div>
<h2>Like Button</h2>
</div>
<style>{`
.like-button {
font-size: 1rem;
padding: 5px 10px;
color: #585858;
}
.liked {
font-weight: bold;
color: #1565c0;
}
`}</style>
</>
);
}
}
import cx from 'classnames';
import { Component } from 'react';
export default class LikeButton extends Component {
state = {
initialCount: 100,
liked: false,
};
render() {
const { liked } = this.state;
return (
<>
<button
className={cx('like-button', { liked })}
type="button"
onClick={this.toggleLike}
>
Like | <span className="likes-counter">{this.getLikes()}</span>
</button>
<style>{`
.like-button {
font-size: 1rem;
padding: 5px 10px;
color: #585858;
}
.liked {
font-weight: bold;
color: #1565c0;
}
`}</style>
</>
);
}
getLikes() {
const { initialCount, liked } = this.state;
return initialCount + (liked ? 1 : 0);
}
toggleLike = () => {
this.setState(({ liked }) => ({ liked: !liked }));
};
}
import cx from 'classnames';
import { Component } from 'react';
export default class LikeButton extends Component {
state = {
initialCount: 100,
liked: false,
};
render() {
const { liked } = this.state;
return (
<>
<button
className={cx('like-button', { liked })}
type="button"
onClick={this.toggleLike}
>
Like | <span className="likes-counter">{this.getLikes()}</span>
</button>
<style>{`
.like-button {
font-size: 1rem;
padding: 5px 10px;
color: #585858;
}
.liked {
font-weight: bold;
color: #1565c0;
}
`}</style>
</>
);
}
getLikes() {
const { initialCount, liked } = this.state;
return initialCount + (liked ? 1 : 0);
}
toggleLike = () => {
this.setState(({ liked }) => ({ liked: !liked }));
};
}
import cx from 'classnames';
import { Component } from 'react';
export default class LikeButton extends Component {
state = {
initialCount: 100,
liked: false,
};
render() {
const { liked } = this.state;
return (
<>
<button
className={cx('like-button', { liked })}
type="button"
onClick={this.toggleLike}
>
Like | <span className="likes-counter">{this.getLikes()}</span>
</button>
<style>{`
.like-button {
font-size: 1rem;
padding: 5px 10px;
color: #585858;
}
.liked {
font-weight: bold;
color: #1565c0;
}
`}</style>
</>
);
}
getLikes() {
const { initialCount, liked } = this.state;
return initialCount + (liked ? 1 : 0);
}
toggleLike = () => {
this.setState(({ liked }) => ({ liked: !liked }));
};
}
import cx from 'classnames';
import { Component } from 'react';
export default class LikeButton extends Component {
state = {
initialCount: 100,
liked: false,
};
render() {
const { liked } = this.state;
return (
<>
<button
className={cx('like-button', { liked })}
type="button"
onClick={this.toggleLike}
>
Like | <span className="likes-counter">{this.getLikes()}</span>
</button>
<style>{`
.like-button {
font-size: 1rem;
padding: 5px 10px;
color: #585858;
}
.liked {
font-weight: bold;
color: #1565c0;
}
`}</style>
</>
);
}
getLikes() {
const { initialCount, liked } = this.state;
return initialCount + (liked ? 1 : 0);
}
toggleLike = () => {
this.setState(({ liked }) => ({ liked: !liked }));
};
}
import cx from 'classnames';
import { Component } from 'react';
export default class LikeButton extends Component {
state = {
initialCount: 100,
liked: false,
};
render() {
const { liked } = this.state;
return (
<>
<button
className={cx('like-button', { liked })}
type="button"
onClick={this.toggleLike}
>
Like | <span className="likes-counter">{this.getLikes()}</span>
</button>
<style>{`
.like-button {
font-size: 1rem;
padding: 5px 10px;
color: #585858;
}
.liked {
font-weight: bold;
color: #1565c0;
}
`}</style>
</>
);
}
getLikes() {
const { initialCount, liked } = this.state;
return initialCount + (liked ? 1 : 0);
}
toggleLike = () => {
this.setState(({ liked }) => ({ liked: !liked }));
};
}
The solution obtained perfect score.
Write a function:
def solution(S)
that, given a string S consisting of N lowercase letters, returns the minimum number of letters that must be deleted to obtain a word in which every letter occurs a unique number of times. We only care about occurrences of letters that appear at least once in result.
Examples:
1. Given S = "aaaabbbb", the function should return 1. We can delete one occurrence of a or one occurrence of b. Then one letter will occur four times and the other one three times.
2. Given S = "ccaaffddecee", the function should return 6. For example, we can delete all occurrences of e and f and one occurrence of d to obtain the word "ccaadc". Note that both e and f will occur zero times in the new word, but that is fine, since we only care about letters that appear at least once.
3. Given S = "eeee", the function should return 0 (there is no need to delete any characters).
4. Given S = "example", the function should return 4.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [0..300,000];
- string S is made only of lowercase letters (a−z).
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(S):
count = [0] * 26
for i in range(len(S)):
tmp = ord(S[i])-ord("a")
count[tmp] += 1
result = 0
count.sort()
avail = len(S) + 1
for i in range(26):
cnt = count[26-1-i]
if avail <= 0
result += cnt
pass
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(S):
count = [0] * 26
for i in range(len(S)):
tmp = ord(S[i])-ord("a")
count[tmp] += 1
result = 0
count.sort()
avail = len(S) + 1
for i in range(26):
cnt = count[26-1-i]
if avail <= 0
result += cnt
else:
if cnt>avail:
result += cnt-avail
avail = min()
pass
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(S):
count = [0] * 26
for i in range(len(S)):
tmp = ord(S[i])-ord("a")
count[tmp] += 1
result = 0
count.sort()
avail = len(S) + 1
for i in range(26):
cnt = count[26-1-i]
if avail <= 0
result += cnt
else:
if cnt>avail:
result += cnt-avail
avail = min(avail,cnt)-1
return result
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(S):
count = [0] * 26
for i in range(len(S)):
tmp = ord(S[i])-ord("a")
count[tmp] += 1
result = 0
count.sort()
avail = len(S) + 1
for i in range(26):
cnt = count[26-1-i]
if avail <= 0
result += cnt
else:
if cnt>avail:
result += cnt-avail
avail = min(avail,cnt)-1
return result
Traceback (most recent call last): File "/tmp/exec_user_lt_x83ca/exec.py", line 109, in <module> main() File "/tmp/exec_user_lt_x83ca/exec.py", line 64, in main sol = __import__('solution') ^^^^^^^^^^^^^^^^^^^^^^ File "/tmp/exec_user_lt_x83ca/solution.py", line 15 if avail <= 0 ^ SyntaxError: expected ':'
Traceback (most recent call last): File "/tmp/exec_user_lt_x83ca/exec.py", line 109, in <module> main() File "/tmp/exec_user_lt_x83ca/exec.py", line 64, in main sol = __import__('solution') ^^^^^^^^^^^^^^^^^^^^^^ File "/tmp/exec_user_lt_x83ca/solution.py", line 15 if avail <= 0 ^ SyntaxError: expected ':'
Traceback (most recent call last): File "/tmp/exec_user_lt_x83ca/exec.py", line 109, in <module> main() File "/tmp/exec_user_lt_x83ca/exec.py", line 64, in main sol = __import__('solution') ^^^^^^^^^^^^^^^^^^^^^^ File "/tmp/exec_user_lt_x83ca/solution.py", line 15 if avail <= 0 ^ SyntaxError: expected ':'
Traceback (most recent call last): File "/tmp/exec_user_lt_x83ca/exec.py", line 109, in <module> main() File "/tmp/exec_user_lt_x83ca/exec.py", line 64, in main sol = __import__('solution') ^^^^^^^^^^^^^^^^^^^^^^ File "/tmp/exec_user_lt_x83ca/solution.py", line 15 if avail <= 0 ^ SyntaxError: expected ':'
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(S):
count = [0] * 26
for i in range(len(S)):
tmp = ord(S[i])-ord("a")
count[tmp] += 1
result = 0
count.sort()
avail = len(S) + 1
for i in range(26):
cnt = count[26-1-i]
if avail <= 0:
result += cnt
else:
if cnt>avail:
result += cnt-avail
avail = min(avail,cnt)-1
return result
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(S):
count = [0] * 26
for i in range(len(S)):
tmp = ord(S[i])-ord("a")
count[tmp] += 1
result = 0
count.sort()
avail = len(S) + 1
for i in range(26):
cnt = count[26-1-i]
if avail <= 0:
result += cnt
else:
if cnt>avail:
result += cnt-avail
avail = min(avail,cnt)-1
return result
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(S):
count = [0] * 26
for i in range(len(S)):
tmp = ord(S[i])-ord("a")
count[tmp] += 1
result = 0
count.sort()
avail = len(S) + 1
for i in range(26):
cnt = count[26-1-i]
if avail <= 0:
result += cnt
else:
if cnt>avail:
result += cnt-avail
avail = min(avail,cnt)-1
return result
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(S):
count = [0] * 26
for i in range(len(S)):
tmp = ord(S[i])-ord("a")
count[tmp] += 1
result = 0
count.sort()
avail = len(S) + 1
for i in range(26):
cnt = count[26-1-i]
if avail <= 0:
result += cnt
else:
if cnt>avail:
result += cnt-avail
avail = min(avail,cnt)-1
return result
The solution obtained perfect score.