.


:




:

































 

 

 

 





  • PEP 255: Simple Generators
  • Understanding Pythons with statement
  • Closures in Python
  • English Irregular Plural Nouns

 

. , (yields) , , . ? , :

class Fib:
'''iterator that yields numbers in the Fibonacci sequence'''

def __init__(self, max):
self.max = max

def __iter__(self):
self.a = 0
self.b = 1
return self

def __next__(self):
fib = self.a
if fib > self.max:
raise StopIteration
self.a, self.b = self.b, self.a + self.b
return fib

:

class Fib:

class? ?

Python -, , , , .

Python . , . . Python class, () . , , , , .

class PapayaWhip: [K 1]
pass [K 2]
  1. ↑ PapayaWhip . , , , , , .
  2. ↑ , , , if, for . class.

PapayaWhip , , . pass. Python pass , : , . , , .

pass Python Java C++.

, . , . Python , . , C++ , Python . , , Python , : __init__().

__init__()

Fib, __init__().

class Fib: '''iterator that yields numbers in the Fibonacci sequence''' [K 1]
def __init__(self, max): [K 2]
  1. ↑ , ( ) (docstrings).
  2. ↑ __init__() . , , . , C++: (, __init__() , ), ( , ). , __init__() , (self)

, __init__(), . self. this C++ Java, , , Python self . , , .

__init__(), self ; , . , self , ; Python .

Python , , __init__(). .

>>> import fibonacci2 >>> fib = fibonacci2.Fib(100)  
>>> fib  
<fibonacci2.Fib object at 0x00DB8810> >>> fib.__class__  
<class 'fibonacci2.Fib'> >>> fib.__doc__  
'iterator that yields numbers in the Fibonacci sequence'  
  1. Fib ( fibonacci2) fib. , 100, max, __init__() Fib.
  2. fib Fib
  3. __class__, . Java Class, getName() getSuperclass(), . Python, , .
  4. (docstring) , . .
Python, , , , new ++ Java, Python .

:

class Fib: def __init__(self, max): self.max = max 1.
  1. self.max? . max, __init__() . self.max . , .

 

class Fib: def __init__(self, max): self.max = max 1.
... def __next__(self): fib = self.a if fib > self.max: 2.
  1. self.max __init__()...
  2. __next__().

. , Fib , .

>>> import fibonacci2
>>> fib1 = fibonacci2.Fib(100)
>>> fib2 = fibonacci2.Fib(200)
>>> fib1.max
100
>>> fib2.max
200

. , __iter__().

class Fib: ①
def __init__(self, max): ②
self.max = max

def __iter__(self): ③
self.a = 0
self.b = 1
return self

def __next__(self): ④
fib = self.a
if fib > self.max:
raise StopIteration ⑤
self.a, self.b = self.b, self.a + self.b
return fib ⑥

① ,Fib , .

 

HAWAII + IDAHO + IOWA + OHIO == STATES. , -, 510199 + 98153 + 9301 + 3593 == 621246. , ? , .

.

HAWAII + IDAHO + IOWA + OHIO == STATES
510199 + 98153 + 9301 + 3593 == 621246

H = 5
A = 1
W = 0
I = 9
D = 8
O = 3
S = 6
T = 2
E = 4

. , 0 9, . , . , 0.

Python, . 14 .

SEND + MORE = MONEY.

import re
import itertools

def solve(puzzle):
words = re.findall('[A-Z]+', puzzle.upper())
unique_characters = set(''.join(words))
assert len(unique_characters) <= 10, 'Too many letters'
first_letters = {word[0] for word in words}
n = len(first_letters)
sorted_characters = ''.join(first_letters) + \
''.join(unique_characters - first_letters)
characters = tuple(ord(c) for c in sorted_characters)
digits = tuple(ord(c) for c in '0123456789')
zero = digits[0]
for guess in itertools.permutations(digits, len(characters)):
if zero not in guess[:n]:
equation = puzzle.translate(dict(zip(characters, guess)))
if eval(equation):
return equation

if __name__ == '__main__':
import sys
for puzzle in sys.argv[1:]:
print(puzzle)
solution = solve(puzzle)
if solution:
print(solution)

. Linux . ( , , . .)

you@localhost:~/diveintopython3/examples$ python3 alphametics.py "HAWAII + IDAHO + IOWA + OHIO == STATES"
HAWAII + IDAHO + IOWA + OHIO = STATES
510199 + 98153 + 9301 + 3593 == 621246
you@localhost:~/diveintopython3/examples$ python3 alphametics.py "I + LOVE + YOU == DORA"
I + LOVE + YOU == DORA
1 + 2784 + 975 == 3760
you@localhost:~/diveintopython3/examples$ python3 alphametics.py "SEND + MORE == MONEY"
SEND + MORE == MONEY
9567 + 1085 == 10652





:


: 2016-11-18; !; : 424 |


:

:

: , .
==> ...

1349 - | 1294 -


© 2015-2024 lektsii.org - -

: 0.019 .