Learn to Program: The Fundamentals Assignment 3 Solution

In this article i am gone to share a Coursera Course: Learn to Program: The Fundamentals Week Assignment | Learn to Program: The Fundamentals Assignment 3 Solution with you..

What to do

There are several functions that you will need to implement. We have listed the functions roughly in order of complexity. Each function body will be quite short. You can submit your assignment for feedback once every hour up until the deadline; we recommend that you do this at least once early on in order to make sure you can submit properly.

Step 1: Download the starter code
Step 2: Complete the body of function seconds_difference
Step 3: Complete the body of function hours_difference
Step 4: Complete the body of function to float_hours
Step 5: Write functions get_hours, get_minutes and get_seconds
Step 6: Complete the bodies of functions time_to_utc and time_from_utc
Step 7: Submit your work.

Learn to Program: The Fundamentals Assignment 3 Solution

You first need to create a python file give name assignment1.py
Copy all the code paste in a file and then save it.
Now upload your file on Coursera Course assignment and then click on submit.

assignment3.py

“””A board is a list of list of str. For example, the board
ANTT
XSOB
is represented as the list
[[‘A’, ‘N’, ‘T’, ‘T’], [‘X’, ‘S’, ‘O’, ‘B’]]

A word list is a list of str. For example, the list of words
ANT
BOX
SOB
TO
is represented as the list
[‘ANT’, ‘BOX’, ‘SOB’, ‘TO’]
“””

def is_valid_word(wordlist, word):
“”” (list of str, str) -> bool

Return True if and only if word is an element of wordlist.

>>> is_valid_word([‘ANT’, ‘BOX’, ‘SOB’, ‘TO’], ‘TO’)
True
“””
found = False
i = 0
while i < len(wordlist):
if wordlist[i] == word:
found = True
i=i+1
return found

def make_str_from_row(board, row_index):
“”” (list of list of str, int) -> str

Return the characters from the row of the board with index row_index
as a single string.

>>> make_str_from_row([[‘A’, ‘N’, ‘T’, ‘T’], [‘X’, ‘S’, ‘O’, ‘B’]], 0)
‘ANTT’
“””

li = (board[row_index])
return “”.join(li)

def make_str_from_column(board, column_index):
“”” (list of list of str, int) -> str

Return the characters from the column of the board with index column_index
as a single string.

>>> make_str_from_column([[‘A’, ‘N’, ‘T’, ‘T’], [‘X’, ‘S’, ‘O’, ‘B’]], 1)
‘NS’
“””
li = []
i = 0
while i < len(board):
li.append(board[i][column_index])
i =i+1

return “”.join(li)

def board_contains_word_in_row(board, word):
“”” (list of list of str, str) -> bool

Return True if and only if one or more of the rows of the board contains
word.

Precondition: board has at least one row and one column, and word is a
valid word.

>>> board_contains_word_in_row([[‘A’, ‘N’, ‘T’, ‘T’], [‘X’, ‘S’, ‘O’, ‘B’]], ‘SOB’)
True
“””

for row_index in range(len(board)):
if word in make_str_from_row(board, row_index):
return True

return False

def board_contains_word_in_column(board, word):
“”” (list of list of str, str) -> bool

Return True if and only if one or more of the columns of the board
contains word.

Precondition: board has at least one row and one column, and word is a
valid word.

>>> board_contains_word_in_column([[‘A’, ‘N’, ‘T’, ‘T’], [‘X’, ‘S’, ‘O’, ‘B’]], ‘NO’)
False
“””

i = len(board[0])
for col_index in range(i):
if word in make_str_from_column(board, col_index):
return True

return False

def board_contains_word(board, word):
“”” (list of list of str, str) -> bool

Return True if and only if word appears in board.

Precondition: board has at least one row and one column.

>>> board_contains_word([[‘A’, ‘N’, ‘T’, ‘T’], [‘X’, ‘S’, ‘O’, ‘B’]], ‘ANT’)
True
“””

if board_contains_word_in_column(board, word) == True :
return True
elif board_contains_word_in_row(board, word) == True :
return True
else:
return False

def word_score(word):
“”” (str) -> int

Return the point value the word earns.

Word length: < 3: 0 points
3-6: 1 point per character for all characters in word
7-9: 2 points per character for all characters in word
10+: 3 points per character for all characters in word

>>> word_score(‘DRUDGERY’)
16
“””
score = 0
count = 0
for c in word:
count = count + 1
if count < 3:
score = 0
elif count >=3 and count <= 6:
score = count * 1
elif count >=7 and count <=9:
score = count * 2
else:
score = count * 3

return score

def update_score(player_info, word):
“”” ([str, int] list, str) -> NoneType

player_info is a list with the player’s name and score. Update player_info
by adding the point value word earns to the player’s score.

>>> update_score([‘Jonathan’, 4], ‘ANT’)
“””
score = word_score(word)
player_info[1] = player_info[1] + score

def num_words_on_board(board, words):
“”” (list of list of str, list of str) -> int

Return how many words appear on board.

>>> num_words_on_board([[‘A’, ‘N’, ‘T’, ‘T’], [‘X’, ‘S’, ‘O’, ‘B’]], [‘ANT’, ‘BOX’, ‘SOB’, ‘TO’])
3
“””
count = 0
for word in words:
if board_contains_word(board, word) == True :
count = count + 1
return count

def read_words(words_file):
“”” (file open for reading) -> list of str

Return a list of all words (with newlines removed) from open file
words_file.

Precondition: Each line of the file contains a word in uppercase characters
from the standard English alphabet.
“””

lines = words_file.readlines()
p = []
i = 0
while (i < len(lines)):
p.append(lines[i].rstrip(‘\n’))
i = i+1
return p

def read_board(board_file):
“”” (file open for reading) -> list of list of str

Return a board read from open file board_file. The board file will contain
one row of the board per line. Newlines are not included in the board.
“””

temp = []
string = “”
for line in board_file:
string = line
temp.append(list(string.rstrip(‘\n’)))
return temp

Note: after submit please wait your marks will we updated within 2 minutes.. If you get any error tell me i will resolved it.. Thank you..

Leave a Comment