Question

In: Computer Science

Given the list values = [], write code that fills the list with each set of...

  1. Given the list values = [], write code that fills the list with each set of numbers below. Note: use loops if it is necessary
  1. 1 2 3 4 5 6 7 8 9 10
  2. 0 2 4 6 8 10 12 14 16 18 20
  3. 1 4 9 16 25 36 49 64 81 100
  4. 0 0 0 0 0 0 0 0 0 0
  5. 1 4 9 16 9 7 4 9 11
  6. 0 1 0 1 0 1 0 1 0 1
  7. 0 1 2 3 4 0 1 2 3 4

2. Write a function that takes a tuple as an argument and returns the tuple sorted.

3. Write a statement that creates a dictionary containing the following key-value pairs:

'a' : 1

'b' : 2

'c' : 3

4. 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)

Solutions

Expert Solution

Question 1:
Given the list values = [], write code that fills the list with each set of numbers below. Note: use loops if it is necessary
1 2 3 4 5 6 7 8 9 10
0 2 4 6 8 10 12 14 16 18 20
1 4 9 16 25 36 49 64 81 100
0 0 0 0 0 0 0 0 0 0
1 4 9 16 9 7 4 9 11
0 1 0 1 0 1 0 1 0 1
0 1 2 3 4 0 1 2 3 4
Code:
#Given the list values = []
values=[]

#code that fills the list with each set of numbers below:

#Sequence: 1 2 3 4 5 6 7 8 9 10
for i in range(1,11):
values.append(i)


#Sequence: 0 2 4 6 8 10 12 14 16 18 20
for i in range(0,21,2):
values.append(i)

#Sequence: 1 4 9 16 25 36 49 64 81 100
for i in range(1,11):
values.append(i*i)

#Sequence: 0 0 0 0 0 0 0 0 0 0
for i in range(1,11):
values.append(0)

#Sequence: 1 4 9 16 9 7 4 9 11
values=values+[1,4,9,16,9,7,4,9,11]

#Sequence: 0 1 0 1 0 1 0 1 0 1
values=values+[0, 1, 0, 1, 0, 1, 0, 1, 0, 1]

#Sequence: 0 1 2 3 4 0 1 2 3 4
for i in range(0,2):
for j in range(0,5):
values.append(j)


Question 2:
Write a function that takes a tuple as an argument and returns the tuple sorted.
Code:
#Define a function that takes a tuple as an argument
#and returns the tuple sorted.
def function(tup):
return tuple(sorted(tup))

Question 3:
Write a statement that creates a dictionary containing the following key-value pairs:
'a' : 1
'b' : 2
'c' : 3
Code:
dictionary={'a':1,'b':2,'c':3}

Question 4:
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)
The elements which are the members of set3 are:
{50, 20, 40, 10, 60, 30}

This is because we are storing the results of union of sets set1 and set2, so basically it will returns all the elements from set1 and set2

Please check the compiled program and its output for your reference:

Question-1:

Output:

Question-2:

Output:

Question-3:

Output:

Question-4:

Output:

(Feel free to drop me a comment, If you need any help)


Hope this Helps!!!
Please upvote as well, If you got the answer?
If not please comment, I will Help you with that...


Related Solutions

3.1 Write code that creates an ArrayList object named list and fills list with these numbers...
3.1 Write code that creates an ArrayList object named list and fills list with these numbers (using one or a pair of for or while loops): 0 1 2 3 4 0 1 2 3 4 3.2 Consider the ArrayList object named list containing these Integers: list = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 } What are the contents of list after this loop completes? for (int i = 1; i < 10; ++i) {...
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of...
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of integers. Assume that the List type has members: int List.length returns the length of the list. void List.push(T n) pushes an element n to the front of the list T List.pop() pops an element from the front of the list. List$$ List$$.concat(List$$ other) returns the concatenation of this list with other. Explain in plain English the reasoning behind your algorithm. Power Lists should be...
write the pseudocode and code to solve the following: you are given a list. determine the...
write the pseudocode and code to solve the following: you are given a list. determine the sum of all the elements in the list without using the built in puthon function sum(). Take your code and create your own function and name it my_sum_algo() which will return the sum of numbers PS please write comments in the code for my understanding, and please write jn jn PYTHON 3 languge. Thank you, have a great day !
Given int B[4][4]={...}; Write a code segment that exchange the values about the main diagonal of...
Given int B[4][4]={...}; Write a code segment that exchange the values about the main diagonal of the matrix.
Given the two stacks S1 and S2 each with a set of numbers, write an algorithm...
Given the two stacks S1 and S2 each with a set of numbers, write an algorithm to check if the two stacks are identical (have the same information). Use only the Push and Pop operations. The original stacks should be kept. Briefly explain the time complexity of your algorithm.
I know this code takes in a set of numbers into a doubly linked list and...
I know this code takes in a set of numbers into a doubly linked list and sorts it using insertion sort. Could you explain exactly how this code is working? main.c Source Code: #include #include #include "node.h" int main() { struct mynode *head=NULL; int value; printf("Give first value: \n"); scanf("%d",&value); printf("Give next values, and input 0 to end list: \n"); do{ if(value>0){    head = pushNode(head, value);    scanf("%d",&value); } }while (value>0); printf("Before insertion sort: "); printlist(head); head=insertsort(head); printf("After insertion...
Write a C++ code to ask the user to enter 4 values (whole numbers). Your code...
Write a C++ code to ask the user to enter 4 values (whole numbers). Your code should print the numbers in descending order.
What are the values in arrays a, b, and c after the following code executes (list...
What are the values in arrays a, b, and c after the following code executes (list all of the elements of the arrays)? double[] a = new double[4]; double[] b = {6,4,2}; a[a.length-1] = b[b.length-1]; double[] c = b; c[0] = -1; b[1] = c[2]; c = a; c[0] = -2; a[1] = c[3];
*****IN JAVA***** A run is a sequence of adjacent repeated values. Write a code snippet that...
*****IN JAVA***** A run is a sequence of adjacent repeated values. Write a code snippet that generates a sequence of 20 random die tosses in an array and that prints the die values, marking the runs by including them in parentheses, like this: 1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 (3 3) Use the following pseudocode: inRun = false for each valid index i in the array If inRun...
Language for this question is Java write the code for the given assignment Given an n...
Language for this question is Java write the code for the given assignment Given an n x n matrix, where every row and column is sorted in non-decreasing order. Print all elements of matrix in sorted order.Input: The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer n denoting the size of the matrix. Then the next line contains the n x n elements...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT