Skip to content

Commit 1f1daaf

Browse files
9harshitcclaussdhruvmanila
authored
feat: add strings/ngram algorithm (#6074)
* feat: added ngram algorithm * fix(test): use `math.isclose` to match floating point numbers approximately Co-authored-by: Christian Clauss <[email protected]> Co-authored-by: Dhruv Manilawala <[email protected]>
1 parent 4064bf4 commit 1f1daaf

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

Diff for: arithmetic_analysis/in_static_equilibrium.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ def polar_force(
1212
"""
1313
Resolves force along rectangular components.
1414
(force, angle) => (force_x, force_y)
15-
>>> polar_force(10, 45)
16-
[7.071067811865477, 7.0710678118654755]
15+
>>> import math
16+
>>> force = polar_force(10, 45)
17+
>>> math.isclose(force[0], 7.071067811865477)
18+
True
19+
>>> math.isclose(force[1], 7.0710678118654755)
20+
True
1721
>>> polar_force(10, 3.14, radian_mode=True)
1822
[-9.999987317275396, 0.01592652916486828]
1923
"""
@@ -50,7 +54,11 @@ def in_static_equilibrium(
5054
if __name__ == "__main__":
5155
# Test to check if it works
5256
forces = array(
53-
[polar_force(718.4, 180 - 30), polar_force(879.54, 45), polar_force(100, -90)]
57+
[
58+
polar_force(718.4, 180 - 30),
59+
polar_force(879.54, 45),
60+
polar_force(100, -90),
61+
]
5462
)
5563

5664
location = array([[0, 0], [0, 0], [0, 0]])

Diff for: strings/ngram.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
https://en.wikipedia.org/wiki/N-gram
3+
"""
4+
5+
6+
def create_ngram(sentence: str, ngram_size: int) -> list[str]:
7+
"""
8+
Create ngrams from a sentence
9+
10+
>>> create_ngram("I am a sentence", 2)
11+
['I ', ' a', 'am', 'm ', ' a', 'a ', ' s', 'se', 'en', 'nt', 'te', 'en', 'nc', 'ce']
12+
>>> create_ngram("I am an NLPer", 2)
13+
['I ', ' a', 'am', 'm ', ' a', 'an', 'n ', ' N', 'NL', 'LP', 'Pe', 'er']
14+
>>> create_ngram("This is short", 50)
15+
[]
16+
"""
17+
return [sentence[i : i + ngram_size] for i in range(len(sentence) - ngram_size + 1)]
18+
19+
20+
if __name__ == "__main__":
21+
from doctest import testmod
22+
23+
testmod()

0 commit comments

Comments
 (0)