added a function to calculate perceived frequency by observer using Doppler Effect (#10776)

* avg and mps speed formulae added

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* avg and mps speed formulae added

* fixed_spacing

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* spacing

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* ws

* added amicable numbers

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* added amicable numbers

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* spacing

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* removed

* changed name of file and added code improvements

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* issues fixed due to pi

* requested changes added

* Created doppler_effect_of_sound.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Updated doppler_effect_of_sound.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* added desc names

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fixed spacing

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* renamed doppler_effect_of_sound.py to doppler_frequency.py

* used expection handling rather than print statements

* fixed spacing for ruff

* Update doppler_frequency.py

This is super slick!  Well done.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
Barun Parua 2023-10-22 04:03:50 +05:30 committed by GitHub
parent 4707fdb0f2
commit d9562033f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 132 additions and 4 deletions

View File

@ -25,6 +25,10 @@ def perfect(number: int) -> bool:
Returns:
True if the number is a perfect number, False otherwise.
Start from 1 because dividing by 0 will raise ZeroDivisionError.
A number at most can be divisible by the half of the number except the number
itself. For example, 6 is at most can be divisible by 3 except by 6 itself.
Examples:
>>> perfect(27)
False
@ -41,15 +45,35 @@ def perfect(number: int) -> bool:
>>> perfect(8128)
True
>>> perfect(0)
>>> perfect(-3)
False
>>> perfect(-1)
False
>>> perfect(12.34)
>>> perfect("day")
>>> perfect(["call"])
Traceback (most recent call last):
...
ValueError: number must be an integer
>>> perfect("Hello")
Traceback (most recent call last):
...
ValueError: number must be an integer
"""
if not isinstance(number, int):
raise ValueError("number must be an integer")
if number <= 0:
return False
return sum(i for i in range(1, number // 2 + 1) if number % i == 0) == number
if __name__ == "__main__":
from doctest import testmod
testmod()
print("Program to check whether a number is a Perfect number or not...")
number = int(input("Enter number: ").strip())
try:
number = int(input("Enter a positive integer: ").strip())
except ValueError:
msg = "number must be an integer"
print(msg)
raise ValueError(msg)
print(f"{number} is {'' if perfect(number) else 'not '}a Perfect Number.")

View File

@ -0,0 +1,104 @@
"""
Doppler's effect
The Doppler effect (also Doppler shift) is the change in the frequency of a wave in
relation to an observer who is moving relative to the source of the wave. The Doppler
effect is named after the physicist Christian Doppler. A common example of Doppler
shift is the change of pitch heard when a vehicle sounding a horn approaches and
recedes from an observer.
The reason for the Doppler effect is that when the source of the waves is moving
towards the observer, each successive wave crest is emitted from a position closer to
the observer than the crest of the previous wave. Therefore, each wave takes slightly
less time to reach the observer than the previous wave. Hence, the time between the
arrivals of successive wave crests at the observer is reduced, causing an increase in
the frequency. Similarly, if the source of waves is moving away from the observer,
each wave is emitted from a position farther from the observer than the previous wave,
so the arrival time between successive waves is increased, reducing the frequency.
If the source of waves is stationary but the observer is moving with respect to the
source, the transmission velocity of the waves changes (ie the rate at which the
observer receives waves) even if the wavelength and frequency emitted from the source
remain constant.
These results are all summarized by the Doppler formula:
f = (f0 * (v + v0)) / (v - vs)
where:
f: frequency of the wave
f0: frequency of the wave when the source is stationary
v: velocity of the wave in the medium
v0: velocity of the observer, positive if the observer is moving towards the source
vs: velocity of the source, positive if the source is moving towards the observer
Doppler's effect has many applications in physics and engineering, such as radar,
astronomy, medical imaging, and seismology.
References:
https://en.wikipedia.org/wiki/Doppler_effect
Now, we will implement a function that calculates the frequency of a wave as a function
of the frequency of the wave when the source is stationary, the velocity of the wave
in the medium, the velocity of the observer and the velocity of the source.
"""
def doppler_effect(
org_freq: float, wave_vel: float, obs_vel: float, src_vel: float
) -> float:
"""
Input Parameters:
-----------------
org_freq: frequency of the wave when the source is stationary
wave_vel: velocity of the wave in the medium
obs_vel: velocity of the observer, +ve if the observer is moving towards the source
src_vel: velocity of the source, +ve if the source is moving towards the observer
Returns:
--------
f: frequency of the wave as perceived by the observer
Docstring Tests:
>>> doppler_effect(100, 330, 10, 0) # observer moving towards the source
103.03030303030303
>>> doppler_effect(100, 330, -10, 0) # observer moving away from the source
96.96969696969697
>>> doppler_effect(100, 330, 0, 10) # source moving towards the observer
103.125
>>> doppler_effect(100, 330, 0, -10) # source moving away from the observer
97.05882352941177
>>> doppler_effect(100, 330, 10, 10) # source & observer moving towards each other
106.25
>>> doppler_effect(100, 330, -10, -10) # source and observer moving away
94.11764705882354
>>> doppler_effect(100, 330, 10, 330) # source moving at same speed as the wave
Traceback (most recent call last):
...
ZeroDivisionError: Division by zero implies vs=v and observer in front of the source
>>> doppler_effect(100, 330, 10, 340) # source moving faster than the wave
Traceback (most recent call last):
...
ValueError: Non-positive frequency implies vs>v or v0>v (in the opposite direction)
>>> doppler_effect(100, 330, -340, 10) # observer moving faster than the wave
Traceback (most recent call last):
...
ValueError: Non-positive frequency implies vs>v or v0>v (in the opposite direction)
"""
if wave_vel == src_vel:
raise ZeroDivisionError(
"Division by zero implies vs=v and observer in front of the source"
)
doppler_freq = (org_freq * (wave_vel + obs_vel)) / (wave_vel - src_vel)
if doppler_freq <= 0:
raise ValueError(
"Non-positive frequency implies vs>v or v0>v (in the opposite direction)"
)
return doppler_freq
if __name__ == "__main__":
import doctest
doctest.testmod()