GitHub Copilot实战 Leetcode和Alpha Vantage API获取股票数据

Posted AI架构师易筋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GitHub Copilot实战 Leetcode和Alpha Vantage API获取股票数据相关的知识,希望对你有一定的参考价值。

GitHub Copilot 可以提升编码速度25%。

需要在 visual studio code 添加插件 GitHub Copilot


1. Base Python 创建数组和Person class


# Create a list of 100 numbers.
numbers = [i for i in range(100)]

def get_even_numbers(numbers):
    """Return a list of even numbers from the given list."""
    return [i for i in numbers if i % 2 == 0]

even_numbers = get_even_numbers(numbers)
odd_numbers = [i for i in numbers if i not in even_numbers]

print("Even numbers: ".format(even_numbers))
print("Odd numbers: ".format(odd_numbers))

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

# A list of 10 people.
PEOPLE = [
    Person("John", 32),
    Person("Jane", 28),
    Person("Bob", 30),
    Person("Alice", 24),
    Person("Jill", 26),
    Person("Jack", 30),
    Person("Jen", 28),
    Person("Bill", 32),
    Person("Barbara", 24),
    Person("Jeff", 30),
]

2. Leetcode 转化罗马数字为阿拉伯数字

"""
    A program that converts a Roman numberal to an integer.
"""

ROMAN_NUMERAL = 
    "I": 1,
    "V": 5,
    "X": 10,
    "L": 50,
    "C": 100,
    "D": 500,
    "M": 1000,


def roman_to_int(roman_numeral: str) -> int:
    """
    Convert a Roman numeral to an integer.
    """
    result = 0
    for i, numeral in enumerate(roman_numeral):
        value = ROMAN_NUMERAL[numeral]
        if i + 1 < len(roman_numeral) and ROMAN_NUMERAL[roman_numeral[i + 1]] > value:
            result -= value
        else:
            result += value
    
    return result

TEST_CASES = [
    ("I", 1),
    ("II", 2),
    ("III", 3),
    ("IV", 4),
    ("V", 5),
    ("VI", 6),
    ("VII", 7),
    ("VIII", 8),
    ("IX", 9),
    ("X", 10),
    ("XI", 11),
    ("XII", 12),
    ("XIII", 13),
    ("XIV", 14),
    ("XV", 15),
    ("XVI", 16),
    ("XVII", 17),
    ("XVIII", 18),
    ("XIX", 19),
    ("XX", 20),
    ("XXI", 21)
]

for test_input, expected in TEST_CASES:
    result = roman_to_int(test_input)
    assert result == expected, f"Expected expected, got result"
    print(result, result == expected)

3. 获取股票数据Alpha Vantage API

https://www.alphavantage.co/documentation/
注册 api key
https://www.alphavantage.co/support/#api-key

"""
    Use Alpha Vantage API to get stock data and plot it in a graph.
 """

import requests
import pandas as pd
import matplotlib.pyplot as plt
 
API_KEY = "RTART83NP1RD7G8X"
STOCK_SYMBOL = "MSFT"
def get_daily_stock_data(symbol: str):
    """
    Get daily stock data for the given symbol.
    """
    url = f"https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=symbol&apikey=API_KEY"
    response = requests.get(url)
    response.raise_for_status()
    data = response.json()
    
    return data

def get_intraday_stock_data(symbol: str):
    """
    Get intraday stock data for the given symbol.
    """
    url = f"https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=symbol&interval=5min&apikey=API_KEY"
    response = requests.get(url)
    response.raise_for_status()
    data = response.json()
    
    return data

def main():
    data = get_intraday_stock_data(STOCK_SYMBOL)
    # print(data)
    # Turn the data into a pandas DataFrame.
    df = pd.DataFrame(data["Time Series (5min)"]).T
    df.index = pd.to_datetime(df.index)
    df = df.astype(float)

    # Print the dataframes.
    print(df.head())

    # Plot the data in a graph.
    plt.plot(df["4. close"])
    plt.show()

    pass

if __name__ == "__main__":
    main()

参考

https://www.youtube.com/watch?v=tG8PPne7ef0&ab_channel=pixegami

以上是关于GitHub Copilot实战 Leetcode和Alpha Vantage API获取股票数据的主要内容,如果未能解决你的问题,请参考以下文章

实战AI生成LeetCode算法答案 应届生开挂利器 GitHub Copilot开启AI自动生成代码的时代

Github Copilot 值得购买吗?使用GitHub Copilot进行快速EDA的示例

ChatGPT 拓展资料:GitHub Copilot 实践课

FauxPilot :可本地运行的开源 GitHub Copilot (Copilot Plugin)

GitHub Copilot 学习笔记

GitHub Copilot开始收费了