Spaces:
Build error
Build error
Update app.py
Browse files
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 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
else:
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
return list1[
|
| 124 |
|
| 125 |
-
|
| 126 |
-
numbers_input_1 = st.text_input(
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
)
|
| 130 |
-
list1=[]
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.")
|