<-^^->

Přednáška 9

Cvičení

  1. Upravte následující kód tak, aby měl minimální počet řádek.
    Expand/Shrink
    Zdroj: (minimal1.1.py)
      1   import math
      2   
      3   a = math.log( 1)
      4   b = math.log( 2)
      5   c = (a + b) / 2
      6   d = math.e ** c
      7   print d
    stdout:
    1.41421356237
    
    Doba běhu: 20.6 ms
    Expand/Shrink
    Zdroj: (minimal1.2.py)
      1   a = "'Hi there'"
      2   b = "how are you"
      3   c = a.strip("'")
      4   d = c + b
      5   e = len( d)
      6   f = e/2
      7   g = e/4
      8   h = d[g:f]
      9   print h
    stdout:
    hereh
    
    Doba běhu: 19.9 ms
    Expand/Shrink
    This is an example text in an example file.
    It even has
    several
    lines.
    
    Zdroj: (minimal1.3.py)
      1   f = file( "example.txt", "r")
      2   out = file( "example2.txt", "w")
      3   
      4   for i in range( 4):
      5     l = f.readline()
      6     l2 = l.strip()
      7     l3 = l2[2:-1]
      8     l4 = l3.strip()
      9     out.write( l4)
     10   
     11   f.close()
     12   out.close()
    is is an example text in an example fileeven haveranes
    Doba běhu: 20.3 ms
    Expand/Shrink
    Zdroj: (minimal1.4.py)
      1   xs = [1,4,2,7,9,10,2,5,8]
      2   y = max( xs)
      3   z = y + 2
      4   xs.append( z)
      5   xs.insert( 3, 3)
      6   ys = xs[1:4]
      7   xs += ys
      8   xs.sort()
      9   print xs
    stdout:
    [1, 2, 2, 2, 3, 3, 4, 4, 5, 7, 8, 9, 10, 12]
    
    Doba běhu: 20.4 ms