Soluție HackerRank pentru Angry Professor. Include cerința formatată, exemple, explicația pașilor și cod sursă.
- Problemă: Angry Professor
Cerinta completa
A Discrete Mathematics professor has a class of students. Frustrated with their lack of discipline, the professor decides to cancel class if fewer than some number of students are present when class starts. Arrival times go from on time ([Expresie matematică indisponibilă în copia arhivată]) to arrived late ([Expresie matematică indisponibilă în copia arhivată]).
Given the arrival time of each student and a threshhold number of attendees, determine if the class is cancelled.
Example
[Expresie matematică indisponibilă în copia arhivată]
[Expresie matematică indisponibilă în copia arhivată]
[Expresie matematică indisponibilă în copia arhivată]
The first [Expresie matematică indisponibilă în copia arhivată] students arrived on. The last [Expresie matematică indisponibilă în copia arhivată] were late. The threshold is [Expresie matematică indisponibilă în copia arhivată] students, so class will go on. Return YES.
Note: Non-positive arrival times ([Expresie matematică indisponibilă în copia arhivată]) indicate the student arrived early or on time; positive arrival times ([Expresie matematică indisponibilă în copia arhivată]) indicate the student arrived [Expresie matematică indisponibilă în copia arhivată] minutes late.
Function Description
Complete the angryProfessor function in the editor below. It must return YES if class is cancelled, or NO otherwise.
angryProfessor has the following parameter(s):
- int k: the threshold number of students
- int a[n]: the arrival times of the [Expresie matematică indisponibilă în copia arhivată] students
Returns
- string: either
YESorNO
Input Format
The first line of input contains [Expresie matematică indisponibilă în copia arhivată], the number of test cases.
Each test case consists of two lines.
The first line has two space-separated integers, [Expresie matematică indisponibilă în copia arhivată] and [Expresie matematică indisponibilă în copia arhivată], the number of students (size of [Expresie matematică indisponibilă în copia arhivată]) and the cancellation threshold.
The second line contains [Expresie matematică indisponibilă în copia arhivată] space-separated integers ([Expresie matematică indisponibilă în copia arhivată]) that describe the arrival times for each student.
Constraints
- [Expresie matematică indisponibilă în copia arhivată]
- [Expresie matematică indisponibilă în copia arhivată]
- [Expresie matematică indisponibilă în copia arhivată]
- [Expresie matematică indisponibilă în copia arhivată]
Sample Input
2
4 3
-1 -3 4 2
4 2
0 -1 2 1
Sample Output
YES
NO
Explanation
For the first test case, [Expresie matematică indisponibilă în copia arhivată]. The professor wants at least [Expresie matematică indisponibilă în copia arhivată] students in attendance, but only [Expresie matematică indisponibilă în copia arhivată] have arrived on time ([Expresie matematică indisponibilă în copia arhivată] and [Expresie matematică indisponibilă în copia arhivată]) so the class is cancelled.
For the second test case, [Expresie matematică indisponibilă în copia arhivată]. The professor wants at least [Expresie matematică indisponibilă în copia arhivată] students in attendance, and there are [Expresie matematică indisponibilă în copia arhivată] who arrived on time ([Expresie matematică indisponibilă în copia arhivată] and [Expresie matematică indisponibilă în copia arhivată]). The class is not cancelled.
Limbajul de programare folosit: python3
Cod:
#!/bin/python3
def angryProfessor(k, a):
return 'NO' if sum(1 for x in a if x<=0) >= k else 'YES'
if __name__ == '__main__':
t = int(input().strip())
out=[]
for _ in range(t):
n, k = map(int, input().split())
a = list(map(int, input().split()))
out.append(angryProfessor(k, a))
print('\n'.join(out))
Scor obtinut: 1.0
Submission ID: 464608114
Link challenge: https://www.hackerrank.com/challenges/angry-professor/problem
