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) {...
Can you write code in Java to find the power set of a given set? For...
Can you write code in Java to find the power set of a given set? For example if S={a,b} the power set is P={{},{a},{b},{a,b}} ( you can also choose any of your favorite programming language). Explaning the code in comment please.
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.
C++ Question Write a code for :- public List Palindrome(); //Check whether a given singly linked...
C++ Question Write a code for :- public List Palindrome(); //Check whether a given singly linked list is palindrome or not. Input: a -> b-> NULL a -> b -> a-> NULL s -> a -> g -> a -> r-> NULL r -> a -> d -> a -> r-> NULL Output: not palindrome palindrome not palindrome palindrome Note:- Code should work on MS-Visual Studio 2017 and provide outputs with code
Given a list of negative integers, write a Python program to display each integer in the...
Given a list of negative integers, write a Python program to display each integer in the list that is evenly divisible by either 5 or 7. Also, print how many of those integers were found. Sample input/output: Enter a negative integer (0 or positive to end): 5 Number of integers evenly divisible by either 5 or 7: 0 Sample input/output: Enter a negative integer (0 or positive to end): -5 -5 is evenly divisible by either 5 or 7. Enter...
Write a function cube_all_lc(values) that takes as input a list of numbers called values, and that...
Write a function cube_all_lc(values) that takes as input a list of numbers called values, and that uses a list comprehension to create and return a list containing the cubes of the numbers in values (i.e., the numbers raised to the third power). This version of the function may not use recursion.
JAVA Write a for loop which will display this set of values 1,3,5,7,9.
JAVA Write a for loop which will display this set of values 1,3,5,7,9.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT