A. Petya and Strings

A. Petya and Strings

CodeForces の問題を解いた。

次のように書いた:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# http://codeforces.com/problemset/problem/112/A

def compareString(s1, s2):
    if  (s1 == s2): return 0
    elif (s1 < s2): return -1
    else:           return 1

s1 = raw_input().lower()
s2 = raw_input().lower()

print compareString(s1, s2)

この問題でも Python 標準関数の知らなさを発揮して compareString 関数などというものを自前でこしらえてしまったけれど、 Python 標準の cmp 関数を使えばこの関数相当のことは処理してくれるみたい。

A. cAPS lOCK

A. cAPS lOCK

CodeForces の問題を解いた。

次のように書いた:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# http://codeforces.com/problemset/problem/131/A

def isMisLock(word):
    if word[0].islower():
        if (1 == len(word)): return True
        return word[1:].isupper()
    else:
        return word.isupper()

    return False

def reverse(word):
    def reverseChar(char):
        if char.islower():
            return char.upper()
        else:
            return char.lower()
    return "".join(map(reverseChar, word))

word = raw_input()
if isMisLock(word):
    print reverse(word)
else:
    print word

他の人がやたらと短いコードを書いているので眺めてみたら、istitle() とか swapcase() とかいう関数を使ってコードを書いていた。ちなみに istitle() は引数に与えられた文字列に現れる一つ以上の語が全て、"大文字で始まり小文字がそれに続く" 形式のときに True が返る関数である。

そんなの知らないよ...。

A. tram

A. tram

CodeForces の問題を解いた。

ぼくは次のように書いた:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# http://codeforces.com/problemset/problem/116/A

import sys

limit = remain = 0
lineNum = int(sys.stdin.readline())
for i in range(0, lineNum):
    a, b = sys.stdin.readline().strip().split()
    t = remain - int(a) + int(b)
    remain = t
    if (limit < t): limit = t

print limit

他の人が書いたコードを読むと、標準入力からの読み込みが:

b , a = map(int, raw_input().split())

...のように書かれているものが多くて、整数として使う入力は読み込み時に map でまとめて整数にするものなのだと学んだ。

Adium のチャットログ

Adium のチャットログのパス

職場で Google Talk を使うことがあるのだけれど、Google Talk を生で使っているわけではなくて Adium を経由してメッセージのやり取りをしている。同僚とのやり取りを確認するため、Adium のチャットログを漁りたかったのだけれど、デフォルトでは ~/Library/Application Support/Adium 2.0/Users/Default/Logs に Adium のチャットログは保存されているものらしい。

参考

A. Epic Game

A. Epic Game

さらにもう一問、 CodeForces の問題を解いた。今のところ、13,666人中9,910番目。今日始めたばかりなので、アテにはならないけれどね。

自分がサブミットしたコードは確認できるけれど、他人がサブミットしたコードは読めないみたい。少なくとも PROBLEMSET の問題に関しては。他人がどう書いているのか読みたいな。

追記

「他人のコードは読めないみたい」は勘違いで、ちゃんと他人のコードを読むことができるみたい。僕が数行書いた問題を他人はワンライナーで書いていたりしていて、レベルの差を感じた。まだまだ努力する余地がある。

さらに追記

自分が解いていない問題であっても他人の解答(コード)を読むことができるっぽい。ここまで望んではいないけれど...。でも、その気になれば青天井に学習できる環境だと言うこともできるか。

A. Way Too Long Words

A. Way Too Long Words

プログラミングコンテストを定期的に開催している CodeForces に登録して、公開されている問題の一つを解いてみた。

競技プログラミングだからソースは公開しない方がいいのかな。いずれにせよ FizzBuzz より少し難しいかな...という程度のレベルの問題なので、本職(?)の競技プログラマにしてみれば遊びのようなものかもしれない。

CodeForces のブログ

CodeForces にブログ機能が付いていることに気づき、とりあえず "Hello, World!" とだけ書いてみたところ、マイナスのレーティングを頂戴することとなった。どうやらプログラミングに関わらない記事を書くとノイズと見做され、マイナスの評価をもらうことになるらしい。気をつけないと。

SICP Exercise 3.25

問題文

Generalizing one- and two-dimensional tables, show how to implement a table in which values are stored under an arbitrary number of keys and different values may be stored under different numbers of keys. The lookup and insert! procedures should take as input a list of keys used to access the table.

解答案

リストそのものをキーとしてしまえば、前の問題(Exercise 3.24)と同じになるのでは?

(define (make-table same-key?)
  (let ((table (list '*table*)))
    (define (assoc key records)
      (cond ((null? records) #f)
            ((same-key? key (caar records)) (car records))
            (else (assoc key (cdr records)))))
    (define (lookup key)
      (let ((record (assoc key (cdr table))))
        (if record
            (cdr record)
            #f)))
    (define (insert! key value)
      (let ((record (assoc key (cdr table))))
        (if record
            (set-cdr! record value)
            (set-cdr! table
                      (cons (cons key value) (cdr table)))))
      'ok)
    (define (print-table)
      (define (iter data)
        (if (null? data)
            (newline)
            (begin
              (display (cdar data))
              (display " ")
              (iter (cdr data)))))
      (iter (cdr table)))
    (define (dispatch m)
      (cond ((eq? m 'print-table) print-table)
            ((eq? m 'lookup) lookup)
            ((eq? m 'insert!) insert!)
            (else (error "No method: " m))))
    dispatch))

(define table (make-table equal?))
((table 'insert!) '(1 2 3) "google")
((table 'insert!) '(3 2 1) "yahoo")
((table 'print-table)) ; 文字列としてテーブルの中身をダンプ
((table 'lookup) '(1 2 3)) ;=> "google"
((table 'lookup) '(3 2 1)) ;=> "yahoo"