SQL, matplotlib, Data Structure辅导、辅导SQL, matplotlib程序、辅导 sqlite3 报告

- 首页 >> Database作业


MTH 3300

E. Fink

3300 Problems, Section 7: SQL, matplotlib, Data Structures

1. Suppose that I have created an SQLite database using the following code:

import sqlite3

connection = sqlite3.connect("new.db")

curs = connection.cursor()

Add code that will create a table named Some Values in this database, which contains the following information (the

top line being column names):

ID Number

1 14.2

2 16.1

3 13.8

(Don’t worry about exceptions.)

2. Suppose that I have an SQLite database in the file sales.db. The database contains one table named Sales, which

contains the following data:

sale id customer sale

1 ’Albert’ 238.99

2 ’John’ 162.54

3 ’John’ 201.82

4 ’Albert’ 200.40

5 ’Albert’ 57.52

6 ’Peter’ 196.54

a. Complete the following code so that it prints out the last two columns of the table Sales. It doesn’t have to be

printed in any particularly nice manner.

import sqlite3

connection = sqlite3.connect("sales.db")

curs = connection.cursor()

b. Use a new query to get the total of the sales for each customer (Albert, John, and Peter).

3. Suppose that I have an SQLite database in the file x.db. The database contains one table named Months, which

contains the following data:

month id shark attacks ice cream sales

1 10 13201

2 3 5706

3 14 18115

4 19 23648

a. Complete the following code so that it prints out the last two columns of the table Months. It doesn’t have to be

printed in any particularly nice manner.

import sqlite3

connection = sqlite3.connect("x.db")

curs = connection.cursor()

b. Use matplotlib to make a scatter plot of the shark attack versus ice cream sales data.

c. Now, use a new query to get only the rows that have ≥ 10 shark attacks. (Still retrieve only the last two columns.)

4. Consider the following list of data, which represents how much of a computer’s CPU is being utilized at time t seconds,

for t = 0 to t = 8:

percents = [0.52, 0.56, 0.61, 0.60, 0.61, 0.57, 0.49, 0.51, 0.38]

Write code that uses matplotlib to create a line chart showing the utilization versus time. Label the axes as you see

fit, and give the chart a title.

5. Consider the following class for elements of a linked list:

class Node:

def __init__(self, d, n):

self.data = d

self.next = n

####################

a = Node(15, None)

b = Node(10, a)

head = Node(4, b)

Then, I execute the following lines:

f = Node(12, b)

If I print out the linked list in order starting from the head now, using

cur = head

while cur != None:

print(cur.data)

cur = cur.next

it will still print out 4 10 15. Explain why f = Node(12, b) appears to do nothing to our linked list.

6. Consider the following class for elements of a linked list:

class Node:

def __init__(self, d):

self.data = d

self.next = None

def link(self, n):

self.next = n

####################

a = Node("PP")

b = Node("QQ")

c = Node("RR")

a.link(b)

b.link(c)

c.link(None)

a. Which node is the head of this linked list?

b. Then, I execute the following lines:

d = Node("TT")

d.link(c)

a.link(d)

Describe what happens to the structure of this list. If I print the linked list starting from the head now, what will be

shown on the console?

7. Consider the following class for elements of a family tree:

class FamTreeNode:

def __init__(self, name, dad = None, mom = None):

self.name = name

self.father = dad

self.mother = mom

2

a. Suppose that I create a family tree by starting with the code:

p1 = FamTreeNode("Bob")

p2 = FamTreeNode("Carol")

p3 = FamTreeNode("Dennis", p1, p2)

p4 = FamTreeNode("Erica")

p5 = FamTreeNode("Faye", p2, p4)

Give Faye a brother named George.

b. Write code that, given a FamTreeNode named x, prints the name of that node’s maternal grandmother (i.e., the

mother’s mother). For full credit, write this code so that it doesn’t cause an error if one of the relevant nodes has its

.mother value set to None – instead, it should print a message saying Not set.

8. Consider the linked list starting with head created below:

class Node:

def __init__(self, d, n = None):

self.data = d

self.next = n

####################

head = Node(12)

b = Node(14)

c = Node(5)

d = Node(6)

head.next = b

b.next = c

c.next = d

The following code, when run, ends with an error.

pos = head

while pos != None:

pos = pos.next

print(pos.data)

a. What does print out from this code, before the error is encountered?

b. Explain in one sentence what causes the error.

9. Consider the linked list starting with head created below:

class Node:

def __init__(self, d, n = None):

self.data = d

self.next = n

####################

head = Node(12)

b = Node(14)

c = Node(5)

d = Node(6)

head.next = b

b.next = c

c.next = d

#################

new = Node(10,b) # Now I add a new node!

d.next = new #

What prints when I execute the following code? Be specific.

pos = head

while pos != None:

print(pos.data)

pos = pos.next

3


站长地图