Skip to content

Commit

Permalink
Adding siblings algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
cs-wellington-santos committed Feb 25, 2018
1 parent fd0ace5 commit 1d622d2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
23 changes: 23 additions & 0 deletions algoritms/siblings_numbers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#
# Two non-negatives integers are called siblings if they can be obtained from each other
# by rearranging the digits of their decimal representations. For example, 123 and 213 are siblings
# 535 and 355 are also siblings.

# A set consisting of a non-negative integer N and all of its siblings is called the
# of N. For example, the family of 553 comprises three numbers: 355, 535 and 553.

# Write a function:
# def solution(n)

# that, given a non-negative integer N, returns the largest number in the family of N.
# For example, given N = 213 the function should return 321. Given N = 553 the function should return 553.
# Assume that:
# N is an integer within the range [0..10,000].
# In your solution, focus on correctness, . The performance of your solution will not be the focus of the assessment.

# Wellington Avelino

def solution(n)
siblings = n.to_s.chars.each_slice(1).to_a
siblings.sort {|x,y| -(x <=> y)}.join.to_i
end

0 comments on commit 1d622d2

Please sign in to comment.