-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add ruby code sample for test (#40) * Added information in ruby file * Update scanner test as per ruby file changes * Update comment block in ruby file * Updated scanner test as per ruby file changes
- Loading branch information
1 parent
b10da78
commit 28c6467
Showing
3 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
=begin | ||
Given an array of 0s and 1s in random order. | ||
Segregate 0s on left side and 1s on right side of the array | ||
=end | ||
def segregate(a) | ||
left, right = 0, a.length - 1 | ||
while left < right | ||
# Increment left index while we see 0 at left | ||
while a[left] == 0 | ||
left += 1 | ||
end | ||
|
||
# Decrement right index while we see 1 at right | ||
while a[right] == 1 | ||
right -= 1 | ||
end | ||
|
||
# Exchange arr[left] and arr[right] | ||
if left < right | ||
a[left], a[right] = a[right], a[left] | ||
left += 1 | ||
right -= 1 | ||
end | ||
end | ||
|
||
return a | ||
end | ||
|
||
p segregate([1,0,1,1,0,0]) | ||
# Output: [0, 0, 0, 1, 1, 1] |