def setGroups(self, groups, name=default_user): '''Changes the user's groups.''' uf = self.portal.acl_users if PLONE25: uf.userSetGroups(name, utils.makelist(groups)) else: uf._updateUser(name, groups=utils.makelist(groups)) if name == getSecurityManager().getUser().getId(): self.login(name)
def setRoles(self, roles, name=default_user): '''Changes the user's roles.''' uf = self.portal.acl_users if PLONE25: uf.userFolderEditUser(name, None, utils.makelist(roles), []) else: uf._updateUser(name, roles=utils.makelist(roles)) if name == getSecurityManager().getUser().getId(): self.login(name)
def decorator(callback): if isinstance(callback, str): callback = load(callback) for rule in makelist(path) or yieldroutes(callback): for verb in makelist(method): verb = verb.upper() route = Route(self, rule, verb, callback, name=name, plugins=plugins, skiplist=skiplist, **config) self.add_route(route) return callback
def route(self, path=None, method='GET', callback=None, name=None, apply=None, skip=None, **config): """ A decorator to bind a function to a request URL. Example:: @app.route('/hello/<name>') def hello(name): return 'Hello %s' % name The ``<name>`` part is a wildcard. See :class:`Router` for syntax details. :param path: Request path or a list of paths to listen to. If no path is specified, it is automatically generated from the signature of the function. :param method: HTTP method (`GET`, `POST`, `PUT`, ...) or a list of methods to listen to. (default: `GET`) :param callback: An optional shortcut to avoid the decorator syntax. ``route(..., callback=func)`` equals ``route(...)(func)`` :param name: The name for this route. (default: None) :param apply: A decorator or plugin or a list of plugins. These are applied to the route callback in addition to installed plugins. :param skip: A list of plugins, plugin classes or names. Matching plugins are not installed to this route. ``True`` skips all. Any additional keyword arguments are stored as route-specific configuration and passed to plugins (see :meth:`Plugin.apply`). """ if callable(path): path, callback = None, path plugins = makelist(apply) skiplist = makelist(skip) def decorator(callback): if isinstance(callback, str): callback = load(callback) for rule in makelist(path) or yieldroutes(callback): for verb in makelist(method): verb = verb.upper() route = Route(self, rule, verb, callback, name=name, plugins=plugins, skiplist=skiplist, **config) self.add_route(route) return callback return decorator(callback) if callback else decorator
""" Given a circular linked list, implement an algorithm which returns node at the beginning of the loop. DEFINITION Circular linked list: A (corrupt) linked list in which a node’s next pointer points to an earlier node, so as to make a loop in the linked list. EXAMPLE Input: A -> B -> C -> D -> E -> C [the same C as earlier] Output: C """ import utils head = utils.makelist(*"ABCDE") utils.printlist(head) head[4].next = head[2] # utils.printlist(head) def containsLoop(head): if head is None: return False sp = fp = head while True: if fp.next is None or fp.next.next is None: return False sp = sp.next fp = fp.next.next if sp == fp:
# self.next = None class Solution(object): def detectCycle(self, head): """ :type head: ListNode :rtype: ListNode """ fast, slow = head, head while True: if not fast or not fast.next or not fast.next.next: return None fast = fast.next.next slow = slow.next if fast is slow: break slow = head while slow is not fast: slow = slow.next fast = fast.next return slow import utils print Solution().detectCycle(utils.makelist([1, 2]))
def setPermissions(self, permissions, role=user_role): '''Changes the user's permissions.''' self.folder.manage_role(role, utils.makelist(permissions))
def setRoles(self, roles, name=user_name): '''Changes the user's roles.''' uf = self.folder.acl_users uf.userFolderEditUser(name, None, utils.makelist(roles), []) if name == getSecurityManager().getUser().getId(): self.login(name)
class Solution(object): def __init__(self, head): """ @param head The linked list's head. Note that the head is guaranteed to be not null, so it contains at least one node. :type head: ListNode """ self.head = head def getRandom(self): """ Returns a random node's value. :rtype: int """ p = self.head n = 0 v = None while p is not None: n += 1 if random.random() < 1.0 / n: v = p.val p = p.next return v import utils l = utils.makelist([1, 2, 3, 4, 5]) from collections import Counter print Counter([Solution(l).getRandom() for i in xrange(100000)])
def setRoles(self, roles, name=user_name): '''Changes the user's roles.''' uf = self.portal.acl_users uf.userFolderEditUser(name, None, utils.makelist(roles), []) if name == getSecurityManager().getUser().getId(): self.login(name)
elif i + 1 == j: nodes[i].next = nodes[j] nodes[j].next = None break elif i == j: nodes[i].next = None break return import utils head = utils.makelist() Solution().reorderList(head) utils.printlist(head) head = utils.makelist(1) Solution().reorderList(head) utils.printlist(head) head = utils.makelist(1,2,3,4) Solution().reorderList(head) utils.printlist(head) head = utils.makelist(1,2,3,4,5) Solution().reorderList(head) utils.printlist(head)
class Solution(object): def removeElements(self, head, val): """ :type head: ListNode :type val: int :rtype: ListNode """ if not head: return head newhead, newtail = None, None p = head while p: if p.val == val: pass else: if not newhead: newhead = newtail = p else: newtail.next = p newtail = p p = p.next if newtail: newtail.next = None return newhead import utils print Solution().removeElements(utils.makelist(*[1, 2, 6, 3, 4, 5, 6]), 6)
if p1: t1.next = p t1 = p else: p1 = t1 = p else: # append to p2 if p2: t2.next = p t2 = p else: p2 = t2 = p p = next if not p1: t2.next = None return p2 if not p2: t1.next = None return p1 t1.next = p2 t2.next = None return p1 from utils import makelist,printlist printlist(Solution().partition(makelist(1,1), 2))
# Definition for singly-linked list. class ListNode: def __init__(self, x): self.val = x self.next = None class Solution: # @param a ListNode # @return a ListNode def swapPairs(self, head): dummy = ListNode(0) p = dummy while head: if head.next: p.next = head.next head.next = head.next.next p.next.next = head p = head head = head.next else: p.next = head p = head head = head.next return dummy.next import utils as u u.printlist(Solution().swapPairs( u.makelist() )) u.printlist(Solution().swapPairs( u.makelist(1, 2, 3, 4) ))
# Write code to remove duplicates from an unsorted linked list. # # FOLLOW UP # # How would you solve this problem if a temporary buffer is not allowed? import utils def removeDuplicates(head): pass head = utils.makelist(1, 2, 2, 3, 4, 5, 4, 4, 3) removeDuplicates(head) utils.printlist(head)
def setPermissions(self, permissions, role='Member'): '''Changes the permissions assigned to role.''' self.portal.manage_role(role, utils.makelist(permissions))
# def __init__(self, x): # self.val = x # self.next = None from utils import ListNode, makelist, printlist class Solution(object): def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ head = tail = ListNode(None) c = 0 while l1 or l2 or c: val = (l1.val if l1 else 0) + (l2.val if l2 else 0) + c node = ListNode(val % 10) c = val >= 10 tail.next = node tail = node if l1: l1 = l1.next if l2: l2 = l2.next return head.next l1 = makelist(1,2,3) l2 = makelist(1,2,7) printlist(Solution().addTwoNumbers(l1, l2))
class Solution(object): def isPalindrome(self, head): """ :type head: ListNode :rtype: bool """ vals = [] while head: vals.append(head.val) head = head.next M = len(vals) // 2 return vals[0:M] == vals[:-M-1:-1] import utils print Solution().isPalindrome(utils.makelist(1,2,3,2,1))
C = 0 node = utils.ListNode(val) if head is not None: tail.next = node tail = node else: head = tail = node if p1: p1 = p1.next if p2: p2 = p2.next return head h1 = utils.makelist(3, 1, 5) h2 = utils.makelist(5, 9, 2) utils.printlist(h1) utils.printlist(h2) utils.printlist(sum(h1, h2)) h1 = utils.makelist(1) h2 = utils.makelist(9, 9, 9) utils.printlist(h1) utils.printlist(h2) utils.printlist(sum(h1, h2))
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def deleteNode(self, node): """ :type node: ListNode :rtype: void Do not return anything, modify node in-place instead. """ self.deleteNodeHelper(node) def deleteNodeHelper(self, node): if not node.next: return None node.val = node.next.val node.next = self.deleteNodeHelper(node.next) return node import utils head = utils.makelist([1, 2, 3, 4]) utils.printlist(head) Solution().deleteNode(head) utils.printlist(head)
k = 0 p = head revhead, revtail = None, None while p: next = p.next k += 1 # print k, p.val, m,n if k < m: prevLast = p # print 'prevLast set to', prevLast.val elif k <= n: if not revhead: revhead = revtail = p p.next = None else: p.next = revhead revhead = p else: if revtail: revtail.next = p break p = next if prevLast and revhead: prevLast.next = revhead return head if prevLast else revhead import utils l = utils.makelist(1, 2, 3) l = Solution().reverseBetween(l, 3, 3) utils.printlist(l)