We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#!/bin/python3importmathimportosimportrandomimportreimportsysimportnumpyasnpfromsklearn.linear_modelimportLinearRegressiondata=[]withopen('trainingdata.txt','r')asfile:forlineinfile:charging_time,battery_lasted=map(float,line.strip().split(','))data.append((charging_time,battery_lasted))data=np.array(data)X=data[:,0]y=data[:,1]# determines the minimum charging time at which the battery life reaches its maximum possible value (8.00 hours).# find the smallest charging time at which the battery life reaches 8.00.threshold=min(X[y==8])filtered_X=X[X<threshold].reshape(-1,1)filtered_y=y[X<threshold]lr=LinearRegression().fit(filtered_X,filtered_y)defpredict_battery_life(charge_time):ifcharge_time>=threshold:return8returnlr.predict(np.array([[charge_time]]))[0]if__name__=='__main__':timeCharged=float(input().strip())predicted_life=predict_battery_life(timeCharged)print(round(predicted_life,2))
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Laptop Battery Life
You are viewing a single comment's thread. Return to all comments →