- 運営しているクリエイター
#SimplePrograms
【Python】21 lines: XML/HTML parsing ~XML HTML を操ろう~
先に注意事項から。
XML/HTML を扱うプログラムの解説です。
Python のドキュメントで次のように警告されています。
XML の脆弱性についてはこちら。
さて。
始めましょう。
プログラム21行プログラムです。
dinner_recipe = '''<html><body><table><tr><th>amt</th><th>unit</th><th>item</th></t
再帰呼出に挑戦 「Python 18 lines: 8-Queens Problem」より
先日、Python の 18行プログラム「8-Queens Problem」を紹介しました。
この記事の最後に次のような問題を2つ提示しました。
(1)関数「under_attack」を再帰呼出で書けるでしょうか。
(2)関数「solve」を再帰呼出を使わずに書けるでしょうか。
そして今回は回答編です。
関数「under_attack」を再帰呼出で書いてみたBOARD_SIZE = 8d
関数「under_attack」解説
先の記事『8-Queens Problem』における関数「under_attack」について解説を追記します。
記事『8-Queens Problem』についてはこちら。
関数「under_attack」まず、関数「under_attack」を再掲します。
def under_attack(col, queens): left = right = col for r, c in
【Python】SimplePrograms 18 lines: 8-Queens Problem (recursion)
プログラム18行プログラムです。
BOARD_SIZE = 8def under_attack(col, queens): left = right = col for r, c in reversed(queens): left, right = left - 1, right + 1 if c in (left, col, right):
【Python】SimplePrograms line 14 Doctest-based testing
プログラム14行プログラムです。
def median(pool): '''Statistical median to demonstrate doctest. >>> median([2, 9, 9, 7, 9, 2, 4, 5, 8]) 6 #change to 7 in order to pass the test ''' copy = sorted(pool
【Python】SimplePrograms line 13 unittest
プログラム13行プログラムです。
import unittestdef median(pool): copy = sorted(pool) size = len(copy) if size % 2 == 1: return copy[int((size - 1) / 2)] else: return (copy[int(size/2 - 1)]
【Python】SimplePrograms line 12 Classes
プログラム12行プログラムです。
class BankAccount(object): def __init__(self, initial_balance=0): self.balance = initial_balance def deposit(self, amount): self.balance += amount def withdraw
【Python】SimplePrograms line 10 (時間とfrom-importとfor-else)
プログラム10行プログラムです。
from time import localtime activities = {8: 'Sleeping', 9: 'Commuting', 17: 'Working', 18: 'Commuting', 20: 'Eating', 22: 'Resting' } time_now = localtime() hour = time_now.tm_hour f
【Python】SimplePrograms line 11 (三重引用符: '''と while)
プログラム11行プログラムです。
REFRAIN = '''%d bottles of beer on the wall,%d bottles of beer,take one down, pass it around,%d bottles of beer on the wall!'''bottles_of_beer = 9while bottles_of_beer > 1: print
【Python】SimplePrograms line 8 コマンドライン引数と例外
8行プログラムです。
# This program adds up integers that have been passed as arguments in the command line import systry: total = sum(int(arg) for arg in sys.argv[1:]) print ('sum =', total)except Value
【Python】SimplePrograms line 7 dictionary、そしてgenerator object
7行プログラムです。
コードprices = {'apple': 0.40, 'banana': 0.50}my_purchase = { 'apple': 1, 'banana': 6}grocery_bill = sum(prices[fruit] * my_purchase[fruit] for fruit in my_purchase)pr
【Python】SimplePrograms line 6 import してライブラリを使ってみようぜ
6行プログラムです。
コードimport refor test_string in ['555-1212', 'ILL-EGAL']: if re.match(r'^\d{3}-\d{4}$', test_string): print (test_string, 'is a valid US local phone number') else: print
【Python】SimplePrograms line 4 フィボナッチ数列を出力する
4行プログラムです。
コードparents, babies = (1, 1)while babies < 100: print ('This generation has {0} babies'.format(babies)) parents, babies = (babies, parents + babies)
実行結果This generation has 1 babiesT
【Python】SimplePrograms line 3 for文を使ってみよう(繰り返し処理)
3行プログラムです。
コードfriends = ['john', 'pat', 'gary', 'michael']for i, name in enumerate(friends): print ("iteration {iteration} is {name}".format(iteration=i, name=name))
実行結果iteration 0 is johniterat