Skip to content

Commit 07c7bd1

Browse files
authored
Merge branch 'master' into jk-isomorphic-strings
2 parents b2773bb + aece3af commit 07c7bd1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2650
-13
lines changed

DIRECTORY.md

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11

2+
## Backtracking
3+
* [Generate Paranthesis](https://github.com/TheAlgorithms/Ruby/blob/master/backtracking/generate_paranthesis.rb)
4+
25
## Bit Manipulation
6+
* [Binary And Operator](https://github.com/TheAlgorithms/Ruby/blob/master/bit_manipulation/binary_and_operator.rb)
7+
* [Binary Count Setbits](https://github.com/TheAlgorithms/Ruby/blob/master/bit_manipulation/binary_count_setbits.rb)
8+
* [Binary Count Trailing Zeroes](https://github.com/TheAlgorithms/Ruby/blob/master/bit_manipulation/binary_count_trailing_zeroes.rb)
9+
* [Binary Or Operator](https://github.com/TheAlgorithms/Ruby/blob/master/bit_manipulation/binary_or_operator.rb)
10+
* [Binary Xor Operator](https://github.com/TheAlgorithms/Ruby/blob/master/bit_manipulation/binary_xor_operator.rb)
311
* [Power Of Two](https://github.com/TheAlgorithms/Ruby/blob/master/bit_manipulation/power_of_two.rb)
12+
* [Single Bit Binary Operations](https://github.com/TheAlgorithms/Ruby/blob/master/bit_manipulation/single_bit_binary_operations.rb)
413

514
## Ciphers
615
* [Merkle Hellman Cryptosystem](https://github.com/TheAlgorithms/Ruby/blob/master/ciphers/merkle_hellman_cryptosystem.rb)
716

817
## Conversions
918
* [Temperature Conversions](https://github.com/TheAlgorithms/Ruby/blob/master/conversions/temperature_conversions.rb)
19+
* [Weight Conversions](https://github.com/TheAlgorithms/Ruby/blob/master/conversions/weight_conversions.rb)
1020

1121
## Data Structures
1222
* Arrays
@@ -15,15 +25,22 @@
1525
* [Find The Highest Altitude](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_the_highest_altitude.rb)
1626
* [Fizz Buzz](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/fizz_buzz.rb)
1727
* [Get Products Of All Other Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb)
28+
* [Good Pairs](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/good_pairs.rb)
29+
* [Intersection](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/intersection.rb)
30+
* [Next Greater Element](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/next_greater_element.rb)
1831
* [Remove Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/remove_elements.rb)
1932
* [Richest Customer Wealth](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/richest_customer_wealth.rb)
33+
* [Shortest Word Distance](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/shortest_word_distance.rb)
2034
* [Shuffle Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/shuffle_array.rb)
2135
* [Single Number](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/single_number.rb)
2236
* [Sort Squares Of An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/sort_squares_of_an_array.rb)
37+
* [Sorted Arrays Intersection](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/sorted_arrays_intersection.rb)
2338
* Strings
2439
* [Anagram Checker](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/anagram_checker.rb)
2540
* [Jewels And Stones](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/jewels_and_stones.rb)
41+
* [Palindrome](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/palindrome.rb)
2642
* [Remove Vowels](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/remove_vowels.rb)
43+
* [Sudoku](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/sudoku.rb)
2744
* [Two Sum](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/two_sum.rb)
2845
* [Two Sum Ii](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/two_sum_ii.rb)
2946
* Binary Trees
@@ -32,15 +49,21 @@
3249
* [Postorder Traversal](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/postorder_traversal.rb)
3350
* [Preorder Traversal](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/preorder_traversal.rb)
3451
* Hash Table
52+
* [Anagram Checker](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/anagram_checker.rb)
53+
* [Arrays Intersection](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/arrays_intersection.rb)
54+
* [Common Characters](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/common_characters.rb)
3555
* [Find All Duplicates In An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/find_all_duplicates_in_an_array.rb)
56+
* [Good Pairs](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/good_pairs.rb)
3657
* [Isomorphic Strings](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/isomorphic_strings.rb)
3758
* [Richest Customer Wealth](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/richest_customer_wealth.rb)
3859
* [Two Sum](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/two_sum.rb)
60+
* [Uncommon Words](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/uncommon_words.rb)
3961
* Linked Lists
4062
* [Circular Linked List](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/linked_lists/circular_linked_list.rb)
4163
* [Doubly Linked List](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/linked_lists/doubly_linked_list.rb)
4264
* [Singly Linked List](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/linked_lists/singly_linked_list.rb)
4365
* Queues
66+
* [Circular Queue](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/queues/circular_queue.rb)
4467
* [Queue](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/queues/queue.rb)
4568
* Stacks
4669
* [Stack](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/stacks/stack.rb)
@@ -54,22 +77,36 @@
5477

5578
## Dynamic Programming
5679
* [Coin Change](https://github.com/TheAlgorithms/Ruby/blob/master/dynamic_programming/coin_change.rb)
80+
* [Count Sorted Vowel Strings](https://github.com/TheAlgorithms/Ruby/blob/master/dynamic_programming/count_sorted_vowel_strings.rb)
5781
* [Fibonacci](https://github.com/TheAlgorithms/Ruby/blob/master/dynamic_programming/fibonacci.rb)
5882

5983
## Maths
6084
* [Abs](https://github.com/TheAlgorithms/Ruby/blob/master/maths/abs.rb)
85+
* [Abs Max](https://github.com/TheAlgorithms/Ruby/blob/master/maths/abs_max.rb)
86+
* [Abs Min](https://github.com/TheAlgorithms/Ruby/blob/master/maths/abs_min.rb)
6187
* [Abs Test](https://github.com/TheAlgorithms/Ruby/blob/master/maths/abs_test.rb)
88+
* [Add](https://github.com/TheAlgorithms/Ruby/blob/master/maths/add.rb)
6289
* [Add Digits](https://github.com/TheAlgorithms/Ruby/blob/master/maths/add_digits.rb)
6390
* [Aliquot Sum](https://github.com/TheAlgorithms/Ruby/blob/master/maths/aliquot_sum.rb)
6491
* [Aliquot Sum Test](https://github.com/TheAlgorithms/Ruby/blob/master/maths/aliquot_sum_test.rb)
92+
* [Armstrong Number](https://github.com/TheAlgorithms/Ruby/blob/master/maths/armstrong_number.rb)
93+
* [Average Mean](https://github.com/TheAlgorithms/Ruby/blob/master/maths/average_mean.rb)
94+
* [Average Median](https://github.com/TheAlgorithms/Ruby/blob/master/maths/average_median.rb)
6595
* [Binary To Decimal](https://github.com/TheAlgorithms/Ruby/blob/master/maths/binary_to_decimal.rb)
6696
* [Ceil](https://github.com/TheAlgorithms/Ruby/blob/master/maths/ceil.rb)
6797
* [Ceil Test](https://github.com/TheAlgorithms/Ruby/blob/master/maths/ceil_test.rb)
98+
* [Count Sorted Vowel Strings](https://github.com/TheAlgorithms/Ruby/blob/master/maths/count_sorted_vowel_strings.rb)
6899
* [Decimal To Binary](https://github.com/TheAlgorithms/Ruby/blob/master/maths/decimal_to_binary.rb)
100+
* [Factorial](https://github.com/TheAlgorithms/Ruby/blob/master/maths/factorial.rb)
101+
* [Factorial Non Recursive Non Iterative](https://github.com/TheAlgorithms/Ruby/blob/master/maths/factorial_non_recursive_non_iterative.rb)
69102
* [Fibonacci](https://github.com/TheAlgorithms/Ruby/blob/master/maths/fibonacci.rb)
103+
* [Find Max](https://github.com/TheAlgorithms/Ruby/blob/master/maths/find_max.rb)
104+
* [Find Min](https://github.com/TheAlgorithms/Ruby/blob/master/maths/find_min.rb)
105+
* [Lucas Series](https://github.com/TheAlgorithms/Ruby/blob/master/maths/lucas_series.rb)
70106
* [Number Of Digits](https://github.com/TheAlgorithms/Ruby/blob/master/maths/number_of_digits.rb)
71107
* [Power Of Two](https://github.com/TheAlgorithms/Ruby/blob/master/maths/power_of_two.rb)
72108
* [Prime Number](https://github.com/TheAlgorithms/Ruby/blob/master/maths/prime_number.rb)
109+
* [Roman To Integer](https://github.com/TheAlgorithms/Ruby/blob/master/maths/roman_to_integer.rb)
73110
* [Square Root](https://github.com/TheAlgorithms/Ruby/blob/master/maths/square_root.rb)
74111
* [Square Root Test](https://github.com/TheAlgorithms/Ruby/blob/master/maths/square_root_test.rb)
75112
* [Sum Of Digits](https://github.com/TheAlgorithms/Ruby/blob/master/maths/sum_of_digits.rb)
@@ -98,28 +135,36 @@
98135
* [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_5/sol1.rb)
99136

100137
## Searches
101-
* [Binary Search](https://github.com/TheAlgorithms/Ruby/blob/master/Searches/binary_search.rb)
102-
* [Depth First Search](https://github.com/TheAlgorithms/Ruby/blob/master/Searches/depth_first_search.rb)
103-
* [Double Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/Searches/double_linear_search.rb)
104-
* [Jump Search](https://github.com/TheAlgorithms/Ruby/blob/master/Searches/jump_search.rb)
105-
* [Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/Searches/linear_search.rb)
106-
* [Recursive Double Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/Searches/recursive_double_linear_search.rb)
107-
* [Recursive Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/Searches/recursive_linear_search.rb)
108-
* [Ternary Search](https://github.com/TheAlgorithms/Ruby/blob/master/Searches/ternary_search.rb)
138+
* [Binary Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/binary_search.rb)
139+
* [Depth First Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/depth_first_search.rb)
140+
* [Double Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/double_linear_search.rb)
141+
* [Jump Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/jump_search.rb)
142+
* [Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/linear_search.rb)
143+
* [Recursive Double Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/recursive_double_linear_search.rb)
144+
* [Recursive Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/recursive_linear_search.rb)
145+
* [Ternary Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/ternary_search.rb)
109146

110147
## Sorting
148+
* [Bead Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bead_sort.rb)
149+
* [Bead Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bead_sort_test.rb)
111150
* [Bogo Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bogo_sort.rb)
112151
* [Bogo Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bogo_sort_test.rb)
113152
* [Bubble Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bubble_sort.rb)
114153
* [Bubble Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bubble_sort_test.rb)
115154
* [Bucket Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bucket_sort.rb)
116155
* [Bucket Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bucket_sort_test.rb)
156+
* [Cocktail Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/cocktail_sort.rb)
157+
* [Cocktail Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/cocktail_sort_test.rb)
158+
* [Comb Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/comb_sort.rb)
159+
* [Comb Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/comb_sort_test.rb)
117160
* [Heap Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/heap_sort.rb)
118161
* [Heap Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/heap_sort_test.rb)
119162
* [Insertion Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/insertion_sort.rb)
120163
* [Insertion Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/insertion_sort_test.rb)
121164
* [Merge Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/merge_sort.rb)
122165
* [Merge Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/merge_sort_test.rb)
166+
* [Pancake Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/pancake_sort.rb)
167+
* [Pancake Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/pancake_sort_test.rb)
123168
* [Quicksort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/quicksort.rb)
124169
* [Quicksort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/quicksort_test.rb)
125170
* [Radix Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/radix_sort.rb)
@@ -128,4 +173,5 @@
128173
* [Selection Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/selection_sort_test.rb)
129174
* [Shell Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/shell_sort.rb)
130175
* [Shell Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/shell_sort_test.rb)
176+
* [Sort Color](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/sort_color.rb)
131177
* [Sort Tests](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/sort_tests.rb)
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Given n pairs of parentheses, write a function to generate all combinations
2+
# of well-formed parentheses.
3+
#
4+
# Example 1:
5+
#
6+
# Input: n = 3
7+
# Output: ["((()))","(()())","(())()","()(())","()()()"]
8+
# Example 2:
9+
#
10+
# Input: n = 1
11+
# Output: ["()"]
12+
#
13+
#
14+
# Constraints:
15+
#
16+
# 1 <= n <= 8
17+
18+
# Approach:
19+
#
20+
# Let's only add '(' or ')' when we know it will remain a valid sequence.
21+
# We can do this by keeping track of the number of opening and closing brackets
22+
# we have placed so far.
23+
#
24+
# We can start an opening bracket if we still have one (of n) left to place.
25+
# And we could start a closing bracket if it'd not exceed the number of opening
26+
# brackets.
27+
28+
# Complexity Analysis
29+
#
30+
# Time Complexity: O(4^n/sqrt(n)). Each valid sequence has at most n steps during the backtracking procedure.
31+
# Space Complexity: O(4^n/sqrt(n)), as described above, and using O(n) space to store the sequence.
32+
33+
# Refer to the attached diagram for recursion,
34+
# The numbers next to each node are the counts of left and right parantheses
35+
36+
# @param {Integer} n
37+
# @return {String[]}
38+
def generate_parenthesis(n)
39+
parenthesis = []
40+
backtrack(parenthesis, "", 0, 0, n)
41+
parenthesis
42+
end
43+
44+
def backtrack(parenthesis, curr, open, close, max)
45+
if curr.length == max * 2
46+
parenthesis.push(curr)
47+
return
48+
end
49+
50+
if open < max
51+
backtrack(parenthesis, curr + "(", open + 1, close, max)
52+
end
53+
54+
if close < open
55+
backtrack(parenthesis, curr + ")", open, close + 1, max)
56+
end
57+
end
58+
59+
n = 3
60+
print(generate_parenthesis(n))
61+
# Output: ["((()))","(()())","(())()","()(())","()()()"]
62+
63+
# *** Example: n = 3 *** Space after each DFS instance
64+
# backtrack called with 0 0 []
65+
# backtrack called with ( 1 0 []
66+
# backtrack called with (( 2 0 []
67+
# backtrack called with ((( 3 0 []
68+
# backtrack called with ((() 3 1 []
69+
# backtrack called with ((()) 3 2 []
70+
# backtrack called with ((())) 3 3 []
71+
# backtrack return with ((()) 3 2 ["((()))"]
72+
# backtrack return with ((() 3 1 ["((()))"]
73+
# backtrack return with ((( 3 0 ["((()))"]
74+
# backtrack called with (() 2 1 ["((()))"]
75+
# backtrack called with (()( 3 1 ["((()))"]
76+
# backtrack called with (()() 3 2 ["((()))"]
77+
# backtrack called with (()()) 3 3 ["((()))"]
78+
# backtrack return with (()() 3 2 ["((()))", "(()())"]
79+
# backtrack return with (()( 3 1 ["((()))", "(()())"]
80+
# backtrack called with (()) 2 2 ["((()))", "(()())"]
81+
# backtrack called with (())( 3 2 ["((()))", "(()())"]
82+
# backtrack called with (())() 3 3 ["((()))", "(()())"]
83+
# backtrack return with (())( 3 2 ["((()))", "(()())", "(())()"]
84+
# backtrack return with (()) 2 2 ["((()))", "(()())", "(())()"]
85+
# backtrack return with (() 2 1 ["((()))", "(()())", "(())()"]
86+
# backtrack return with (( 2 0 ["((()))", "(()())", "(())()"]
87+
# backtrack called with () 1 1 ["((()))", "(()())", "(())()"]
88+
# backtrack called with ()( 2 1 ["((()))", "(()())", "(())()"]
89+
# backtrack called with ()(( 3 1 ["((()))", "(()())", "(())()"]
90+
# backtrack called with ()(() 3 2 ["((()))", "(()())", "(())()"]
91+
# backtrack called with ()(()) 3 3 ["((()))", "(()())", "(())()"]
92+
# backtrack return with ()(() 3 2 ["((()))", "(()())", "(())()", "()(())"]
93+
# backtrack return with ()(( 3 1 ["((()))", "(()())", "(())()", "()(())"]
94+
# backtrack called with ()() 2 2 ["((()))", "(()())", "(())()", "()(())"]
95+
# backtrack called with ()()( 3 2 ["((()))", "(()())", "(())()", "()(())"]
96+
# backtrack called with ()()() 3 3 ["((()))", "(()())", "(())()", "()(())"]
97+
# backtrack return with ()()( 3 2 ["((()))", "(()())", "(())()", "()(())", "()()()"]
98+
# backtrack return with ()() 2 2 ["((()))", "(()())", "(())()", "()(())", "()()()"]
99+
# backtrack return with ()( 2 1 ["((()))", "(()())", "(())()", "()(())", "()()()"]
100+
# backtrack return with () 1 1 ["((()))", "(()())", "(())()", "()(())", "()()()"]
101+
# backtrack return with ( 1 0 ["((()))", "(()())", "(())()", "()(())", "()()()"]
102+
# backtrack return with 0 0 ["((()))", "(()())", "(())()", "()(())", "()()()"]
103+
104+
n = 1
105+
print(generate_parenthesis(n))
106+
# Output: ["()"]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def binary_and(x, y)
2+
raise 'Input must only contain positive integers' if x < 0 or y < 0
3+
4+
"0b" + (x & y).to_s(2)
5+
end
6+
7+
begin
8+
binary_and(-1, 0)
9+
rescue => e
10+
puts e.message
11+
end
12+
# Input must only contain positive integers
13+
14+
puts binary_and(1, 1)
15+
# 0b1
16+
puts binary_and(0, 1)
17+
# 0b0
18+
puts binary_and(1024, 1024)
19+
# 0b10000000000
20+
puts binary_and(0, 1023)
21+
# 0b0000000000
22+
puts binary_and(16, 58)
23+
# 0b010000
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def binary_count_setbits(x)
2+
raise 'Input must be a positive integer' if x < 0
3+
4+
binary = x.to_s(2)
5+
6+
binary.chars.map { |c| c.to_i }.reduce(:+)
7+
end
8+
9+
begin
10+
binary_count_setbits(-1)
11+
rescue => e
12+
puts e.message
13+
end
14+
# Input must be a positive integer
15+
16+
puts binary_count_setbits(0)
17+
# 0
18+
19+
puts binary_count_setbits(1)
20+
# 1
21+
22+
puts binary_count_setbits(1024)
23+
# 1
24+
25+
puts binary_count_setbits(1023)
26+
# 10
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
def binary_count_trailing_zeroes(x)
2+
raise 'Input must be a positive integer' if x < 0
3+
4+
binary = x.to_s(2)
5+
6+
count = 0
7+
binary.chars.reverse_each do |char|
8+
break if char == "1"
9+
10+
count += 1
11+
end
12+
13+
count
14+
end
15+
16+
begin
17+
binary_count_trailing_zeroes(-1)
18+
rescue => e
19+
puts e.message
20+
end
21+
# Input must be a positive integer
22+
23+
puts binary_count_trailing_zeroes(0)
24+
# 1
25+
26+
puts binary_count_trailing_zeroes(1023)
27+
# 0
28+
29+
puts binary_count_trailing_zeroes(1024)
30+
# 10
31+
32+
puts binary_count_trailing_zeroes(54)
33+
# 1
34+
35+
puts binary_count_trailing_zeroes(121024)
36+
# 6
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def binary_or(x, y)
2+
raise 'Input must only contain positive integers' if x < 0 or y < 0
3+
4+
"0b" + (x | y).to_s(2)
5+
end
6+
7+
begin
8+
binary_or(-1, 0)
9+
rescue => e
10+
puts e.message
11+
end
12+
# Input must only contain positive integers
13+
14+
puts binary_or(1, 1)
15+
# 0b1
16+
puts binary_or(0, 1)
17+
# 0b1
18+
puts binary_or(1024, 1024)
19+
# 0b10000000000
20+
puts binary_or(0, 1023)
21+
# 0b1111111111
22+
puts binary_or(16, 58)
23+
# 0b110010
24+

0 commit comments

Comments
 (0)