2017/07/27 ~ 2017/07/30

centos6にglibc2.14をビルド

何かと必要になるアイツ
centos6.5のglibcは2.12

configureのときにlinuxbrewで新しいgccをインストールしていた場合はコケることになる.
システムのgccを使うようにしてやればよい

wget http://ftp.gnu.org/gnu/glibc/glibc-2.14.tar.gz
tar xvf glib-2.14.tar.gz
mkdir build
cd build
CC=/usr/bin/gcc ../glibc-2.14/configure --prefix=$HOME/.local
Python3系ではkey in dict.keys()をすればよい

dictionary.keys()ってリストを返すと思っていた.
リストに対するin演算子は線形探索をするだろうからめちゃくちゃ遅そう.
KeyErrorを引っ掛ける実装をしたほうが良いのでは?と思ってずっとそのようにしていた.

key = 3334
dict = {i: 1 for i in range(100000)}
try:
    dict[key]
    print('found')
except KeyError:
    print('not found')

だが,調べてみたところPython3系ではdictionary.keys()はdict_keysというビューオブジェクト(イテレータ)を返す.
Pythonのドキュメントには以下のようにあった.

Calling d.keys() will return a dictionary view object. It supports operations like membership test and iteration, but its contents are not independent of the original dictionary – it is only a view.

5. Data Structures — Python 3.6.2 documentation

そしてこのオブジェクトに対するin演算子は定数時間で実行可能なようだ.

というわけで,Python3系を使っているなら安心して

key in dict.keys()

を使いましょう.

  • Python2系,dictionary.keys()がリストを返していることがわかる
In [3]: type(dic.keys())
Out[3]: list

In [4]: dic = {i: 1 for i in range(100000000)}

In [5]: %time 3333334 in dic.keys()
CPU times: user 1.34 s, sys: 160 ms, total: 1.5 s
Wall time: 1.5 s
Out[5]: True

In [6]: type(dic.keys())
Out[6]: list

In [7]:
  • Python3系
In [1]: dic = {i: 1 for i in range(100000000)}

In [2]: %time 3333334 in dic.keys()
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 11.9 µs
Out[2]: True

In [3]: %time 0 in dic.keys()
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 10.7 µs
Out[3]: True

In [4]: %time 11111120 in dic.keys()
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 11.7 µs
Out[4]: True

In [5]: %time 1111112343220 in dic.keys()
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 10.3 µs
Out[5]: False

In [6]: type(dic.keys())
Out[6]: dict_keys

In [7]:
pandas.read_csvのときに特定のラベルのデータの型を指定したい

read_csvの引数としてdtype=dictionaryを与えてやればよい.
たとえば以下のようなcsvファイルがあったとする.(test.csv

label, precision
+1,0.8
-1,0.7

このとき,ラベルのカラムのデータは数値というよりは文字列である.
pandasは+1も数値としてパースして1にしてしまうので,以下のようにしてやる.

pandas.read_csv('test.csv', dtype={'label': str}

pandas.read_csv — pandas 0.20.3 documentation

tmuxのタイトルにssh接続してるホスト名を表示させる

たくさんのサーバにsshする場合,tmuxのstatusが"ssh"で埋まってしまう.

f:id:himkt:20170730173747p:plain

以下のスニペットによってstatusにhost名を表示させることができる.

ssh() {
    if [ "$(ps -p $(ps -p $$ -o ppid=) -o comm=)" = "tmux" ]; then
        tmux rename-window "$(echo $* | cut -d . -f 1)"
        command ssh "$@"
        tmux set-window-option automatic-rename "on" 1>/dev/null
    else
        command ssh "$@"
    fi
}

こうなる.
f:id:himkt:20170730174126p:plain

Set tmux pane title on SSH connections – blog.no-panic.at

When Can We Meet?

When Can We Meet? | Aizu Online Judge

n人のミーティングに参加可能な日程が与えられ,もっとも参加人数の多い日を求める問題.
std::mapを使って簡単に解ける.最後に制約条件qを満たすかどうかを確認するのを忘れないように注意.

//#define _GRIBCXX_DEBUG
#include <bits/stdc++.h>
# define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

int main() {
  int n, q, m, d;
  map<int, int>::iterator it;

  while (cin >> n >> q, n) {
    map<int, int> freq;
    rep (i, n) {

      cin >> m;
      rep (k, m) {
        cin >> d;

        it = freq.find(d);
        if (it == freq.end()) {
          freq[d] = 1;
        }
        else {
          freq[d]++;
        }
      }
    }

    int max_d = -1;
    int max_v = 0;

    for (pair<int, int> p : freq) {
      if (max_v < p.second) {
        max_d = p.first;
        max_v = p.second;
      }
    }

    if (max_v >= q) {
      cout << max_d << endl;
    }
    else {
      cout << 0 << endl;
    }
  }
  return 0;
}