def BFS_or_DFS(G, start, goal, bfs): visited = set() prev = dict() prev[start] = None queue = [start] while queue: cur = queue.pop() visited.add(cur) if cur == goal: break for neighbor in G.neighbors(cur): if neighbor not in visited: if bfs: queue.insert(0, neighbor) else: queue.append(neighbor) prev[neighbor] = cur path = [] cur = goal while cur: path.insert(0, cur) cur = prev[cur] return path, list(visited)
def insort_event_right(self, event): """Insert event in queue with sorting. This function assumes the queue is already sorted by ``event.when`` and inserts ``event`` in the queue, maintaining the sort. For events with same `event.when`, new events are inserted to the right, to keep FIFO order. Args: event (_Event): The event to insert. """ queue = self.queue low = 0 high = len(queue) while low < high: mid = (low + high) // 2 if event.when < queue[mid].when: high = mid else: low = mid + 1 queue.insert(low, event)
def solve_maze(maze): dist = [[NUM for i in range(SIZE)] for j in range(SIZE)] dist[1][1] = 0 queue = [(1, 1)] route = [[1, 1]] #ゴールに到達するまで迷路を探索する while len(queue): x, y = queue.pop() if x == 13 and y == 13: break # ↑ → ↓ ← を探索し、進行先があればqueueに追加し探索する for i in range(0, 4): nx, ny = x + [1, 0, -1, 0][i], y + [0, 1, 0, -1][i] if (1 <= nx and nx < SIZE - 1 and 1 <= ny and ny < SIZE - 1): if maze[nx][ny] == '*': continue elif (maze[nx][ny] == '-' and dist[nx][ny] == NUM): queue.insert(0, ((nx, ny))) dist[nx][ny] = dist[x][y] + 1 # 出力のため成形 if int(maze[x][y]) < 9: maze[nx][ny] = str(int(maze[x][y]) + 1) + ' ' else: maze[nx][ny] = str(int(maze[x][y]) + 1) route.append([nx, ny]) #探索結果を出力 for i in range(SIZE): for j in range(SIZE): sys.stdout.write(str(maze[i][j])) print() #探索順序を出力 order = [[13, 13]] pursue_best(maze, order[0], 40, order)
def fire_test(fire_node_num, send_node_num1, send_node_num2, node, linked_node_list): print("fire test") queue = [] del_list = [] fire_start = fire_node_num send1 = send_node_num1 send2 = send_node_num2 node[fire_node_num].distance = float('inf') if fire_node_num not in knu.fire_place_num: knu.fire_place_num.append(fire_node_num) for i in linked_node_list[send2]: if i != send1 and i not in knu.exit: #not in fire_place_num: queue.insert(0,i) for i in queue: substract = [] node_list = [] shortest_node_num = [] temp_exit_diret = 0 for j in linked_node_list[i]: node_list.append(j) if j != send2: substract.append(node[j].distance - node[i].distance) else: substract.append(float('inf')) #print(node_list) shortest_distance_node_index = [i for i, value in enumerate(substract) if value == min(substract)] temp_exit_diret = node[i].exit_diret if (min(substract) != -1 or node[i].exit_diret_num != shortest_distance_node_index) and min(substract) != float('inf'): node[i].exit_diret = 0 if send2 not in node[i].visited_place: node[i].visited_place.append(send2) print('7') #node[i].distance += 2 node[i].set_exit_diret_num(shortest_distance_node_index) for k in shortest_distance_node_index: shortest_node_num.append(node_list[k]) if node[i].forward != None and node[i].forward.index in shortest_node_num: if node[i].forward.index not in node[i].visited_place: node[i].exit_diret += 32 if node[i].backward != None and node[i].backward.index in shortest_node_num : if node[i].backward.index not in node[i].visited_place: node[i].exit_diret += 16 if node[i].right != None and node[i].right.index in shortest_node_num : if node[i].right.index not in node[i].visited_place: node[i].exit_diret += 4 if node[i].left != None and node[i].left.index in shortest_node_num: if node[i].left.index not in node[i].visited_place: node[i].exit_diret += 8 if node[i].up != None and node[i].up.index in shortest_node_num : if node[i].up.index not in node[i].visited_place: node[i].exit_diret += 2 if node[i].down != None and node[i].down.index in shortest_node_num: if node[i].down.index not in node[i].visited_place: node[i].exit_diret += 1 else: del_list.append(i) for i in del_list: queue.remove(i) for i in queue: fire_test(fire_start,send2,i,node,linked_node_list)
def fire_test(fire_node_num, send_node_num1, send_node_num2, node, linked_node_list): queue = [] del_list = [] fire_start = fire_node_num send1 = send_node_num1 send2 = send_node_num2 node[fire_node_num].distance = float('inf') if fire_node_num not in fire_place_num: fire_place_num.append(fire_node_num) for i in linked_node_list[send2]: if i != send1 and i: #not in fire_place_num: queue.insert(0, i) for i in queue: substract = [] node_list = [] shortest_node_num = [] temp_exit_diret = [] print("in ->", i) for j in linked_node_list[i]: node_list.append(j) if j != send2: substract.append(node[j].distance - node[i].distance) else: substract.append(float('inf')) #print(node_list) shortest_distance_node_index = [ i for i, value in enumerate(substract) if value == min(substract) ] temp_exit_diret = node[i].exit_diret if (min(substract) != -1 or node[i].exit_diret_num != shortest_distance_node_index ) and min(substract) != float('inf'): node[i].exit_diret = [] print(i, " in change") if send2 not in node[i].visited_place: node[i].visited_place.append(send2) #node[i].distance += 2 node[i].set_exit_diret_num(shortest_distance_node_index) for k in shortest_distance_node_index: shortest_node_num.append(node_list[k]) if i == 19: print(node_list) print(substract) print(node[19].visited_place) print(shortest_node_num) print(node[i].exit_diret) print("short =>", shortest_node_num) if node[i].forward != None and node[ i].forward.index in shortest_node_num: if node[i].forward.index not in node[i].visited_place: node[i].exit_diret.append('forward') if node[i].backward != None and node[ i].backward.index in shortest_node_num: if node[i].backward.index not in node[i].visited_place: node[i].exit_diret.append('backward') if node[i].right != None and node[ i].right.index in shortest_node_num: if node[i].right.index not in node[i].visited_place: node[i].exit_diret.append('right') if node[i].left != None and node[i].left.index in shortest_node_num: if node[i].left.index not in node[i].visited_place: node[i].exit_diret.append('left') if node[i].up != None and node[i].up.index in shortest_node_num: if node[i].up.index not in node[i].visited_place: node[i].exit_diret.append('up') if node[i].down != None and node[i].down.index in shortest_node_num: if node[i].down.index not in node[i].visited_place: node[i].exit_diret.append('down') if i == 19: print("exit1", node[19].exit_diret) if not node[i].exit_diret: node[i].exit_diret = temp_exit_diret if i == 19: print("exit2", node[19].exit_diret) else: del_list.append(i) print(i, "visited_place ->", node[i].visited_place) for i in del_list: queue.remove(i) for i in queue: fire_test(fire_start, send2, i, node, linked_node_list)
def fire_test(fire_node_num, send_node_num1, send_node_num2, node, linked_node_list): queue = [] del_list = [] fire_start = fire_node_num send1 = send_node_num1 send2 = send_node_num2 node[fire_node_num].distance = float('inf') if fire_node_num not in fire_place_num: fire_place_num.append(fire_node_num) for i in linked_node_list[send2]: if i != send1 and i not in fire_place_num: queue.insert(0, i) for i in queue: substract = [] node_list = [] shortest_node_num = [] for j in linked_node_list[i]: node_list.append(j) if j != send2: substract.append(node[j].distance - node[i].distance) else: substract.append(float('inf')) if min(substract) != -1 or node[i].exit_diret_num != substract.count( -1): node[i].exit_diret = [] shortest_distance_node_index = [ i for i, value in enumerate(substract) if value == min(substract) ] for k in shortest_distance_node_index: shortest_node_num.append(node_list[k]) if node[i].forward != None and node[ i].forward.index in shortest_node_num: node[i].exit_diret.append('forward') if node[i].backward != None and node[ i].backward.index in shortest_node_num: node[i].exit_diret.append('backward') if node[i].right != None and node[ i].right.index in shortest_node_num: node[i].exit_diret.append('right') if node[i].left != None and node[i].left.index in shortest_node_num: node[i].exit_diret.append('left') if node[i].up != None and node[i].up.index in shortest_node_num: node[i].exit_diret.append('up') if node[i].down != None and node[i].down.index in shortest_node_num: node[i].exit_diret.append('down') node[i].set_exit_diret_num() else: del_list.append(i) for i in del_list: queue.remove(i) for i in queue: fire_test(fire_start, send2, i, node, linked_node_list)
auth = tweepy.OAuthHandler(key_token.consumer_key, key_token.consumer_secret) auth.set_access_token(key_token.access_token, key_token.access_token_secret) api = tweepy.API(auth) userList.append('elonmusk') tweetsReplies = api.user_timeline(screen_name='elonmusk', count=100, include_rts=True) for tweetReply in tweetsReplies: twRe = json.dumps(tweetReply._json) tR = json.loads(twRe) if len(tR['entities']['user_mentions']) > 0: replyUser = tR['entities']['user_mentions'][0]["screen_name"] ReplyQueue = queue.insert(replyUser) count = 0 check = 0 while (len(ReplyQueue) != 0): # print(ReplyQueue[0]) user = queue.pop() if user not in userList: userList.append(user) with open("userList.txt", "a") as userFile: userFile.write(replyUser + "\n") userFile.close() try: tweetsReplies = api.user_timeline(screen_name=user, count=20, include_rts=True)