python英文题13-14

10.

Continuing on from the previous questions, extend the CircularQueue class by adding the method named that prints out all the elements from the circular queue in order. print_all(self)

Submit the entire class definition in the answer box below.

class CircularQueue:
    def __init__(self,capacity=8):
        self.__items=[]
        self.__front=0
        self.__capacity=capacity
        self.__back=capacity - 1
        self.__count=len(self.__items)
        self.__s=[]
        self.__l=[]
    def is_empty(self):
        return self.__items==[]
    def __str__(self):
        return f"{self.__items+[None for i in range(self.__capacity-len(self.__items))]}, front:{self.__front}, back:{self.__back}, count:{self.__count}"
    def is_full(self):
        return self.__capacity==len(self.__items)
    def enqueue(self, item):
        if self.__capacity==len(self.__items):
            self.__back=(self.__back+1)%self.__capacity
            if self.__back in self.__s:
                self.__items[self.__back]=item
                self.__l.append(item)
                self.__count+=1
                self.__s.remove(self.__back)
            else:
                raise IndexError("ERROR: The queue is full." )
        else:
            self.__back=(self.__back+1)%self.__capacity
            self.__items.insert(self.__back,item)
            self.__l.append(item)
            self.__count+=1
    def dequeue(self):
        if self.__capacity==len(self.__s) or len(self.__s)==len(self.__items):
            raise IndexError("ERROR: The queue is empty." )
        else:
            n=self.__front
            self.__s.append(n)
            self.__l=self.__l[1:]
            self.__front=(self.__front+1)%self.__capacity
            self.__count-=1
            return self.__items[n]
    def print_all(self):
        for i in self.__l:
            print(i,end=" ")
        print("")

1-5 Node

Continuing on from the previous questions, add the get_sum(self) method to the Node class. The get_sum() method returns the sum of all the values of the nodes in the chain. 

class Node():
    def __init__(self,data,next = None):
        self.__data = data
        self.__next = next
    def get_data(self):
        return self.__data
    def get_next(self):
        return self.__next
    def set_data(self,new_data):
        self.__data = new_data
    def set_next(self,new_next):
        self.__next = new_next
    def __str__(self):
        return str(self.get_data())
    def add_after(self,value):
        a = self.__next
        b = Node(value,a)
        self.set_next(b)
    def remove_after(self):
        a = self.__next.__next
        self.set_next(a)
    def __contains__(self,value):
        current = self
        while current != None:
            if current.__data == value:
                return True
            else:
                current = current.__next
        return False
    def get_sum(self):
        current  = self
        value = self.__data
        while current.__next != None:
            value = value + current.__next.__data
            current =current.__next
        return value

6.Define a function called create_sample_node_chain() which creates three Node objects and links them together as shown in the following chain:

def create_sample_node_chain():
    node3 = Node('nodes',None)
    node2 = Node('linked',node3)
    node1 = Node('three',node2)
    return node1

7.Define a function called print_node_chain(node_of_chain) which takes a Node object (or a reference to a linked chain of nodes) as a parameter, and traverses the chain of nodes starting from the parameter node_of_chain object.  As the chain of nodes is traversed, the function prints the data value from each node (one value per line).

def print_node_chain(node_of_chain):
        current  = node_of_chain
        while current != None:
            print(current.get_data())
            current =current.get_next()

8.

It can be inconvenient to build a linked chain of nodes by manually connecting them one after another.  A more convenient approach would be to have a function that takes a Python list as a parameter, and generates a linked chain of nodes with the same values, and in the same order, as the values in the Python list.

Define the create_node_chain(values) function which does this job. The create_node_chain(values) function takes a Python list as a parameter and returns a reference to a linked chain of nodes.

def create_node_chain(values):
    x = Node(values[0])
    b = x
    for i in range(1,len(values)):
        a = Node(values[i])
        b.set_next(a)
        b = b.get_next()
    return x

9.Define a function called convert_to_list(first_node) which takes a reference to a linked chain of nodes as a parameter and returns all the elements in the chain as a Python list.

class Node():
    def __init__(self,data,next = None):
        self.__data = data
        self.__next = next
    def get_data(self):
        return self.__data
    def get_next(self):
        return self.__next
    def set_data(self,new_data):
        self.__data = new_data
    def set_next(self,new_next):
        self.__next = new_next
    def __str__(self):
        return str(self.get_data())
    def add_after(self,value):
        a = self.__next
        b = Node(value,a)
        self.set_next(b)
    def remove_after(self):
        a = self.__next.__next
        self.set_next(a)
    def __contains__(self,value):
        current = self
        while current != None:
            if current.__data == value:
                return True
            else:
                current = current.__next
        return False
    def get_sum(self):
        current  = self
        value = self.__data
        while current.__next != None:
            value = value + current.__next.__data
            current =current.__next
        return value
 

def create_sample_node_chain():
    node3 = Node('nodes',None)
    node2 = Node('linked',node3)
    node1 = Node('three',node2)
    return node1

def print_node_chain(node_of_chain):
        current  = node_of_chain
        while current != None:
            print(current.get_data())
            current =current.get_next()
            

def create_node_chain(values):
    x = Node(values[0])
    b = x
    for i in range(1,len(values)):
        a = Node(values[i])
        b.set_next(a)
        b = b.get_next()
    return x
        
        
def print_node_chain(node_of_chain):
        current  = node_of_chain
        while current != None:
            print(current.get_data())
            current =current.get_next()
            
def convert_to_list(first_node):
        current = first_node
        list = []
        while current != None:
            list.append(current.get_data())
            current = current.get_next()
        return list

Define a function called  get_consecutive_sum(first_node) function which takes a  Node object (a reference to a linked chain of nodes) as a parameter and returns a list of consecutive sum of values in this linked chain. For example, the chain of nodes is: 1 -> 2 -> 3 -> 4, the result list will be [10, 9, 7, 4]. The values are:

  • 1 + 2 + 3 + 4 = 10
  • 2 + 3 + 4 = 9
  • 3 + 4 = 7
  • 4 = 4

class Node():
    def __init__(self,data,next = None):
        self.__data = data
        self.__next = next
    def get_data(self):
        return self.__data
    def get_next(self):
        return self.__next
    def set_data(self,new_data):
        self.__data = new_data
    def set_next(self,new_next):
        self.__next = new_next
    def __str__(self):
        return str(self.get_data())
    def add_after(self,value):
        a = self.__next
        b = Node(value,a)
        self.set_next(b)
    def remove_after(self):
        a = self.__next.__next
        self.set_next(a)
    def __contains__(self,value):
        current = self
        while current != None:
            if current.__data == value:
                return True
            else:
                current = current.__next
        return False
    def get_sum(self):
        current  = self
        value = self.__data
        while current.__next != None:
            value = value + current.__next.__data
            current =current.__next
        return value
 

def create_sample_node_chain():
    node3 = Node('nodes',None)
    node2 = Node('linked',node3)
    node1 = Node('three',node2)
    return node1

def print_node_chain(node_of_chain):
        current  = node_of_chain
        while current != None:
            print(current.get_data())
            current =current.get_next()
            

def create_node_chain(values):
    x = Node(values[0])
    b = x
    for i in range(1,len(values)):
        a = Node(values[i])
        b.set_next(a)
        b = b.get_next()
    return x
        
        
def print_node_chain(node_of_chain):
        current  = node_of_chain
        while current != None:
            print(current.get_data())
            current =current.get_next()
            
def convert_to_list(first_node):
        current = first_node
        list = []
        while current != None:
            list.append(current.get_data())
            current = current.get_next()
        return list
def print_node_chain(node_of_chain):
        current  = node_of_chain
        while current != None:
            print(current.get_data())
            current =current.get_next()
            
def convert_to_list(first_node):
        current = first_node
        list = []
        while current != None:
            list.append(current.get_data())
            current = current.get_next()
        return list
def print_node_chain(node_of_chain):
        current  = node_of_chain
        while current != None:
            print(current.get_data())
            current =current.get_next()
def get_consecutive_sum(first_node):
    list = convert_to_list(first_node)
    for i in range(0,len(list)):
        list[i] = sum(list[i:])
    return list

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值