Last active
February 13, 2016 00:21
-
-
Save seoyoochan/81cadab7ac2b8c5782a2 to your computer and use it in GitHub Desktop.
Return the largest int from an array of numerical strings
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
#! /usr/bin/env ruby | |
# Author: Yoochan Seo | |
# Email: [email protected] | |
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
install ruby and
thor
gem to run this program as cli