Python Data Representations Coursera Quiz Answers

All Weeks Python Data Representations Coursera Quiz Answers

Python Data Representations Coursera Quiz Answers

Week 1: Strings

Q1. Which of the expressions below select the last character in the string \color{red}{\verb|”Grail”|}”Grail”?

  • “Grail”[-1]
  • “Grail”[4]
  • “Grail”[len(“Grail”)]
  • “Grail”[-4])]

Q2. Which of the string slices below selects the string \color{red}{\verb|”Sir”|}”Sir” from the string \color{red}{\verb|”Sir Robin”|}”Sir Robin”?

  • “Sir Robin”[1 : 4]
  • “Sir Robin”[1 : 3]
  • “Sir Robin”[: 3]
  • “Sir Robin”[0 : 3]

Q3. Which one of the operators below can not be used with strings in Python?

  • -(subtraction)
  • +(addition)
  • in (membership)
  • *(multiplication)

Q4. What does the expression a_str.index(sub) do when the string sub is not a substring of the string a_str?

  • Return the value -1.
  • Raise an error.
  • Return the value 0.
  • Return the value None.

Q5. Which of the string format expressions below return the string “abracadabra”?

  • “{0}{1}{0}”.format(“abra”, “cad”)
  • “.format(“abra”, “cad”, “abra”)|}”{1}{2}{3}”.format(“abra”, “cad”, “abra”)
  • “.format(“abra”, “cad”, “abra”)|}”{0}{0}{0}”.format(“abra”, “cad”, “abra”)
  • “{0}{1}{2}”.format(“abra”, “cad”, “abra”)|}”{}{}{}”.format(“abra”, “cad”, “abra”)

Q6. Write a function count_vowels(word) that takes the string word as input and returns the number of occurrences of lowercase vowels (i.e. the lowercase letters \color{red}{\verb|”aeiou”|}”aeiou”) in \color{red}{\verb|word|}word. Hint: Python has a built-in string method that can count the number of occurrences of a letter in a string.

After you have implemented count_vowels, run the following two statements:

1 print(count_vowels("aaassseefffgggiiijjjoOOkkkuuuu"))
2 print(count_vowels("aovvouOucvicIIOveeOIclOeuvvauouuvciOIsle"))

The first statement should print 13 in the console. Enter the second number printed in the console in the box below.

Answers : 17

Q7. Write a function demystify(l1_string) that takes a string composed of the characters “l” and “1” and returns the string formed by replacing each instance of \color{red}{\verb|”l”|}”l” by \color{red}{\verb|”a”|}”a” and each instance of \color{red}{\verb|”1″|}”1″ by \color{red}{\verb|”b”|}”b”.

Once you have implemented demystify, test your function with calls below.

1 print(demystify("lll111l1l1l1111lll"))
2 print(demystify("111l1l11l11lll1lll1lll11111ll11l1ll1l111"))

The first call should print the string “aaabbbabababbbbaaa” in the console. Enter the second string printed in the console in the text box below. Do not include enclosing quotes.

Answers: bbbaaabababaaabbb

Python Data Representations Week 02 Quiz Answers

Q1. Which of the following expressions evaluates to the list \color{red}{\verb|[1, 2, 3, 4, 5]|}[1, 2, 3, 4, 5]?

  • list(range(1, 6))
  • range(1, 6)
  • list(range(1, 6, 1))
  • list(range(5))

Q2. Let my_list be the list [“This”, “course”, “is”, “great”].

What is len(my_list)?

  • What non-negative number is the index of “great”? I.e., how would you replace the question marks in my_list[???] so that the resulting value is “great”?
  • Submit two numbers, one for each of these two questions, separated by spaces.
Answers: 43

Q3. If we want to split a list \color{red}{\verb|my_list|}my_list into two halves, which of the following uses slices to do so correctly?

More precisely, if the length of \color{red}{\verb|my_list|}my_list is 2n, i.e., even, then the two parts should each have length n. If its length is 2n+1, i.e., odd, then the two parts should have lengths n and n+1.

  • my_list[0 : len(my_list) // 2] and \color{red}{\verb|my_list[len(my_list) // 2 : len(my_list)]|}my_list[len(my_list) // 2 : len(my_list)]
  • \color{red}{\verb|my_list[0 : len(my_list) // 2]|}my_list[0 : len(my_list) // 2] and \color{red}{\verb|my_list[len(my_list) // 2 + 1 : len(my_list)]|}my_list[len(my_list) // 2 + 1 : len(my_list)]
  • \color{red}{\verb|my_list[: len(my_list) // 2]|}my_list[: len(my_list) // 2] and \color{red}{\verb|my_list[len(my_list) // 2 :]|}my_list[len(my_list) // 2 :]
  • \color{red}{\verb|my_list[0 : len(my_list) // 2 – 1]|}my_list[0 : len(my_list) // 2 – 1] and \color{red}{\verb|my_list[len(my_list) // 2 : len(my_list)]|}my_list[len(my_list) // 2 : len(my_list)]

Q4. If nn and mm are non-negative integers, consider the list \color{red}{\verb|final_list|}final_list computed by the code snippet below.

1 init_list = list(range(1, n))
2 final_list = init_list * m

The length of this list depends on the particular values of nn and mm used in computation. Which option below correctly expresses the length of \color{red}{\verb|final_list|}final_list in terms of nn and mm?

  • n + m
  • n ×m
  • (n – 1) ×m
  • n ×(m−1)

Q5. If nn is a non-negative integer, consider the list \color{red}{\verb|split_list|}split_list computed by the code snippet below.

1 test_string = "xxx" + " " * n + "xxx"
2 split_list = test_string.split(" ")

The length of this list depends on the particular values of nn used in computation. Which option below correctly expresses the length of \color{red}{\verb|split_list|}split_list in terms of nn?

  • n+1
  • 33
  • 22
  • nn

Q6. Select the code snippets below in which \color{red}{\verb|list2|}list2 is a copy of list \color{red}{\verb|list1|}list1 (as opposed to simply being another reference to the list \color{red}{\verb|list1|}list1).

1 list1 = list(range(1, 10))
2 list2 = [] + list1
1 list1 = list(range(1, 10))
2 list2 = list(list1)
1 list1 = list(range(1, 10))
2 list2 = list1[:]
1 list1 = list(range(1, 10))
2 list2 = list1

Q7. Write a function \color{red}{\verb|strange_sum(numbers)|}strange_sum(numbers) that takes a list of integers and returns the sum of those items in the list that are not divisible by 33. When you are done, test your function using the code snippet below.

1 print(strange_sum([1, 2, 3, 4, 5, 1, 2, 3, 4, 5]))
2 print(strange_sum(list(range(123)) + list(range(77))))

The first line in the test should print the number \color{red}{\verb|24|}24 in the console. Enter the second number printed in the console in the box below.

Enter answer here

Python Data Representations Week 03 Quiz Answers

Q1. Given the list \color{red}{\verb|my_list = [1, 3, 5, 7, 9]|}my_list = [1, 3, 5, 7, 9], which of the following slices returns the list \color{red}{\verb|[3, 5, 7, 9]|}[3, 5, 7, 9]?

  • my_list[ 1 : 4]
  • my_list[ 1 : ]
  • my_list[ 2 : 4]
  • my_list[ 1 : -1]

Q2. While of the following expressions returns a tuple of length one?

  • tuple ([1])
  • (1, )
  • ((1, ))
  • ((1))

Q3. Why does following code snippet raise an error in Python?

1 instructors = ("Scott", "Joe", "John", "Stephen")
2 instructors[2 : 4] = []
3 print(instructors)
  • John and Stephen are irreplaceable.
  • Slices cannot be used with tuples.
  • The tuple doesn’t contain an element with index 4.
  • Tuples are immutable.

Q4. Given a non-empty list \color{red}{\verb|my_list|}my_list, which item in the list does the operation \color{red}{\verb|my_list.pop()|}my_list.pop() remove?

  • The item my_list[0]
  • The item my_list[1]
  • The item my_list[-1]
  • The item my_list[len(my_list) ]

Q5. What output does the following code snippet print to the console?

1 my_list = [1, 3, 5, 7, 9]
2 my_list.reverse()
3 print(my_list.reverse())

Note that this question is easily answered by running this snippet in Python. Instead, carefully evaluate this code snippet mentally when you attempt this problem.

  • [9, 7, 5, 3, 1]|}[9, 7, 5, 3, 1]
  • Executing this code snippet raises an error.
  • None
  • [1, 3, 5, 7, 9]

Q6. Given a list |fib = [0, 1]|}fib = [0, 1], write a loop that appends the sum of the last two items in \color{red}{\verb|fib|}fib to the end of \color{red}{\verb|fib|}fib. What is the value of the last item in \color{red}{\verb|fib|}fib after twenty iterations of this loop? Enter the answer below as an integer.

As a check, the value of the last item in \fib after ten iterations is 8989.

Enter answer here

Q7. One of the first examples of an algorithm was the Sieve of Eratosthenes. This algorithm computes all prime numbers up to a specified bound. The provided code below implements all but the innermost loop for this algorithm in Python. Review the linked Wikipedia page and complete this code.

1 """
2 Implement the Sieve of Eratosthenes
3 https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
4 """
5
6 def compute_primes(bound):
7 """
8 Return a list of the prime numbers in range(2, bound)
9 """

Running your completed code should print two numbers in the console. The first number should be 4646. Enter the second number printed in the console as the answer below.

Answers: 303

Get All Course Quiz Answers of Introduction to Scripting in Python Specialization

Python Programming Essentials Coursera Quiz Answers

Python Data Representations Coursera Quiz Answers

Python Data Analysis Coursera Quiz Answers

Python Data Visualization Coursera Quiz Answers

Team Networking Funda
Team Networking Funda

We are Team Networking Funda, a group of passionate authors and networking enthusiasts committed to sharing our expertise and experiences in networking and team building. With backgrounds in Data Science, Information Technology, Health, and Business Marketing, we bring diverse perspectives and insights to help you navigate the challenges and opportunities of professional networking and teamwork.

Leave a Reply

Your email address will not be published. Required fields are marked *