Compare commits

...

6 Commits

Author SHA1 Message Date
nikhitha79
98d07b1478
Merge aff6698fff into ad6395d340 2024-10-05 13:28:57 -04:00
Andrey Ivanov
ad6395d340
Update ruff usage example in CONTRIBUTING.md (#11772)
* Update ruff usage example

* Update CONTRIBUTING.md

Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>

---------

Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
2024-10-05 10:24:58 -07:00
Jeel Rupapara
50aca04c67
feat: increase test coverage of longest_common_subsequence to 75% (#11777) 2024-10-05 10:21:43 -07:00
1227haran
5a8655d306
Added new algorithm to generate numbers in lexicographical order (#11674)
* Added algorithm to generate numbers in lexicographical order

* Removed the test cases

* Updated camelcase to snakecase

* Added doctest

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

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

* Added descriptive name for n

* Reduced the number of letters

* Updated the return type

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

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

* Updated import statement

* Updated return type to Iterator[int]

* removed parentheses

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-10-05 10:19:58 -07:00
pre-commit-ci[bot]
aff6698fff [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-04 12:31:29 +00:00
nikhitha79
c3cc048a04
Create bragg_angle.py 2024-10-04 18:00:25 +05:30
4 changed files with 98 additions and 1 deletions

View File

@ -96,7 +96,7 @@ We want your work to be readable by others; therefore, we encourage you to note
```bash
python3 -m pip install ruff # only required the first time
ruff .
ruff check
```
- Original code submission require docstrings or comments to describe your work.

View File

@ -0,0 +1,38 @@
from collections.abc import Iterator
def lexical_order(max_number: int) -> Iterator[int]:
"""
Generate numbers in lexical order from 1 to max_number.
>>> " ".join(map(str, lexical_order(13)))
'1 10 11 12 13 2 3 4 5 6 7 8 9'
>>> list(lexical_order(1))
[1]
>>> " ".join(map(str, lexical_order(20)))
'1 10 11 12 13 14 15 16 17 18 19 2 20 3 4 5 6 7 8 9'
>>> " ".join(map(str, lexical_order(25)))
'1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25 3 4 5 6 7 8 9'
>>> list(lexical_order(12))
[1, 10, 11, 12, 2, 3, 4, 5, 6, 7, 8, 9]
"""
stack = [1]
while stack:
num = stack.pop()
if num > max_number:
continue
yield num
if (num % 10) != 9:
stack.append(num + 1)
stack.append(num * 10)
if __name__ == "__main__":
from doctest import testmod
testmod()
print(f"Numbers from 1 to 25 in lexical order: {list(lexical_order(26))}")

View File

@ -28,6 +28,24 @@ def longest_common_subsequence(x: str, y: str):
(2, 'ph')
>>> longest_common_subsequence("computer", "food")
(1, 'o')
>>> longest_common_subsequence("", "abc") # One string is empty
(0, '')
>>> longest_common_subsequence("abc", "") # Other string is empty
(0, '')
>>> longest_common_subsequence("", "") # Both strings are empty
(0, '')
>>> longest_common_subsequence("abc", "def") # No common subsequence
(0, '')
>>> longest_common_subsequence("abc", "abc") # Identical strings
(3, 'abc')
>>> longest_common_subsequence("a", "a") # Single character match
(1, 'a')
>>> longest_common_subsequence("a", "b") # Single character no match
(0, '')
>>> longest_common_subsequence("abcdef", "ace") # Interleaved subsequence
(3, 'ace')
>>> longest_common_subsequence("ABCD", "ACBD") # No repeated characters
(3, 'ABD')
"""
# find the length of strings

41
physics/bragg_angle.py Normal file
View File

@ -0,0 +1,41 @@
import math
def bragg_angle(distance: float, order: int, wavelength: float) -> float:
"""
Calculate the Bragg diffraction angle using the formula:
sin(θ) = (n * λ) / (2 * d)
Parameters:
distance d (float): Distance between crystal planes (in meters).
order n (int): Order of reflection.
wavelength λ (float): Wavelength of the radiation (in meters).
Examples:
>>> bragg_angle(2.2e-10, 1, 2.2e-10)
30.0
>>> bragg_angle(5e-10, 2, 1e-10)
11.5
>>> bragg_angle(4e-10, 1, 4e-10)
30.0
# Test case for an invalid sine value (out of range)
>>> bragg_angle(1e-10, 2, 3e-10)
Traceback (most recent call last):
...
ValueError: The calculated sine value is out of the valid range.
"""
sine_theta = (order * wavelength) / (2 * distance)
if sine_theta > 1 or sine_theta < -1:
raise ValueError("The calculated sine value is out of the valid range.")
theta_radians = math.asin(sine_theta)
theta_degrees = math.degrees(theta_radians)
return round(theta_degrees, 1)
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)