HackerRank Course
HackerRank Course
Narrator: Here, we have a few examples of sets in action. When we create an empty
set and print it, we get an empty set as expected. But when we create a set from
the string 'HackerRank,' we observe that the elements appear in a different order.
This is because sets do not preserve the original order of elements.
Similarly, when we create sets from other data types like lists, tuples,
dictionaries, or even enumerations, we get unique elements without any duplicates.
Sets are excellent for membership testing and eliminating duplicate entries.
Now, let's apply our understanding of sets to solve a problem faced by Mickey, a
student at District College.
[Scenario transition]
Narrator: Mickey's botany professor, Ms. Gabriel Williams, has a greenhouse with
various plants of different heights. She wants to compute the average height of all
the plants with distinct heights. Mickey, being the diligent student, wants to help
her out. Let's see how we can assist Mickey using sets.
Narrator: Here we have a function called average that takes an array of integers as
input and returns the average as a floating-point value rounded to three decimal
places.
To calculate the average, we first create a set called user_set from the input
array. This set will automatically eliminate any duplicate entries, giving us the
distinct heights of plants. We then find the length of the set and calculate the
sum of its elements using the sum function.
Finally, we divide the sum by the length of the set to obtain the average. The
result is returned after rounding it to three decimal places.
Now, let's walk through the code and analyze its time complexity.
Narrator: The time complexity of the given code is O(N), where N represents the
length of the input array arr.
To understand why, let's break it down step by step. First, we create a set from
the input array, which requires iterating through the array once. Since it iterates
through N elements, this step has a time complexity of O(N).
After that, we calculate the sum of the elements in the set using the sum function.
Again, this operation takes O(N) time, as it iterates through the set of N
elements.
Understanding the time complexity helps us assess the efficiency of our code and
its scalability to larger inputs. In this case, with a linear time complexity, our
code performs well even for larger arrays.
Now that we have analyzed the code and its time complexity, let's see how it
performs on a sample input.
Narrator: For the given sample input, where the size of the array is 10 and the
elements are 161, 182, 161, 154, 176, 170, 167, 171, 170, and 174, the expected
output is 169.375.
Let's run the code with this input and see if it produces the correct result.
Narrator: As we can see, the code successfully computes the average of the distinct
heights in the array and rounds the output to three decimal places, giving us the
expected result of 169.375.
In conclusion, sets are powerful data structures for storing unique elements and
performing membership testing. They are efficient for eliminating duplicates and
can be used to solve various computational problems. By understanding the time
complexity of our code, we can assess its efficiency and scalability to handle
larger inputs.
I hope you enjoyed this lecture on "Introduction to Sets" and found it informative.
Sets are just one aspect of the vast world of data structures, and exploring
further will open doors to exciting possibilities. Happy coding!
Narrator: Thank you for joining us today. We look forward to seeing you in our next
lecture. Have a wonderful day!