Hatena::ブログ(Diary)

グレーゲーム

2009-03-04

[]SRM431_Div2_Hard

長さnのリスト[x1, x2, ... , xn](要素は0より大きい)があったとき、全ての要素の和をS、全ての要素の積をPとする。正の整数SとPが与えられたときに可能な限り最小のnを返せ。不可能な場合-1を返せ。という感じの問題文の最後に”リストには整数じゃない値も入ってるかもよ(笑)”みたいな事が書かれてて大変困った。


例えば足して5、掛けて1になるリストだと、整数だけなら[1, 1, 1, 1, 1]ってなるけど整数以外もありなら[0.5, 0.5, 4]とかもいける。色々悩んだ末に答え見たら超シンプルなコードだった。整数以外という制約はむしろヒントだったんじゃないかと思える。

public int smallestSet(int S, int P){
  if(S == P) return 1;
  for(int i=2; i<=S; i++)
    if(S/(double)i >= Math.pow(P, 1.0/i)) return i;
  return -1;
}

要するにnを順に増やしていって、リストは掛けたらPになるような値(Pの1/n乗)で埋めてS/nがその値を超えた時点で最小という話。

[]こんにちは世界に一苦労

Pythonの3.0を試してみたら日本語表示でドモった。エンコーディングutf-8hoge.txtというファイルの中身を表示させてみる。環境はMacOSX(10.4.11)とPython3.0.1。


とりあえずやってみたら

>>> fp = open("hoge.txt", "r")
>>> hoge = fp.read()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.0/lib/python3.0/io.py", line 1728, in read
    decoder.decode(self.buffer.read(), final=True))
  File "/Library/Frameworks/Python.framework/Versions/3.0/lib/python3.0/io.py", line 1299, in decode
    output = self.decoder.decode(input, final=final)
UnicodeDecodeError: 'shift_jis' codec can't decode bytes in position 8-9: illegal multibyte sequence
>>>

どうもデフォルトエンコーディングshift_jisになってるみたい。

なんで、引数で指定してみる。

>>> fp = open("hoge.txt", "r", encoding="utf-8")
>>> hoge = fp.read()
>>> type(hoge)
<class 'str'>

読み込めた。

typeはstrと表示されているけどこのstrは2系で言うunicodeらしい。

>>> print(hoge)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.0/lib/python3.0/io.py", line 1494, in write
    b = encoder.encode(s)
  File "/Library/Frameworks/Python.framework/Versions/3.0/lib/python3.0/encodings/ascii.py", line 22, in encode
    return codecs.ascii_encode(input, self.errors)[0]
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)
>>> 

表示しようとするとエラー。

標準出力エンコードがイケてないみたいなので変える。

>>> import sys
>>> import codecs
>>> sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
>>> print(hoge)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.0/lib/python3.0/codecs.py", line 356, in write
    self.stream.write(data)
  File "/Library/Frameworks/Python.framework/Versions/3.0/lib/python3.0/io.py", line 1487, in write
    s.__class__.__name__)
TypeError: can't write bytes to text stream

見慣れないエラー。

そもそもhogeってバイトだっけと思って確認しようとすると

>>> type(hoge)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.0/lib/python3.0/codecs.py", line 356, in write
    self.stream.write(data)
  File "/Library/Frameworks/Python.framework/Versions/3.0/lib/python3.0/io.py", line 1487, in write
    s.__class__.__name__)
TypeError: can't write bytes to text stream

エラー。この辺から混乱。

さっきのsys.stdoutをいじろうとしたのがいけなかったらしく、

>>> print("hoge")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.0/lib/python3.0/codecs.py", line 356, in write
    self.stream.write(data)
  File "/Library/Frameworks/Python.framework/Versions/3.0/lib/python3.0/io.py", line 1487, in write
    s.__class__.__name__)
TypeError: can't write bytes to text stream
>>>

何も出力できなくなった。

色々試してみた結果どうも

>>> import sys
>>> sys.getdefaultencoding()
'utf-8'
>>> sys.stdout.encoding
'US-ASCII'

この辺が問題らしい。システムのデフォルトエンコーディングutf-8なのに標準出力ascii。これは環境変数で変えれた。

LANG=ja_JP.UTF-8
export LANG

これでデフォルトが変わる。

>>> import sys
>>> sys.getdefaultencoding()
'utf-8'
>>> sys.stdout.encoding
'UTF-8'

ではいよいよ文字を出力

>>> fp = open("hoge.txt", "r", encoding="utf-8")
>>> hoge = fp.read()
>>> fp.close()
>>> print(hoge)
'あいうえお'

めでたしめでたし。


参考