Skip to content

Commit d956203

Browse files
Baron105pre-commit-ci[bot]cclauss
authored
added a function to calculate perceived frequency by observer using Doppler Effect (TheAlgorithms#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 <[email protected]>
1 parent 4707fdb commit d956203

File tree

3 files changed

+132
-4
lines changed

3 files changed

+132
-4
lines changed

maths/special_numbers/perfect_number.py

+28-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ def perfect(number: int) -> bool:
2525
Returns:
2626
True if the number is a perfect number, False otherwise.
2727
28+
Start from 1 because dividing by 0 will raise ZeroDivisionError.
29+
A number at most can be divisible by the half of the number except the number
30+
itself. For example, 6 is at most can be divisible by 3 except by 6 itself.
31+
2832
Examples:
2933
>>> perfect(27)
3034
False
@@ -41,15 +45,35 @@ def perfect(number: int) -> bool:
4145
>>> perfect(8128)
4246
True
4347
>>> perfect(0)
44-
>>> perfect(-3)
48+
False
49+
>>> perfect(-1)
50+
False
4551
>>> perfect(12.34)
46-
>>> perfect("day")
47-
>>> perfect(["call"])
52+
Traceback (most recent call last):
53+
...
54+
ValueError: number must be an integer
55+
>>> perfect("Hello")
56+
Traceback (most recent call last):
57+
...
58+
ValueError: number must be an integer
4859
"""
60+
if not isinstance(number, int):
61+
raise ValueError("number must be an integer")
62+
if number <= 0:
63+
return False
4964
return sum(i for i in range(1, number // 2 + 1) if number % i == 0) == number
5065

5166

5267
if __name__ == "__main__":
68+
from doctest import testmod
69+
70+
testmod()
5371
print("Program to check whether a number is a Perfect number or not...")
54-
number = int(input("Enter number: ").strip())
72+
try:
73+
number = int(input("Enter a positive integer: ").strip())
74+
except ValueError:
75+
msg = "number must be an integer"
76+
print(msg)
77+
raise ValueError(msg)
78+
5579
print(f"{number} is {'' if perfect(number) else 'not '}a Perfect Number.")
File renamed without changes.

physics/doppler_frequency.py

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
"""
2+
Doppler's effect
3+
4+
The Doppler effect (also Doppler shift) is the change in the frequency of a wave in
5+
relation to an observer who is moving relative to the source of the wave. The Doppler
6+
effect is named after the physicist Christian Doppler. A common example of Doppler
7+
shift is the change of pitch heard when a vehicle sounding a horn approaches and
8+
recedes from an observer.
9+
10+
The reason for the Doppler effect is that when the source of the waves is moving
11+
towards the observer, each successive wave crest is emitted from a position closer to
12+
the observer than the crest of the previous wave. Therefore, each wave takes slightly
13+
less time to reach the observer than the previous wave. Hence, the time between the
14+
arrivals of successive wave crests at the observer is reduced, causing an increase in
15+
the frequency. Similarly, if the source of waves is moving away from the observer,
16+
each wave is emitted from a position farther from the observer than the previous wave,
17+
so the arrival time between successive waves is increased, reducing the frequency.
18+
19+
If the source of waves is stationary but the observer is moving with respect to the
20+
source, the transmission velocity of the waves changes (ie the rate at which the
21+
observer receives waves) even if the wavelength and frequency emitted from the source
22+
remain constant.
23+
24+
These results are all summarized by the Doppler formula:
25+
26+
f = (f0 * (v + v0)) / (v - vs)
27+
28+
where:
29+
f: frequency of the wave
30+
f0: frequency of the wave when the source is stationary
31+
v: velocity of the wave in the medium
32+
v0: velocity of the observer, positive if the observer is moving towards the source
33+
vs: velocity of the source, positive if the source is moving towards the observer
34+
35+
Doppler's effect has many applications in physics and engineering, such as radar,
36+
astronomy, medical imaging, and seismology.
37+
38+
References:
39+
https://en.wikipedia.org/wiki/Doppler_effect
40+
41+
Now, we will implement a function that calculates the frequency of a wave as a function
42+
of the frequency of the wave when the source is stationary, the velocity of the wave
43+
in the medium, the velocity of the observer and the velocity of the source.
44+
"""
45+
46+
47+
def doppler_effect(
48+
org_freq: float, wave_vel: float, obs_vel: float, src_vel: float
49+
) -> float:
50+
"""
51+
Input Parameters:
52+
-----------------
53+
org_freq: frequency of the wave when the source is stationary
54+
wave_vel: velocity of the wave in the medium
55+
obs_vel: velocity of the observer, +ve if the observer is moving towards the source
56+
src_vel: velocity of the source, +ve if the source is moving towards the observer
57+
58+
Returns:
59+
--------
60+
f: frequency of the wave as perceived by the observer
61+
62+
Docstring Tests:
63+
>>> doppler_effect(100, 330, 10, 0) # observer moving towards the source
64+
103.03030303030303
65+
>>> doppler_effect(100, 330, -10, 0) # observer moving away from the source
66+
96.96969696969697
67+
>>> doppler_effect(100, 330, 0, 10) # source moving towards the observer
68+
103.125
69+
>>> doppler_effect(100, 330, 0, -10) # source moving away from the observer
70+
97.05882352941177
71+
>>> doppler_effect(100, 330, 10, 10) # source & observer moving towards each other
72+
106.25
73+
>>> doppler_effect(100, 330, -10, -10) # source and observer moving away
74+
94.11764705882354
75+
>>> doppler_effect(100, 330, 10, 330) # source moving at same speed as the wave
76+
Traceback (most recent call last):
77+
...
78+
ZeroDivisionError: Division by zero implies vs=v and observer in front of the source
79+
>>> doppler_effect(100, 330, 10, 340) # source moving faster than the wave
80+
Traceback (most recent call last):
81+
...
82+
ValueError: Non-positive frequency implies vs>v or v0>v (in the opposite direction)
83+
>>> doppler_effect(100, 330, -340, 10) # observer moving faster than the wave
84+
Traceback (most recent call last):
85+
...
86+
ValueError: Non-positive frequency implies vs>v or v0>v (in the opposite direction)
87+
"""
88+
89+
if wave_vel == src_vel:
90+
raise ZeroDivisionError(
91+
"Division by zero implies vs=v and observer in front of the source"
92+
)
93+
doppler_freq = (org_freq * (wave_vel + obs_vel)) / (wave_vel - src_vel)
94+
if doppler_freq <= 0:
95+
raise ValueError(
96+
"Non-positive frequency implies vs>v or v0>v (in the opposite direction)"
97+
)
98+
return doppler_freq
99+
100+
101+
if __name__ == "__main__":
102+
import doctest
103+
104+
doctest.testmod()

0 commit comments

Comments
 (0)