#!/usr/bin/env python # coding: utf-8 # # Hàm # ## Định nghĩa hàm # In[1]: def is_even(i): """ Input: i, a positive int Returns True if i is even, otherwise False """ print("hi") return i % 2 == 0 is_even(3) # ### Bài tập # # Write a function isIn that accepts two strings as arguments and returns True if either string occurs anywhere in the other, and False otherwise. Hint: you might want to use the built-in str operation in. # ## Phạm vi của biến # In[3]: def f( x ): x = x + 1 print('in f(x): x =', x) return x x = 3 z = f( x ) print('out f(x): x =', x) # In[2]: def is_even( i ): i % 2 == 0 a = is_even(3) type(a) # In[11]: def func_a(): print('inside func_a') def func_b(y): print('inside func_b') return y def func_c(z): print('inside func_c') return z() print(func_a()) print(5 + func_b(2)) print(func_c(func_a)) # In[12]: def f(y): x = 1 x += 1 print(x) x = 5 f(x) print(x) # In[13]: def g(y): print(x) print(x + 1) x = 5 g(x) print(x) # In[14]: def h(y): x = x + 1 x = 5 h(x) print(x) # In[16]: def g(x): def h(): x = 'abc' x = x + 1 print('in g(x): x =', x) h() return x x = 3 z = g(x) z # ## Tham số đặt tên - Keyword Arguments # In[19]: def printName(firstName, lastName, reverse): if reverse: print(lastName + ', ' + firstName) else: print(firstName, lastName) printName('Eric', 'Grimson', False) printName('Eric', 'Grimson', reverse = False) printName('Eric', lastName = 'Grimson', reverse = False) printName(lastName = 'Grimson', firstName = 'Eric', reverse = False) # ## Giá trị mặc định - default values # In[20]: def printName(firstName, lastName, reverse = False): if reverse: print(lastName + ', ' + firstName) else: print(firstName, lastName) printName('Eric', 'Grimson') printName('Eric', 'Grimson', True) # ## Các đặt tả - specification/docstring # In[24]: def is_even( i ): """ Input: i, a positive int Returns True if i is even, otherwise False """ print('hi') return i % 2 == 0 # hit Shift-Tab to see docstring is_even(3) # ## Đệ qui # In[47]: def mult_iter(a, b): result = 0 while b > 0: result += a b -= 1 return result mult_iter(2, 3) # In[48]: def mult(a, b): if b == 1: return a else: return a + mult(a, b-1) mult(2, 3) # In[49]: def fact(n): if n == 1: return 1 else: return n*fact(n-1) fact(4) # ## Towers of Hanoi # # [Video giới thiệu](https://youtu.be/rVPuzFYlfYE?t=1m20s) # In[53]: def printMove(fr, to): print('move from ' + str(fr) + ' to ' + str(to)) def Towers(n, fr, to, spare): if n == 1: printMove(fr, to) else: Towers(n - 1, fr, spare, to) Towers(1, fr, to, spare) Towers(n - 1, spare, to, fr) Towers(4, 'P1', 'P2', 'P3') # ## Đệ qui với nhiều trường hợp ngừng (base cases) # In[43]: def fib(x): """ assumes x an int >= 0 return Fibonacci of x """ if x == 0 or x == 1: return 1 else: return fib(x - 1) + fib(x - 2) def testFib(n): for i in range(n + 1): print('fib of', i, '=', fib(i)) testFib(10) # ### Bài tập # # When the implementation of fib in the above program is used to compute fib(5), how many times does it compute the value of fib(2) on the way to computing fib(5)? # ## Đệ qui với phi số # In[44]: def isPalindrome(s): def toChars(s): s = s.lower() ans = '' for c in s: if c in 'abcdefghijklmnopqrstuvwxyz': ans = ans + c return ans def isPal(s): if len(s) <= 1: return True else: return s[0] == s[-1] and isPal(s[1:-1]) return isPal(toChars(s)) def testIsPalindrome(): print('Try dogGod') print(isPalindrome('dogGod')) print('Try Able was I, ere I saw Elba') print(isPalindrome("Able was I, ere I saw Elba")) testIsPalindrome() # ## Tập tin # In[30]: # %load Files/circle.py pi = 3.14159 def area(radius): return pi * (radius**2) def circumference(radius): return 2*pi*radius # In[2]: from Files import circle pi = 3 print(pi) print(circle.pi) print(circle.area(3)) print(circle.circumference(3)) # In[39]: nameHandle = open('Files/kids', 'w') for i in range(2): name = input('Enter name: ') nameHandle.write(name + '\n') nameHandle.close() # In[40]: nameHandle = open('Files/kids', 'r') for line in nameHandle: print(line) nameHandle.close() # - - - # [Trước: Các phương pháp cơ bản giải quyết bài toán trên máy tính](http://nbviewer.jupyter.org/github/manleviet/introCSusingPython/blob/master/3%20-%20C%C3%A1c%20ph%C6%B0%C6%A1ng%20ph%C3%A1p%20c%C6%A1%20b%E1%BA%A3n%20gi%E1%BA%A3i%20quy%E1%BA%BFt%20b%C3%A0i%20to%C3%A1n%20tr%C3%AAn%20m%C3%A1y%20t%C3%ADnh.ipynb) | # [Mục lục](http://nbviewer.jupyter.org/github/manleviet/introCSusingPython/blob/master/index.ipynb) | # [Tiếp: Tuple và List](http://nbviewer.jupyter.org/github/manleviet/introCSusingPython/blob/master/5%20-%20Tuple%2C%20List.ipynb)