Skip to content

Commit 44cd424

Browse files
committed
Longest common subsequence
1 parent b279f24 commit 44cd424

File tree

5 files changed

+88
-2
lines changed

5 files changed

+88
-2
lines changed

codes/generate_docs

0 Bytes
Binary file not shown.

codes/generate_docs.mojo

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ def update_index(
8686
print(new_entry)
8787

8888
# Append new entry
89-
index_lines.append(new_entry)
90-
INDEX_MD.write_text(StringSlice("\n").join(index_lines) + "\n")
89+
index_lines.append("\n"+ new_entry)
90+
#INDEX_MD.write_text(StringSlice("\n").join(index_lines) + "\n")
91+
INDEX_MD.write_text(StringSlice("\n").join(index_lines))
9192
print("Updated: ", INDEX_MD)
9293
print()
9394
print(INDEX_MD.read_text().splitlines()[-1])

codes/longest_common_subeq.mojo

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
### Longest Common Subsequence Recursive
2+
### Return the length of the longest common subsequence between two strings, or 0 if none exists
3+
4+
5+
fn longest_subseq(mut text1: String, mut text2: String) raises -> Int:
6+
if len(text1) == 0 or len(text2) == 0:
7+
return 0
8+
if text1[len(text1) - 1] == text2[len(text2) - 1]:
9+
text1 = text1[0:-1]
10+
text2 = text2[0:-1]
11+
return 1 + longest_subseq(text1, text2)
12+
else:
13+
text_1 = text1[0:-1]
14+
text_2 = text2[0:-1]
15+
count1 = longest_subseq(text1, text_2)
16+
count2 = longest_subseq(text2, text_1)
17+
return max(count1, count2)
18+
19+
20+
from testing import assert_equal
21+
22+
23+
fn main() raises:
24+
var text1: String = "abcde"
25+
var text2: String = "ace"
26+
result = longest_subseq(text1, text2)
27+
assert_equal(result, 3, "Assertion failed")
28+
29+
text1 = "abc"
30+
text2 = "abc"
31+
result = longest_subseq(text1, text2)
32+
assert_equal(result, 3, "Assertion failed")
33+
34+
text1 = "abc"
35+
text2 = "xyz"
36+
result = longest_subseq(text1, text2)
37+
assert_equal(result, 0, "Assertion failed")
38+

docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,5 @@
4848
🟢 [Combination sum](combination_sum.md) ➔ Find all unique combinations of numbers from candidates (reusable unlimited times) that sum to target.
4949

5050
🟢 [Word Search](wordsearch.md) ➔ Check if a word can be formed in a grid by sequentially adjacent (non-repeating) horizontal or vertical letters.
51+
52+
🟢 [Longest Common Subsequence Recursive](longest_common_subeq.md) ➔ Return the length of the longest common subsequence between two strings, or 0 if none exists

docs/longest_common_subeq.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
### Longest Common Subsequence Recursive
2+
### Return the length of the longest common subsequence between two strings, or 0 if none exists
3+
4+
```python
5+
6+
7+
fn longest_subseq(mut text1: String, mut text2: String) raises -> Int:
8+
if len(text1) == 0 or len(text2) == 0:
9+
return 0
10+
if text1[len(text1) - 1] == text2[len(text2) - 1]:
11+
text1 = text1[0:-1]
12+
text2 = text2[0:-1]
13+
return 1 + longest_subseq(text1, text2)
14+
else:
15+
text_1 = text1[0:-1]
16+
text_2 = text2[0:-1]
17+
count1 = longest_subseq(text1, text_2)
18+
count2 = longest_subseq(text2, text_1)
19+
return max(count1, count2)
20+
21+
22+
from testing import assert_equal
23+
24+
25+
fn main() raises:
26+
var text1: String = "abcde"
27+
var text2: String = "ace"
28+
result = longest_subseq(text1, text2)
29+
assert_equal(result, 3, "Assertion failed")
30+
31+
text1 = "abc"
32+
text2 = "abc"
33+
result = longest_subseq(text1, text2)
34+
assert_equal(result, 3, "Assertion failed")
35+
36+
text1 = "abc"
37+
text2 = "xyz"
38+
result = longest_subseq(text1, text2)
39+
assert_equal(result, 0, "Assertion failed")
40+
41+
42+
```
43+
44+
45+
[Source](https://github.com/ratulb/mojo_programming/blob/main/codes/longest_common_subeq.mojo)

0 commit comments

Comments
 (0)