Skip to content

Instantly share code, notes, and snippets.

import sys
import math
import random
from common import print_tour, read_input
def distance(city1, city2):
return math.sqrt((city1[0] - city2[0]) ** 2 + (city1[1] - city2[1]) ** 2)
def construct_dist_matrix(cities):
N = len(cities)
@shiyifu0416
shiyifu0416 / 2opt.py
Created June 26, 2025 05:45
2opt and greedy
import sys
import math
import random
from common import print_tour, read_input
def distance(city1, city2):
return math.sqrt((city1[0] - city2[0]) ** 2 + (city1[1] - city2[1]) ** 2)
def construct_dist_matrix(cities):
N = len(cities)
import sys
from collections import deque
class Wikipedia:
# Initialize the graph of pages.
def __init__(self, pages_file, links_file):
# A mapping from a page ID (integer) to the page title.
# For example, self.titles[1234] returns the title of the page whose
def readNumber(line, index):
number = 0
while index < len(line) and line[index].isdigit():
number = number * 10 + int(line[index])
index += 1
decimal = 0
if index < len(line) and line[index] == ".":
index += 1
decimal_digit = 1/10
while index < len(line) and line[index].isdigit():
import random, sys, time
###########################################################################
# #
# Implement a hash table from scratch! (⑅•ᴗ•⑅) #
# #
# Please do not use Python's dictionary or Python's collections library. #
# The goal is to implement the data structure yourself. #
# #
###########################################################################
import bisect
def createDictionary(filename="words.txt"):
with open(filename, 'r') as f:
return set(word.strip().lower() for word in f if word.strip().isalpha())
def solution(random_word, dictionary):
sorted_random_word = sorted(random_word)
new_dictionary = [(sorted(word), word) for word in dictionary]
new_dictionary.sort()
def dictionary(filename="words.txt"):