a) Write a program that takes three integer values a, b, and c as command-line a
ID: 3593625 • Letter: A
Question
a) Write a program that takes three integer values a, b, and c as command-line arguments in any order and prints True if they form a Pythagorean triple (see https://en.wikipedia.org/wiki/Pythagorean_triple), and False otherwise.
b) Write a program that takes three integer values a, b, and c from the command line and prints them in ascending order, separated by a space. You can use the built-in min() and max() functions.
Assume we are using Python 2.x
Please explain how you got the code for each problem. Thanks!
Explanation / Answer
a)
#!/usr/bin/python
import sys
#storing the inputs in variables
a = sys.argv[1]
b = sys.argv[2]
c = sys.argv[3]
#checking pythagoras triplets
if(a*a+b*b == c*c):
print("True") #return True if true
else:
print("False") #return false
b)
#!/usr/bin/python
import sys
#printing integers into ascending order
def minmaxsort(a,b,c):
print(min(a,b,c), min(max(a,b), max(b,c), max(a,c)),max(a,b,c))
return
#if running without arguments
if (len(sys.argv)==1):
print("Format: minmax.py integer1 integer2 integer3")
sys.exit(-1)
#wrong number of arguments
if (len(sys.argv)!=4):
print("invalid number of arguments")
print("Format: minmax.py integer1 integer2 integer3" )
sys.exit(-1)
try:
minmaxsort(int(sys.argv[1]),int(sys.argv[2]),int(sys.argv[3]))
#if there is no integer input
except ValueError:
print("invalid input: arguments must be integers")
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.