Skip to content

Update 001. Laptop Battery Life.py #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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))