Skip to content

Commit b99a89c

Browse files
committed
Add sample python scripts
1 parent 008967f commit b99a89c

33 files changed

+934
-0
lines changed

binarytree.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
class Node:
2+
def __init__(self,data):
3+
self.left = None
4+
self.right = None
5+
self.key = data
6+
def insert(root,node):
7+
if root is None:
8+
root = node
9+
else:
10+
if node.key < root.key:
11+
if root.right is None:
12+
root.right = node
13+
else:
14+
insert(root.right,node)
15+
else:
16+
if root.left is None:
17+
root.left = node
18+
else:
19+
insert(root.left,node)
20+
21+
def inorder(root):
22+
if root:
23+
inorder(root.left)
24+
print(root.key)
25+
inorder(root.right)
26+
27+
r = Node(50)
28+
insert(r,Node(30))
29+
insert(r,Node(20))
30+
insert(r,Node(70))
31+
inorder(r)
32+
33+
'''
34+
# Python program to demonstrate insert operation in binary search tree
35+
36+
# A utility class that represents an individual node in a BST
37+
class Node:
38+
def __init__(self,key):
39+
self.left = None
40+
self.right = None
41+
self.val = key
42+
43+
# A utility function to insert a new node with the given key
44+
def insert(root,node):
45+
if root is None:
46+
root = node
47+
else:
48+
if root.val < node.val:
49+
if root.right is None:
50+
root.right = node
51+
else:
52+
insert(root.right, node)
53+
else:
54+
if root.left is None:
55+
root.left = node
56+
else:
57+
insert(root.left, node)
58+
59+
# A utility function to do inorder tree traversal
60+
def inorder(root):
61+
if root:
62+
inorder(root.left)
63+
print(root.val)
64+
inorder(root.right)
65+
66+
67+
# Driver program to test the above functions
68+
# Let us create the following BST
69+
# 50
70+
# / \
71+
# 30 70
72+
# / \ / \
73+
# 20 40 60 80
74+
r = Node(50)
75+
insert(r,Node(30))
76+
insert(r,Node(20))
77+
insert(r,Node(40))
78+
insert(r,Node(70))
79+
80+
81+
# Print inoder traversal of the BST
82+
inorder(r)
83+
'''
84+

camera.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from kivy.app import App
2+
from kivy.lang import Builder
3+
kv = '''
4+
BoxLayout:
5+
orientation:'vertical'
6+
Camera:
7+
id:camera
8+
resolution: (640, 480)
9+
play:False
10+
ToggleButton:
11+
text:'Play'
12+
on_press:camera.play = not camera.play
13+
size_hint_y:None
14+
height:'48dp'
15+
'''
16+
class TestCamera(App):
17+
def build(self):
18+
return Builder.load_string(kv)
19+
TestCamera().run()

client.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import socket # Import socket module
2+
3+
s = socket.socket() # Create a socket object
4+
host = socket.gethostname() # Get local machine name
5+
port = 3000 # Reserve a port for your service.
6+
7+
s.connect((host, port))
8+
print s.recv(1024)
9+
s.close

data-model.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Polynomial:
2+
def __init__(self,*coeffs):
3+
self.coeffs = coeffs
4+
def __repr__(self):
5+
return 'Polynomial(*{!r})'.format(self.coeffs)
6+
def __add__(self,other):
7+
return Polynomial(*(x+y for x,y in zip(self.coeffs,other.coeffs)))
8+
def __len__(self):
9+
return len(self.coeffs)
10+
a = Polynomial(1,2,3)
11+
b = Polynomial(3,4,5)
12+
13+
print(a)
14+
print(b)
15+
print(a+b)

fibonacci_series.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
num = int(input())
2+
def fibo(n):
3+
a,b = 0,1
4+
for i in range(n):
5+
yield a
6+
a,b = b,a+b
7+
for i in fibo(num):
8+
print(i)

functional_tests.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from selenium import webdriver
2+
browser = webdriver.Firefox()
3+
browser.get('http://localhost:8000')
4+
assert 'Django' in browser.title

game.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pygame
2+
pygame.init()
3+
size = (700,600)
4+
screen = pygame.display.set_mode(size)
5+
done = False
6+
clock = pygame.time.Clock()
7+
while not done:
8+
#--main event loop
9+
for event in pygame.event.get():
10+
if event.type == pygame.QUIT:
11+
done = True
12+
screen.fill((255,255,255))
13+
pygame.display.flip()
14+
clock.tick(1060)
15+

grid.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
num = 5
2+
#print("+", "-"*4, "+", "-"*4, "+")
3+
def bottomLine(num):
4+
print("|",end=" ")
5+
print(" "*num,end=" ")
6+
print("|",end=" ")
7+
print(" "*num,end=" ")
8+
print("|")
9+
10+
def horizontalLine(num):
11+
print("+",end=" ")
12+
print("-"*num,end=" ")
13+
print("+",end=" ")
14+
print("-"*num,end=" ")
15+
print("+")
16+
horizontalLine(num+1)
17+
for i in range(num+1):
18+
bottomLine(num+1)
19+
horizontalLine(num+1)
20+
for i in range(num+1):
21+
bottomLine(num+1)
22+
horizontalLine(num+1)
23+
24+
25+

gui.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import sys
2+
from PyQt4 import QtGui
3+
def window():
4+
app = QtGui.QApplication(sys.argv)
5+
w = QtGui.QWidget()
6+
b = QtGui.QLabel(w)

hackerearth.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#! -*- coding: utf-8 -*-
2+
3+
import requests
4+
5+
# constants
6+
RUN_URL = u'http://api.hackerearth.com/code/run/'
7+
8+
CLIENT_SECRET = '81b00a5cbbabde29b268730f9a7cefac8ca4174a'
9+
10+
source = open('test.py', 'r')
11+
"""
12+
test.py
13+
#! -*- coding: utf-8 -*-
14+
15+
def square(no):
16+
return no * no
17+
18+
print(square(-23))
19+
"""
20+
21+
data = {
22+
'client_secret': CLIENT_SECRET,
23+
'async': 0,
24+
'source': source.read(),
25+
'lang': "PYTHON",
26+
'time_limit': 5,
27+
'memory_limit': 262144,
28+
}
29+
30+
r = requests.post(RUN_URL, data=data)
31+
source.close()
32+
print r.json()

0 commit comments

Comments
 (0)