Skip to content

Instantly share code, notes, and snippets.

@seoyoochan
Last active February 13, 2016 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seoyoochan/81cadab7ac2b8c5782a2 to your computer and use it in GitHub Desktop.
Save seoyoochan/81cadab7ac2b8c5782a2 to your computer and use it in GitHub Desktop.
Return the largest int from an array of numerical strings
#! /usr/bin/env ruby
# Author: Yoochan Seo
# Email: supergnee@gmail.com
# If you want this program executable,
# `chmod 755 largest`
=begin
./largest 99, 997, 101, 2, 33 # execute this program and pass your value
Your input... ["99,", "997,", "101,", "2,", "33"]
Max array... [99997, 997101, 2101, 332]
The largest is 997101
=end
require "rubygems"
require "thor"
class Largest < Thor
desc "parse", "Make the largest int from the input"
def self.parse(input)
puts "Your input... #{input}"
calculate
end
def self.calculate
str_set = ARGV
int_set = str_set
int_set.map!(&:to_i)
max_sort = []
int_set.each_with_index do |v, i|
num1 = v
num2 = int_set[i+1]
if num2
maybe_max1 = "#{num1}#{num2}".to_i
maybe_max2 = "#{num2}#{num1}".to_i
if maybe_max1 > maybe_max2
max_sort << maybe_max1
elsif maybe_max1 == maybe_max2
max_sort << maybe_max1
else
max_sort << maybe_max2
end
end
end
puts "Max array... #{max_sort}"
max = max_sort.max
puts "The largest is #{max}"
max
end
default_task :parse
end
Largest.parse(ARGV)
@seoyoochan
Copy link
Author

install ruby and thor gem to run this program as cli

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment