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:
- 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.
- The letter 'k' appears in the string "Kokomo?" just 1 time, if
we consider 'k' (small case) and 'K' (capitalized) to be different
letters.
- And the letter '?' appears in the string "Kokomo?" only 1
time.
-
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:
- 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.
- 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.
- 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]]
|
- 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.
- 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.
#################################################################################################################################################
- [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