Skip to content

[Module] add algorithm module - #273

Open
khchen wants to merge 3 commits into
ThakeeNathees:masterfrom
khchen:algorithm
Open

[Module] add algorithm module#273
khchen wants to merge 3 commits into
ThakeeNathees:masterfrom
khchen:algorithm

Conversation

@khchen

@khchen khchen commented Jul 8, 2022

Copy link
Copy Markdown
  1. Add built-in string comparing.
  2. Add algorithm module, include sort, isSorted, binarySearch, reverse.
  3. Wrap these functions to methods of List.

Example:

print([4, 5, 1, 3, 2].sort())
print([4, 5, 1, 3, 2].sort().isSorted())
print([4, 5, 1, 3, 2].sort().binarySearch(4))
print([4, 5, 1, 3, 2].reverse())

from math import sqrt

class Point
  def _init(x, y)
    self.x = x; self.y = y
  end

  def _str
    return "(${self.x}, ${self.y})"
  end
end

l = [Point(3, 4), Point(1, 3), Point(2, 5)]
l.sort fn(p1, p2)
  return sqrt(p1.x**2 + p1.y**2) - sqrt(p2.x**2 + p2.y**2)
end
print(l[0], l[1], l[2])

Output:

[1, 2, 3, 4, 5]
true
3
[2, 3, 1, 5, 4]
(1, 3) (3, 4) (2, 5)

@khchen khchen changed the title Algorithm module [Module] add algorithm module Jul 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant