Algorithm(5)
-
[HackerRank] All Domains - Python - Basic Data Types - Lists
When we talk about storing multiple values in a container-like data structure, the first thing that comes to mind is a list. You can initialize a list as: arr = list() # or simply arr = [] or with a few elements as: arr = [1,2,3] Elements can be accessed easily similar to most programming languages: print arr[0] # result is 1 print arr[0] + arr[1] + arr[2] # result is 6 Lists in Python are very ..
2022.03.14 -
[HackerRank] All Domains - Python - Basic Data Types - Tuples
Tuples are data structures that look a lot like lists. Unlike lists, tuples are immutable (meaning that they cannot be modified once created). This restricts their use because we cannot add, remove, or assign values; however, it gives us an advantage in space and time complexities. A common tuple use is the swapping of numbers: a, b = b, a Here is a tuple, and it assigns itself the values of . A..
2022.03.13 -
[HackerRank] All Domains - Python - Basic Data Types - Nested Lists
Let's implement a nested list! A nested list is a list that contains another list (i.e.: a list of lists). For example: nested_list = [['blue', 'green'], ['red', 'black'], ['blue', 'white']] print len(a) # prints 3 print nested_list[1] # prints ['red', 'black'] print nested_list[1][0] # prints red To go through every element in a list, use a nested for loop. -------------------------------------..
2022.03.13 -
[HackerRank] All Domains - Python - Basic Data Types - Finding the percentage
A dictionary is a data type which stores values in pairs. For each element in the dictionary, there is a unique key that points to a value. A dictionary is mutable. It can be changed. For example: a_dict = {'one': 1} # Here 'one' is the key. Note: The key of a dictionary is immutable. We cannot use a list as a key because a list is mutable. But we can make a tuple of list and use as key. a_dict[..
2022.03.13 -
[HackerRank] All Domains - Python - Basic Data Types - Find the Second Largest Number
You are given numbers. Store them in a list and find the second largest number. Input Format The first line contains . The second line contains an array [] of integers each separated by a space. Constraints Output Format Output the value of the second largest number. Sample Input 5 2 3 6 6 5 Sample Output 5 ------------------------------------------------ if __name__ == '__main__': n = int(input..
2022.03.13