Skip to content

Electric power #3976

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

Merged
merged 5 commits into from
Nov 29, 2020
Merged

Electric power #3976

merged 5 commits into from
Nov 29, 2020

Conversation

erdum
Copy link
Contributor

@erdum erdum commented Nov 27, 2020

Describe your change:

Used code from ohms_law algorithm, but now for power calculation

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@ghost ghost added the awaiting reviews This PR is ready to be reviewed label Nov 27, 2020
@erdum
Copy link
Contributor Author

erdum commented Nov 27, 2020

@cclauss hello, sir please debug it and give me some tips and instructions, some lines in code are longer then 79 so what should i need to do????

@ghost ghost added tests are failing Do not merge until tests pass and removed awaiting reviews This PR is ready to be reviewed labels Nov 27, 2020
@cclauss
Copy link
Member

cclauss commented Nov 27, 2020

Maximum line length is 88 characters.

result = (float(voltage * current))
if result < 0:
result = result * -1
return {'power': result}
Copy link
Member

@cclauss cclauss Nov 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the builtin function abs()
https://docs.python.org/3/library/functions.html#abs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

@ghost ghost added awaiting reviews This PR is ready to be reviewed and removed tests are failing Do not merge until tests pass labels Nov 28, 2020
@erdum
Copy link
Contributor Author

erdum commented Nov 28, 2020

@cclauss all done sir.

Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's spice up the tests and let's return a namedtuple instead of a dict. Namedtuples are more optimized than dicts and they provides helpful names.

>>> electric_power(voltage=2, current=2, power=0)
{'power': 4.0}
>>> electric_power(voltage=-2, current=3, power=0)
{'power': 6.0}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to test:

  1. No zeros
  2. Two zeros
  3. Negative numbers for power
  4. Floating-point numbers
Suggested change
{'power': 6.0}
>>> electric_power(voltage=-2, current=3, power=0).name
'power'
>>> electric_power(voltage=-2, current=3, power=0).value
6.0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, should i also do this on ohms_law as well??

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's get this PR landed before you modify Ohm's law...

Coincidentally, @rhettinger (who wrote the namedtuple) just tweeted about Ohm's law...
https://twitter.com/raymondh/status/1331850115674345473

@@ -0,0 +1,33 @@
# https://en.m.wikipedia.org/wiki/Electric_power

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from collections import namedtuple
result = namedtuple("result", "name value")

fundamental value of electrical system.
examples are below:
>>> electric_power(voltage=0, current=2, power=5)
{'voltage': 2.5}
Copy link
Member

@cclauss cclauss Nov 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{'voltage': 2.5}
result(name='voltage', value=2.5)

"Power cannot be negative in any electrical/electronics system"
)
elif voltage == 0:
return {"voltage": power / current}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return {"voltage": power / current}
return result("voltage", power / current)

@ghost ghost added awaiting changes A maintainer has requested changes to this PR and removed awaiting reviews This PR is ready to be reviewed labels Nov 28, 2020
@ghost ghost added awaiting reviews This PR is ready to be reviewed and removed awaiting changes A maintainer has requested changes to this PR labels Nov 29, 2020
@erdum
Copy link
Contributor Author

erdum commented Nov 29, 2020

@cclauss sir run this code and you will see that with floating point values it will give you very long decimal value output, so my question is how to wrapped it, to only for 3 values after decimal point

@ghost ghost added tests are failing Do not merge until tests pass and removed awaiting reviews This PR is ready to be reviewed labels Nov 29, 2020
@erdum
Copy link
Contributor Author

erdum commented Nov 29, 2020

@cclauss I figured out, now all done???

@ghost ghost added awaiting reviews This PR is ready to be reviewed and removed tests are failing Do not merge until tests pass labels Nov 29, 2020
@erdum erdum requested a review from cclauss November 29, 2020 20:04
Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@cclauss cclauss merged commit daceb87 into TheAlgorithms:master Nov 29, 2020
@ghost ghost removed the awaiting reviews This PR is ready to be reviewed label Nov 29, 2020
stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
* Electric power

* updated as suggested by cclauss

* updated as suggested by cclauss

* decimal value error

* All done
peRFectBeliever pushed a commit to peRFectBeliever/Python that referenced this pull request Apr 1, 2021
* Electric power

* updated as suggested by cclauss

* updated as suggested by cclauss

* decimal value error

* All done
Panquesito7 pushed a commit to Panquesito7/Python that referenced this pull request May 13, 2021
* Electric power

* updated as suggested by cclauss

* updated as suggested by cclauss

* decimal value error

* All done
shermanhui pushed a commit to shermanhui/Python that referenced this pull request Oct 22, 2021
* Electric power

* updated as suggested by cclauss

* updated as suggested by cclauss

* decimal value error

* All done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants