Starting from:

$30

HW4 Shell Programming

CS507 Computing Foundation for Computational Science HW4
Shell Programming

Submission command: submit minlong CS507 HW4
Instruction to electronic submission: http://cs.boisestate.edu/∼cs221/SubmissionProcedure.html
• The written assignment can be done in a pure text format (*.txt, for example, problem1.txt) on
Onyx.
• The programming assignment should be presented with source codes.
• Each problem should have its own working directory, such as HW2/prob1, HW2/prob2 ... For
example, the following table shows the structure of HW1 from the user “student1” and how to
submit HW to us through Onyx
1 [ student1@onyx :HW1] $ l s −l
2 drwxr−x−−−. 2 s t u d e n t 1 S tuden t s 13 Sep 19 2021 prob1
3 drwxr−xr−x . 3 s t u d e n t 1 S tuden t s 14 Sep 19 2021 prob2
4 [ student1@onyx :HW1] $ cd prob1 /
5 [ minlong@onyx : prob1 ] $ l s
6 −rw−r−−−−−. 1 s t u d e n t 1 S tuden t s 943 Sep 19 2021 problem1 . t x t
7 $ cd . .
8 [ student1@onyx :HW1] $ pwd
9 /home/ s t u d e n t 1 /CS507/HW1
10 [ student1@onyx :HW1] $ submit minlong CS507 HW1
Listing 1: A sample structure of homework and submission procedure.
• Your source codes (if any) must compile and run on Onyx.
• Documentation is important and proper comments are expected in your source code.
– comments giving description of: purpose, parameters, and return value if applicable
– other comments where clarification of source code is needed
– proper and consistent indentation
– proper structure and modularity
Don’t ask us or your classmates directly for solutions (it happened); just try as much as possible.
Be patient and enjoy coding!
1
Programming Problems
The grading will be based on the simplicity and quality of the code. The simpler, shorter, well-designed
code will get the higher score.
1. (10 pts) Angle between Stars. Given two stars with angles of declination and right ascension
(d1, a1) and (d2, a2), respectively, the angle they subtend is given by the formula
2 arcsin n
[sin2
(d/2) + cos(d1) cos(d2) sin2
(a/2)]1/2
o
,
where a1 and a2 are angles between -180 and 180 degrees, d1 and d2 are angles between -90 and
90 degrees, a = a2 − a1, and d = d2 − d1. Compose a program to take the declination and right
ascension of two stars as command-line arguments and write the angle they subtend. Select the
coordinates of the following two bright stars. Hint: Be careful about converting from degrees to
radians.
Table 1: Coordinates of Altair and Vega stars
Star’s name Right Ascension (RA) Declination (DEC)
Altair 19h 50m 46.99855s +08◦ 52’ 05.9563”
Vega 18h 36m 56.33635s +38◦ 47’ 01.2802”
Here the right ascension is represented using “hour angles”, so you need to convert it to degrees
first. Basically, 3600 s = 60 m = 1 h = 15◦
. For example, 23h=345◦=-15◦
.
Extended reading. Any units of angular measure could have been chosen for right ascension,
but it is customarily measured in hours (h), minutes (m), and seconds (s), with 24h being equivalent to a full circle (360 ◦
). Astronomers have chosen this unit to measure right ascension because
they measure a star’s location by timing its passage through the highest point in the sky as the
Earth rotates. The line which passes through the highest point in the sky, called the meridian,
is the projection of a longitude line onto the celestial sphere.
Since a complete circle contains 24h of right ascension or 360◦
(degrees of arc), 1/24 of a circle is
measured as 1h of right ascension, or 15◦
; 1/1440 of a circle is measured as 1m of right ascension,
or 15 minutes of arc (also written as 15’); and 1/86400 of a circle contains 1s of right ascension, or
15 seconds of arc (also written as 15”). A full circle, measured in right-ascension units, contains
24 × 60 × 60 = 86400s or 24 × 60 = 1440m, or 24h.
2. (10 pts) Matrix multiplication. Compose a function multiply() that takes two square matrices
of the same dimension and returns their product. Make sure your output is formatted.


1 2 3
4 5 6
7 8 9

 ·


9 8 7
6 5 4
3 2 1

 =?
Extended Reading. If you are not familiar with the matrix multiplication, check your math
book for details. We briefly summarize it below. If you have 2 matrices A and B, their product
2
C is represented as,
C = A · B
A = [aij ] i, j = 1, 2, ..., n
B = [bij ]
C = [cij ]
The matrix forms are,


c11 c12 . . . c1n
c21 c22 . . . c2n
.
.
.
.
.
.
.
.
.
.
.
.
cn1 cn2 . . . cnn


=


a11 a12 . . . a1n
a21 a22 . . . a2n
.
.
.
.
.
.
.
.
.
.
.
.
an1 an2 . . . ann


·


b11 b12 . . . b1n
b21 b22 . . . b2n
.
.
.
.
.
.
.
.
.
.
.
.
bn1 bn2 . . . bnn


Each term in C is computed as,
cij =
Xn
k=1
aik · bkj
3. (10pts) Polynomials using array. Compose a program polynomial.py with a function evaluate(x,
a) that evaluates the polynomial a(x) whose coefficients are the elements in the array a[]:
a0 + a1x + a2x
2 + ... + an−2x
n−2 + an−1x
n−1
An efficient way to perform the computations that is suggested by the following parenthesization:
a0 + x(a1 + x(a2 + ... + x(an−2 + xan−1)...))
Then compose a function exp() that calls evaluate() to compute an approximation to e
x
, using
the first n terms of the Taylor series expansion e
x = 1 +x+x
2/2! +x
3/3! +.... Take an argument
x (e.g., try e
1 =?) from the command line, and compare your result against that computed by
math.exp(x).
4. (10pts) Day of the week and Calendar. Compose a program that accepts a date as input and
writes the day of the week that date falls on. Your program should accept three command-line
arguments: m (month), d (day), and y (year). For m, use 1 for January, 2 for February, and
so forth. For output, write 0 for Sunday, 1 for Monday, 2 for Tuesday, and so forth. Use the
following formulas for the Gregorian calendar:
y0 = y − (14 − m)//12
x = y0 + y0//4 − y0//100 + y0//400
m0 = m + 12 × ((14 − m)//12) − 2
d0 = (d + x + (31 × m0)//12)%7
where “//” denotes floored division operator and “%” denotes the remainder operator. Example:
On what day of the week was February 14, 2000?
y0 = 2000 − (14 − 2)//12 = 1999
x = 1999 + 1999//4 − 1999//100 + 1999//400 = 1999 + 499 − 19 + 4 = 2483
m0 = 2 + 12 × ((14 − 2)//12) − 2 = 2 + 12 × 1 − 2 = 12
d0 = (14 + 2483 + (31 × 12)//12)%7 = (14 + 2483 + 31)%7 = 1(Monday)
3
1 $python d a te o fwee k . py 2 14 2000
2 1
Listing 2: results of dayofweek.py.
Based on this program and the leapyear.py listed below,
1 import s y s
2
3 # Accept an i n t ye a r a s a command−l i n e argument . Write True t o
4 # s t and a rd output i f ye a r i s a l e a p ye a r . O the rwi se w ri t e F al s e .
5
6 ye a r = i n t ( s y s . argv [ 1 ] )
7
8 i sLe apYe a r = ( ye a r % 4 == 0 )
9 i sLe apYe a r = i sLe apYe a r and ( ye a r % 100 != 0 )
10 i sLe apYe a r = i sLe apYe a r o r ( ye a r % 400 == 0 )
11
12 p r i n t ( s y s . argv [ 1]+ ’ i s a l e a p ye a r ? ’ , i sLe apYe a r )
Listing 3: leapyear.py.
compose a program cal.py that takes two command-line arguments m and y and writes the
monthly calendar for the mth month of year y, as in this example:
1 October 2022
2 Su Mo Tu We Th Fr Sa
3 1
4 2 3 4 5 6 7 8
5 9 10 11 12 13 14 15
6 16 17 18 19 20 21 22
7 23 24 25 26 27 28 29
8 30 31
Listing 4: results of cal.py.
5. (10 pts) Binary representation using recursion. Compose a program that takes a positive
integer n (in decimal) from the command line and writes its binary representation. Recall that
in binary.py we used the method of subtracting out powers of 2.
1 import s y s
2 n = i n t ( s y s . argv [ 1 ] )
3
4 # Compute v a s the l a r g e s t power o f 2 <= n .
5 v = 1
6 w hil e v <= n / / 2:
7 v ∗= 2
8
9 # Cast out powers o f 2 i n d e c r e a si n g o r d e r .
10 w hil e v > 0 :
11 i f n < v :
12 p r i n t ( 0 , end=”” )
13 e l s e :
14 p r i n t ( 1 , end=”” )
15 n −= v
16 v //= 2
17 p r i n t ( )
Listing 5: binary.py.
4
1 $ python i n t e g e r t o b i n a r y . py 8
2 1000
Listing 6: results of binary.py.
Instead, use the following simpler method: repeatedly divide 2 into n and read the remainders
backward. First, compose a while loop to carry out this computation and write the bits in the
wrong order. Then, use recursion to write the bits in the correct order.
6. (10 pts) Using Object’s methods to process string. We will discuss this problem on Monday’s class. There are three strings, a=’now is ’, b=’the time ’, c=’to’. Use built-in str class
and its instance method to print out the following results in a formatted style.
1 $ python s t ri n g −p r o c e s s i n g . py
2 a : ”now i s ”
3 b : ” the time ”
4 c : ” t o ”
5 l e n ( a ) : 7
6 a [ 4 ] : ” i ”
7 a [ 2 : 5 ] : ”w i ”
8 c . upper ( ) : ”TO”
9 b . s t a r t s w i t h ( ” the ” ) : True
10 a . f i n d ( ” i s ” ) : 4
11 a+c : ”now i s t o ”
12 b . r e p l a c e ( ” t ” , ”T” ) : ”The Time ”
13 a . s p l i t ( ) : [ ’ now ’ , ’ i s ’ ]
14 b==c : F al s e
15 a . s t r i p ( ) : ”now i s ”
Listing 7: Sample output.
Think: can you do it in less than 10 lines of code? If you want to make your code short, you may
need to use Python’s built-in functions isinstance(), eval().
Overall, the grading will be based on the simplicity and quality of the code. The simpler, shorter,
well-designed code will get a higher score.
5

More products