Download (right-click, save target as ...) this page as a jupyterlab notebook from: Lab5-HW


Exercise Set 5: Sequence, Selection, and Repetition - Oh My!

LAST NAME, FIRST NAME

R00000000

ENGR 1330 ES-5 - Homework

In [6]:
# Preamble script block to identify host, user, and kernel
import sys
! hostname
! whoami
print(sys.executable)
print(sys.version)
print(sys.version_info)
atomickitty
sensei
/opt/jupyterhub/bin/python3
3.8.10 (default, Nov 26 2021, 20:14:08) 
[GCC 9.3.0]
sys.version_info(major=3, minor=8, micro=10, releaselevel='final', serial=0)

Exercise 1 : Find the Treasure Part 1

Consider the structure below (a treasure map)

              +-------------------------+
              ¦ 34 ¦ 21 ¦ 32 ¦ 41 ¦ 25  ¦
              +----+----+----+----+-----¦
              ¦ 14 ¦ 42 ¦ 43 ¦ 14 ¦ 31  ¦
              +----+----+----+----+-----¦
              ¦ 54 ¦ 45 ¦ 52 ¦ 42 ¦ 23  ¦
              +----+----+----+----+-----¦
              ¦ 33 ¦ 15 ¦ 51 ¦ 31 ¦ 35  ¦
              +----+----+----+----+-----¦
              ¦ 21 ¦ 52 ¦ 33 ¦ 13 ¦ 23  ¦
              +-------------------------+

In this problem you are to write a program to explore the above array for a treasure. The values in the array are clues. Each cell contains an integer between 11 and 55; for each value the ten's digit represents the row number and the unit's digit represents the column number of the cell containing the next clue. Starting in the upper left corner (at 1,1), use the clues to guide your search of the array. (The first three clues are 11, 34, 42). The treasure is a cell whose value is the same as its coordinates. Your program must first read in the treasure map data into a 5 by 5 array. Your program should output the cells it visits during its search, and a message indicating where you found the treasure.

The "Treasure Hunt Problem" is from the HackerRank.com avaiable at https://www.hackerrank.com/contests/startatastartup/challenges/treasure-hunt

Now for your problem we have not yet learned how to read from files, its a small problem so lets just construct the map manually by a sequence of expressions; Your colleague got it started, but three of the rows are duplicates of the second row; repair the script and demonstrate that the repair correctly prints the treasure map.

In [ ]:
treasuremap = [] # empty list to store row, column information
treasuremap.append([34,21,32,41,25]) # first row of the map
treasuremap.append([14,42,43,14,31]) # second row of the map
treasuremap.append([14,42,43,14,31])# third row of the map
treasuremap.append([14,42,43,14,31])# fourth row of the map
treasuremap.append([14,42,43,14,31])# fifth row of the map
print(treasuremap) # print the map (which is a list)

Exercise 1 Part 2 -- Adding Some Selection

Using your corrected treasure map; script a selection statement that tests if a cell contains treasure (does the cell value agree with the row and column index), but it needs useful messages - add the messages.

Test your script using:

  • Case 1 row=1 column=3
  • Case 2 row=5 column=2
In [ ]:
row = 1
column = 3
print(treasuremap[row-1][column-1]) # print a single element
maprowval = str(treasuremap[row-1][column-1])[0]
mapcolval = str(treasuremap[row-1][column-1])[1]
if int(maprowval) == row and int(mapcolval) == column :
    # message here for treasure
    pass #comment this line out when have message
else:
    # message here for no treasure
    pass #comment this line out when have message

Exercise 1 : Part 3 - Completing the Hunt

Now you have all the parts needed to automatically find the treasure using your script from Part 2, and the scaffold below (incomplete - thats your job) insert your selection script code into the appropriate place and find the treasure. Stop the search when the treasure is found.

              +-------------------------+
              ¦ 34 ¦ 21 ¦ 32 ¦ 41 ¦ 25  ¦
              +----+----+----+----+-----¦
              ¦ 14 ¦ 42 ¦ 43 ¦ 14 ¦ 31  ¦
              +----+----+----+----+-----¦
              ¦ 54 ¦ 45 ¦ 52 ¦ 42 ¦ 23  ¦
              +----+----+----+----+-----¦
              ¦ 33 ¦ 15 ¦ 51 ¦ 31 ¦ 35  ¦
              +----+----+----+----+-----¦
              ¦ 21 ¦ 52 ¦ 33 ¦ 13 ¦ 23  ¦
              +-------------------------+
In [ ]:
# put the correct treasure map here
########################
for i in range(0,5,1): 
    print(treasuremap[i][:]) # print the map by row
########################
for i in range(0,5,1): # visit the rows
    for j in range(0,5,1): # visit the columns
        print(treasuremap[i][j]) # print the element by element, suppress when working
        # get row and column from i and j values
        # get maprowval and mapcolval as in part 2
        # test if cell is a treasure cell or not as in part 2 and issue message

Exercise 2: A Loop for Leaps!

1904 was a leap year. Create a script that prints out all the leap years from in the 20th century (1904-1999).

In [14]:
# A loop for leaps - some hints
# Make a script that prints all years from 1904-1999; then modify to just print the leaps!

Exercise 3: Whats your sine?

Print a table of the sines of angles (in radians) between 0 and 1.57 with steps of 0.01. The script below might help (it will need modification!)

In [ ]:
import math # package that contains trig functions
print("     Cosines     ")
print("   x   ","|"," cos(x) ")
print("--------|--------")
for i in range(0,10,1):
    x = float(i)*0.01
    print("%.3f" % x, "  |", " %.4f "  % math.cos(x)) # note the format code and the placeholder % and syntax of using package