コード例 #1
0
ファイル: thread_v2ex.py プロジェクト: wf1314/spider
 def __init__(self):
     self.headers = {
         'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
     }
     self.url_q = q()
     self.proxies = {
         'https': 'https://10.10.9.1:8888'
     }
     self.start_url = 'https://www.v2ex.com/go/jobs?p=1'
 def printLevelWise(self, root):
     if (root == None):
         return None
     qu = q()
     qu.put(root)
     while (not (qu.empty())):
         currentNode = qu.get()
         print(currentNode.data)
         if (root.left != None):
             currentNode.left = root.left.data
             qu.put(root.left.data)
         if (root.right != None):
             currentNode.right = root.right.data
             qu.put(root.right.data)
コード例 #3
0
    def _ID3_Dtree(self, S_train, target_attr, Attr, AttrDic):
        """
        using the BFS Traversal method, build a ID3-Decision Tree.
        return a root address.
        """
        attr_set = set(Attr)
        attr_name = self.select_attr(S_train, list(attr_set))
        # root = self.subtree_gen(AttrDic, Attr)
        root = DTree(value='None', leaf_len=len(AttrDic[attr_name]))
        que_node = q()
        que_S_train = q()

        que_node.put_nowait(root)
        que_S_train.put_nowait(S_train)

        while len(attr_set) != 0 and que_node.empty() is False:
            cur_tree = que_node.get_nowait()
            S = que_S_train.get_nowait()
            bool_same, same_value = self.S_all_same(S)

            if bool_same is True:
                self.subtree_gen(cur_tree, AttrDic[target_attr][same_value],
                                 True)
            else:
                attr_name = self.select_attr(S, list(attr_set))
                addr_list = self.subtree_gen(cur_tree, attr_name, False)
                S_list = self.S_list_gen(S, attr_name)

                for addr in addr_list:
                    que_node.put_nowait(addr)

                for S_v in S_list:
                    que_S_train.put_nowait(S_v)

            attr_set.difference_update({attr_name})
        return root
 def takeInput(self):
     rootData = int(input())
     qu = q()
     if (rootData == -1):
         return None
     root = Binary(rootData)
     qu.put(root)
     while (not (qu.empty())):
         currentNode = qu.get()
         print("Enter left child of ", currentNode.data)
         leftChildData = int(input())
         if (leftChildData != -1):
             leftChild = Binary(leftChildData)
             currentNode.left = leftChild
             qu.put(leftChild)
         print("Enter right child of ", currentNode.data)
         rightChildData = int(input())
         if (rightChildData != -1):
             rightChild = Binary(rightChildData)
             currentNode.right = rightChild
             qu.put(rightChild)
     return root
コード例 #5
0
ファイル: HasPath.py プロジェクト: 1465267829/leetcodePython
    def hasPath(self, maze: List[List[int]], start: List[int],
                destination: List[int]) -> bool:
        queue = q()
        queue.put(start)
        # right, down, left, up
        children = [[0, 1], [1, 0], [0, -1], [-1, 0]]
        while not queue.empty():
            row, col = queue.get()

            if row == destination[0] and col == destination[1]:
                return True

            for dir_row, dir_col in children:
                child_row = row + dir_row
                child_col = col + dir_col
                while 0 <= child_row < len(maze) and 0 <= child_col < len(
                        maze[0]) and maze[child_row][child_col] != 1:
                    child_row += dir_row
                    child_col += dir_col
                child_row -= dir_row
                child_col -= dir_col
                if maze[child_row][child_col] == 0:
                    queue.put([child_row, child_col])
        return False
コード例 #6
0
ファイル: spider.py プロジェクト: AlexXQJ/Python
__author__ = "Alex"
# coding=utf-8
import queue

initial_page = "http://www.renminribao.com"

url_queue = queue.q()
seen = set()

seen.insert(initial_page)
url_queue.put(initial_page)

while True:  # 一直进行直到海枯石烂
    if url_queue.size() > 0:
        current_url = url_queue.get()  # 拿出队例中第一个的url
        store(current_url)  # 把这个url代表的网页存储好
        for next_url in extract_urls(current_url):  # 提取把这个url里链向的url
            if next_url not in seen:
                seen.put(next_url)
                url_queue.put(next_url)
    else:
        break
コード例 #7
0
 def __init__(self):
     self.headers = {
         'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
     }
     self.url_q = q()
コード例 #8
0
         current += 1
     g.add_edge(order[u], order[v])
 infile.close()
 n = g.number_of_nodes()
 print(n)
 pos = []
 for line in open(file + 'pos.txt').readlines():
     [u, v] = line.strip().split()
     pos.append([float(u) * n**0.5, float(v) * n**0.5])
 for maxhop in [1]:
     for co in [2.85, 2.9, 2.95]:
         start = time()
         distance=co/100*(n**(1-2/d*math.floor(maxhop/2)))\
             *math.log10(n)/math.pi
         source = 500
         trees = [q()] * n
         flags = [0] * n
         total = []
         new = [source]
         checktime = 0
         special = []
         specialtarget = []
         specialcount = []
         while len(total) < nx.number_of_nodes(g):
             checktime += 1
             #new nodes lead to new eager nodes
             eager = []
             for i in new:
                 flags[i] = 2
                 total.append(i)
                 for j in g.neighbors(i):
コード例 #9
0
ファイル: Codrescu_Assignment1.py プロジェクト: 5tefan/ai3202
"""	Stefan Codrescu stco8643
	Assignment 1: A review of basic data structures.
	CSCI 3202 Introduction to Artificial Intelligence
"""


import random


###########################################
# QUEUE
from queue import CSCI3202_Queue as q

# initialize
my_q = q()

# put integers 0 .. 19 into the queue
for i in range(20):
	my_q.put(i)

# get integers out of the queue
while not my_q.empty():
	print my_q.get()


###########################################
# STACK
from stack import CSCI3202_Stack as stack

# intialize
コード例 #10
0
#queue
""" queue is first in first out"""
from queue import Queue as q

L = q()
""" Data is inserted into Queue """

L.put(5)
L.put(9)
L.put(1)
L.put(7)
""" data is deleted from queue from front"""
print(L.get())
print(L.get())
print(L.get())
print(L.get())