From b26603f1fe755f9f43f997e6e03493e460420e74 Mon Sep 17 00:00:00 2001 From: Rishabh Singh Chauhan <143814425+Rsc2414@users.noreply.github.com> Date: Sat, 19 Jul 2025 20:58:01 +0530 Subject: [PATCH] Update 001. Laptop Battery Life.py --- .../001. Laptop Battery Life.py | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/Artificial Intelligence/Statistics and Machine Learning/001. Laptop Battery Life.py b/Artificial Intelligence/Statistics and Machine Learning/001. Laptop Battery Life.py index 3c62a74..c0ce2ec 100644 --- a/Artificial Intelligence/Statistics and Machine Learning/001. Laptop Battery Life.py +++ b/Artificial Intelligence/Statistics and Machine Learning/001. Laptop Battery Life.py @@ -1,15 +1,29 @@ # Problem: https://www.hackerrank.com/challenges/battery/problem # Score: 10 - +# By @Rsc2414 import pandas as pd from sklearn.linear_model import LinearRegression +# Read the input time charged +time_charged = float(input()) -timeCharged = float(input()) +# Load the training data from the file data = pd.read_csv('trainingdata.txt', names=['charged', 'lasted']) -train = data[data['lasted'] < 8] + +# Use only data points where battery lasted less than 8 hours (as per problem note) +filtered_data = data[data['lasted'] < 8] + +# Prepare the training input (X) and output (y) +X = filtered_data['charged'].values.reshape(-1, 1) +y = filtered_data['lasted'].values.reshape(-1, 1) + +# Create and train the linear regression model model = LinearRegression() -model.fit(train['charged'].values.reshape(-1, 1), train['lasted'].values.reshape(-1, 1)) -ans = model.predict([[timeCharged]]) -print(min(ans[0][0], 8)) +model.fit(X, y) + +# Predict battery life for the given charging time +predicted = model.predict([[time_charged]]) + +# Print the predicted time, but cap it at 8 hours +print(min(predicted[0][0], 8))