CSH 1.5 Pythonic Functions

2969 days ago by CSH2012

#1) def mod3(n): return n%3 mod3(-11) 
       
1
1
show(-11//3) show(-11%3) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}-4
\newcommand{\Bold}[1]{\mathbf{#1}}1
\newcommand{\Bold}[1]{\mathbf{#1}}-4
\newcommand{\Bold}[1]{\mathbf{#1}}1
#2) def incrementByOne(n): return n+1 incrementByOne(1.5) 
       
2.50000000000000
2.50000000000000
#3) absolute value function! show(abs(3.5)) show(abs(6.5)) show(abs(-3.5)) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}3.50000000000000
\newcommand{\Bold}[1]{\mathbf{#1}}6.50000000000000
\newcommand{\Bold}[1]{\mathbf{#1}}3.50000000000000
\newcommand{\Bold}[1]{\mathbf{#1}}3.50000000000000
\newcommand{\Bold}[1]{\mathbf{#1}}6.50000000000000
\newcommand{\Bold}[1]{\mathbf{#1}}3.50000000000000
#4) 1+2+3+...+n def sum1ToN(n): return n*(n+1)/2 show(sum1ToN(5)) show(sum1ToN(10)) show(sum1ToN(100)) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}15
\newcommand{\Bold}[1]{\mathbf{#1}}55
\newcommand{\Bold}[1]{\mathbf{#1}}5050
\newcommand{\Bold}[1]{\mathbf{#1}}15
\newcommand{\Bold}[1]{\mathbf{#1}}55
\newcommand{\Bold}[1]{\mathbf{#1}}5050
#5) def reciprocal(x): return 1/x #show(reciprocal(0)) show(reciprocal(2/3)) show(reciprocal(2/3).n()) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{3}{2}
\newcommand{\Bold}[1]{\mathbf{#1}}1.50000000000000
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{3}{2}
\newcommand{\Bold}[1]{\mathbf{#1}}1.50000000000000
#6) def double(x): return 2*x show(double(incrementByOne(5))) show(incrementByOne(double(5))) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}12
\newcommand{\Bold}[1]{\mathbf{#1}}11
\newcommand{\Bold}[1]{\mathbf{#1}}12
\newcommand{\Bold}[1]{\mathbf{#1}}11
#7) incrementByOne('123') 'joe'+1 
       
Traceback (click to the left of this block for traceback)
...
TypeError: unsupported operand parent(s) for '+': '<type 'str'>'
and 'Integer Ring'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_30.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("IzcpCmluY3JlbWVudEJ5T25lKCcxMjMnKQonam9lJysx"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/tmp/tmppeZbJY/___code___.py", line 3, in <module>
    incrementByOne('123')
  File "/tmp/tmpqiXvtB/___code___.py", line 4, in incrementByOne
    return n+_sage_const_1 
  File "element.pyx", line 1302, in sage.structure.element.RingElement.__add__ (sage/structure/element.c:11504)
  File "coerce.pyx", line 766, in sage.structure.coerce.CoercionModel_cache_maps.bin_op (sage/structure/coerce.c:7337)
TypeError: unsupported operand parent(s) for '+': '<type 'str'>' and 'Integer Ring'
#8) show(double(double('123'))) show('joe'+'bob') 
       
\newcommand{\Bold}[1]{\mathbf{#1}}123123123123
\newcommand{\Bold}[1]{\mathbf{#1}}\hbox{joebob}
\newcommand{\Bold}[1]{\mathbf{#1}}123123123123
\newcommand{\Bold}[1]{\mathbf{#1}}\hbox{joebob}
#9) def firstLetter(s): return s[0] firstLetter("super") 
       
's'
's'
def lastLetter(s): return s[-1] lastLetter("super") 
       
'r'
'r'
#10) show(firstLetter([1.5,"5","school"])) show(lastLetter([1.5,"5","school"])) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}1.50000000000000
\newcommand{\Bold}[1]{\mathbf{#1}}\hbox{school}
\newcommand{\Bold}[1]{\mathbf{#1}}1.50000000000000
\newcommand{\Bold}[1]{\mathbf{#1}}\hbox{school}
#11) show(sum([10,2,3,0])) show(min([10,2,3])) show(max([3,10,2])) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}15
\newcommand{\Bold}[1]{\mathbf{#1}}2
\newcommand{\Bold}[1]{\mathbf{#1}}10
\newcommand{\Bold}[1]{\mathbf{#1}}15
\newcommand{\Bold}[1]{\mathbf{#1}}2
\newcommand{\Bold}[1]{\mathbf{#1}}10
#12) show(range(5)) show(min(range(5))) show(max(range(5))) show(sum(range(5))) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[0, 1, 2, 3, 4\right]
\newcommand{\Bold}[1]{\mathbf{#1}}0
\newcommand{\Bold}[1]{\mathbf{#1}}4
\newcommand{\Bold}[1]{\mathbf{#1}}10
\newcommand{\Bold}[1]{\mathbf{#1}}\left[0, 1, 2, 3, 4\right]
\newcommand{\Bold}[1]{\mathbf{#1}}0
\newcommand{\Bold}[1]{\mathbf{#1}}4
\newcommand{\Bold}[1]{\mathbf{#1}}10
#13) def newSum1ToN(n): return sum(range(n+1)) newSum1ToN(5) 
       
15
15
#14) def numDigits(n): count =1 power = 10 while power <= n: count = count + 1 power = power * 10 return count numDigits(1234) 
       
4
4