def divide(a, b):
try:
return a / b
except:
x = 17
My smart exception handling before meeting "pass" :))
A,B=map(str,input().split()) # let's begin by casting strings to strings!
while(int(A)!=0 and int(B)!=0):
tamanhoa=0
tamanhob=0
for i in range(len(A)):
tamanhoa+=1 # wonderful way to determine the length of
# a string!
for i in range(len(B)):
tamanhob+=1
vA = [0]*tamanhoa
vB = [0]*tamanhob
if tamanhoa>tamanhob:
vA = [0]*tamanhoa # Just to make sure it really gets set!!!
vB = [0]*tamanhoa
for i in range(tamanhoa-1,-1,-1):
vA[i]=int(A[i]) # he even saved an operation by casting
# the char to int at the same time!
for i in range(tamanhob):
vB[i+1]=int(B[i])
elif tamanhoa<tamanhob:
vA = [0]*tamanhob # Just to make sure it really gets set!!!
vB = [0]*tamanhob
for i in range(tamanhoa):
vA[i+1]=int(A[i])
for i in range(tamanhob-1,-1,-1):
vB[i]=int(B[i])
tamanhoa=tamanhob
else:
for i in range(tamanhoa):
vA[i]=int(A[i])
for i in range(tamanhob):
vB[i]=int(B[i])
print(vA)
print(vB)
carry = 0
for i in range(tamanhoa-1,-1,-1):
soma = vA[i] + vB[i]
if soma > 9:
carry += 1
if vA[i-1]!= 9:
vA[i-1] += 1
else:
vB[i-1] +=1
if carry == 0:
print('No carry operation.')
elif carry == 1:
print(carry, 'carry operation.')
else:
print(carry, 'carry operations.')
A,B=map(str,input().split()) # why bother casting strings to int? yay
if int(A)==0 and int(B)==0:
break
This wonder was found within answers to a university programming test. Code as-is, comments added by me.
def get_first_index_of_array(array):
"""
this function is very usefull for get first index of array
"""
return array[0]
assert get_first_index_of_array([1, 2, 3, 4, 5]) == 1
assert get_first_index_of_array([1, 2, 3, 4, 5]) != 2
assert get_first_index_of_array([1, 2, 3, 4, 5]) != 3
assert get_first_index_of_array([1, 2, 3, 4, 5]) != 4
assert get_first_index_of_array([1, 2, 3, 4, 5]) != 4
def _take_items_from_list_and_put_them_into_string(self, list):
string = ''
for element in list:
string += element + ','
if len(string) > 0 and string[-1] == ',':
string = string[0:-1]
return string
from itertools import combinations as comb
from functools import reduce
def all_arrangements(k):
m=k*(k+1)/2
m_bits_on=set([tuple(reduce(lambda x,y:x[:y]+[1]+x[y+1:],c,[0]*(2*m+1))) for c in comb(range(2*m+1),m)])
return set([tuple(sorted(filter(lambda i:i>0,reduce(lambda x,y: x+[y] if y==0 else x[:-1]+[x[-1]+1,],p,[0])))) for p in m_bits_on])
Returns all arrangements in the Bulgarian solitaire game with k piles https://en.wikipedia.org/wiki/Bulgarian_solitaire
if (SelectionAndTimeData[1] < 2000 or \
SelectionAndTimeData[2] < 1 or SelectionAndTimeData[2] > 12 or \
SelectionAndTimeData[3] < 1 or SelectionAndTimeData[3] > 31 or \
SelectionAndTimeData[4] < 0 or SelectionAndTimeData[4] > 24 or \
SelectionAndTimeData[5] < 0 or SelectionAndTimeData[5] > 60 or \
SelectionAndTimeData[2] < 0 or SelectionAndTimeData[2] >60):
print('***************************************************************************')
print(' Entered date is not valid')
print('****************************************************************************')
for module in next_possible_modules:
import math; math.factorial(40000) # approx. a 1 second operation
end_time = start_time + timedelta(minutes=module.duration)
def copy( variable ):
return variable
Decade old code, used in 2 places to "copy" a float and a string. No one wants to touch the working code.
try:
self._update_plan()
except Exception as e:
raise Exception('{0}'.format(e))
RIP debuggability
def absolute_value(value):
if str(value)[0]=='-':
value = -1 * value
return value
else:
return value
def open(self, *filename):
if len(filename) > 1:
print(("Usage:\nopen() - opens with the initialized "
"filename\nopen(filename) - opens with given filename"))
return 1
if len(filename) == 1:
self.filename = filename[0]
if self.filename is None:
print("No filename given")
return 1
self.struct_p = open_struct(self.last_error, self.error_size, \
self.filename)
if self.struct_p is None:
print("Cannot open file: " + self.filename)
return 1
self.load_data()
return 0
reduc_ind = list(xrange(1, len(x.get_shape())))
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while (1):
_, frame = cap.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_green = np.array([40, 50, 50])
upper_green = np.array([80, 102, 200])
# Threshold the HSV image to get only green colors
mask = cv2.inRange(hsv, lower_green, upper_green)
total_pixels = mask.shape[0] * mask.shape[1]
print "Number of pixels: %", total_pixels
pixel_counter = 0
x_counter = 0
y_counter = 0
for y in xrange(640):
for x in xrange(480):
pixel = mask[x, y]
if pixel == 255:
pixel_counter += 1
x_counter += x
y_counter += y
x_center = x_counter / pixel_counter
y_center = y_counter / pixel_counter
print x_center, y_center
cv2.line(frame, (x_center+15, y_center), (x_center+2, y_center), (235, 218, 100), 1)
cv2.line(frame, (x_center-15, y_center), (x_center-2, y_center), (235, 218, 100), 1)
cv2.line(frame, (x_center, y_center+15), (x_center, y_center+2), (235, 218, 100), 1)
cv2.line(frame, (x_center, y_center-15), (x_center, y_center-2), (235, 218, 100), 1)
cv2.circle(frame, (x_center, y_center), 4, (235, 218, 100), 2)
cv2.imshow('frame', frame)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
track the green color
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while (1):
_, frame = cap.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_green = np.array([40, 50, 50])
upper_green = np.array([80, 102, 200])
# Threshold the HSV image to get only green colors
mask = cv2.inRange(hsv, lower_green, upper_green)
total_pixels = mask.shape[0] * mask.shape[1]
print "Number of pixels: %", total_pixels
pixel_counter = 0
x_counter = 0
y_counter = 0
for y in xrange(640):
for x in xrange(480):
pixel = mask[x, y]
if pixel == 255:
pixel_counter += 1
x_counter += x
y_counter += y
x_center = x_counter / pixel_counter
y_center = y_counter / pixel_counter
print x_center, y_center
cv2.line(frame, (x_center+15, y_center), (x_center+2, y_center), (235, 218, 100), 1)
cv2.line(frame, (x_center-15, y_center), (x_center-2, y_center), (235, 218, 100), 1)
cv2.line(frame, (x_center, y_center+15), (x_center, y_center+2), (235, 218, 100), 1)
cv2.line(frame, (x_center, y_center-15), (x_center, y_center-2), (235, 218, 100), 1)
cv2.circle(frame, (x_center, y_center), 4, (235, 218, 100), 2)
cv2.imshow('frame', frame)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
track the green color
if HOST == 'AdaLovelace' or HOST == 'vbu' or HOST == 'asus':
urlpatterns += patterns('',
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': MEDIA_ROOT}),
)