码迷,mamicode.com
首页 > 其他好文 > 详细

tmp

时间:2019-05-07 21:34:32      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:val   col   lag   ini   pytho   dict   elf   root   object   

 

def calculate_sum():
    s = 0
    i = 1
    flag = 1
    while not (1/i < 1e-4):
        s += flag*1/i
        i += 1
        flag = -flag
    return s

print("%.6f" % calculate_sum())

 

 

trie = Trie()
trie.insert("A")
trie.insert("to")
trie.insert("ten")
trie.insert("i")
trie.insert("in")
trie.insert("inn")
trie.search("in") # True
trie.search("ink") # False


import collections

class Node(object):
    def __init__(self, val=""):
        self.val = val
        self.children = collections.defaultdict(Node)

class Trie(object):
    def __init__(self):
        self.root = Node("")
    
    def insert(self, word):
        node = self.root
        for char in word:
            node = node.children[char]
        node.val = word
    
    def search(self, word):
        node = self.root
        for char in word:
            if char in node.children:
                node = node.children[char]
            else:
                return False
        return len(node.val) > 0

 

tmp

标签:val   col   lag   ini   pytho   dict   elf   root   object   

原文地址:https://www.cnblogs.com/bonelee/p/10827858.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!