This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def binary_search(lst, num): | |
| if num not in lst: | |
| return None | |
| last = len(lst) | |
| first = 0 | |
| while(last>first): | |
| mid = (last + first) / 2 | |
| if lst[mid] == num: | |
| return mid | |
| elif lst[mid] > num: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def count(lst): | |
| r = {} | |
| for i in lst: | |
| if i in r: | |
| r[i] += 1 | |
| else: | |
| r[i] = 1 | |
| return r | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ### run.sh ### | |
| #!/bin/bash | |
| for((n=0; n<2; n++)) | |
| do | |
| ./print.sh $n & | |
| done | |
| wait | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| version: '3' | |
| services: | |
| mysql: | |
| image: mysql:latest | |
| restart: always | |
| ports: | |
| - 3306:3306 | |
| environment: | |
| MYSQL_ROOT_PASSWORD: 123 | |
| MYSQL_DATABASE: test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| a = int('101', base=2) | |
| from functools import partial | |
| b2i = partial(int, base=2) | |
| b = b2i('101') | |
| def btoi(x, base=2): | |
| return int(x, base) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from collections import Iterator | |
| class Foo(Iterator): | |
| def __init__(self, stop): | |
| self.x = 0 | |
| self.stop = stop | |
| def __iter__(self): | |
| return self |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def gen(stop): | |
| for i in range(stop): | |
| yield i | |
| f = gen(10) | |
| print(type(f)) | |
| for i in f: | |
| print(i), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def gen(stop): | |
| i = 0 | |
| while True: | |
| if i >= stop: | |
| raise StopIteration | |
| i = (yield i) or i+1 | |
| g = gen(100) | |
| print(next(g)) # Python 3 compatibility | |
| print(next(g)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def log(func): | |
| def foo(): | |
| print('call %s' % func.__name__) | |
| return func() | |
| return foo | |
| @log | |
| def test(): | |
| pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Method-1 | |
| class Foo(object): | |
| __instance = None | |
| def __new__(cls, *args, **kwargs): | |
| if Foo.__instance is None: | |
| Foo.__instance = object.__new__(cls, *args, **kwargs) | |
| return Foo.__instance | |
| a = Foo() | |
| b = Foo() |
OlderNewer