Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

[pull] master from TheAlgorithms:master #70

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 4 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
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
83 changes: 47 additions & 36 deletions physics/newtons_second_law_of_motion.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
"""
Description :
Newton's second law of motion pertains to the behavior of objects for which
all existing forces are not balanced.
The second law states that the acceleration of an object is dependent upon two variables
- the net force acting upon the object and the mass of the object.
The acceleration of an object depends directly
upon the net force acting upon the object,
and inversely upon the mass of the object.
As the force acting upon an object is increased,
the acceleration of the object is increased.
As the mass of an object is increased, the acceleration of the object is decreased.
r"""
Description:
Newton's second law of motion pertains to the behavior of objects for which
all existing forces are not balanced.
The second law states that the acceleration of an object is dependent upon
two variables - the net force acting upon the object and the mass of the object.
The acceleration of an object depends directly
upon the net force acting upon the object,
and inversely upon the mass of the object.
As the force acting upon an object is increased,
the acceleration of the object is increased.
As the mass of an object is increased, the acceleration of the object is decreased.

Source: https://www.physicsclassroom.com/class/newtlaws/Lesson-3/Newton-s-Second-Law
Formulation: Fnet = m • a
Diagrammatic Explanation:

Formulation: F_net = m • a

Diagrammatic Explanation::

Forces are unbalanced
|
|
Expand All @@ -26,35 +30,42 @@
/ \
/ \
/ \
__________________ ____ ________________
|The acceleration | |The acceleration |
|depends directly | |depends inversely |
|on the net Force | |upon the object's |
|_________________| |mass_______________|
Units:
1 Newton = 1 kg X meters / (seconds^2)
__________________ ____________________
| The acceleration | | The acceleration |
| depends directly | | depends inversely |
| on the net force | | upon the object's |
| | | mass |
|__________________| |____________________|

Units: 1 Newton = 1 kg • meters/seconds^2

How to use?
Inputs:
___________________________________________________
|Name | Units | Type |
|-------------|-------------------------|-----------|
|mass | (in kgs) | float |
|-------------|-------------------------|-----------|
|acceleration | (in meters/(seconds^2)) | float |
|_____________|_________________________|___________|

Output:
___________________________________________________
|Name | Units | Type |
|-------------|-------------------------|-----------|
|force | (in Newtons) | float |
|_____________|_________________________|___________|

Inputs::

______________ _____________________ ___________
| Name | Units | Type |
|--------------|---------------------|-----------|
| mass | in kgs | float |
|--------------|---------------------|-----------|
| acceleration | in meters/seconds^2 | float |
|______________|_____________________|___________|

Output::

______________ _______________________ ___________
| Name | Units | Type |
|--------------|-----------------------|-----------|
| force | in Newtons | float |
|______________|_______________________|___________|

"""


def newtons_second_law_of_motion(mass: float, acceleration: float) -> float:
"""
Calculates force from `mass` and `acceleration`

>>> newtons_second_law_of_motion(10, 10)
100
>>> newtons_second_law_of_motion(2.0, 1)
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
beautifulsoup4
fake_useragent
fake-useragent
imageio
keras
lxml
Expand All @@ -11,7 +11,7 @@ pillow
requests
rich
scikit-learn
sphinx_pyproject
sphinx-pyproject
statsmodels
sympy
tweepy
Expand Down
28 changes: 24 additions & 4 deletions strings/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def join(separator: str, separated: list[str]) -> str:
'a'
>>> join(" ", ["You", "are", "amazing!"])
'You are amazing!'
>>> join(",", ["", "", ""])
',,'

This example should raise an
exception for non-string elements:
Expand All @@ -37,15 +39,33 @@ def join(separator: str, separated: list[str]) -> str:
'apple-banana-cherry'
"""

joined = ""
# Check that all elements are strings
for word_or_phrase in separated:
# If the element is not a string, raise an exception
if not isinstance(word_or_phrase, str):
raise Exception("join() accepts only strings")

joined: str = ""
"""
The last element of the list is not followed by the separator.
So, we need to iterate through the list and join each element
with the separator except the last element.
"""
last_index: int = len(separated) - 1
"""
Iterate through the list and join each element with the separator.
Except the last element, all other elements are followed by the separator.
"""
for word_or_phrase in separated[:last_index]:
# join the element with the separator.
joined += word_or_phrase + separator

# Remove the trailing separator
# by stripping it from the result
return joined.strip(separator)
# If the list is not empty, join the last element.
if separated != []:
joined += separated[last_index]

# Return the joined string.
return joined


if __name__ == "__main__":
Expand Down
5 changes: 4 additions & 1 deletion strings/split.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ def split(string: str, separator: str = " ") -> list:

>>> split("12:43:39",separator = ":")
['12', '43', '39']

>>> split(";abbb;;c;", separator=';')
['', 'abbb', '', 'c', '']
"""

split_words = []
Expand All @@ -23,7 +26,7 @@ def split(string: str, separator: str = " ") -> list:
if char == separator:
split_words.append(string[last_index:index])
last_index = index + 1
elif index + 1 == len(string):
if index + 1 == len(string):
split_words.append(string[last_index : index + 1])
return split_words

Expand Down