Guest User

Untitled

a guest
Aug 18th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. ## Python Program to Find the Second Smallest Number in a List
  2. This is a Python Program to find the Second Smallest number in a list.
  3.  
  4. ### Problem Description
  5. The program takes a list and prints the second smallest number in the list.
  6.  
  7. ### Problem Solution
  8. 1. Take in the number of elements and store it in a variable.
  9. 2. Take in the elements of the list one by one.
  10. 3. Sort the list in ascending order.
  11. 4. Print the last element of the list.
  12. 5. Exit.
  13.  
  14. ### Source Code
  15.  
  16.  
  17.  
  18. ```python
  19. li = []
  20. n = int(input("Enter the number of elements: "))
  21. for i in range(1, n+1):
  22. elem = int(input("Enter the elements: "))
  23. li.append(elem)
  24. li.sort()
  25.  
  26. print("The sorted list: ", li)
  27. print("The second smallest value of this list: ",li[1])
  28. ```
  29.  
  30. ### Program Explanation
  31. 1. User must enter the number of elements and store it in a variable.
  32. 2. User must then enter the elements of the list one by one using a for loop and store it in a list.
  33. 3. The list should then be sorted.
  34. 4. Then the first element of the list is printed which is also the smallest element of the list.
  35.  
  36.  
  37.  
  38. ### Test Case-1
  39.  
  40.  
  41. ```python
  42. li = []
  43. n = int(input("Enter the number of elements: "))
  44. for i in range(1, n+1):
  45. elem = int(input("Enter the elements: "))
  46. li.append(elem)
  47. li.sort()
  48. print("The sorted list: ", li)
  49. print("The second smallest value of this list: ",li[1])
  50. ```
  51.  
  52. Enter the number of elements: 4
  53. Enter the elements: 50
  54. Enter the elements: 45
  55. Enter the elements: 36
  56. Enter the elements: 12
  57. The sorted list: [12, 36, 45, 50]
  58. The second smallest value of this list: 36
  59.  
  60.  
  61. ### Test Case-2
  62.  
  63.  
  64. ```python
  65. li = []
  66. n = int(input("Enter the number of elements: "))
  67. for i in range(1, n+1):
  68. elem = int(input("Enter the elements: "))
  69. li.append(elem)
  70. li.sort()
  71.  
  72. print("The sorted list: ", li)
  73. print("The second smallest value of this list: ",li[1])
  74. ```
  75.  
  76. Enter the number of elements: 5
  77. Enter the elements: 10
  78. Enter the elements: 40
  79. Enter the elements: 70
  80. Enter the elements: 100
  81. Enter the elements: 130
  82. The sorted list: [10, 40, 70, 100, 130]
  83. The second smallest value of this list: 40
  84.  
  85.  
  86. ### Test Case-3
  87.  
  88.  
  89. ```python
  90. li = []
  91. n = int(input("Enter the number of elements: "))
  92. for i in range(1, n+1):
  93. elem = int(input("Enter the elements: "))
  94. li.append(elem)
  95. li.sort()
  96.  
  97. print("The sorted list: ", li)
  98. print("The second smallest value of this list: ",li[1])
  99. ```
  100.  
  101. Enter the number of elements: 10
  102. Enter the elements: -3
  103. Enter the elements: -2
  104. Enter the elements: -1
  105. Enter the elements: 3
  106. Enter the elements: 2
  107. Enter the elements: 1
  108. Enter the elements: 0
  109. Enter the elements: 4
  110. Enter the elements: 3
  111. Enter the elements: 4
  112. The sorted list: [-3, -2, -1, 0, 1, 2, 3, 3, 4, 4]
  113. The second smallest value of this list: -2
  114.  
  115. Jubayer Hossain
  116.  
  117. > Please share your tips or tricks for solving this problem in a better way! **Happy Coding!**
Add Comment
Please, Sign In to add comment