Assignment04 (1)
.docx
keyboard_arrow_up
School
Texas A&M University, Commerce *
*We aren’t endorsed by this school
Course
450
Subject
Computer Science
Date
May 13, 2024
Type
docx
Pages
2
Uploaded by GrandSteel14470 on coursehero.com
CSCI 333 Assignment 04
Data structures (List, tuple, set, and dict)
50 points
1.
(15 points) Given the list values = []
, write code that fills the list with each set of numbers below. Note: use loops if it is necessary
a)
1 2 3 4 5 6 7 8 9 10
b)
0 2 4 6 8 10 12 14 16 18 20
c)
1 4 9 16 25 36 49 64 81 100
d)
0 0 0 0 0 0 0 0 0 0
e)
1 4 9 16 9 7 4 9 11
f)
0 1 0 1 0 1 0 1 0 1
g)
0 1 2 3 4 0 1 2 3 4
# python code
2.
(5 points) Write a function that takes a tuple as an argument and returns the tuple sorted.
# python code
3.
(5 points) Write a statement that creates a dictionary containing the following key-value pairs:
'a' : 1
'b' : 2
'c' : 3
# python code
4.
(5 points) What will the following code display?
dct = {'Monday':1, 'Tuesday':2, 'Wednesday':3}
print(dct.get('Monday', 'Not found'))
# result
5.
(5 points) After the following code executes, what elements will be members of set3, ?
set1 = set([10, 20, 30, 40])
set2 = set([40, 50, 60])
set3 = set1.union(set2)
set4 = set1.intersection(set2)
set5= set1.difference(set2)
# result
6.
(15 points) Given a dictionary
gradeCounts = { "A": 8, "D": 3, "B": 15, "F": 2, "C": 6 }
write the Python statement(s) to print:
a)
all the keys.
b)
all the values.
c)
all the key and value pairs.
d)
all of the key and value pairs in key order.
e)
the average value.
f)
a chart similar to the following in which each row contains a key followed by a number of asterisks equal to the key’s data value. The rows should be printed in key order, as shown below.
A: ********
B: ***************
C: ******
D: ***
F: **
(10 points) Python program # python source code
(3 points) Screenshot of the output
# python source code
(2 points) Save the python program and submit it to D2L
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
Related Questions
Code in Perl
Create an array which holds a list of Video Games, consisting of title, the year it was released (guess if you don’t know) , platform (NES, XBOX One, etc) and publisher (in that order). Add at least ten albums to the list. Then, use the split function and a for or foreach loop to display the publisher followed by the title and platform for each list element.
arrow_forward
python
LAB: Subtracting list elements from max
When analyzing data sets, such as data for human heights or for human weights, a common step is to adjust the data. This can be done by normalizing to values between 0 and 1, or throwing away outliers.
Write a program that adjusts a list of values by subtracting each value from the maximum value in the list. The input begins with an integer indicating the number of integers that follow.
arrow_forward
Complete the display_coins_list(coins_list) function. This function takes a list (always nine elements in length) as a parameter. The function displays 3 lines in total. An example of the output is as shown below:
123456789 --------- $ $$ $
The first line shows the digits 1 to 9 (as indicated in the example output above).
The second line shows a single line of 9 "-" symbols (hyphens).
The third line shows the coins list, formatted as in the examples.
For example:
Test
Result
display_coins_list(['-', '$', '-', '$', '-', '$', '-', '-', '$'])
123456789 --------- -$-$-$--$
data = ['$', '-', '$', '-', '$', '-', '-', '-', '$'] display_coins_list(data)
123456789 --------- $-$-$---$
arrow_forward
reverse_number_in_list(number_list:list)-> list
This function will be given a list of numbers your job is to reverse all the numbers in the list and return a list with the reversed numbers. If a number ends with 0 you need to remove all the trailing zeros before reversing the number. An example of reversing numbers with trailing zeros: 10 -> 1, 590 -> 95. None of the numbers in the number_list will be less than 1.
Example:
number_list = [13, 45, 690, 57]
output = [31, 54, 96, 75]
arrow_forward
tails_same(number_list:list) -> bool
This function should return true if the value at the beginning and the end of the list are equal. False otherwise.
Example:
number_list = [1, 239, 949, 0, 84, 0, 1]
output: True
number_list = [1, 239, 949, 0, 84, 0, 13]
output: False
arrow_forward
Ex1: a) Write a function for the user to enter a positive integer n with a value between 10 and 1000. If entered correctly, the function generates a list containing n random values that do not overlap between 1 and 5000 and the function returns the newly created list. Conversely, if entered incorrectly, the program will ask to re-enter until it is correct. Cases where n is considered incorrect:(i)- If the user enters a non-integer data type (like string, float, bool, ...)The program will display the error message 'Must enter a positive integer. Requires re-entry'.(ii)- If the input value is of the correct integer data type but the value is not in the range10 to 1000, the program gives the error 'Only values between 10 and 1000. Re-enter required'.Installation requirements: must use the try ... except statement to handle possible exceptionsin the program.
b) Write a function that lists the amicable numbers in the list just created in a)
(If there are no amicable numbers in the list,…
arrow_forward
11111
2)
Name:
2. The following procedure is intended to return the number of times the value val appears in the list myList.
The procedure does not work as intended.
Line 01: PROCEDURE countNumOccurences (myList, val)
Line 02: {
Line 03:
FOR EACH item IN myList
Line 04:
}
BL
Line 05:
count 0
Line 06:
IF(item = val)
%3D
Line 07:
Line 08:
count count + 1
:60
Line 10:
{
{
RETURN(count)
Line 11:
Line 12: }
Which of the following changes can be made so that the procedure will work as intended?
a. Moving the statement in line 5 so that it
appears between lines 2 and 3
C.
Changing line 6 to IF(item = count)
b. Changing line 6 to IF(myList[item] =
val)
d. Moving the statement in line 11 so that it
appears between lines 9 and 10
arrow_forward
dict = { 'a': [1,2,3,4] 'b': [3,6,7,8] 'c': [9,1,9,1]{
Iterate over the above dictionary 1- display letter and highest value2- display letter and mean of values in corresponding list3- display the letter that the highest value among all lists
Example output:highest value in each letters lista : 4b : 8c : 9
Mean value for each letter:a : 2.5b : 6c : 5
The highest valuec : 9
arrow_forward
operator is used to combine 2 or more lists.
arrow_forward
T/F
10) The statement int[ ] list = {5, 10, 15, 20}; initializes list to have 4 int values
arrow_forward
a) Write a function to get the value of the Nu node in a Linked List. [Note: The first (N=1) item in
the list means the item at index 0.] It takes two parameters: the list or its head, and N. Return
False if the list has fewer than N elements. The Linked List structure supports the following
function.
def getlead(self):
return selt.head # it points to a Node structure
The Node structure supports the following functions.
def getData(self):
return self.data # it returns the value stored in the Node
def getNext(self):
return self next # it points to the next Node
b) Write a function that counts the number of times a given integer occurs in a Linked List.
Assume similar structures as defined in 1.
arrow_forward
FUNCTION ONE: MAINO
In this lab we will create three functions. The first will be called
main. Main will simply be the first function executed and will
call the other two funetions. It will take in no arguments and
will only create an empty list and call the other two functions.
FUNCTION TWO: GETDATAO
Getdata() will take in a list and then prompt the user to enter a
song as a string in the format of song-band. You may assume
the user will correctly enter this data. These songs will be
appended to our list. The user can continue entering songs until
they enter "exit".
FUNCTION THREE: PLAYRANDOMO
PlayRandom() will take in a list. It will then generate a random
number and use it as index to select a random song to print
"Now Playing: RandomSongHere".
SAMPLE OUTPUT
Enter a song title and band (song-band) or exit to quit: I Found-
Amber Run
Enter a song title and band (song-band) or exit to quit: Bark At
The Moon-Ozzy Osbourne
Enter a song title and band (song-band) or exit to quit: Real…
arrow_forward
PYTHON ONLY AND PLEASE COMMENT CODE
1.
A) write a For loop that iterates through a list of products (any number of products) that are sold in a store and prints them. [examples: fan, fridge, freezer, washer, etc.].
b) copy the code from a) and use the same loop to add more products(any)to the list. You must use list methods to add.
c) Copy the code from c), and search for a specific product (any), if the product is found the loop is terminated. Make sure to use else in the for loop.
d) Create an empty dictionary, then add 4 entries (the key is a word, the value is word’s frequency in a book). [example: {‘street’: 23}].
e) Create a for loop to access the entries from d) and print the keys and values.
f) Copy the code from e) and search for a specific key in the dictionary and prints its value. The program prints a message “ the {key} is not found”, if the key does not exist.
arrow_forward
Return Growing NumList
This function will be given a single number, it should return a list of strings of numbers. Each string in the list will only contain a single number repeated an arbitrary amount of times. The number each string will contain will be equal to the current string's index+1. The number in the string should be repeated the same number of times as the string's index+1. Each number in the string should be separated by a space. This list should stop when its size equals the max number specified.
Signature:
public static ArrayList<String> returnGrowingNumList(int max)
Example:
INPUT: 3OUTPUT: [1, 2 2, 3 3 3]
INPUT: 6OUTPUT: [1, 2 2, 3 3 3, 4 4 4 4, 5 5 5 5 5, 6 6 6 6 6 6]
arrow_forward
Array List.
arrow_forward
debugg help
def print_categories(main_list): if id not in main_list: print("There are no categories.") return 0 idx = 0 for items in main_list: item = items[i:] print("{idx + 1} . {item[0]} - {item[1]}%") idx += 1 return idx """ Given a list of lists, for each list stored in main_list, output its contents as follows: f"{index of category}. {item[0]} - {item[1]}%". note that indexing must start at 1 for the first item, not 0, which is first item's actual index in main_list. If `main_list` is empty, the function prints "There are no categories." Returns the number of categories. """
def is_valid_index(idx, in_list): if (idx in in_list - 1): if len(idx) != 0: """ Checks whether the provided index `idx` is a valid positive index that can retrieve an element from `in_list`. Returns False if `idx` is negative or exceeds the size of…
arrow_forward
Tails Function
Purpose: Produce a new column-based ( e.g. dict[str, list[str]] ) table with only the first N (a parameter) rows
of data for each column.
* Function name: Tails
Parameters:
1. dict[str, list[str]] - a column-based table of data that_will not be mutated_
2. int - The number of "rows" to include in the resulting list
* Return type: dict[str, list[str]]
Implementation strategy:
1. Establish an empty dictionary that will serve as the returned dictionary this function is building up.
2. Loop through each of the columns in the first row of the table given as a parameter.
1. Inside of the loop, establish an empty list to store each of the first N values in the column.
2. Loop through the first N items of the table's column,
1. Appending each item to the previously list established in step 2.1.
3. Assign the produced list of column values to the dictionary established in step 1.
3. Return the dictionary.
arrow_forward
Code the following
write a For loop that iterates through a list of products (any number of products) that are sold in a store and prints them. [examples: fan, fridge, freezer, washer, etc.].
b) copy the code from a) and use the same loop to add more products(any)to the list. You must use list methods to add.
c) Copy the code from c), and search for a specific product (any), if the product is found the loop is terminated. Make sure to use else in the for loop.
d) Create an empty dictionary, then add 4 entries (the key is a word, the value is word’s frequency in a book). [example: {‘street’: 23}].
e) Create a for loop to access the entries from d) and print the keys and values.
f) Copy the code from e) and search for a specific key in the dictionary and prints its value. The program prints a message “ the {key} is not found”, if the key does not exist. ..?
arrow_forward
c++ double linked list program where you need to create a playlist of songs and you need to loop the songs in the playlist
arrow_forward
List
Write a complete Java program in the that will require the user to input and store 10 double data type values in a list. The program should output the following information:
lowest value
the highest value
the sum (also called total) of all the input values
the average value.
Note: the input data maybe positive or negative.
arrow_forward
5) list_append, adds a new element to the end of the list (don't use the append method).
6) list_insert, inserts a new element to the list at a desired location (do not use the insert list function).
7) list_add, adds an inputed number to every value in the list.
8) list_subtract, subtracts an inputed number to every value in the list.
9) list_multiply, multiples an inputed number to every value in the list.
10) list_divide, divides an inputed number to every value in the list (be sure to use some sort of error handling for division by zero).
11) vector_add, takes two lists of the same size and adds the elements together.
12) Do subtraction, division, and multiplication for number 11.
Here is the example of code use simple python for #5 don't use append method
Need help with coding for those questions. Thanks
def list_add(numList,val): numList.append(val) return(numList) def list_sub(numList,val): numList.append(val) return(numList)
def list_multiply(numList,val):…
arrow_forward
Median Function – In statistics, the median of a set of values is the value that lies in the middle when the values are arranged in sorted order. If the set has an even number of values, the median is the average of the two middle values.
Your program should start with two arrays of integers containing the following values:
Even numbered array: 17 32 45 68 99 101 67 89 22 27
Odd numbered array: 17 32 45 68 99 101 67 89 22
Using a sort function of your choice, first sort the arrays. NOTE: you may use the Standard Template Library sort function or your own sort function.
Then, write a function that determines the median of a sorted array. The function should take an array of numbers and an integer indicating the size of the array and return the median of the values in the array. The same function should be called twice – once for the even array and once for the odd array.
Your program should also have a printArray function that can be used to print the sorted array. (It…
arrow_forward
SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
Related Questions
- Code in Perl Create an array which holds a list of Video Games, consisting of title, the year it was released (guess if you don’t know) , platform (NES, XBOX One, etc) and publisher (in that order). Add at least ten albums to the list. Then, use the split function and a for or foreach loop to display the publisher followed by the title and platform for each list element.arrow_forwardpython LAB: Subtracting list elements from max When analyzing data sets, such as data for human heights or for human weights, a common step is to adjust the data. This can be done by normalizing to values between 0 and 1, or throwing away outliers. Write a program that adjusts a list of values by subtracting each value from the maximum value in the list. The input begins with an integer indicating the number of integers that follow.arrow_forwardComplete the display_coins_list(coins_list) function. This function takes a list (always nine elements in length) as a parameter. The function displays 3 lines in total. An example of the output is as shown below: 123456789 --------- $ $$ $ The first line shows the digits 1 to 9 (as indicated in the example output above). The second line shows a single line of 9 "-" symbols (hyphens). The third line shows the coins list, formatted as in the examples. For example: Test Result display_coins_list(['-', '$', '-', '$', '-', '$', '-', '-', '$']) 123456789 --------- -$-$-$--$ data = ['$', '-', '$', '-', '$', '-', '-', '-', '$'] display_coins_list(data) 123456789 --------- $-$-$---$arrow_forward
- reverse_number_in_list(number_list:list)-> list This function will be given a list of numbers your job is to reverse all the numbers in the list and return a list with the reversed numbers. If a number ends with 0 you need to remove all the trailing zeros before reversing the number. An example of reversing numbers with trailing zeros: 10 -> 1, 590 -> 95. None of the numbers in the number_list will be less than 1. Example: number_list = [13, 45, 690, 57] output = [31, 54, 96, 75]arrow_forwardtails_same(number_list:list) -> bool This function should return true if the value at the beginning and the end of the list are equal. False otherwise. Example: number_list = [1, 239, 949, 0, 84, 0, 1] output: True number_list = [1, 239, 949, 0, 84, 0, 13] output: Falsearrow_forwardEx1: a) Write a function for the user to enter a positive integer n with a value between 10 and 1000. If entered correctly, the function generates a list containing n random values that do not overlap between 1 and 5000 and the function returns the newly created list. Conversely, if entered incorrectly, the program will ask to re-enter until it is correct. Cases where n is considered incorrect:(i)- If the user enters a non-integer data type (like string, float, bool, ...)The program will display the error message 'Must enter a positive integer. Requires re-entry'.(ii)- If the input value is of the correct integer data type but the value is not in the range10 to 1000, the program gives the error 'Only values between 10 and 1000. Re-enter required'.Installation requirements: must use the try ... except statement to handle possible exceptionsin the program. b) Write a function that lists the amicable numbers in the list just created in a) (If there are no amicable numbers in the list,…arrow_forward
- 11111 2) Name: 2. The following procedure is intended to return the number of times the value val appears in the list myList. The procedure does not work as intended. Line 01: PROCEDURE countNumOccurences (myList, val) Line 02: { Line 03: FOR EACH item IN myList Line 04: } BL Line 05: count 0 Line 06: IF(item = val) %3D Line 07: Line 08: count count + 1 :60 Line 10: { { RETURN(count) Line 11: Line 12: } Which of the following changes can be made so that the procedure will work as intended? a. Moving the statement in line 5 so that it appears between lines 2 and 3 C. Changing line 6 to IF(item = count) b. Changing line 6 to IF(myList[item] = val) d. Moving the statement in line 11 so that it appears between lines 9 and 10arrow_forwarddict = { 'a': [1,2,3,4] 'b': [3,6,7,8] 'c': [9,1,9,1]{ Iterate over the above dictionary 1- display letter and highest value2- display letter and mean of values in corresponding list3- display the letter that the highest value among all lists Example output:highest value in each letters lista : 4b : 8c : 9 Mean value for each letter:a : 2.5b : 6c : 5 The highest valuec : 9arrow_forwardoperator is used to combine 2 or more lists.arrow_forward
- T/F 10) The statement int[ ] list = {5, 10, 15, 20}; initializes list to have 4 int valuesarrow_forwarda) Write a function to get the value of the Nu node in a Linked List. [Note: The first (N=1) item in the list means the item at index 0.] It takes two parameters: the list or its head, and N. Return False if the list has fewer than N elements. The Linked List structure supports the following function. def getlead(self): return selt.head # it points to a Node structure The Node structure supports the following functions. def getData(self): return self.data # it returns the value stored in the Node def getNext(self): return self next # it points to the next Node b) Write a function that counts the number of times a given integer occurs in a Linked List. Assume similar structures as defined in 1.arrow_forwardFUNCTION ONE: MAINO In this lab we will create three functions. The first will be called main. Main will simply be the first function executed and will call the other two funetions. It will take in no arguments and will only create an empty list and call the other two functions. FUNCTION TWO: GETDATAO Getdata() will take in a list and then prompt the user to enter a song as a string in the format of song-band. You may assume the user will correctly enter this data. These songs will be appended to our list. The user can continue entering songs until they enter "exit". FUNCTION THREE: PLAYRANDOMO PlayRandom() will take in a list. It will then generate a random number and use it as index to select a random song to print "Now Playing: RandomSongHere". SAMPLE OUTPUT Enter a song title and band (song-band) or exit to quit: I Found- Amber Run Enter a song title and band (song-band) or exit to quit: Bark At The Moon-Ozzy Osbourne Enter a song title and band (song-band) or exit to quit: Real…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage Learning
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning