Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Module] add algorithm module #273

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

khchen
Copy link

@khchen khchen commented Jul 8, 2022

  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.

None yet

1 participant