-
Notifications
You must be signed in to change notification settings - Fork 0
Slow twosum solution #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
* Add two-sum example * Allow GH action to post review as comment
Here are the improvements I'd suggest:
Here's an updated version of the code incorporating some of these suggestions: def two_sum(nums: list[int], target: int) -> tuple[int, int]:
"""Returns a tuple of two numbers in the input list that add up to the target sum."""
num_map = {}
for num in nums:
complement = target - num
if complement in num_map:
return (complement, num)
num_map[num] = True
return ()
print(two_sum([2, 7, 11, 15], 9)) # (2, 7) |
Code Review The provided code diff appears to be a GitHub Actions workflow configuration file and a Python script for a code review bot. Here are some improvements and potential optimizations: Improvements:
Potential Optimizations:
Security:
Best Practices:
Here is an updated version of the code incorporating some of these suggestions: reviewbot/main.py import os
import requests
import logging
from reviewbot.utils import get_diff, post_pr_comment, get_pr_number
llm_prompt = "Please review this code diff, list improvements and potential optimizations in bullet points"
def review_with_groq(diff: str) -> str:
api_key = os.environ.get("GROQ_API_KEY")
if not api_key:
logging.error("GROQ_API_KEY environment variable is not set")
return ""
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
try:
response = requests.post(
"https://api.groq.com/v1/chat/completions",
headers=headers,
json={
"model": "meta-llama/llama-4-scout-17b-16e-instruct",
"messages": [
{"role": "system", "content": "You are a senior software engineer doing code reviews."},
{"role": "user", "content": f"{llm_prompt} given the following diff:\n{diff}"}
],
"temperature": 0.3,
},
timeout=30 # Add a timeout to prevent hanging
)
response.raise_for_status()
return response.json()["choices"][0]["message"]["content"]
except requests.RequestException as e:
logging.error(f"Error reviewing code: {e}")
return ""
def main():
diff = get_diff()
review = review_with_groq(diff)
post_pr_comment(review)
if __name__ == "__main__":
main() reviewbot/utils.py import os
import requests
def get_pr_number() -> int:
event = requests.get("https://api.github.com/repository").json()
return event["pull_request"]["number"]
def get_diff() -> str:
repo = os.environ.get("GITHUB_REPOSITORY")
pr_number = get_pr_number()
token = os.environ.get("GITHUB_TOKEN")
if not repo or not token:
logging.error("GITHUB_REPOSITORY or GITHUB_TOKEN environment variable is not set")
return ""
try:
response = requests.get(
f"https://api.github.com/repos/{repo}/pulls/{pr_number}/diff",
headers={"Authorization": f"Bearer {token}"}
)
response.raise_for_status()
return response.text
except requests.RequestException as e:
logging.error(f"Error getting diff: {e}")
return "" Note that this is not an exhaustive review, and there may be other improvements and optimizations that can be made. |
Code Review The provided code diff appears to be a GitHub Actions workflow configuration file and a Python script for a code review bot. Here are some improvements and potential optimizations: Improvements:
Potential Optimizations:
Code Smells:
Security:
Here is an updated version of the code incorporating some of these suggestions: reviewbot/main.py import os
import requests
from reviewbot.utils import get_diff, post_pr_comment, get_pr_number
LLM_PROMPT = "Please review this code diff, list improvements and potential optimizations in bullet points"
def review_with_groq(diff: str) -> str:
api_key = os.environ.get("GROQ_API_KEY")
if not api_key:
raise ValueError("GROQ_API_KEY environment variable is not set")
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
payload = {
"model": "meta-llama/llama-4-scout-17b-16e-instruct",
"messages": [
{"role": "system", "content": "You are a senior software engineer doing code reviews."},
{"role": "user", "content": f"{LLM_PROMPT} given the following diff:\n{diff}"},
],
"temperature": 0.3,
}
response = requests.post("https://api.groq.com/v1/chat", headers=headers, json=payload)
response.raise_for_status()
return response.json()["text"]
def main():
diff = get_diff()
review = review_with_groq(diff)
post_pr_comment(review)
if __name__ == "__main__":
main() reviewbot/utils.py import os
import requests
def get_pr_number() -> int:
event = requests.get("https://api.github.com/repos/{}/events".format(os.environ.get("GITHUB_REPOSITORY"))).json()
return event["pull_request"]["number"]
def get_diff() -> str:
repo = os.environ.get("GITHUB_REPOSITORY")
pr_number = get_pr_number()
token = os.environ.get("GITHUB_TOKEN")
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
}
response = requests.get(f"https://api.github.com/repos/{repo}/pulls/{pr_number}/diff", headers=headers)
response.raise_for_status()
return response.text |
No description provided.