Skip to content

Instantly share code, notes, and snippets.

@nishimotz
Last active June 29, 2017 04:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nishimotz/3c047c61dba03baf4c59c6b4afca871f to your computer and use it in GitHub Desktop.
Save nishimotz/3c047c61dba03baf4c59c6b4afca871f to your computer and use it in GitHub Desktop.
170628-great-h-python 「あれ」の例題をやってみた

Python で「あれ」の例題をやってみた

https://great-h.connpass.com/event/60306/

https://www.disco.co.jp/procon/

セットアップ

特に必要ないけど Windows + Python 3.6.1 + venv

> py
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

> py -m venv env

> env\Scripts\activate

(env) >

Q1

課題

使用されているアルファベット 上位5つを除くと現れるメッセージは?

前提 テキストファイル Q1.txt は UTF-8 で、内容は

//	以下の文字列を使用して問題を解いてください。
//	データ型は任意
//	mainのローカル変数として定義してください。
{
"L...(omitted)...X"
};

のようになっている。

a1.py についてのコメント

  • dict.items() は辞書のキーと値の組の「ビューオブジェクト」を返す。すべてのキーを列挙することなどができる。ビューオブジェクトは辞書をコピーせず辞書のアイテムをそのまま参照する(Python 文法詳解 p.158 あたり)
src = None
with open('Q1/Q1.txt', encoding='utf-8') as f:
for s in f.readlines():
if s and s[0] == '"':
src = s.strip('"\n')
break
print(src)
#counts = {}
#for c in src:
# if c in counts:
# counts[c] += 1
# else:
# counts[c] = 1
#print(repr(counts))
# {'L': 76, 'R': 5, 'V': 73, 'E': 16, ...}
#cv = counts.items()
#print(repr(cv))
# dict_items([('L', 76), ('R', 5), ('V', 73), ('E', 16), ...])
#sc = sorted(cv, key=lambda x:x[1], reverse=True)
#print(repr(sc))
# [('Q', 87), ('X', 85), ...]
#skip_chars = [i[0] for i in sc[:5]]
# print(repr(skip_chars))
# ['Q', 'X', ...]
#answer_chars = [c for c in src if c not in skip_chars]
#print(repr(answer_chars))
#print(''.join(answer_chars))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment