Skip to content

Instantly share code, notes, and snippets.

@bgnori
Created May 2, 2012 04:56
Show Gist options
  • Save bgnori/2573845 to your computer and use it in GitHub Desktop.
Save bgnori/2573845 to your computer and use it in GitHub Desktop.
def shortest(*rs):
i = 0;
while True:
for r in rs:
try:
x = r[i]
except IndexError:
return r
i += 1
pass
print shortest([1, 2], [3, 4, 5], [6, 7])
@bgnori
Copy link
Author

bgnori commented May 2, 2012

ujihisa

[問] 配列の配列から、一番要素の短いものを取得する関数shortestを書け

候補が複数ある場合は、そのうちどれを返しても正解とする。

shortest([1, 2], [3, 4, 5], [6, 7]) == [1, 2]

ただし、引数として無限長の配列が与えられる場合もある。

少なくともひとつの有限長の配列があるときに計算が終了するようせよ。

s/配列/リスト/g

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