Day 2: Arguments

Discussion in 'Web Design & Coding' started by X-Istence, Mar 26, 2007.

  1. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    So far we have been outputting to the shell, hello world and the osnn.net lines. However, there has to be a way that we can specify what we want it to do by checking the argument we get sent when we start the program.

    Task 2-1: Create a program that prints all the arguments passed to it
    Task 2-2: Modify the above program to sort them alphabetically first
    Task 2-3: Find all duplicate arguments and remove them
    Task 2-4: Using your knowledge about arguments. Create a program that depending on what the argument passed is will ask for either of the following:
    Arg 1: Your name and birthday, and outputs how old they are in days.
    Arg 2: Random line of text, to be printed on new lines
    Arg 3: Print hello world.

    So "program 2" would ask me for a random line of text and print it out on new lines.

    If no argument is passed it should print the list of options as well as the name of the current program, and how it should be used.

    Good luck. I will post my solution to Day 1 tomorrow.
     
  2. fitz

    fitz Just Floating Along Staff Member Political User Folding Team

    Messages:
    4,076
    Location:
    Chicagoland
    Reviving the thread using VBScipt/WSH:
    Well, VBScript has no built in sort function, so I used my own quick sort function.

    edit: this is using a straight string/ascii sort so numbers aren't sorted correctly. I could correct it, but I'm too lazy right now ---> okay, this is fixed now
    Code:
    if wscript.arguments.count=0 then
    	wscript.echo "No Arguements Passed"
    	wscript.quit
    else
    	arrCount=0
    	dim arrList()
    	for i = 0 to wscript.arguments.count-1
    		strArg = wscript.arguments(i)
    		if isNumeric(strArg) then strArg = cInt(strArg)
    		strLine = "," & join(ArrList,",") & ","
    		if inStr(strLine,"," & strArg & ",")=0 then
    			redim preserve arrList(arrCount)
    			arrList(arrCount) = strArg
    			arrCount = arrCount+1
    		end if
    	next
    End If
    
    if uBound(arrList) > 0 then call QuickSort(arrList,0,uBound(arrList))
    
    for i = 0 to uBound(arrList)
    	wscript.echo arrList(i)
    next
    
    
    '/*************************
    Sub QuickSort(arrData,intLow,intHigh)
    
    	if intHigh - intLow = 1 then
    		if arrData(intLow) > arrData(intHigh) then
    			strTemp=arrData(intLow)
    			arrData(intLow) = arrData(intHigh)
    			arrData(intHigh) = strTemp
    		End If
    	End If
    
    	intPivot = arrData(int((intLow + intHigh) / 2))
    	arrData(int((intLow + intHigh) / 2)) = arrData(intLow)
    	arrData(intLow) = intPivot
    	intLowTemp = intLow + 1
    	intHighTemp = intHigh
    
    	do
    		while intLowTemp < intHighTemp and arrData(intLowTemp) <= intPivot
    			intLowTemp = intLowTemp + 1
    		wend
    
    		while arrData(intHighTemp) > intPivot
    			intHighTemp = intHighTemp - 1
    		wend
    
    		if intLowTemp < intHighTemp then
    			strTemp = arrData(intLowTemp)
    			arrData(intLowTemp) = arrData(intHighTemp)
    			arrData(intHighTemp) = strTemp
    		End If
    
    	loop while intLowTemp < intHighTemp
      
    	arrData(intLow) = arrData(intHighTemp)
    	arrData(intHighTemp) = intPivot
      
    	if intLow < (intHighTemp - 1) then Call QuickSort(arrData,intLow,intHighTemp-1)
    
    	if intHighTemp + 1 < intHigh then Call QuickSort(arrData,intHighTemp+1,intHigh)
    
    End Sub
    
    I'll add this last one tomorrow (maybe) if I get around to it.
     
    Last edited: Apr 17, 2007
  3. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    Ruby

    Code:
    #!/usr/bin/env ruby
    #
    # Task 1: 
    #  Create a program that prints all the arguments passed to it
    
    puts ARGV.join(' ')
    
    Code:
    #!/usr/bin/env ruby
    #
    # Task 2: 
    #  Modify the above program to sort them alphabetically first
    
    puts ARGV.sort.join(' ')
    
    Code:
    #!/usr/bin/env ruby
    #
    # Task 3: 
    #  Find all duplicate arguments and remove them
    
    puts ARGV.uniq.join(' ')
    
    Code:
    #!/usr/bin/env ruby
    #
    # Task 4: 
    #  Create a program that depending on what the argument passed is will ask for either of the following:
    #   Arg 1: Your name and birthday, and outputs how old they are in days.
    #   Arg 2: Random line of text, to be printed on new lines
    #   Arg 3: Print hello world.
    
    require 'time'
    
    SECONDS_PER_DAY = 86_400
    
    case ARGV.first.to_i
    when 1
      print "Enter your birthday yyyy/mm/dd: "
      bday = Time.parse($stdin.readline.chomp)
      days = (Time.now.utc - bday.utc).to_i / SECONDS_PER_DAY
      puts ("You are #{days} days old.")
    when 2
      print "Gimme some random words: "
      puts $stdin.readline.chomp.split(' ').join("\n")
    when 3
      puts "hello world"
    else
      puts <<-EOM
    Usage: task4.rb [option]
    
    Options:
      1   - Will prompt for birthdate and give your age in days
      2   - Will take a random string of words and print them on new lines
      3   - Will print out "hello world"
    EOM
    end
    
     
  4. dubstar

    dubstar format c:

    Messages:
    1,357
    Location:
    Southern California
    wow, i want to learn Ruby.
     
  5. LeeJend

    LeeJend Moderator

    Messages:
    5,291
    Location:
    Fort Worth, TX
    Reminds me of APL circa 1975. A program langauge designed so you can write the entire program in one line of code.