0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Hackerrankで使える、listの要素を横並びで抽出する方法

0
Posted at

未来電子テクノロジーでインターンをしている<小栗>です。
「プログラミング初心者であるため、内容に誤りがあるかもしれません。
もし、誤りがあれば修正するのでどんどん指摘してください。

はじめに

Hackerrank(Algorithm,Data structures)に取り組むにあたり、初心者の僕にはデフォルトのコードが理解できないということに苦しみました。

関数をつくってreturnで返せばいいだけならいいですが、listの要素だけをprintするときに役立った方法を紹介します。

listをそのまま出力

以下はコードの一部なのでわかりにくいかもしれませんが、leftRotationという変数にはlistが返された関数を代入しています。

これをこのまま

print(leftRotation)

とすると、

[5, 1, 2, 3, 4]

このように出力されてしまいます。

しかしここで、


for ans in leftRotation:
        print(str(ans),end=' ')

こうするだけで、

5 1 2 3 4

このように要素のみの出力に成功します。

もととなる考え

・print関数のエンドオプション
https://qiita.com/tortuepin/items/4fbf29e42f05cb4b02a5

・イテレーターの考え

以上を勉強すると理解できました。
iterは奥が深いみたいなので 、まだ学んでいきます。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?