Question

In: Computer Science

This question is concerned with an extension to the flashcard problem you studied in Block 3...

This question is concerned with an extension to the flashcard problem you studied in Block 3 Part 2.

In the original flashcard problem, a user can ask the program to show an entry picked randomly from a glossary. When the user presses return, the program shows the definition corresponding to that entry. The user is then given the option of seeing another entry or quitting.

A sample session might run as follows:

Enter s to show a flashcard and q to quit: s Define: word1 Press return to see the definition definition1 Enter s to show a flashcard and q to quit: s Define: word3 Press return to see the definition definition3 Enter s to show a flashcard and q to quit: q

The flashcard program is required to be extended as follows:

Box 1 – Specification of extended problem

As well as being offered a choice between seeing a flashcard or quitting, the user is offered the option of seeing a definition first.

If the user chooses this option, the program picks an entry at random from the glossary and shows the definition for that entry. It then asks the user what word is being defined. When the user presses return the program shows the word concerned.

The user is then given the option of seeing another flashcard, seeing another definition, or quitting.

Apart from this the program behaves like the original version

A sample dialogue might run as follows: (the additional dialogue is underlined.)

Enter s to show a flashcard, d to see a definition, or q to quit: s Define: word3 Press return to see the definition definition3 Enter s to show a flashcard, d to see a definition, or q to quit: d What word is defined by: definition2 Press return to see the word word2 Enter s to show a flashcard, d to see a definition, or q to quit: q

Box 2 – Keeping a notebook

As you work through part (a) of this question you should keep a notebook. You will need this for your answer to part (a)(vi). This should be very brief: it is simply a record of your personal experience while working on the task and what you feel you have learned from it.

In your notebook we suggest that you record the following information

How A brief description of how you went about the task.
Resources What documentation if any you consulted (including course materials and any online sources) and which you found most useful. There is no need for full references, just note the source, and in the case of the course materials what the relevant part and section or activity was.
Difficulties Anything you found difficult about the task, and how you dealt with it.
Lessons learnt Anything you learned from the task that would be useful if you faced a similar problem in the future.

There is more than one way of solving the extended problem but the approach we ask you to follow for this TMA starts by addressing the sub-problem of showing the definition for a random entry and, after the user enters return, showing the word being defined.

  • a.

    • i.Begin by writing an algorithm for the subproblem, show definition, described in the middle paragraph of Box 1 above, and repeated here for convenience:

      … the program picks an entry at random from the glossary and shows the definition for that entry. It then asks the user what word is being defined. When the user presses return the program shows the word concerned.

      At this stage no looping is involved and the steps of the algorithm only need to do what is asked for in the paragraph above and nothing more.

      The steps of your algorithm must be written in English and not use any Python code. The algorithm should be high-level and at a similar level of detail to the solution to Activity 2.24 of Block 3 Part 2, where an algorithm is given for show flashcard.

    • ii.Next you will translate your algorithm into Python code.

      Begin with the first complete version of the flashcard program, a copy of which is included in the download for this TMA as Q2.py. Save a copy this program as Q2_OUCU.py (where OUCU is your OU computer username, e.g. abc123).

      In the next few question parts, you will be amending this file. You will only have to submit the final amended file (as per the instructions in Part v).

      Add a new function show_definition() to the program, which translates into Python the steps of the algorithm you wrote in Part i. You should insert the new function just after the show_flashcard() function.

      Make sure you write a suitable docstring for the function.

      Include your code that defines the function show_definition() in your Solution document.

    • iii.When you have written the show_definition() function test it by calling it several times in the shell. Remember to first run the program and only after that use the shell to call the function.

      Debug the code and/or algorithm as necessary. If you need to make modifications you should record them in your notebook.

      Copy and paste an example test into your Solution Document. This should demonstrate a definition being shown, the user being asked to enter return, and then the program showing the word concerned.

      Alternatively, if you were unable to get the function working correctly, you should still paste in an example test, and explain briefly how the results are different from what you were expecting.

    • iv.Now you need to make changes to the part of the program that implements the interactive loop, so the user is offered the additional option of seeing a definition, and if they choose this option the show_definition() function is called.

      Once you have made the changes, run the whole program. Copy a test dialogue into your Solution document to show the user selecting the option to see a definition, then being asked what word it defines, and then been shown the word concerned.

      Include your amended code for the interactive loop in your Solution document.

      Alternatively, if you were unable to produce a test dialogue because you could not get the program to function as intended, you should briefly explain how a successful test dialogue would look.

    • v.Next modify the docstring for the program as a whole to reflect the changes you have made.

      Save your final version of the Python program and submit it as Q2_OUCU.py (where OUCU is your OU computer username, e.g. abc123) in your TMA zip file.

      Also paste a copy of your final Python program into your solution document as text.

    • vi.Finally, copy the notebook you have kept for this question into the corresponding part of your Solution Document.

  • b.Suggest one further small extension or improvement of your own to the modified flashcard program. Outline what the extension does and include any additional algorithm step(s) needed, either in the functions, or in the interactive loop. Note that you are only required to describe the extension, as specified above, and do not need to implement it in code.

Solutions

Expert Solution

code:

""" add docstring info here to describe what program does"""

from random import *
import csv

def file_to_dictionary(filename):
""" Return a dictionary with the contents of a file"""
file = open(filename, ‘r’)
reader = csv.reader(file)
dictionary = {}
for row in reader:
        dictionary[row[O]] = row[1]
return dictionary


def show _flashcard():
        key _list = list(glossary)
        random_key = choice(key _list)
        print (random _key)
        input("")
        print (glossary[random_key])
        print ("")
        correct = input("Did you know the definition? (Enter y for yes)")
        if correct == "y":
                print ("Now you know the definition that flashcard will be removed."}
                del glossary[random_key]
                dict_length = len(glossary)
                print (str(dict_length) + ' cards remain.')


glossary = file_to_dictionary('TM112_Glossary. txt')
exit = False
while exit == False:
        user_input = input("Enter s to show flashcard and q to quit > ")
        if user_input == "q":
                exit = True
        elif user_input == “s":
                show _flashcard()


Related Solutions

Problem 3. Let F ⊆ E be a field extension. (i) Suppose α ∈ E is...
Problem 3. Let F ⊆ E be a field extension. (i) Suppose α ∈ E is algebraic of odd degree over F. Prove that F(α) = F(α^2 ). Hints: look at the tower of extensions F ⊆ F(α^2 ) ⊆ F(α) and their degrees. (ii) Let S be a (possibly infinite) subset of E. Assume that every element of S is algebraic over F. Prove that F(S) = F[S]
This is an extension of the Birthday Problem. Suppose you have 500 Facebook friends. Make the...
This is an extension of the Birthday Problem. Suppose you have 500 Facebook friends. Make the same assumptions here as in the Birthday Problem. a) Write a program in R to estimate the probability that, on at least 1 day during the year, Facebooks tells you three (or more) of your friends shat that birthday. Based on your answer, should you be surprised by this occurrence? b) Write a program in R to estimate the probability that, on at least...
This is a two part question You are concerned about one of the assets in your...
This is a two part question You are concerned about one of the assets in your fully diversified portfolio. You just have an uneasy feeling about the CFO, Ian Malcolm, of that particular firm. You do believe, however that the firm makes a good product and that it is appropriately priced by the market. Should you be concerned about the effect on your portfolio if Malcolm embezzles a portion of the firm’s cash? Discuss in light of your readings on...
In this problem, you need to derive the block-pricing scheme that maximizes profit in the case...
In this problem, you need to derive the block-pricing scheme that maximizes profit in the case of 2nd-degree price discrimination when a monopolist faces a consumer with high demand PH = 80 − QH, a consumer with low demand PL = 50 − QL, and constant marginal cost of $10. For your derivations, assume that the monopolist serves both consumer types and use TL and TH to denote the fixed fees (dollar amounts) charged to the consumers with low and...
A comprehensive plan is Question 3 options: 1) clearly defined by law. 2) primarily concerned with...
A comprehensive plan is Question 3 options: 1) clearly defined by law. 2) primarily concerned with environmental impact. 3) decreasingly being used by state and municipal governments. 4) an attempt to set general guidelines for community development.
Question 3 Mr. Livingstone is concerned about the inventories of bottled coconut water the company has...
Question 3 Mr. Livingstone is concerned about the inventories of bottled coconut water the company has been experiencing because of its limited shelf life. He suspects that it is related to bonus packages that the supervisors receive but is uncertain and does not know how to calculate it. The supervisors of Farm Management and the Processing Unit equally share a 2% bonus based on the income of Saddleback Farms every quarter and the company uses the absorption costing approach to...
A 3 kg block (block A) is released from rest at the top of a 20...
A 3 kg block (block A) is released from rest at the top of a 20 m long frictionless ramp that is 3 m high. At the same time, an identical block (block B) is released next to the ramp so that it drops straight down the same 3 m. Find the values for each of the following for the blocks just before they reach ground level. (a) gravitational potential energy Block A____J Block B____J (b) kinetic energy Block a____...
A block of mass 2 kg and a block of mass 3 kg are sliding on...
A block of mass 2 kg and a block of mass 3 kg are sliding on a frictionless surface. They collide and stick together. Before the collision the first block was travelling at 5 m/s in the positive x direction. After the collision the two blocks are travelling at 6 m/s in the negative x direction. What was the x component of the velocity of the second block before the collision?
Question 1 (10 pts). The genetic linkage among 3 gene loci in horses is being studied....
Question 1 (10 pts). The genetic linkage among 3 gene loci in horses is being studied. The loci are part of autosomal chromosomes and they MIGHT be on the same pair of homologous chromosomes. There are two alleles at each locus. At the coat color locus, allele B is for black, and b is for white. At the height locus, allele H is for tall, and h is for short. At the tail locus, allele T is for long, and...
You may need to use the appropriate technology to answer this question. An amusement park studied...
You may need to use the appropriate technology to answer this question. An amusement park studied methods for decreasing the waiting time (minutes) for rides by loading and unloading riders more efficiently. Two alternative loading/unloading methods have been proposed. To account for potential differences due to the type of ride and the possible interaction between the method of loading and unloading and the type of ride, a factorial experiment was designed. Use the following data to test for any significant...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT