開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- Script言語:Perl
『初めてのPerl 第6版』(Randal L. Schwartz, Tom Phoenix, brian d foy 共著、近藤 嘉雪 訳、オライリー・ジャパン、2012年、ISBN978-4-87311-567-2) の14章(文字列処理とソート)、14.5(練習問題)1を解いてみる。
その他参考書籍
1.
コード(BBEdit)
sample.pl
#!/usr/bin/env perl use strict; use warnings; use utf8; use 5.016; binmode STDIN, ':utf8'; binmode STDOUT, ':utf8'; my @numbers = (17, 1000, 04, 1.50, 3.14159, -10, 1.5, 4, 2001, 90210, 666); for(sort{$a <=> $b} @numbers){ printf "%10g\n", $_; }
入出力結果(Terminal)
$ ./sample.pl -10 1.5 1.5 3.14159 4 4 17 666 1000 2001 90210 $
ちなみにJavaScriptの場合。
コード(BBEdit)
var result = ""; String.prototype.rjust = function(n){ var result = ""; n -= this.length; for(var i = 0; i < n; i++){ result += " "; } result += this; return result; }; var numbers = [17, 1000, 04, 1.50, 3.14159, -10, 1.5, 4, 2001, 90210, 666]; numbers.sort(function(a, b){ return a- b; }); for(var i = 0; i < numbers.length; i++){ result += numbers[i].toString().rjust(20) + "\n"; } $('#pre0').text(result);
pythonの場合。
sample.py
コード(BBEdit)
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- numbers = [17, 1000, int("04"), 1.50, 3.14159, -10, 1.5, 4, 2001, 90210, 666] numbers.sort() for x in numbers: print("{0: 20}".format(x))
入出力結果(Terminal)
$ ./sample.py -10 1.5 1.5 3.14159 4 4 17 666 1000 2001 90210 $
0 コメント:
コメントを投稿