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で出会った、Counter,swapcaseについて

0
Posted at

未来電子テクノロジーでプログラミングの勉強をしている竹内です。
今回はHackerRankを解いているうちに出会ったCounterについて書いていこうと思います。

Counterとは

CounterはPython標準ライブラリcollectionsのクラスです。
機能としてはリストの各要素の出現回数を返してくれます。
実際に試してみましょう!

from collections import Counter
lis=[2,3,4,5,6,8,7,6,5,18]
print(Counter(lis))
# Counter({5: 2, 6: 2, 2: 1, 3: 1, 4: 1, 8: 1, 7: 1, 18: 1})

実際にやってみると上記のような感じになります。
HackerRankではCounterを使用して靴屋さんの売り上げを求めるプログラムを作りました。

swapcaseとは

swapcaseでは指定した文字列の小文字を大文字に大文字を小文字にすることができます。
こちらもpythonの問題にあり、めちゃくちゃ簡単でした。
では、実際に使ってみましょう

s="Hacker Rank"
print(s)
s=s.swapcase()
print(s)
# Hacker Rank
# hACKER rANK

上記のように動作しました。
こんな感じで、大文字は小文字に。小文字は大文字になります。

まとめ

今回はHackerRankで出会ったものの紹介をしました。
HackerRankではこんな感じで、自分の知らないものを知ることができ、なおかつそれを使用して問題を解かなければいけないので、英語ができる方にはおすすめです。
最後まで読んでいただきありがとうございます。

0
0
0

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?