Skip to content

Instantly share code, notes, and snippets.

@xatier
Last active May 6, 2019 15:51
Show Gist options
  • Save xatier/10795843 to your computer and use it in GitHub Desktop.
Save xatier/10795843 to your computer and use it in GitHub Desktop.
My midterm problems NCTU NA 2014

2014 NA midterm Perl part

By TA darkx

  • 是非 1 for each, total 10 points
  • 選擇 2 for each, total 6 points
  • 簡答 3 for each, total 33 points
  • bonus 好多分

  • 部份題目由 pmli 學長篩選掉,並重新配分過
  • 答案僅供參考,如有任何疑問歡迎來向助教詢問
  • Page 為講義參考頁數,問問題之前請先閱讀該頁
  • 上課講義

  • 是非

  • perl 在 FreeBSD 下的安裝路徑是 /usr/port/lang/perl5.1x

  • Ans: X, ports XD

  • page 3


  • 是非

  • CPAN 是 Perl community 大家各自貢獻各式各樣 library 作品的地方

  • Ans: O

  • page 4, 43


  • 是非
  • 以下 Regex 可以拿來作為辨別 IPv4 IP address 的過濾器使用。
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
  • Ans: X, 雖然 digits 對了,但是 IP 有 0~255 的 range 限制XD
  • page 35, 36

  • 是非

  • Perl 的 hash table 不能取 slice,只能透過 keys 等 method 來操作

  • Ans: X, hash 也可以取 slice

  • page 14


  • 是非

  • Perl 中,可以使用 0b011110 的方式表達 literal binary numbers

  • Ans: O

  • page 11


  • 是非

  • Perl 中執行 (@onetwothree, $four) = (1, 2, 3, 4);@onetwothree = (1,2,3)$four = 4

  • Ans: X, swallow!

  • page 12


  • 是非

  • Perl 中 (10 .. 1) 可以表示構造一個 (10, 9, 8, 7, 6, 5, 4, 3, 2, 1) 的 list

  • Ans: X, 要用 reverse

  • page 12


  • 是非

  • Perl 中,大量的預設變數可使 system administraion script 撰寫變得簡單,但也可能影響可讀性,需謹慎使用。

  • Ans: O

  • page 17


  • 是非

  • Perl 和 Python 中,表達 else if 的邏輯語法分別是 elifelsif

  • Ans: X, 反了

  • page 20


  • 是非

  • Perl if-else 或是 while block 的大括號在只有一行的情況下是可省略的

  • Ans: X, 不可省略

  • page 20


  • 單選

  • 下列何者是 Perl 的 programming motto

      1. TINTOWTDI
      1. TIMTOWTDI
      1. WYSIWYG
      1. NPNT
  • Ans: 2) TIMTOWTDI There's more than one way to do it.

  • page 1


  • 單選

  • 下列何者是 Perl Foundation 的 官方 代表圖騰

      1. 單峰駱駝
      1. 雙峰駱駝
      1. 洋蔥
      1. 羊駝
      1. 草泥馬
  • Ans: 3) 洋蔥 (Onion 是 Perl Foundation 的圖騰,駱駝/羊駝是 O'reilly 的書上的 logo)

  • any page at perldoc.perl.org


  • 多選

  • 以下何者為 Perl 的 module management tool?

      1. cpan
      1. cpanplus
      1. cpanminus
      1. cpantimes
  • Ans: 1) 2) 3)

  • page 43


  • 簡答

  • 以下是天才程式設計師 神代フラウ 寫的 Perl script ,但是其中含有 compilation error ,請寫出三個可能的原因和其解法

  • Ans:

    1. 可能是 perl 程式沒有正確安裝,請依照使用系統的 perl 安裝說明安裝之
    2. 可能是 perl 執行檔路徑不在 /usr/bin/perl ,改用 #!/usr/bin/env perl 作為 she-bang
    3. 沒有使用 perl 5.14 或以上的版本,加上 use 5.014;
    4. 系統沒有 perl 5.10 以上的版本,使用 print 函式代替 say
#!/usr/bin/perl

say "Hello, world!";
  • page 2, 3

  • 簡答

  • 以下是天才程式設計師 神代フラウ 寫的 Perl script ,請寫出其輸出

  • Ans: 讓晨曦照亮民主 (字串比大小要使用 cmp operator)

#!/usr/bin/perl

use 5.014;

if ('Z' > 'B') {
    say "力大魚斃";
}
else {
    say "讓晨曦照亮民主";
}
  • page 15

  • 簡答

  • Perl 和 perl 有何區別?

  • Ans: Perl 是指語言本身,perl 是指 interpreter

  • page 3


  • 簡答
  • 公安九課的石川先生在十年前寫的 Perl 程式不知道為什麼拿來重跑後不會動了,你能幫他找出原因嗎?
#!/usr/bin/perl

use strict;

$name = "Ishikawa";
print "I'm $name";
print <<END;


     _/_/_/  _/                              _/          _/
  _/        _/_/_/      _/_/      _/_/_/  _/_/_/_/          _/_/_/
 _/  _/_/  _/    _/  _/    _/  _/_/        _/          _/  _/    _/
_/    _/  _/    _/  _/    _/      _/_/    _/          _/  _/    _/
 _/_/_/  _/    _/    _/_/    _/_/_/        _/_/      _/  _/    _/



   _/      _/                                _/                  _/  _/
_/_/_/_/  _/_/_/      _/_/          _/_/_/  _/_/_/      _/_/    _/  _/
 _/      _/    _/  _/_/_/_/      _/_/      _/    _/  _/_/_/_/  _/  _/
_/      _/    _/  _/                _/_/  _/    _/  _/        _/  _/
 _/_/  _/    _/    _/_/_/      _/_/_/    _/    _/    _/_/_/  _/  _/


END
  • Ans: 因為沒有加上 my XD
  • page 5

  • 簡答

  • 請寫出 Perl 的三種資料資料型別?各為何種用途?

  • Ans: 送分題,講義 page 6

A scalar is a
    - single string (of any size, limited only by the available memory)
    - number
    - or a reference to something

Arrays are
    - ordered lists of scalars indexed by number
    - starting with 0

Hashes are
    - unordered collections of scalar values
    - indexed by their associated string key
A Scalar is  ________,  ________ or  ________.
Arrays are ________, ________.
Hashes are ________, ________.

  • 簡答

  • 請問 Perl 程式語言中,胖逗號 (fat comma) 與太空船 (spaceship) 語法以及其用途。

  • Ans: => operator & <=> operator 用於 hash 初始化 / 數字比大小

  • page 13, <=> 上課有特別講


  • 簡答

  • Perl 是一個看 context 的語言,請簡單舉例比較 list context 和 scalar context 的異同,並舉出一個可以同時支援兩種 context 用法的 builtin function

  • Ans: chop function 可以接 list 也可以接 scalar

  • page 9


  • 簡答

  • Perl quoted string 的語法中,單引號 ' 和雙引號 " 有何不同?試舉例說明之

  • Ans: double quoted string 會有 interpolation ,single quoted string 沒有

my $foo = "foo";
say "$foo";       # foo
say '$foo';       # $foo
  • page 11

  • 簡答

  • Perl Numeric literals 的語法中,底線 _ 的用途是?

  • Ans: 作為區分位數用,例如 4_294_967_296

  • page 11


  • 簡答

  • There's more than one way to do it,請寫出三種 清空整個 hash 的方法

  • Ans:

%hash = ();
undef %hash;
%hash = undef;
  • page 16

  • 簡答
  • 以下是天才程式設計師 神代フラウ 寫的 Perl one-liner ,請 盡量詳盡 解釋其工作原理
print while <>;
  • page 23

  • 簡答
  • Perl 以下兩種 backquote ( `) 語法有何區別?
my $output = `some command`;
my @output = `some command`;
  • Ans: 前者是直接抓取所有輸出,後者是以 newline 切開
  • page 27

  • bonus (5 points)
  • 限制為程式大小為 25 (含)字元內,請寫出一個 Perl 版本的 grep
    1. regex 部分以 /re/ 表示即可,不記入程式大小
    2. 可以使用 perlrun tricks
    3. 程式本體大小 <= 15 可額外獲得 5 points bonus
    4. 你的程式必須可以透過以下方式執行並達成同樣效果
cat file | perl -E '<your program>'

cat file | grep 're'
  • Ans:
while(<>){print if /re/}

perl -ne 'print if /re/'
  • page 27

  • bonus (10 points)

  • JAPH

  • Ans: JAPH 是 Perl 文化中很重要的一環,希望以加分題的方式鼓勵同學寫出有創意的答案

Just another Perl hacker, or JAPH, typically refers to a Perl program which prints "Just another Perl hacker," (the comma is canonical but is occasionally omitted). Short JAPH programs are often used as signatures in online forums, or as T-shirt designs. The phrase or acronym is also occasionally used (without code) for a signature. JAPH programs are classically done using extremely obfuscated methods, in the spirit of the Obfuscated C Contest. More recently, as the phenomenon has become so well known, the phrase is sometimes used in ordinary examples (without obfuscation).

  • Please write your own JAPH program, bonus will be up to 10 points with your high creativity, happy hacking!

  • Here're some examples:

$_="krJhruaesrltre c a cnP,ohet";$_.=$1,print$2while s/(..)(.)//;
''=~('(?{'.('-)@.)@_*([]@!@/)(@)@-@),@(@@+@)'
^'][)@]`}`]()`@.@]@%[`}%[@`@!#@%[').',"})')
#!/usr/local/bin/perl
  
undef$/;$_=<DATA>;y/ODA\n / /ds;@yoda=map{length}split;print chr
oct join('',splice(@yoda,0,3))-111 while@yoda;
__DATA__
         00O00O000O00O0000      000O         DD000000O0
        0DO0000000O0000O00     O00000        00O00000O0O
        0000      0O0         O00  O00       00D     0DO
         00O0     0O0        00D    000      DO0D00000D
          0O00    DOD       000000O00000     000  O00O
 DD0000D000O0     000      0O00O0000D00DO    0OD   D00O000D00O0
 00000DO00O0      000     000O        00D0   O0D    O00000O0DO0
  
 0O000   OD0D   O00O0   0000         DDDO000000      O00O000000
  0O000 O00DDO 00000   0O0D00        00O0O00000O    0O00O000000
   0O0O00OD00000DDD   00O  0D0       DDD     D0O    00O0D
    00000O00000000   O00    DO0      D00D00O000      00D00
     D0O00  O0000   000O000O00DO     000  00O0        0OD00
      O00    000   0O000D000O00O0    000   0D0O000000O00O00
       0      0   0O0D        0000   0O0    0O0000000O000O

  • bonus (20%)

  • Please write down the output of the following script.

  • Ans: I love CSCC

  • 出處:ppencode

  • 其實拆開來看就是一連串的 print chr ord (some quoted char) 慢慢按照順序拼出即可

#!/usr/bin/perl
length q caller exec and print chr oct ord q pop and print chr ord q q q and print chr ord qw q lc q and print chr ord q local and print chr ord q eval ne and print chr ord q ne sin and print chr ord qq q q and print chr ord uc q lc eval and print chr ord uc q msgctl m and print chr ord uc q oct do and print chr ord uc q oct no 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment