Coursera Learn to Program The Fundamentals Answers

coursera learn to program the fundamentals answers


Coursera Learn to Program The Fundamentals Answers


week 1 

assignment-1.py



def seconds_difference(time_1, time_2):

    """ (number, number) -> number


    Return the number of seconds later that a time in seconds

    time_2 is than a time in seconds time_1.

        

    >>> seconds_difference(1800.0, 3600.0)

    1800.0

    >>> seconds_difference(3600.0, 1800.0)

    -1800.0

    >>> seconds_difference(1800.0, 2160.0)

    360.0

    >>> seconds_difference(1800.0, 1800.0)

    0.0

    """

    return time_2 - time_1



def hours_difference(time_1, time_2):

    """ (number, number) -> float


    Return the number of hours later that a time in seconds

    time_2 is than a time in seconds time_1.

        

    >>> hours_difference(1800.0, 3600.0)

    0.5

    >>> hours_difference(3600.0, 1800.0)

    -0.5

    >>> hours_difference(1800.0, 2160.0)

    0.1

    >>> hours_difference(1800.0, 1800.0)

    0.0

    """

    return (time_2 - time_1)/3600.0



def to_float_hours(hours, minutes, seconds):

    """ (int, int, int) -> float


    Return the total number of hours in the specified number

    of hours, minutes, and seconds.


    Precondition: 0 <= minutes < 60  and  0 <= seconds < 60


    >>> to_float_hours(0, 15, 0)

    0.25

    >>> to_float_hours(2, 45, 9)

    2.7525

    >>> to_float_hours(1, 0, 36)

    1.01

    """

    return (hours + (minutes/60) + (seconds/3600))




def to_24_hour_clock(hours):

    """ (number) -> number


    hours is a number of hours since midnight. Return the

    hour as seen on a 24-hour clock.


    Precondition: hours >= 0


    >>> to_24_hour_clock(24)

    0

    >>> to_24_hour_clock(48)

    0

    >>> to_24_hour_clock(25)

    1

    >>> to_24_hour_clock(4)

    4

    >>> to_24_hour_clock(28.5)

    4.5

    """


    return hours % 24




### Write your get_hours function definition here:

def get_hours(time):

    """(int) -> int

    

    Return the number of hours that have

    elapsed since midnight, as seen on a 24-hour clock


    >>> get_hours(3800)

    1

    """

    x= time/3600

    return int(to_24_hour_clock(x))



### Write your get_minutes function definition here:

def get_minutes(time):

    """(int) -> int


    Return the number of minutes that have

    elapsed since midnight as seen on a clock.


    >>> get_minutes(3800)

    3

    """

    x=int(time/3600)

    z= time -(x*3600)

    y=z/60

    return int(y)


### Write your get_seconds function definition here:

def get_seconds(time):

    """(int) -> int


    Return the number of seconds that have

    elapsed since midnight as seen on a clock.


    >>> get_seconds(3800)

    20

    """

    x=int(time/3600)

    z= time -(x*3600)

    y=z%60

    return int(y)



def time_to_utc(utc_offset, time):

    """ (number, float) -> float


    Return time at UTC+0, where utc_offset is the number of hours away from

    UTC+0.


    >>> time_to_utc(+0, 12.0)

    12.0

    >>> time_to_utc(+1, 12.0)

    11.0

    >>> time_to_utc(-1, 12.0)

    13.0

    >>> time_to_utc(-11, 18.0)

    5.0

    >>> time_to_utc(-1, 0.0)

    1.0

    >>> time_to_utc(-1, 23.0)

    0.0

    """

    time_2 = (time -(utc_offset))

    return (to_24_hour_clock(time_2))



def time_from_utc(utc_offset, time):

    """ (number, float) -> float


    Return UTC time in time zone utc_offset.


    >>> time_from_utc(+0, 12.0)

    12.0

    >>> time_from_utc(+1, 12.0)

    13.0

    >>> time_from_utc(-1, 12.0)

    11.0

    >>> time_from_utc(+6, 6.0)

    12.0

    >>> time_from_utc(-7, 6.0)

    23.0

    >>> time_from_utc(-1, 0.0)

    23.0

    >>> time_from_utc(-1, 23.0)

    22.0

    >>> time_from_utc(+1, 23.0)

    0.0

    """

    time_2 = (time +(utc_offset))

    return (to_24_hour_clock(time_2))



___________________________________________________________________________________


week 4 

assignment-2.py



def get_length(dna):
    """ (str) -> int

    Return the length of the DNA sequence dna.

    >>> get_length('ATCGAT')
    6
    >>> get_length('ATCG')
    4
    """
    return len(dna)

def is_longer(dna1, dna2):
    """ (str, str) -> bool

    Return True if and only if DNA sequence dna1 is longer than DNA sequence
    dna2.

    >>> is_longer('ATCG', 'AT')
    True
    >>> is_longer('ATCG', 'ATCGGA')
    False
    """
    return (len(dna1)>len(dna2))

def count_nucleotides(dna, nucleotide):
    """ (str, str) -> int

    Return the number of occurrences of nucleotide in the DNA sequence dna.

    >>> count_nucleotides('ATCGGC', 'G')
    2
    >>> count_nucleotides('ATCTA', 'G')
    0
    """
    count = 0
    for char in dna:
        if (char == nucleotide):
            count = count +1
    return count

def contains_sequence(dna1, dna2):
    """ (str, str) -> bool

    Return True if and only if DNA sequence dna2 occurs in the DNA sequence
    dna1.

    >>> contains_sequence('ATCGGC', 'GG')
    True
    >>> contains_sequence('ATCGGC', 'GT')
    False

    """
    return (dna2 in dna1)

def is_valid_sequence(dna):
    """ (str) -> bool

    The parameter is a potential DNA sequence.
    Return True if and only if the DNA sequence
    is valid (that is, it contains no characters
    other than 'A', 'T', 'C' and 'G').

    >>> is_valid_sequence("ATCGCGTAT")
    True
    >>> is_valid_sequence("ATCGCHGTAT")
    False

    """
    count = True

    for char in dna:
        if char not in "ATCG":
            count = False
        
    return count
    
def insert_sequence(dna1, dna2, i):
    """ (str, str, int) -> str

    The first two parameters are DNA sequences
    and the third parameter is an index.
    Return the DNA sequence obtained by inserting the
    second DNA sequence into the first DNA sequence
    at the given index. (You can assume that the index is valid.)

    >>> insert_sequence("ATCA", "CGCG", 3)
    "ATCCGCGA"
    >>> insert_sequence("ATCA", "CGCG", 0)
    "CGCGATCA"

    """

    dna = ""
    l=len(dna2)
    dna = dna1[:i] + dna2 + dna1[i:]
    return dna

def get_complement(n):
    """ (str) -> str

    The first parameter is a nucleotide
    ('A', 'T', 'C' or 'G'). Return the nucleotide's complement.

    >>> get_complement('A')
    'T'
    >>> get_complement('T')
    'A'
    >>> get_complement('C')
    'G'
    >>> get_complement('G')
    'C'

    """

    if (n == 'A'):
        return 'T'
    elif (n == 'T'):
        return 'A'
    elif (n == 'C'):
        return 'G'
    else:
        return 'C'

def get_complementary_sequence(dna):
    """ (str) -> str

    The parameter is a DNA sequence.
    Return the DNA sequence that is complementary
    to the given DNA sequence.

    >>> get_complementary_sequence("ATCGAT")
    "TAGCTA"
    >>> get_complementary_sequence("CCCGTTTATCGA")
    "GGGCAAATAGCT"

    """

    seq = ""
    for char in dna:
        if (char == "A"):
            seq = seq + "T"
        elif (char == 'T'):
            seq = seq + "A"
        elif (char == "C"):
            seq = seq + "G"
        else:
            seq = seq + "C"

    return seq


___________________________________________________________________________________



week 6 

assignment-3.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





Post a Comment

1 Comments