Day 2: Arguments

X-Istence

*
Political User
Joined
Dec 5, 2001
Messages
6,498
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.
 

fitz

Woah.. I'm still here?
Staff member
Political User
Joined
Apr 26, 2004
Messages
4,084
Reviving the thread using VBScipt/WSH:
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
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
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.
I'll add this last one tomorrow (maybe) if I get around to it.
 
Last edited:

Geffy

OSNN Veteran Addict
Joined
Mar 18, 2002
Messages
7,805
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
 

LeeJend

OSNN Veteran Addict
Joined
Jan 25, 2003
Messages
5,291
Reminds me of APL circa 1975. A program langauge designed so you can write the entire program in one line of code.
 

Members online

No members online now.

Latest profile posts

Perris Calderon wrote on Electronic Punk's profile.
Hey ep!

All good with me, applying for microsoft mvp right now, should have done this a while ago.

Notifications don't work, I only found your response by comming back to hunt up some threads, if you want, give me your email address so we can keep in touch easier, mine is perriscalderon at gmail
Perris Calderon wrote on Electronic Punk's profile.
Ep, glad to see you come back and tidy up...did want to ask a one day favor, I want to enhance my resume , was hoping you could make me administrator for a day, if so, take me right off since I won't be here to do anything, and don't know the slightest about the board, but it would be nice putting "served administrator osnn", if can do, THANKS

Been running around Quora lately, luv it there https://tinyurl.com/ycpxl
Electronic Punk wrote on Perris Calderon's profile.
All good still mate?
Hello, is there anybody in there? Just nod if you can hear me ...

Forum statistics

Threads
61,978
Messages
673,277
Members
89,052
Latest member
KateBentley