Skip to content

Commit 8be683b

Browse files
authored
Merge branch 'TheAlgorithms:master' into interpolation-search
2 parents 5acdb6a + 56a00cc commit 8be683b

7 files changed

Lines changed: 54 additions & 5 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ jobs:
77
- uses: actions/checkout@v4
88
- uses: ruby/setup-ruby@v1
99
with:
10-
ruby-version: '3.4'
10+
ruby-version: '4.0'
1111
- name: Run tests
1212
run: rake test

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
* [Find Min](https://github.com/TheAlgorithms/Ruby/blob/master/maths/find_min.rb)
142142
* [Lucas Series](https://github.com/TheAlgorithms/Ruby/blob/master/maths/lucas_series.rb)
143143
* [Number Of Digits](https://github.com/TheAlgorithms/Ruby/blob/master/maths/number_of_digits.rb)
144+
* [Palindrome](https://github.com/TheAlgorithms/Ruby/blob/master/maths/palindrome.rb)
144145
* [Pascal Triangle Ii](https://github.com/TheAlgorithms/Ruby/blob/master/maths/pascal_triangle_ii.rb)
145146
* [Power Of Two](https://github.com/TheAlgorithms/Ruby/blob/master/maths/power_of_two.rb)
146147
* [Prime Number](https://github.com/TheAlgorithms/Ruby/blob/master/maths/prime_number.rb)

maths/palindrome.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Palindrome Number Checking in Ruby
2+
3+
# Checks if a given integer is a palindrome without converting it to a string.
4+
# A palindrome is a number that reads the same backward as forward.
5+
# Negative numbers are not considered palindromes by this definition.
6+
#
7+
# @param number [Integer] The integer to check.
8+
# @return [Boolean] true if the number is a palindrome, false otherwise.
9+
#
10+
# Examples:
11+
# is_palindrome(121) #=> true
12+
# is_palindrome(123) #=> false
13+
# is_palindrome(7) #=> true
14+
# is_palindrome(0) #=> true
15+
# is_palindrome(-121) #=> false
16+
def is_palindrome(number)
17+
# Negative numbers are not considered palindromes
18+
return false if number < 0
19+
20+
# Single digits are always palindromes
21+
# return true if number < 10 # This line is optional but efficient
22+
23+
original_number = number
24+
reversed_number = 0
25+
26+
# Loop while the number is greater than 0
27+
while number > 0
28+
# Get the last digit
29+
remainder = number % 10
30+
31+
# Build the reversed number
32+
reversed_number = (reversed_number * 10) + remainder
33+
34+
# Remove the last digit using integer division
35+
number /= 10
36+
end
37+
38+
# Return true if the original and reversed numbers are the same
39+
original_number == reversed_number
40+
end
41+
42+
# --- Examples ---
43+
puts "Is 121 a palindrome? #{is_palindrome(121)}" # Output: true
44+
puts "Is 123 a palindrome? #{is_palindrome(123)}" # Output: false
45+
puts "Is 7 a palindrome? #{is_palindrome(7)}" # Output: true
46+
puts "Is 0 a palindrome? #{is_palindrome(0)}" # Output: true
47+
puts "Is -101 a palindrome? #{is_palindrome(-101)}" # Output: false
48+
puts "Is 1221 a palindrome? #{is_palindrome(1221)}" # Output: true

sorting/bead_sort_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require_relative './sort_tests'
33
require_relative './bead_sort'
44

5-
class TestInsertionSort < Minitest::Test
5+
class TestBeadSort < Minitest::Test
66
include SortTests
77

88
def sort(input)

sorting/cocktail_sort_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require_relative './sort_tests'
33
require_relative './cocktail_sort'
44

5-
class TestInsertionSort < Minitest::Test
5+
class TestCocktailSort < Minitest::Test
66
include SortTests
77

88
def sort(input)

sorting/comb_sort_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require_relative './sort_tests'
33
require_relative './comb_sort'
44

5-
class TestInsertionSort < Minitest::Test
5+
class TestCombSort < Minitest::Test
66
include SortTests
77

88
def sort(input)

sorting/pancake_sort_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require_relative './sort_tests'
33
require_relative './pancake_sort'
44

5-
class TestInsertionSort < Minitest::Test
5+
class TestPancakeSort < Minitest::Test
66
include SortTests
77

88
def sort(input)

0 commit comments

Comments
 (0)