Question

In: Computer Science

Task 1: This is the instructions for the previous set, where we worked with list of...

Task 1: This is the instructions for the previous set, where we worked with list of lists as character records, (there is already a solved Chegg for task 1 (J. [40 points]). I need help with the code of converting isInrecord(), AddToRecord(), createRecord(), and frequencies() from task J to use dictionaries instead of lists (as is asked in task A, which is located below the line of ########).

J. [40 points]
This tasks includes working with lists and lists of lists.

For this task, you'll keep track of how many times a certain value occurs in a set of data. You'll be tracking and counting characters, i.e. how many times any letter, digit, or punctuation mark appears in a string. You'll be writing Python code to create and work with such character records. What is a character record? It's the information about how many times any character (in a string or text) appears in that string or text. Let's look at this example:

  1. For example, we may want to keep track of how many times does the letter 'o' appear in the string "Kokomo?" ...it appears 3 times.
  2. The letter 'k' appears in the string "Kokomo?" just 1 time, if we consider 'k' (small case) and 'K' (capitalized) to be different letters.
  3. And the letter '?' appears in the string "Kokomo?" only 1 time.
  4. For Task J, you'll be representing the data structure for character records as a list of pairs (i.e. lists of two-item lists). The first item in each pair is the character, and the second is an integer representing how many times that character appears in some particular string. So if ['e',20] is in the list, that means that there are 20 lowercase e's in the string. Here's an example of such a data structure: [['K',1],['o',3],['k',1],['m',1],['?',1]], which represents a string with 1 K, 3 o's, 1 k, 1 m, and 1 question mark (perhaps "Kokomo?"?). Any time you see the words "character record" in Task J, you can assume it refers to a list of pairs like this (where the first item in the pair is a one-character string and the second is an integer).

    steps in Task J:
  5. For this task, save your function(s) in the same script named ps08-characterRecords.py as in the above Tasks B, etc. You'll be putting all of the code you write for Task J into this script. Include a comment in your script, explaining how you are representing character records. This comment is your data definition[1], and it might start with something like:

    ?

    1

    # a *character record* is a list of two-item lists where...

    This comment is a kind of informal data definition. After this comment, you can and should use the words "character record" as part of your other comments in the submitted Python code.

    [1]This is in addition to another writing requirement:
    all functions you write for A201/A597 must have a correctly and well-defined docstring, including a signature and a purpose statement.

  6. Write a Python function named isInRecord() that takes one character and one character record as parameters, and returns True if the character is listed in the character record, False otherwise. So 'o' and [['K',1],['o',3],['k',1],['m',1],['?',1]] should have the isInRecord() return True, and 'c' and  [['K',1],['o',3],['k',1],['m',1],['?',1]] should have isInRecord() return False.
  7. Write a Python function named addToRecord() code that takes one character and one character record, and adds an occurrence of that character to the character record. Note that if the character doesn't already have an entry, it should create one. Note that this part of the script should be modifying the existing character record, not creating a new character record. Here is an example how this would work:

    ?

    1

    myCharRecord = [['K',1],['o',3],['k',1],['m',1],['?',1]]

    then adding 'o' to myCharRecord:

    ?

    1

    2

    myCharRecord

    [['K',1],['o',4],['k',1],['m',1],['?',1]]

    then adding 'X' to myCharRecord:

    ?

    1

    2

    myCharRecord

    [['K',1],['o',4],['k',1],['m',1],['?',1],['X',1]]

  8. Write a Python function named createRecord() that takes any string, and returns out a new character record from that string. So starting from "Kokomo?" should generate [['K',1],['o',3],['k',1],['m',1],['?',1]]. If you reuse your previous code for the above parts of Task J, this shouldn't be too hard.
  9. Write a Python function named frequencies() that takes one character and one character record, and returns one number: the frequency associated with that character in the given character record. If the character doesn't appear in the character record, it should return 0. So for example, 'o' and [['K',1],['o',3],['k',1],['m',1],['?',1]] ) should return 3 and 'X' and [['K',1],['o',3],['k',1],['m',1],['?',1]] ) should return 0.

#################################################################################################################################################

  1. [16 points]
    For Task A, you'll be reimplementing the character-frequency character record script that you first implemented for Problem Set 08 Task J.
    However, this time you need to use dictionaries instead of lists of two-item lists.

    You should be representing a character record by a dictionary with single-character strings as keys and integers as values.
    So for example, the character-frequency character record for "Kokomo?" would be {'K':1, 'o':3, 'k':1, 'm':1, '?':1}.

    Write your Python code for Task A in a script called ps09-taskA.py.
    Place all of the code you write for Task A into this script. Include a comment in your script, explaining how you are representing character records. This comment is your data definition[1], and it might start with something like:

    ?

    1

    # a *character record* is a dictionary where...

    This comment is a kind of informal data definition. After this comment, you can and should use the words "character record" as part of your other comments in the submitted Python code.

    Note[1]This is in addition to the usual requirement:
    all functions you write for A201/A597 must have a correctly and well-defined docstring, including a signature and a purpose statement.


    Implement four functions named in the same way as you named them for Problem Set 08 Task J, i.e.:Please refer to Problem Set 08 Task J instructions about how those functions should process "character record" data.

    Note 1: the purpose statement for those 4 functions is going to be pretty similar to the purpose statement in Problem Set 08 Task J , but the signature will be necessarily different, since we're now using dictionaries instead of lists of lists.

    Note 2: the script solving this task may be a little less complex than the Problem Set 08 Task J script: this is because here we are using our new data definition for character records (i.e. using dictionaries), and there's a very simple built-in Python command that can be used to determine whether or not a particular character appears in the character record. Make sure you know what that command is, before you implement Task A.
    • Write a Python function named isInRecord()
    • Write a Python function named addToRecord()
    • Write a Python function named createRecord()
    • Write a Python function named frequencies()
      (Hint: this should be a lot easier than if you were working with lists of lists. To implement this function, no loops are needed at all. The only slightly tricky part is dealing with the case when the character isn't present in the histogram.)

############################# my code that needs to be converted from list of list into dictionaries (they can be modified as much as needed )############################

def isInRecord(char,charRec):
""" returns True if character is in character Record, otherwise false

string, charRecord -> Boolean Value"""
for i in charRec:
if i[0]==char:
return True
return False


def addToRecord(char,charRec):
""" returns new character record with updated frequency of each character occurance

character, charRecord(myCharRecord) -> list of character records"""
check=False
for i in charRec:
if i[0]==char:
check=True
if not check:
charRec.append([char,1])
myCharRecord = [['K',1],['o',3],['k',1],['m',1],['?',1]]


def createRecord(pString):
""" returns character records for argument entered

string -> list of character records"""
aList=[]
for i in pString:
addToRecord(i,aList)
return aList


def frequencies(char,charRec):
""" returns number for frequency of character in charRec list

character, charRecord -> integer"""
for i in charRec:
if i[0]==char:
return i[1]
return 0

Solutions

Expert Solution

def isInRecord(char,charRec):
return char in charRec

def addToRecord(char,charRec):
if char in charRec:
charRec[char]+=1
else:
charRec[char]=1

def createRecord(pString):
aRec={}
for i in pString:
addToRecord(i,aRec)
return aRec

def frequencies(char,charRec):
if char in charRec:
return charRec[char]
else:
return 0


Related Solutions

Writing Assignment #1 Set of Instructions for a Website Summary of the Assignment: • Task: In...
Writing Assignment #1 Set of Instructions for a Website Summary of the Assignment: • Task: In this assignment, you will write a set of instructions that explain how to accomplish a task on a website. • Length: There is no minimum or maximum word count. However, your instructions must have 6 or more steps. More information on the number of steps is provided below. • Graphics: You must include at least one graphic for each step. o at least 6...
A Task environment as a set of four things, with the acronym PEAS. A) Please list...
A Task environment as a set of four things, with the acronym PEAS. A) Please list the names of the PEAS components. B) Please the describe the properties of task environment. C) Please list the basic types of agents in order of increasing generality.
This task will go beyond the traditional idea to measure central tendency where a set of...
This task will go beyond the traditional idea to measure central tendency where a set of data is given and usually has been asked to compute mean. median or mode. The task will give you five questions and will ask to use analytical mind to provide solution. 1) A sequence consists of 7 terms arranged in descending order. The mean value of the sequence is 70. If 30 is added to each term, and then each term is divided by...
Elaborate a list of personal pitfalls-the aspects (any) of your previous problem set that you wish...
Elaborate a list of personal pitfalls-the aspects (any) of your previous problem set that you wish to work on in the near future
Translate the following tasks into Hack C-Instructions: 1) Set D to A - 1 2) Set...
Translate the following tasks into Hack C-Instructions: 1) Set D to A - 1 2) Set both A and D to A + 1 3) Set D to 19 4) Set both A and D to A + D 5) Set RAM[5034] to D - 1 6) Set RAM[543] to 171 7) Add 1 to RAM[7], and store result in D 8) Add 3 to RAM[12], and store result in D
Task 8.2 - Individual report. * Instructions: According to the instructions in module one, present an...
Task 8.2 - Individual report. * Instructions: According to the instructions in module one, present an individual report on the assigned topic (choose one of the 10 presented). The report will include aspects such as: definition of the topic, Historical background, advantages and disadvantages and your arguments on the contribution that you consider most significant and impactful for business administration. Present examples of corporations currently applying this method successfully. The report must be written using the essay type writing format...
1) The set of instructions that tells a computer what to do is called a ______________?...
1) The set of instructions that tells a computer what to do is called a ______________? 2) A program design tool used to list the attributes and methods of objects within an object-oriented program is called _______________. 3) Of the programming languages discussed in the textbook, which language is used to create web applications (called applets) that can be inserted into web pages? 4) If a programmer makes a spelling mistake or violates the rules of the programming language the...
Part 1- Delete an element from an Array. See the instructions below: -Use the previous Array...
Part 1- Delete an element from an Array. See the instructions below: -Use the previous Array code provided to you (to demonstrate how to insert elements in an Array). It is an Array with the following details: Size 10 Length 5. Your task is to Delete element on index 4 please use the code below and write in C++ how to delete the 4th element. #include struct Array { int A[10]; int size; int length; }; void Display(struct Array arr)...
A college looked at a random sample of students who worked the previous summer.   Use the...
A college looked at a random sample of students who worked the previous summer.   Use the following information to answer questions 1 - 7. The following data show the average earnings and sample standard deviations for a random sample of males and a random sample of females. Group n x̄ s males 15 3400 2495 females 20 2500 1920 The following degrees of freedom may be helpful: 25.49 1) Find the point estimate of the difference between the mean summer...
Task # 1. For the homogeneous autocatalytic reaction A + B → 2B, where –rA =...
Task # 1. For the homogeneous autocatalytic reaction A + B → 2B, where –rA = kCACB, aconversion rate of A of X = 0.44. The velocity constant is k = 10-3  m3 / (mol s). The feed stream of 0.1 m3 / s contains A with CA0  = 90 mol / m3 and B with CB0 = 10 mol / m3. It is considered whichreactor configuration that will be most suitable 1.1 The reaction must take place in a flow reactor....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT