Download (right-click, save target as ...) this page as a jupyterlab notebook from: Lab5-HW
LAST NAME, FIRST NAME
R00000000
ENGR 1330 ES-5 - Homework
# Preamble script block to identify host, user, and kernel
import sys
! hostname
! whoami
print(sys.executable)
print(sys.version)
print(sys.version_info)
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.
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)
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:
row=1
column=3
row=5
column=2
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
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 ¦
+-------------------------+
# 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
1904 was a leap year. Create a script that prints out all the leap years from in the 20th century (1904-1999).
# A loop for leaps - some hints
# Make a script that prints all years from 1904-1999; then modify to just print the leaps!
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!)
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