Phase 2 - Basic Functionality

Problem 01: Sample Paragraphs

Specs: https://inst.eecs.berkeley.edu/~cs61a/su19/proj/typing_test/#problem-01-sample-paragraphs-1-pts

lines_from_file:

def lines_from_file(path=''):
    assert isinstance(path, str) and len(path) > 0
    
    lines = []
    with open(path) as file:
        for line in file:
            lines += [strip(line)]
    return lines

new_sample:

def new_sample(path, line_nr):
    assert isinstance(path, str) and len(path) > 0

    sample = ''
    
    with open(path) as file:
        for i, line in enumerate(file):
            if i == line_nr:
                sample = strip(line)
                break
    return sample

Problem 02: Analyze

Specs: https://inst.eecs.berkeley.edu/~cs61a/su19/proj/typing_test/#problem-02-analyze-3-pts

def analyze(sample_paragraph, typed_string, start_time, end_time):    
    #words typed per minute
    stripped_typed = strip(typed_string)

    def words_per_minute(typed_string, start_time, end_time):
        ONE_WORD_LENGTH = 5
        SECONDS_IN_MINUTE = 60
        
        time_typing = end_time - start_time
        #words in string
        words_amount = len(typed_string) / ONE_WORD_LENGTH       
        words_per_minute = SECONDS_IN_MINUTE / time_typing * words_amount
        
        return words_per_minute
    
    #the accuracy of typed text
        
    def accuracy(sample, typed):
        sample_words = sample.split()
        typed_words = typed.split()

        if len(typed_words) <= 0:
            return 0.0

        words_matched = 0
        for s, t in zip(sample_words, typed_words):
            if lower(s) == lower(t):
                words_matched += 1

        #calculate based on required words. If less then required typed, calculate based on typed amount
        total_words = len(sample_words) if len(sample_words) < len(typed_words) else len(typed_words)
        match_percentage = (words_matched / total_words) * 100

        return match_percentage
    
    return [words_per_minute(typed_string, start_time, end_time), accuracy(sample_paragraph, stripped_typed)]

Problem 03: Pig Latin

Specs: https://inst.eecs.berkeley.edu/~cs61a/su19/proj/typing_test/#problem-03-pig-latin-2-pts

def pig_latin(word):
    VOWELS = 'aeiouAEIOU'
    CNSNANT_SUFFIX = 'ay'
    VOWEL_SUFFIX = 'way'
    
    begins_with_vowel = word[0] in VOWELS

    if begins_with_vowel:
        return word + VOWEL_SUFFIX
    else:
        cnsnant_cluster = ''
        slice_index = 0

        for i in range(len(word)):
            if word[i] not in VOWELS:
                 cnsnant_cluster += word[i]
                 slice_index += 1
            else:
                 break
        return word[slice_index:] + cnsnant_cluster + CNSNANT_SUFFIX

Last updated