開発環境
- 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(演習問題)1.を解いてみる。
1.
HTML、PHPのソースコード(BBEdit)
<?php
function p($people, $str){
print '<table border=1 width=400>';
print '<caption>2000年のアメリカの10大都市の人口規模(' . $str . ')</caption>';
print '<tr align=center ><th>都市</th><th>人口</th></tr>';
$total = 0;
foreach ($people as $key => $value) {
print '<tr><td align=left>' . $key . '</td><td align=right >' . $value . '</td></tr>';
$total += $value;
}
print '<tr><th align=center>合計</th><th align=right>' . $total . '</th></tr>';
print '</table>';
}
$people = array("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);
asort($people);
p($people, "人口順");
print '<br />';
ksort($people);
p($people, "都市名順");
?>
ちなみにJavaScriptの場合。
コード(BBEdit)
function p(people, str){
var result = '<table border=1 width=400>' +
'<caption>2000年のアメリカの10大都市の人口規模(' + str + ')</caption>' +
'<tr align=center ><th>都市</th>' +
'<th>人口</th></tr>';
var total = 0;
var i,
max;
for(i = 0, max = people.length; i < max; i += 1){
result += '<tr><td align=left>' + people[i][0] +
'</td><td align=right >' + people[i][1] +
'</td></tr>';
total += people[i][1];
}
result += '<tr><th align=center>合計</th><th align=right>' +
total + '</th></tr></table>';
return result;
}
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]
];
people.sort(function(a, b){
return a[1] - b[1];
});
var result = p(people, "人口順") + "<br />";
people.sort(function(a, b){
return ( a[0] < b[0]) ? -1 : 1;
});
result += p(people, "都市名順");
$('#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)]
def f(people, s):
print(s.center(30))
print("city".center(20), "people".center(10), sep="")
for c, p in people:
print(c.ljust(20), str(p).rjust(10), sep="")
people.sort(key=lambda x: x[1])
f(people, "people")
print()
people.sort(key=lambda x: x[0])
f(people, "city")
入出力結果(Terminal)
$ ./sample.py
people
city people
Detroit, MI 951270
San Antonio, TX 1144646
Dallas, TX 1188580
San Diego, CA 1223400
Phoenix, AZ 1321045
Philadelphia, PA 1517550
Houston, TX 1953631
Chicago, IL 2896016
Los Angeles, CA 3694820
New York, NY 8008278
city
city people
Chicago, IL 2896016
Dallas, TX 1188580
Detroit, MI 951270
Houston, TX 1953631
Los Angeles, CA 3694820
New York, NY 8008278
Philadelphia, PA 1517550
Phoenix, AZ 1321045
San Antonio, TX 1144646
San Diego, CA 1223400
$
0 コメント:
コメントを投稿