Upgrade to Pro — share decks privately, control downloads, hide ads and more …

競技プログラミングサイトでPythonを学ぶ.pdf

ksnt
December 25, 2019

 競技プログラミングサイトでPythonを学ぶ.pdf

ksnt

December 25, 2019
Tweet

More Decks by ksnt

Other Decks in Programming

Transcript

  1. 競技プログラミングプログラミングサイトでで
    Pythonを学ぶ学ぶぶ
    By ksnt

    View Slide

  2. 代表的な競技プログラな競技プログラミ競技プログラミングプログラミングサイトで
    (私見)

    AtCoder

    Topcoder

    Codeforces

    LeetCode (面接対策?)
    └ 過去問
    └ mock (模擬試験?)
    └ コンテストで

    HackerRank (面接対策?)

    ProjectEuler (数学ぶ)

    Kaggle (データサイエンス)
    ※黄色くマーカーを引くマーカーを引いマーカーを学ぶ引いたものはやっいたものはやったことがある
    ※日本人ははAtCoderを学ぶやっている人はを学ぶよくマーカーを引い見かける
    参考) コーディング面接対策のために解きたい解きたいきたいLeetCode 60問
    https://1kohei1.com/leetcode/

    View Slide

  3. 競技プログラミングプログラミングとは
    参加者全員に同一の課題がに解きたい同一の課題が出題さの課題が出題され、よが出題が出題され、よされ、より早く早くくマーカーを引い
    与えられた要求をえられた要求を満足するプロを学ぶ満足するプログラムするプログラムを正確にを学ぶ正確にに解きたい
    記述することを競うすることを学ぶ競う。(Wikipedia)

    View Slide

  4. な競技プログラミぜ競技プログラミ競技プログラミングプログラミング
    を学ぶやるのか?(私見)
    1. 名誉のためのため(レッドコーダー,グランドマスターetc)
    2. コーディング面接対策
    3. プログラミング学ぶ習
    4. 楽しいからしいから(楽しいからしくマーカーを引いプログラミング、ゲームを正確に感覚)

    View Slide

  5. どのプラットでフォームを正確にでやり早くはじめ
    ればいいの?
    ● 数学ぶが趣味の人 の人は => ProjectEuler
    ● データサイエンスが好き き => Kaggle
    ● 面接対策
    企業面接の過去問が解きたいきたい => LeetCode
    過去問でな競技プログラミくマーカーを引いてもよい => HackerRank
    ● 楽しいからしそう => どれでもOK!

    View Slide

  6. LeetCodeの見た目(1)

    View Slide

  7. LeetCodeの見た目(2)

    View Slide

  8. HackerRankの見た目

    View Slide

  9. 第1問

    View Slide

  10. View Slide

  11. 解きたい答
    if __name__ == '__main__':
    n = int(input())
    print(*range(1,n+1),sep='')

    View Slide

  12. 第2問

    View Slide

  13. 問題が出題され、よ
    Given a fixed length array arr of integers,
    duplicate each occurrence of zero, shifting
    the remaining elements to the right.
    Note that elements beyond the length of
    the original array are not written.
    Do the above modifications to the input
    array in place, do not return anything from
    your function.
    class Solution:
    def duplicateZeros(self, arr: List[int]) -> None:
    """
    Do not return anything, modify arr in-place instead.
    """

    View Slide

  14. 解きたい答

    View Slide

  15. 休憩

    View Slide

  16. 最終問題が出題され、よ

    View Slide

  17. LRUキャッシュの実装の実装
    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.
    get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
    put(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the
    least recently used item before inserting a new item.
    The cache is initialized with a positive capacity.
    Follow up:
    Could you do both operations in O(1) time complexity?
    class LRUCache:
    def __init__(self, capacity: int)
    def get(self, key: int) -> int::
    def put(self, key: int, value: int) -> None:
    Example:
    LRUCache cache = new LRUCache( 2 /* capacity */ );
    cache.put(1, 1);
    cache.put(2, 2);
    cache.get(1); // returns 1
    cache.put(3, 3); // evicts key 2
    cache.get(2); // returns -1 (not found)
    cache.put(4, 4); // evicts key 1
    cache.get(1); // returns -1 (not found)
    cache.get(3); // returns 3
    cache.get(4); // returns 4

    View Slide

  18. 解きたい答
    import collections
    class LRUCache:
    def __init__(self, capacity: int):
    self.capacity = capacity
    self.cache = collections.OrderedDict()
    def get(self, key: int) -> int:
    if key in self.cache.keys():
    tmp = self.cache[key]
    self.cache.pop(key)
    self.cache[key] = tmp
    return tmp
    else:
    return -1
    def put(self, key: int, value: int) -> None:
    if key not in self.cache.keys():
    if len(self.cache) >= self.capacity:
    self.cache.popitem(last=False) # last=False -> FIFO
    else:
    self.cache.pop(key)
    self.cache[key] = value
    # Your LRUCache object will be instantiated and called as such:
    # obj = LRUCache(capacity)
    # param_1 = obj.get(key)
    # obj.put(key,value)

    View Slide

  19. まとめ
    ● 競技プログラミングプログラミングサイトではコンテストでに解きたい参加
    しな競技プログラミくマーカーを引いても楽しいからしめるし勉強できる(私はわできる(私はわりと私はわり早くと
    こういう使い方をしているい方をしている)を学ぶしている)
    ● 自分にあったプラッに解きたいあったプラットでフォームを正確にを学ぶ見つけて楽しいからし
    くマーカーを引い学ぶびましょう!

    View Slide