Skip to content

Commit

Permalink
Merge pull request #139 from Peefy/feat-difflib-module
Browse files Browse the repository at this point in the history
feat: add difflib module to help kcl unit test cases
  • Loading branch information
Peefy authored May 29, 2024
2 parents 55b9297 + adfb9cb commit b214e86
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
2 changes: 1 addition & 1 deletion difflib/kcl.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "difflib"
edition = "v0.9.0"
version = "0.1.0"
version = "0.2.0"

12 changes: 9 additions & 3 deletions difflib/main.k
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ looper = lambda initial: any, elements: [any], func: (any, any) -> any -> any {
_looper_n(elements, 0, func, initial)
}

looper_enumerate = lambda initial: any, elements: [any] | {str:}, func: (any, str | int, any) -> any -> any {
looper_enumerate1 = lambda initial: any, elements: [any] | {str:}, func: (any, str | int, any) -> any -> any {
looper(initial, [{"k" = k, "v" = v} for k, v in elements], lambda initial, value {
func(initial, value.k, value.v)
})
}

looper_enumerate2 = lambda initial: any, elements: [any] | {str:}, func: (any, str | int, any) -> any -> any {
looper(initial, [{"k" = k, "v" = v} for k, v in elements], lambda initial, value {
func(initial, value.k, value.v)
})
Expand All @@ -40,8 +46,8 @@ longest_common_subsequence = lambda a: [], b: [] -> [] {
"""Longest Common Subsequence (LCS) is a typical algorithm for calculating the length of the longest common subsequence between two sequences."""
# Build the lengths matrix for dp
lengths = [[0] * (len(b) + 1) for _ in range(len(a) + 1)]
lengths = looper_enumerate(lengths, a, lambda m, i, x {
looper_enumerate(m, b, lambda v, j, y {
lengths = looper_enumerate1(lengths, a, lambda m, i, x {
looper_enumerate2(m, b, lambda v, j, y {
list_set_index(v, i + 1, list_set_index(v[i + 1], j + 1, v[i][j] + 1 if x == y else max(v[i + 1][j], v[i][j + 1])))
})
})
Expand Down
42 changes: 42 additions & 0 deletions difflib/main_test.k
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,45 @@ test_longest_common_subsequence = lambda {
assert result == case.result, "expect ${case.result}, got ${result}"
})
}

test_yaml_diff = lambda {
data1 = {
"firstName": "John",
"lastName": "Doe",
"age": 30,
"address": {
"streetAddress": "1234 Main St",
"city": "New York",
"state": "NY",
"postalCode": "10001"
},
"phoneNumbers": [
{
"type": "home",
"number": "212-555-1234"
},
{
"type": "work",
"number": "646-555-5678"
}
]
}
data2 = {
"firstName": "John",
"lastName": "Doe",
"age": 30,
"address": {
"streetAddress": "1234 Main St",
"city": "New York",
"state": "NY",
"postalCode": None
},
"phoneNumbers": [
{
"type": "work",
"number": "646-555-5678"
}
]
}
diff_text = diff(yaml.encode(data1), yaml.encode(data2))
}

0 comments on commit b214e86

Please sign in to comment.