Skip to content

Instantly share code, notes, and snippets.

@BurcakAsal
Last active November 15, 2015 07:57
Show Gist options
  • Save BurcakAsal/e8e01544b96bc25092f0 to your computer and use it in GitHub Desktop.
Save BurcakAsal/e8e01544b96bc25092f0 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Analyzing and testing a program with binary search method\n",
"def isPal(x):\n",
" assert type(x)==list\n",
" temp=x # Third bug, unsafe use for assignment of the list, Solution: try 'temp=x[:]' to copy 'x' list to 'temp' safely\n",
" #print(temp,x) # Fourth use of print, try to see what 'temp' and 'x' lists shows before and after reversion \n",
" temp.reverse # -> Second bug, Solution: temp.reverse()\n",
" #print(temp,x) # Third use of print, searching another potential bug on the upper half of the function\n",
" if temp==x:\n",
" return True\n",
" else:\n",
" return False\n",
" \n",
"def silly(n):\n",
" #result=[] -> Solution for the first bug, place it outside of the loop\n",
" for i in range(n):\n",
" result=[] # -> First bug, causing the 'result' list to be empty list each time in the loop\n",
" elem=raw_input('Enter element: ')\n",
" result.append(elem)\n",
" #print(result) -> Second use of print, to see the 'result' more detailed within the 'for' loop \n",
" #print(result) -> First use of print, searching the potential bug on the upper half of the code \n",
" if isPal(result): #Make sure the upper half of the code dosen't include any bugs, then search other bugs in other half\n",
" print('Yes')\n",
" else:\n",
" print('No')\n",
" \n",
"# k,a,y,a,k m,a,k,a,m / z,a,m,a,n \n",
"\n",
"# Try palindrome and non-palindrome sequences for testing the 'silly' function\n",
"silly(5)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#Example for using time function for calculating elapsed time in a program\n",
"import time\n",
"\n",
"start_time = time.time() # Setting starting time (time.time())\n",
"#------------------------------------------------------------\n",
"i=0\n",
"loop_range = 100000000 # try to increase or decrease 'loop_range' variable and see the results for 'elapsed_time' variable\n",
"while i<loop_range:\n",
" i=i+1\n",
"#------------------------------------------------------------\n",
"elapsed_time = time.time() - start_time # Setting ending time (time.time()) and subtracting start time from it\n",
"print elapsed_time\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment