DOMMETI commited on
Commit
7d72bf0
·
verified ·
1 Parent(s): 2793d86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -17
app.py CHANGED
@@ -113,22 +113,33 @@ st.latex(r'''
113
  \text{Median} = \frac{X_{\left(\frac{n}{2}\right)} + X_{\left(\frac{n}{2}+1\right)}}{2}
114
  ''')
115
  def median(list1):
116
- length=len(list1)
117
- if length%2== 0:
118
- even_median_value=((length/2)+(length/2)+1)/2
119
- return (list1[int(even_median_value)]+ list1[int(even_median_value)-1])/2
 
 
 
 
120
  else:
121
- odd_median=((length/2)+1)
122
- odd_median_value=math.floor(odd_median)
123
- return list1[odd_median_value-1]
124
 
125
- # Use a unique key for the text input widget to avoid DuplicateWidgetID error
126
- numbers_input_1 = st.text_input(
127
- "Enter a list of numbers separated by commas (e.g., 1, 2, 2, 3, 4):",
128
- key="numbers_input_1"
129
- )
130
- list1=[]
131
- for i in numbers_input_1:
132
- list1+=[int(i)]
133
- result=median(list1)
134
- st.write("median_result", result)
 
 
 
 
 
 
 
 
113
  \text{Median} = \frac{X_{\left(\frac{n}{2}\right)} + X_{\left(\frac{n}{2}+1\right)}}{2}
114
  ''')
115
  def median(list1):
116
+ list1.sort() # Ensure the list is sorted
117
+ length = len(list1)
118
+
119
+ if length % 2 == 0:
120
+ # For even length, calculate the average of the two middle values
121
+ mid1 = length // 2 - 1
122
+ mid2 = length // 2
123
+ return (list1[mid1] + list1[mid2]) / 2
124
  else:
125
+ # For odd length, return the middle value
126
+ mid = length // 2
127
+ return list1[mid]
128
 
129
+ st.title("Calculate Median")
130
+ numbers_input_1 = st.text_input("Enter a list of numbers separated by commas (e.g., 1, 2, 3, 4, 5):", key="numbers_input_1")
131
+
132
+ if numbers_input_1:
133
+ parts = numbers_input_1.split(',')
134
+ list1 = []
135
+
136
+ for num in parts:
137
+ num = num.strip() # Remove any leading or trailing whitespace
138
+ if num.isdigit():
139
+ list1.append(int(num))
140
+
141
+ if list1: # Check if the list is not empty
142
+ result = median(list1)
143
+ st.write("Median result:", result)
144
+ else:
145
+ st.write("No valid numbers provided.")