patterncsharpMinor
Hackerrank: Kindergarten Adventures
Viewed 0 times
hackerrankkindergartenadventures
Problem
Problem statement
Description:
Meera teaches a class of n students, and every day in her classroom is
an adventure. Today is drawing day!
The students are sitting around
a round table, and they are numbered from \$1\$ to \$n\$ in the clockwise
direction. This means that the students are numbered \$1,2,3,...,n-1,n\$,
and students \$1\$ and \$n\$ are sitting next to each other.
After letting the students draw for a certain period of time, Meera
starts collecting their work to ensure she has time to review all the
drawings before the end of the day. However, some of her students
aren't finished drawing! Each student \$i\$ needs \$t_i\$ extra minutes to
complete their drawing.
Meera collects the drawings sequentially in
the clockwise direction, starting with student ID \$x\$, and it takes her
exactly \$1\$ minute to review each drawing. This means that student \$x\$
gets \$0\$ extra minutes to complete their drawing, student \$x+1\$ gets \$1\$
extra minute, student \$x+2\$ gets \$2\$ extra minutes, and so on. Note that
Meera will still spend \$1\$ minute for each student even if the drawing
isn't ready.
Given the values of \$t_1,t_2,...,t_n\$, help Meera choose the best possible
\$x\$ to start collecting drawings from, such that the number of students
able to complete their drawings is maximal. Then print \$x\$ on a new
line. If there are multiple such IDs, select the smallest one.
Input Format
The first line contains a single positive integer, n, denoting the
number of students in the class. The second line contains n
space-separated integers describing the respective amounts of time
that each student needs to finish their drawings (i.e., \$t_1,t_2,...,t_n\$).
Constraints
-
\$1 \le n \le 10^5\$
-
\$0 \le t_i \le n\$
Subtasks
\$1 \le n \le 10^4\$ for \$30\%\$ of the maximum score.
Output Format
Print an integer denoting the ID number,
Description:
Meera teaches a class of n students, and every day in her classroom is
an adventure. Today is drawing day!
The students are sitting around
a round table, and they are numbered from \$1\$ to \$n\$ in the clockwise
direction. This means that the students are numbered \$1,2,3,...,n-1,n\$,
and students \$1\$ and \$n\$ are sitting next to each other.
After letting the students draw for a certain period of time, Meera
starts collecting their work to ensure she has time to review all the
drawings before the end of the day. However, some of her students
aren't finished drawing! Each student \$i\$ needs \$t_i\$ extra minutes to
complete their drawing.
Meera collects the drawings sequentially in
the clockwise direction, starting with student ID \$x\$, and it takes her
exactly \$1\$ minute to review each drawing. This means that student \$x\$
gets \$0\$ extra minutes to complete their drawing, student \$x+1\$ gets \$1\$
extra minute, student \$x+2\$ gets \$2\$ extra minutes, and so on. Note that
Meera will still spend \$1\$ minute for each student even if the drawing
isn't ready.
Given the values of \$t_1,t_2,...,t_n\$, help Meera choose the best possible
\$x\$ to start collecting drawings from, such that the number of students
able to complete their drawings is maximal. Then print \$x\$ on a new
line. If there are multiple such IDs, select the smallest one.
Input Format
The first line contains a single positive integer, n, denoting the
number of students in the class. The second line contains n
space-separated integers describing the respective amounts of time
that each student needs to finish their drawings (i.e., \$t_1,t_2,...,t_n\$).
Constraints
-
\$1 \le n \le 10^5\$
-
\$0 \le t_i \le n\$
Subtasks
\$1 \le n \le 10^4\$ for \$30\%\$ of the maximum score.
Output Format
Print an integer denoting the ID number,
Solution
I'm not going to talk about your algorithm and whatnot, instead let's discuss abstraction and SRP for a moment. Yours isn't bad, but it could use a few tweaks.
First:
We're entirely reliant upon the
And our
Similarly, the
Next we are still in that same method, because it still does too many things. Let's break it down further:
So we still have three major things it does that can be abstracted, which we should do. It may not seem like much, but failing to extract this processing properly can create unmaintainable code. Each method should have one responsibility, if it has
First:
public static void ProcessInput()
{
int n = int.Parse(Console.ReadLine());
var minutes = Console.ReadLine().Split().Select(int.Parse).ToArray();We're entirely reliant upon the
Console for input handling, which may (is) not a good thing. Instead, let's develop that method to take two strings: one is the n, and the next is the line.public static void ProcessInput(string nLine, string minutesLine)And our
Console.ReadLine calls would happen outside this class. The class doesn't care where the input came from, it cares what input it got.Similarly, the
Console.WriteLine(bestIndex); should not appear, instead you should return bestIndex and let the caller decide what to do with it. This way you take the responsibility of acquiring input and delivering output away from the ProcessInput method, which can now be named to RunAlgorithm et. al.Next we are still in that same method, because it still does too many things. Let's break it down further:
- Read input
- Parse input
- Build and fill
SegmentTree
- Query for best index from
SegmentTree
- Print best index
So we still have three major things it does that can be abstracted, which we should do. It may not seem like much, but failing to extract this processing properly can create unmaintainable code. Each method should have one responsibility, if it has
responsibilities > 1 then it should have each responsibility split to a new method, and then make calls and transfer data between those methods. This way, when we debug our code and find something that's wrong, we have a method to narrow it down to. (Especially once a released version is out, and someone says 'I got an exception that mentions the ProcessInput method', well that's bad right now because we have no way of telling where in the method the exception happened.)Code Snippets
public static void ProcessInput()
{
int n = int.Parse(Console.ReadLine());
var minutes = Console.ReadLine().Split().Select(int.Parse).ToArray();public static void ProcessInput(string nLine, string minutesLine)Context
StackExchange Code Review Q#158242, answer score: 5
Revisions (0)
No revisions yet.