Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions archive/r/ruby/linear-search.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
USAGE = 'Usage: please provide a list of integers ("1, 4, 5, 11, 12") and the integer to find ("11")'


if ARGV.length < 2
puts USAGE
return
end

# the first input (list of numbers)
list_input = ARGV[0]
#second input (number to find)
target_input = ARGV[1]

#check if the input is empty
if list_input.strip.empty? || target_input.strip.empty?
puts USAGE
return
end

begin
#split list by the commas, trim the spaces, then turn into intgers
numbers = list_input.split(',').map { |s| s.strip.to_i }

#convert second number to integer
target = Integer(target_input)
rescue ArgumentError
# if conversion fails we show the usage message
puts USAGE
return
end

#track if we find the number
found = false

# go through each number in the list
numbers.each do |n|
if n == target
found = true
# stop searching once we find it
break
end
end

# print result as true or false
if found
puts "true"
else
puts "false"
end