開発環境
- OS X Lion - Apple(OS)
- Apache (Web Server)
- PHP (サーバーサイドプログラミング言語、スクリプト言語)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
『初めてのPHP5』 (David Sklar 著、 桑村 潤 翻訳、 廣川 類 翻訳、 オライリー・ジャパン、2005年、ISBN978-4-87311-257-2)の第 4章(配列の操作)4.7(演習問題)3.を解いてみる。
3.
HTML、PHPのソースコード(BBEdit)
<?php $people = array( "New York" => array("NY", 8008278), "Los Angeles" => array("CA", 3694820), "Chicago" => array("IL", 2896016), "Houston" => array("TX", 1953631), "Philadelphia" => array("PA", 1517550), "Phoenix" => array("AZ", 1321045), "San Diego" => array("CA", 1223400), "Dallas" => array("TX", 1188580), "San Antonio" => array("TX", 1144646), "Detroit" => array("MI", 951270) ); print '<table border=1 width=350>'; print '<caption>2000年のアメリカの10大都市の人口規模</caption>'; print '<tr align=center ><th>都市</th><th>人口</th></tr>'; $total = 0; $total_state = array(); foreach ($people as $key => $value) { print '<tr><td align=left>' . $key . ', ' . $value[0] . '</td><td align=right >' . $value[1] . '</td></tr>'; if(array_key_exists($value[0], $total_state)) { $total_state[$value[0]] += $value[1]; } else { $total_state[$value[0]] = $value[1]; } $total += $value[1]; } print '<tr><td>州ごとの人口総数</td><td></td></tr>'; foreach ($total_state as $key => $value) { print '<tr><td align=left>' . $key . '</td><td align=right >' . $value . '</td></tr>'; } print '<tr><th align=center>合計</th><th align=right>' . $total . '</th></tr>'; print '</table>'; ?>
ちなみにJavaScriptの場合。
コード(BBEdit)
$('#d0').html(''); var people = [ ["New York", ["NY" , 8008278]], ["Los Angeles", ["CA", 3694820]], ["Chicago", ["IL", 2896016]], ["Houston", ["TX", 1953631]], ["Philadelphia", ["PA", 1517550]], ["Phoenix", ["AZ", 1321045]], ["San Diego", ["CA", 1223400]], ["Dallas", ["TX", 1188580]], ["San Antonio", ["TX", 1144646]], ["Detroit", ["MI", 951270]] ]; var states = ["NY", "CA", "IL", "TX", "PA", "AZ", "MI"]; var result = '<table border=1 width=400>' + '<caption>2000年のアメリカの10大都市の人口規模</caption>' + '<tr align=center ><th>都市</th>' + '<th>人口</th></tr>'; var total = 0; var total_state = {}; var i, max; for ( i = 0, max = states.length; i < max; i++ ) { total_state[states[i]] = 0; } for(i = 0, max = people.length; i < max; i += 1){ result += '<tr><td align=left>' + people[i][0] + ", " + people[i][1][0] + '</td><td align=right >' + people[i][1][1] + '</td></tr>'; total_state[people[i][1][0]] += people[i][1][1]; total += people[i][1][1]; } result += '<tr><td>州ごとの人口総数</td><td></td></tr>'; for ( i = 0, max = states.length; i < max; i++ ) { result += '<tr><td align=left>' + states[i] + '</td><td align=right >' + total_state[states[i]] + '</td></tr>'; } result += '<tr><th align=center>合計</th><th align=right>' + total + '</th></tr></table>'; $('#d0').html(result);
pythonの場合。
sample.py
コード(BBEdit)
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- people = [ (("New York", "NY"), 8008278), (("Los Angeles", "CA"), 3694820), (("Chicago", "IL"), 2896016), (("Houston", "TX"), 1953631), (("Philadelphia", "PA"), 1517550), (("Phoenix", "AZ"), 1321045), (("San Diego", "CA"), 1223400), (("Dallas", "TX"), 1188580), (("San Antonio", "TX"), 1144646), (("Detroit", "MI"), 951270)] total = 0 total_state = {} print("city".center(20), "people".center(10), sep="") for c, p in people: print("{0}, {1}".format(c[0], c[1]).ljust(20), str(p).rjust(10), sep="") if c[1] in total_state.keys(): total_state[c[1]] += p else: total_state[c[1]] = p total += p print("州ごとの人口総数") for k, v in total_state: print(k.ljust(20), str(p).rjust(10), sep="") print("total".ljust(20), str(total).rjust(10), sep="")
入出力結果(Terminal)
$ ./sample.py city people New York, NY 8008278 Los Angeles, CA 3694820 Chicago, IL 2896016 Houston, TX 1953631 Philadelphia, PA 1517550 Phoenix, AZ 1321045 San Diego, CA 1223400 Dallas, TX 1188580 San Antonio, TX 1144646 Detroit, MI 951270 州ごとの人口総数 P 951270 T 951270 N 951270 A 951270 M 951270 C 951270 I 951270 total 23899236 $
0 コメント:
コメントを投稿