Skip to content

Instantly share code, notes, and snippets.

@evanthebouncy
Last active April 5, 2016 16:58
Show Gist options
  • Save evanthebouncy/0e6225941d3a93857b46 to your computer and use it in GitHub Desktop.
Save evanthebouncy/0e6225941d3a93857b46 to your computer and use it in GitHub Desktop.
=======
def oddTuples (x0 ):
x1 =()
for x2 in range (len (x0 ))
if x2 %2==0:
x1 =x1 +x0 [x2 ]
return x1
def oddTuples (x0 ):
x1 =()
for x2 in range (len (x0 )):
if x2 %2==0:
x1 =x1 +(x0 [x2 ],)
return x1
===========
def oddTuples (x0 ):
x1 =()
for x2 in range (len (x0 )+1):
if x2 %2==1:
x1 =x1 +(x0 [x2 -1:x2 ])
def oddTuples (x0 ):
x1 =()
for x2 in range (len (x0 )+1):
if x2 %2==1:
x1 =x1 +(x0 [x2 -1:x2 ])
return x1
========
def oddTuples (x0 ):
x1 =()
for x2 in x0 :
if x2 %2==0:
x1 =x1 +(x2 ,)
return x1
def oddTuples (x0 ):
x1 =()
x2 =0
for x2 in range (len (x0 )):
if x2 %2==0:
x1 =x1 +(x0 [x2 ],)
return x1
========
NOVEL SOLUTION:
def evaluatePoly ( x0 , x1 ) :
x2 = 0
x3 = 0.0
for x2 in range ( 0 , len ( x0 ) - 1 ) :
x3 = x0 [ x2 ] * x1 ** x2 + x3
x2 += 1
return x3
corrected prog
def evaluatePoly ( x0 , x1 ) :
x2 = 0
x3 = 0.0
while x2 in range ( len ( x0 ) ) :
x3 = x0 [ x2 ] * x1 ** x2 + x3
x2 += 1
return x3
\paragraph{Fixing type mismatch} The incorrect program mistakenly raised a number x1 to a power of a list [x3].
\begin{lstlisting}
# incorrect prog
def evaluatePoly ( x0 , x1 ) :
x2 = 0.0
for x3 in range ( len ( x0 ) ) :
x2 = x2 + (x0[x3] * (x1 ** [x3]))
return x2
# corrected prog
def evaluatePoly ( x0 , x1 ) :
x2 = 0.0
for x3 in range ( len ( x0 ) ) :
x2 += x0 [ x3 ] * x1 ** x3
return x2
\end{lstlisting}
\paragraph{Fixing missing indentations} The incorrect program forgot to indent the first line (remember that python use relative indent and dedent so only 1 line is corrected here).
\begin{lstlisting}
# incorrect prog
def evaluatePoly ( x0 , x1 ) :
x2 = 0.0
for x3 in range ( 0 , len ( x0 ) ) :
x2 += x0 [ x3 ] * x1 ** x3
return x2
# corrected prog
def evaluatePoly ( x0 , x1 ) :
x2 = 0
for x3 in range ( 0 , len ( x0 ) ) :
x2 += x0 [ x3 ] * x1 ** x3
return x2
\end{lstlisting}
\paragraph{Inserting a missing update function} The incorrect program forgot to add an update to the variable x3 inside the loop.
\begin{lstlisting}
# incorrect prog
def evaluatePoly ( x0 , x1 ) :
x2 = 0
x3 = 0
for x4 in x0 :
x2 = x2 + ( x4 * ( x1 ** x3 ) )
return x2
# corrected prog
def evaluatePoly ( x0 , x1 ) :
x2 = 0
x3 = 0
for x4 in x0 :
x2 = x2 + ( x4 * ( x1 ** x3 ) )
x3 += 1
return x2
\end{lstlisting}
\paragraph{Suggesting to use the enumerate function} Our algorithm replaced the original for loop with a for loop that uses enumeration, it also re-named the variables in the update call inside the loop to match.
\begin{lstlisting}
# incorrect prog
def evaluatePoly ( x0 , x1 ) :
x2 = 0
for x3 in x0 :
x2 += x3 * ( x1 ** x4 )
x4 += 1
return x2
# corrected prog
def evaluatePoly ( x0 , x1 ) :
x2 = 0
for x3 , x4 in enumerate ( x0 ) :
x2 += x4 * ( x1 ** x3 )
x4 += 1
return x2
\end{lstlisting}
\paragraph{Suggestion of a novel program} This novel suggestion fixes the incorrect program by replacing the ``for'' loop with a very strange ``while'' loop, which only work because of the extraneous update function ``x2 += 1'' present in the incorrect program.
\begin{lstlisting}
# incorrect prog
def evaluatePoly ( x0 , x1 ) :
x2 = 0
x3 = 0.0
for x2 in range ( 0 , len ( x0 ) - 1 ) :
x3 = x0 [ x2 ] * x1 ** x2 + x3
x2 += 1
return x3
# corrected prog
def evaluatePoly ( x0 , x1 ) :
x2 = 0
x3 = 0.0
while x2 in range ( len ( x0 ) ) :
x3 = x0 [ x2 ] * x1 ** x2 + x3
x2 += 1
return x3
\end{lstlisting}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment