mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
some simple examples
This commit is contained in:
parent
2e3116f207
commit
52fdba36de
28
src/extra_interview_problems/basic_examples/example_args.py
Executable file
28
src/extra_interview_problems/basic_examples/example_args.py
Executable file
@ -0,0 +1,28 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
def simple2(a, *args):
|
||||||
|
print args
|
||||||
|
|
||||||
|
def simple(*args):
|
||||||
|
print args
|
||||||
|
|
||||||
|
def simple3(**kwargs):
|
||||||
|
print kwargs
|
||||||
|
|
||||||
|
|
||||||
|
simple(1, 2, 3)
|
||||||
|
simple2(1, 2, 3)
|
||||||
|
simple3(x=1, y=2)
|
||||||
|
|
||||||
|
|
||||||
|
def logger(func):
|
||||||
|
def inner(*args): #1
|
||||||
|
print "Arguments were: {0}".format(args)
|
||||||
|
return func(*args)
|
||||||
|
return inner
|
||||||
|
|
||||||
|
@logger
|
||||||
|
def foo(x, y):
|
||||||
|
return x+y
|
||||||
|
|
||||||
|
print foo(1, 2)
|
11
src/extra_interview_problems/basic_examples/example_queue.py
Executable file
11
src/extra_interview_problems/basic_examples/example_queue.py
Executable file
@ -0,0 +1,11 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
import Queue
|
||||||
|
|
||||||
|
q = Queue.Queue()
|
||||||
|
|
||||||
|
for i in range(10):
|
||||||
|
q.put(i)
|
||||||
|
|
||||||
|
for i in range(10):
|
||||||
|
print q.get(i)
|
@ -1,4 +1,4 @@
|
|||||||
BITWISE
|
BIT-WISE
|
||||||
----------------------
|
----------------------
|
||||||
|
|
||||||
1. To find a number:
|
1. To find a number:
|
||||||
@ -30,4 +30,4 @@
|
|||||||
|
|
||||||
6. Is power of 2?
|
6. Is power of 2?
|
||||||
just do x&(x-1).
|
just do x&(x-1).
|
||||||
if 0 --> yes!
|
if 0 --> yes!
|
||||||
|
19
src/extra_interview_problems/bitwise/swap_in_place.py
Executable file
19
src/extra_interview_problems/bitwise/swap_in_place.py
Executable file
@ -0,0 +1,19 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
'''
|
||||||
|
swapping values in place without extra memory
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
def swap_bit(a, b):
|
||||||
|
a = a^b
|
||||||
|
b = a^b
|
||||||
|
a = a^b
|
||||||
|
return a, b
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
a = 14
|
||||||
|
b = 73
|
||||||
|
a2, b2 = swap_bit(a, b)
|
||||||
|
print "a was {0}, now it is {1}. \nb was {2}, now it is {3}".format(a, a2, b, b2)
|
39
src/extra_interview_problems/design/example_decorator.py
Executable file
39
src/extra_interview_problems/design/example_decorator.py
Executable file
@ -0,0 +1,39 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
|
||||||
|
# testing generator
|
||||||
|
|
||||||
|
def interate(x):
|
||||||
|
for i in range(x):
|
||||||
|
yield i
|
||||||
|
|
||||||
|
a = interate(10)
|
||||||
|
|
||||||
|
print a.next()
|
||||||
|
print a.next()
|
||||||
|
print a.next()
|
||||||
|
|
||||||
|
# testing decorator
|
||||||
|
|
||||||
|
def sum(func):
|
||||||
|
s = 0
|
||||||
|
for i in func():
|
||||||
|
s += i
|
||||||
|
return s
|
||||||
|
|
||||||
|
@sum
|
||||||
|
def interate():
|
||||||
|
a = []
|
||||||
|
for i in range(10):
|
||||||
|
a.append(i)
|
||||||
|
return a
|
||||||
|
|
||||||
|
print interate
|
||||||
|
|
||||||
|
def interate():
|
||||||
|
a = []
|
||||||
|
for i in range(10):
|
||||||
|
a.append(i)
|
||||||
|
return a
|
||||||
|
|
||||||
|
print sum(interate)
|
Loading…
x
Reference in New Issue
Block a user