delete the previous cli code

This commit is contained in:
Saurabh Misra 2025-02-13 01:02:25 -05:00
parent 13e608cd82
commit 16d1a9146b
222 changed files with 0 additions and 30013 deletions

View file

@ -1,57 +0,0 @@
# Codeflash
Codeflash is an AI optimization tool that automatically improves the performance of your Python code while maintaining its correctness.
![img.png](https://res.cloudinary.com/dkg30tdvl/image/upload/v1731590846/readme-img_df52le.png)
## Features
- Automatically optimizes your code using AI
- Maintains code correctness through extensive testing
- Opens a pull request for each optimization
- Continuously optimizes your codebase through CI
- Dynamically optimizes your real workflows through tracing
## Installation
To install Codeflash, run:
```
pip install codeflash
```
## Quick Start
1. Configure Codeflash for your project:
```
codeflash init
```
2. Optimize a function:
```
codeflash --file path/to/your/file.py --function function_name
```
3. Optimize your entire codebase:
```
codeflash --all
```
## Getting the Best Results
To get the most out of Codeflash:
1. Install the Github App and actions workflow
2. Find optimizations on your whole codebase with codeflash --all
3. Find and optimize bottlenecks with the Codeflash Tracer
4. Review the PRs Codeflash opens
## Learn More
- [Codeflash Website](https://www.codeflash.ai)
- [Documentation](https://docs.codeflash.ai)
## License
Codeflash is licensed under the BSL-1.1 License. See the LICENSE file for details.

View file

@ -1,55 +0,0 @@
from __future__ import annotations
from sqlalchemy import ForeignKey, Integer, String, create_engine
from sqlalchemy.engine.base import Engine
from sqlalchemy.orm import (
DeclarativeBase,
Mapped,
Relationship,
Session,
mapped_column,
relationship,
sessionmaker,
)
# Custom base class
class Base(DeclarativeBase):
pass
engine: Engine = create_engine('sqlite:///example.db')
session_factory = sessionmaker(bind=engine)
session: Session = session_factory()
class User(Base):
__tablename__: str = 'users'
id: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String)
posts: Relationship[list[Post]] = relationship("Post", order_by="Post.id", back_populates="user")
class Post(Base):
__tablename__: str = 'posts'
id: Mapped[int] = mapped_column(Integer, primary_key=True)
title: Mapped[str] = mapped_column(String)
user_id: Mapped[int] = mapped_column(Integer, ForeignKey('users.id'))
user: Relationship[User] = relationship("User", back_populates="posts")
Base.metadata.create_all(engine)
def get_user_posts() -> dict[User, list[Post]]:
users: list[User] = session.query(User).all() # Query all users
user_posts: dict[User, list[Post]] = {}
for u in users:
user_posts[u] = session.query(Post).filter(Post.user_id == u.id).all()
return user_posts
# Example usage
for user, posts in get_user_posts().items():
print(f"User: {user.name}, Posts: {[post.title for post in posts]}")

View file

@ -1,109 +0,0 @@
from time import time
from typing import List
from sqlalchemy import Boolean, Column, ForeignKey, Integer, Text, func
from sqlalchemy.engine import Engine, create_engine
from sqlalchemy.orm import DeclarativeBase, Session, relationship, sessionmaker
from sqlalchemy.orm.relationships import Relationship
POSTGRES_CONNECTION_STRING: str = ("postgresql://cf_developer:XJcbU37MBYeh4dDK6PTV5n@sqlalchemy-experiments.postgres"
".database.azure.com:5432/postgres")
class Base(DeclarativeBase):
pass
class Author(Base):
__tablename__: str = "authors"
id: Column[int] = Column(Integer, primary_key=True)
name: Column[str] = Column(Text, nullable=False)
class Book(Base):
__tablename__: str = "books"
id: Column[int] = Column(Integer, primary_key=True)
title: Column[str] = Column(Text, nullable=False)
author_id: Column[int] = Column(Integer, ForeignKey("authors.id"), nullable=False)
is_bestseller: Column[bool] = Column(Boolean, default=False)
author: Relationship[Author] = relationship("Author", backref="books")
def init_table() -> Session:
catalog_engine: Engine = create_engine(POSTGRES_CONNECTION_STRING, echo=True)
session: Session = sessionmaker(bind=catalog_engine)()
i: int
for i in range(50):
author: Author = Author(id=i, name=f"author{i}")
session.add(author)
for i in range(100000):
book: Book = Book(id=i, title=f"book{i}", author_id=i % 50, is_bestseller=i % 2 == 0)
session.add(book)
session.commit()
return session
def get_authors(books: list[Book]) -> list[Author]:
_authors: list[Author] = []
book: Book
for book in books:
_authors.append(book.author)
return sorted(
list(set(_authors)),
key=lambda x: x.id,
)
def get_authors2(num_authors) -> list[Author]:
engine: Engine = create_engine(POSTGRES_CONNECTION_STRING, echo=True)
session_factory: sessionmaker[Session] = sessionmaker(bind=engine)
session: Session = session_factory()
books: list[Book] = session.query(Book).all()
_authors: list[Author] = []
book: Book
for book in books:
_authors.append(book.author)
return sorted(
list(set(_authors)),
key=lambda x: x.id,
)[:num_authors]
def get_top_author(authors: List[Author]) -> Author:
engine: Engine = create_engine(POSTGRES_CONNECTION_STRING, echo=True)
session_factory: sessionmaker[Session] = sessionmaker(bind=engine)
session: Session = session_factory()
# Step 1: Initialize variables to keep track of the author with the maximum bestsellers
max_bestsellers = 0
top_author = None
# Step 2: Iterate over each author to count their bestsellers
for author in authors:
bestseller_count = (
session.query(func.count(Book.id))
.filter(Book.author_id == author.id, Book.is_bestseller == True)
.scalar()
)
# Step 3: Update the author with the maximum bestsellers
if bestseller_count > max_bestsellers:
max_bestsellers = bestseller_count
top_author = author
return top_author
if __name__ == "__main__":
engine: Engine = create_engine(POSTGRES_CONNECTION_STRING, echo=True)
session_factory: sessionmaker[Session] = sessionmaker(bind=engine)
_session: Session = session_factory()
_t: float = time()
authors: list[Author] = get_authors(_session)
print("TIME TAKEN", time() - _t)
authors_name = list(map(lambda x: x.name, authors))
print("len(authors_name)", len(authors_name))
print(set(authors_name))

View file

@ -1,32 +0,0 @@
from __future__ import annotations
from typing import Any, cast
from _typeshed import SupportsDunderGT, SupportsDunderLT
from sqlalchemy.orm import Session
from code_to_optimize.book_catalog import (
POSTGRES_CONNECTION_STRING,
Author,
Base,
Book,
_session,
_t,
authors,
authors_name,
engine,
init_table,
session_factory,
)
def get_authors(session: Session) -> list[Author]:
books: list[Book] = session.query(Book).all()
_authors: list[Author] = []
book: Book
for book in books:
_authors.append(book.author)
return sorted(
list(set(_authors)),
key=lambda x: cast(SupportsDunderLT[Any] | SupportsDunderGT[Any], x.id),
)

View file

@ -1,23 +0,0 @@
from __future__ import annotations
from code_to_optimize.book_catalog import (
POSTGRES_CONNECTION_STRING,
Author,
Base,
Book,
_session,
_t,
authors,
authors_name,
engine,
init_table,
session_factory,
)
def get_authors(session):
books = session.query(Book).all()
_authors = []
for book in books:
_authors.append(book.author)
return sorted(list(set(_authors)), key=lambda x: x.id)

View file

@ -1,8 +0,0 @@
def sorter(arr):
for i in range(len(arr)):
for j in range(len(arr) - 1):
if arr[j] > arr[j + 1]:
temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
return arr

View file

@ -1,6 +0,0 @@
def sorter(arr):
arr.sort()
return arr
CACHED_TESTS = "import unittest\ndef sorter(arr):\n for i in range(len(arr)):\n for j in range(len(arr) - 1):\n if arr[j] > arr[j + 1]:\n temp = arr[j]\n arr[j] = arr[j + 1]\n arr[j + 1] = temp\n return arr\nclass SorterTestCase(unittest.TestCase):\n def test_empty_list(self):\n self.assertEqual(sorter([]), [])\n def test_single_element_list(self):\n self.assertEqual(sorter([5]), [5])\n def test_ascending_order_list(self):\n self.assertEqual(sorter([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5])\n def test_descending_order_list(self):\n self.assertEqual(sorter([5, 4, 3, 2, 1]), [1, 2, 3, 4, 5])\n def test_random_order_list(self):\n self.assertEqual(sorter([3, 1, 4, 2, 5]), [1, 2, 3, 4, 5])\n def test_duplicate_elements_list(self):\n self.assertEqual(sorter([3, 1, 4, 2, 2, 5, 1]), [1, 1, 2, 2, 3, 4, 5])\n def test_negative_numbers_list(self):\n self.assertEqual(sorter([-5, -2, -8, -1, -3]), [-8, -5, -3, -2, -1])\n def test_mixed_data_types_list(self):\n self.assertEqual(sorter(['apple', 2, 'banana', 1, 'cherry']), [1, 2, 'apple', 'banana', 'cherry'])\n def test_large_input_list(self):\n self.assertEqual(sorter(list(range(1000, 0, -1))), list(range(1, 1001)))\n def test_list_with_none_values(self):\n self.assertEqual(sorter([None, 2, None, 1, None]), [None, None, None, 1, 2])\n def test_list_with_nan_values(self):\n self.assertEqual(sorter([float('nan'), 2, float('nan'), 1, float('nan')]), [1, 2, float('nan'), float('nan'), float('nan')])\n def test_list_with_complex_numbers(self):\n self.assertEqual(sorter([3 + 2j, 1 + 1j, 4 + 3j, 2 + 1j, 5 + 4j]), [1 + 1j, 2 + 1j, 3 + 2j, 4 + 3j, 5 + 4j])\n def test_list_with_custom_class_objects(self):\n class Person:\n def __init__(self, name, age):\n self.name = name\n self.age = age\n def __repr__(self):\n return f\"Person('{self.name}', {self.age})\"\n input_list = [Person('Alice', 25), Person('Bob', 30), Person('Charlie', 20)]\n expected_output = [Person('Charlie', 20), Person('Alice', 25), Person('Bob', 30)]\n self.assertEqual(sorter(input_list), expected_output)\n def test_list_with_uncomparable_elements(self):\n with self.assertRaises(TypeError):\n sorter([5, 'apple', 3, [1, 2, 3], 2])\n def test_list_with_custom_comparison_function(self):\n input_list = [5, 4, 3, 2, 1]\n expected_output = [5, 4, 3, 2, 1]\n self.assertEqual(sorter(input_list, reverse=True), expected_output)\nif __name__ == '__main__':\n unittest.main()"

View file

@ -1,8 +0,0 @@
def sorter(arr):
for i in range(len(arr)):
for j in range(len(arr) - 1):
if arr[j] > arr[j + 1]:
temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
return arr

View file

@ -1,2 +0,0 @@
def dep1_comparer(arr, j: int) -> bool:
return arr[j] > arr[j + 1]

View file

@ -1,4 +0,0 @@
def dep2_swap(arr, j):
temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp

View file

@ -1,150 +0,0 @@
from code_to_optimize.bubble_sort_dep1_helper import dep1_comparer
from code_to_optimize.bubble_sort_dep2_swap import dep2_swap
def sorter_deps(arr):
for i in range(len(arr)):
for j in range(len(arr) - 1):
if dep1_comparer(arr, j):
dep2_swap(arr, j)
return arr
CACHED_TESTS = """import dill as pickle
import os
def _log__test__values(values, duration, test_name):
iteration = os.environ["CODEFLASH_TEST_ITERATION"]
with open(os.path.join(
'/var/folders/ms/1tz2l1q55w5b7pp4wpdkbjq80000gn/T/codeflash_jk4pzz3w/',
f'test_return_values_{iteration}.bin'), 'ab') as f:
return_bytes = pickle.dumps(values)
_test_name = f"{test_name}".encode("ascii")
f.write(len(_test_name).to_bytes(4, byteorder='big'))
f.write(_test_name)
f.write(duration.to_bytes(8, byteorder='big'))
f.write(len(return_bytes).to_bytes(4, byteorder='big'))
f.write(return_bytes)
import time
import gc
from code_to_optimize.bubble_sort_deps import sorter_deps
import timeout_decorator
import unittest
def dep1_comparer(arr, j: int) -> bool:
return arr[j] > arr[j + 1]
def dep2_swap(arr, j):
temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
class TestSorterDeps(unittest.TestCase):
@timeout_decorator.timeout(15, use_signals=True)
def test_integers(self):
gc.disable()
counter = time.perf_counter_ns()
return_value = sorter_deps([5, 3, 2, 4, 1])
duration = time.perf_counter_ns() - counter
gc.enable()
_log__test__values(
return_value, duration,
'code_to_optimize.tests.unittest.test_sorter_deps__unit_test_0:TestSorterDeps.test_integers:sorter_deps:0')
gc.disable()
counter = time.perf_counter_ns()
return_value = sorter_deps([10, -3, 0, 2, 7])
duration = time.perf_counter_ns() - counter
gc.enable()
_log__test__values(
return_value, duration,
('code_to_optimize.tests.unittest.test_sorter_deps__unit_test_0:'
'TestSorterDeps.test_integers:sorter_deps:1'))
@timeout_decorator.timeout(15, use_signals=True)
def test_floats(self):
gc.disable()
counter = time.perf_counter_ns()
return_value = sorter_deps([3.2, 1.5, 2.7, 4.1, 1.0])
duration = time.perf_counter_ns() - counter
gc.enable()
_log__test__values(return_value, duration,
'code_to_optimize.tests.unittest.test_sorter_deps__unit_test_0:TestSorterDeps.test_floats:sorter_deps:0')
gc.disable()
counter = time.perf_counter_ns()
return_value = sorter_deps([-1.1, 0.0, 3.14, 2.71, -0.5])
duration = time.perf_counter_ns() - counter
gc.enable()
_log__test__values(return_value, duration,
'code_to_optimize.tests.unittest.test_sorter_deps__unit_test_0:TestSorterDeps.test_floats:sorter_deps:1')
@timeout_decorator.timeout(15, use_signals=True)
def test_identical_elements(self):
gc.disable()
counter = time.perf_counter_ns()
return_value = sorter_deps([1, 1, 1, 1, 1])
duration = time.perf_counter_ns() - counter
gc.enable()
_log__test__values(return_value, duration,
('code_to_optimize.tests.unittest.test_sorter_deps__unit_test_0:'
'TestSorterDeps.test_identical_elements:sorter_deps:0'))
gc.disable()
counter = time.perf_counter_ns()
return_value = sorter_deps([3.14, 3.14, 3.14])
duration = time.perf_counter_ns() - counter
gc.enable()
_log__test__values(return_value, duration,
('code_to_optimize.tests.unittest.test_sorter_deps__unit_test_0:'
'TestSorterDeps.test_identical_elements:sorter_deps:1'))
@timeout_decorator.timeout(15, use_signals=True)
def test_single_element(self):
gc.disable()
counter = time.perf_counter_ns()
return_value = sorter_deps([5])
duration = time.perf_counter_ns() - counter
gc.enable()
_log__test__values(return_value, duration, 'code_to_optimize.tests.unittest.test_sorter_deps__unit_test_0:TestSorterDeps.test_single_element:sorter_deps:0')
gc.disable()
counter = time.perf_counter_ns()
return_value = sorter_deps([-3.2])
duration = time.perf_counter_ns() - counter
gc.enable()
_log__test__values(return_value, duration, 'code_to_optimize.tests.unittest.test_sorter_deps__unit_test_0:TestSorterDeps.test_single_element:sorter_deps:1')
@timeout_decorator.timeout(15, use_signals=True)
def test_empty_array(self):
gc.disable()
counter = time.perf_counter_ns()
return_value = sorter_deps([])
duration = time.perf_counter_ns() - counter
gc.enable()
_log__test__values(return_value, duration, 'code_to_optimize.tests.unittest.test_sorter_deps__unit_test_0:TestSorterDeps.test_empty_array:sorter_deps:0')
@timeout_decorator.timeout(15, use_signals=True)
def test_strings(self):
gc.disable()
counter = time.perf_counter_ns()
return_value = sorter_deps(['apple', 'banana', 'cherry', 'date'])
duration = time.perf_counter_ns() - counter
gc.enable()
_log__test__values(return_value, duration, 'code_to_optimize.tests.unittest.test_sorter_deps__unit_test_0:TestSorterDeps.test_strings:sorter_deps:0')
gc.disable()
counter = time.perf_counter_ns()
return_value = sorter_deps(['dog', 'cat', 'elephant', 'ant'])
duration = time.perf_counter_ns() - counter
gc.enable()
_log__test__values(return_value, duration, 'code_to_optimize.tests.unittest.test_sorter_deps__unit_test_0:TestSorterDeps.test_strings:sorter_deps:1')
@timeout_decorator.timeout(15, use_signals=True)
def test_mixed_types(self):
with self.assertRaises(TypeError):
gc.disable()
counter = time.perf_counter_ns()
return_value = sorter_deps([1, 'two', 3.0, 'four'])
duration = time.perf_counter_ns() - counter
gc.enable()
_log__test__values(return_value, duration, 'code_to_optimize.tests.unittest.test_sorter_deps__unit_test_0:TestSorterDeps.test_mixed_types:sorter_deps:0_0')
if __name__ == '__main__':
unittest.main()
"""

View file

@ -1,6 +0,0 @@
from code_to_optimize.bubble_sort import sorter
def sort_from_another_file(arr):
sorted_arr = sorter(arr)
return sorted_arr

View file

@ -1,18 +0,0 @@
def hi():
pass
class BubbleSortClass:
def __init__(self):
pass
def sorter(self, arr):
n = len(arr)
for i in range(n):
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
return arr
def helper(self, arr, j):
return arr[j] > arr[j + 1]

View file

@ -1,26 +0,0 @@
def hi():
pass
class WrapperClass:
def __init__(self):
pass
class BubbleSortClass:
def __init__(self):
pass
def sorter(self, arr):
def inner_helper(arr, j):
return arr[j] > arr[j + 1]
for i in range(len(arr)):
for j in range(len(arr) - 1):
if arr[j] > arr[j + 1]:
temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
return arr
def helper(self, arr, j):
return arr[j] > arr[j + 1]

View file

@ -1,12 +0,0 @@
class BubbleSorter:
def __init__(self, x=0):
self.x = x
def sorter(self, arr):
for i in range(len(arr)):
for j in range(len(arr) - 1):
if arr[j] > arr[j + 1]:
temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
return arr

View file

@ -1,8 +0,0 @@
def sorter(arr: list[int]) -> list[int]:
for i in range(len(arr)):
for j in range(len(arr) - 1):
if arr[j] > arr[j + 1]:
temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
return arr

View file

@ -1,7 +0,0 @@
[tool.codeflash]
disable-imports-sorting = true
disable-telemetry = true
formatter-cmds = ["ruff check --exit-zero --fix $file", "ruff format $file"]
module-root = "src/aviary"
test-framework = "pytest"
tests-root = "tests"

View file

@ -1,14 +0,0 @@
from __future__ import annotations
def find_common_tags(articles: list[dict[str, list[str]]]) -> set[str]:
i = 0
for _ in range(1000000):
i += 1
if not articles:
return set()
common_tags = set(articles[0]["tags"])
for article in articles[1:]:
common_tags.intersection_update(article["tags"])
return common_tags

View file

@ -1,22 +0,0 @@
from aviary.common_tags import find_common_tags
def test_common_tags_1() -> None:
articles_1 = [
{"title": "Article 1", "tags": ["Python", "AI", "ML"]},
{"title": "Article 2", "tags": ["Python", "Data Science", "AI"]},
{"title": "Article 3", "tags": ["Python", "AI", "Big Data"]},
]
expected = {"Python", "AI"}
assert find_common_tags(articles_1) == expected
articles_2 = [
{"title": "Article 1", "tags": ["Python", "AI", "ML"]},
{"title": "Article 2", "tags": ["Python", "Data Science", "AI"]},
{"title": "Article 3", "tags": ["Python", "AI", "Big Data"]},
{"title": "Article 4", "tags": ["Python", "AI", "ML"]},
]
assert find_common_tags(articles_2) == expected

View file

@ -1,47 +0,0 @@
def sorter_one_level_depth(arr):
return sorter(arr)
def sorter(arr):
for i in range(len(arr)):
for j in range(len(arr) - 1):
if arr[j] > arr[j + 1]:
temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
return arr
async def decompress_braces(string):
numbers = "123456789"
stack = []
for char in string:
if char in numbers:
stack.append(int(char))
elif char == "{":
continue
elif "a" <= char <= "z" or "A" <= char <= "Z":
stack.append(char)
elif char == "}":
segment = ""
while isinstance(stack[-1], str):
popped_char = stack.pop()
segment = popped_char + segment
num = stack.pop()
stack.append(segment * num)
return "".join(stack)
async def sorter_one_level_depth_lower(arr):
return sorter(arr)
def add_one_level_depth(a, b):
return add(a, b)
def add(a, b):
return a + b
def multiply_and_add(a, b, c):
return a * b + c

View file

@ -1,7 +0,0 @@
[tool.codeflash]
# All paths are relative to this pyproject.toml's directory.
module-root = "."
tests-root = "tests"
test-framework = "pytest"
ignore-paths = []
formatter-cmds = ["black $file"]

View file

@ -1,40 +0,0 @@
import pytest
from bubble_sort import sorter, sorter_one_level_depth, add_one_level_depth, add, multiply_and_add
def test_sort():
input = [5, 4, 3, 2, 1, 0]
output = sorter(input)
assert output == [0, 1, 2, 3, 4, 5]
input = [5.0, 4.0, 3.0, 2.0, 1.0, 0.0]
output = sorter(input)
assert output == [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
input = list(reversed(range(5000)))
output = sorter(input)
assert output == list(range(5000))
def test_sorter_one_level_depth():
input = [3, 2, 1]
output = sorter_one_level_depth(input)
assert output == [1, 2, 3]
def test_add_one_level_depth():
assert add_one_level_depth(1, 2) == 3
assert add_one_level_depth(-1, 1) == 0
assert add_one_level_depth(0, 0) == 0
def test_add():
assert add(1, 2) == 3
assert add(-1, 1) == 0
assert add(0, 0) == 0
def test_multiply_and_add():
assert multiply_and_add(2, 3, 4) == 10
assert multiply_and_add(0, 3, 4) == 4
assert multiply_and_add(-1, 3, 4) == 1
assert multiply_and_add(2, 0, 4) == 4

View file

@ -1,6 +0,0 @@
from bubble_sort_with_math import sorter
def sort_from_another_file(arr):
sorted_arr = sorter(arr)
return sorted_arr

View file

@ -1,8 +0,0 @@
import math
def sorter(arr):
arr.sort()
x = math.sqrt(2)
print(x)
return arr

View file

@ -1,2 +0,0 @@
# Define a global variable
API_URL = "https://api.example.com/data"

View file

@ -1,37 +0,0 @@
import requests # Third-party library
from globals import API_URL # Global variable defined in another file
from utils import DataProcessor
def fetch_and_process_data():
# Use the global variable for the request
response = requests.get(API_URL)
response.raise_for_status()
raw_data = response.text
# Use code from another file (utils.py)
processor = DataProcessor()
processed = processor.process_data(raw_data)
processed = processor.add_prefix(processed)
return processed
def fetch_and_transform_data():
# Use the global variable for the request
response = requests.get(API_URL)
raw_data = response.text
# Use code from another file (utils.py)
processor = DataProcessor()
processed = processor.process_data(raw_data)
transformed = processor.transform_data(processed)
return transformed
if __name__ == "__main__":
result = fetch_and_process_data()
print("Processed data:", result)

View file

@ -1,27 +0,0 @@
from code_to_optimize.code_directories.retriever.utils import DataProcessor
class DataTransformer:
def __init__(self):
self.data = None
def transform(self, data):
self.data = data
return self.data
def transform_using_own_method(self, data):
return self.transform(data)
def transform_using_same_file_function(self, data):
return update_data(data)
def transform_data_all_same_file(self, data):
new_data = update_data(data)
return self.transform_using_own_method(new_data)
def circular_dependency(self, data):
return DataProcessor().circular_dependency(data)
def update_data(data):
return data + " updated"

View file

@ -1,47 +0,0 @@
import math
from transform_utils import DataTransformer
GLOBAL_VAR = 10
class DataProcessor:
"""A class for processing data."""
number = 1
def __init__(self, default_prefix: str = "PREFIX_"):
"""Initialize the DataProcessor with a default prefix."""
self.default_prefix = default_prefix
self.number += math.log(self.number)
def __repr__(self) -> str:
"""Return a string representation of the DataProcessor."""
return f"DataProcessor(default_prefix={self.default_prefix!r})"
def process_data(self, raw_data: str) -> str:
"""Process raw data by converting it to uppercase."""
return raw_data.upper()
def add_prefix(self, data: str, prefix: str = "PREFIX_") -> str:
"""Add a prefix to the processed data."""
return prefix + data
def do_something(self):
print("something")
def transform_data(self, data: str) -> str:
"""Transform the processed data"""
return DataTransformer().transform(data)
def transform_data_own_method(self, data: str) -> str:
"""Transform the processed data using own method"""
return DataTransformer().transform_using_own_method(data)
def transform_data_same_file_function(self, data: str) -> str:
"""Transform the processed data using a function from the same file"""
return DataTransformer().transform_using_same_file_function(data)
def circular_dependency(self, data: str) -> str:
"""Test circular dependency"""
return DataTransformer().circular_dependency(data)

View file

@ -1,6 +0,0 @@
[tool.codeflash]
disable-telemetry = true
formatter-cmds = ["ruff check --exit-zero --fix $file", "ruff format $file"]
module-root = "."
test-framework = "pytest"
tests-root = "tests"

View file

@ -1,14 +0,0 @@
def funcA(number):
k = 0
for i in range(number * 100):
k += i
# Simplify the for loop by using sum with a range object
j = sum(range(number))
# Use a generator expression directly in join for more efficiency
return " ".join(str(i) for i in range(number))
if __name__ == "__main__":
for i in range(10, 31, 10):
funcA(10)

View file

@ -1,577 +0,0 @@
from __future__ import annotations
from typing import Any, Callable, Iterable, NewType, Optional, Protocol, TypeVar
try:
from typing import _TypingBase # type: ignore[attr-defined]
except ImportError:
from typing import _Final as _TypingBase # type: ignore[attr-defined]
typing_base = _TypingBase
_T = TypeVar("_T")
class Comparable(Protocol):
def __lt__(self: _T, __other: _T) -> bool: ...
ComparableT = TypeVar("ComparableT", bound=Comparable)
def sorter(arr: list[ComparableT]) -> list[ComparableT]:
for i in range(len(arr)):
for j in range(len(arr) - 1):
if arr[j] > arr[j + 1]:
temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
return arr
def sorter2(arr: list[ComparableT]) -> list[ComparableT]:
n = len(arr)
for i in range(n):
swapped = False
for j in range(n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
swapped = True
if not swapped:
break
return arr
def sorter3(arr: list[ComparableT]) -> list[ComparableT]:
arr.sort()
return arr
def is_valid_field_name(name: str) -> bool:
return not name.startswith("_")
def is_valid_field_name2(name: str) -> bool:
return not (name and name[0] == "_")
def is_self_type(tp: Any) -> bool:
"""Check if a given class is a Self type (from `typing` or `typing_extensions`)"""
return isinstance(tp, typing_base) and getattr(tp, "_name", None) == "Self"
def is_self_type2(tp: Any) -> bool:
"""Check if a given class is a Self type (from `typing` or `typing_extensions`)"""
if not isinstance(tp, _TypingBase):
return False
return tp._name == "Self" if hasattr(tp, "_name") else False
test_new_type = NewType("test_new_type", str)
def is_new_type(type_: type[Any]) -> bool:
"""Check whether type_ was created using typing.NewType.
Can't use isinstance because it fails <3.10.
"""
return isinstance(type_, test_new_type.__class__) and hasattr(type_, "__supertype__") # type: ignore[arg-type]
def is_new_type2(type_: type[Any]) -> bool:
"""Check whether type_ was created using typing.NewType.
Can't use isinstance because it fails <3.10.
"""
return type(type_) is type(test_new_type) and hasattr(type_, "__supertype__")
def _to_str(
size: int,
suffixes: Iterable[str],
base: int,
*,
precision: Optional[int] = 1,
separator: Optional[str] = " ",
) -> str:
if size == 1:
return "1 byte"
elif size < base:
return f"{size:,} bytes"
for i, suffix in enumerate(suffixes, 2): # noqa: B007
unit = base**i
if size < unit:
break
return "{:,.{precision}f}{separator}{}".format(
(base * size / unit),
suffix,
precision=precision,
separator=separator,
)
# Given: (size=-1, suffixes=(), base=-1, precision=0, separator=None),
# code_to_optimize.bubble_sort_typed._to_str : raises UnboundLocalError("cannot access local variable 'unit' where it is not associated with a value")
# code_to_optimize.bubble_sort_typed._to_str2 : raises IndexError()
def _to_str2(
size: int,
suffixes: Iterable[str],
base: int,
*,
precision: Optional[int] = 1,
separator: Optional[str] = " ",
) -> str:
if size == 1:
return "1 byte"
elif size < base:
return f"{size:,} bytes"
unit = base
for suffix in suffixes:
unit *= base
if size < unit:
return f"{size / (unit / base):,.{precision}f}{separator}{suffix}"
# Extra condition if size exceeds the largest unit
return f"{size / (unit / base):,.{precision}f}{separator}{suffixes[-1]}"
def find_common_tags(articles: list[dict[str, list[str]]]) -> set[str]:
if not articles:
return set()
common_tags = articles[0]["tags"]
for article in articles[1:]:
common_tags = [tag for tag in common_tags if tag in article["tags"]]
return set(common_tags)
# crosshair diffbehavior --max_uninteresting_iterations 64 code_to_optimize.bubble_sort_typed.find_common_tags code_to_optimize.bubble_sort_typed.find_common_tags2
# Given: (articles=[{'tags': ['', '']}, {'tags': ['', '']}, {'tags': []}, {}]),
# code_to_optimize.bubble_sort_typed.find_common_tags : returns set()
# code_to_optimize.bubble_sort_typed.find_common_tags2 : raises KeyError()
def find_common_tags2(articles: list[dict[str, list[str]]]) -> set[str]:
if not articles:
return set()
common_tags = set(articles[0]["tags"])
for article in articles[1:]:
common_tags.intersection_update(article["tags"])
return common_tags
# Given: (articles=[{'\x00\x00\x00\x00': [], 'tags': ['']}, {'\x00\x00\x00\x00': [], 'tags': ['']}, {'\x00\x00\x00\x00': [], 'tags': ['']}, {'tags': ['']}, {}, {'\x00\x00\x00\x00': [], 'tags': ['']}, {}]),
# code_to_optimize.bubble_sort_typed.find_common_tags : raises KeyError()
# code_to_optimize.bubble_sort_typed.find_common_tags2_1 : returns set()
def find_common_tags2_1(articles: list[dict[str, list[str]]]) -> set[str]:
if not articles:
return set()
common_tags = set(articles[0].get("tags", []))
for article in articles[1:]:
common_tags.intersection_update(article.get("tags", []))
return common_tags
# % crosshair diffbehavior --max_uninteresting_iterations 64 code_to_optimize.bubble_sort_typed.find_common_tags code_to_optimize.bubble_sort_typed.find_common_tags2_2
# Given: (articles=[{'\x00\x00\x00\x00': [''], 'tags': ['']}, {'\x00\x00\x00\x00': [''], 'tags': ['']}, {'\x00\x00\x00\x00': [], 'tags': ['']}, {'\x00\x00\x00\x00': [], '': []}, {'\x00\x00\x00\x00': [], 'tags': ['']}]),
# code_to_optimize.bubble_sort_typed.find_common_tags : raises KeyError()
# code_to_optimize.bubble_sort_typed.find_common_tags2_2 : returns set()
# (codeflash312) renaud@Renauds-Laptop codeflash %
def find_common_tags2_2(articles: list[dict[str, list[str]]]) -> set[str]:
if not articles:
return set()
common_tags = set(articles[0]["tags"])
for article in articles[1:]:
if not common_tags:
break
common_tags.intersection_update(article["tags"])
return common_tags
# % crosshair diffbehavior --max_uninteresting_iterations 128 code_to_optimize.bubble_sort_typed.find_common_tags code_to_optimize.bubble_sort_typed.find_common_tags2_3
# Given: (articles=[{'tags': ['', '']}, {'tags': ['', '']}, {'tags': []}, {}]),
# code_to_optimize.bubble_sort_typed.find_common_tags : returns set()
# code_to_optimize.bubble_sort_typed.find_common_tags2_3 : raises KeyError()
# Given: (articles=[{'\x00\x00\x00\x00': [], 'tags': []}, {'\x00\x00\x00\x00': [], 'tags': []}, {'\x00\x00\x00\x00': [], 'tags': []}, {'\x00\x00\x00\x00': []}, {}, {}]),
# code_to_optimize.bubble_sort_typed.find_common_tags : returns set()
# code_to_optimize.bubble_sort_typed.find_common_tags2_3 : raises KeyError()
def find_common_tags2_3(articles: list[dict[str, list[str]]]) -> set[str]:
if not articles:
return set()
common_tags = set(articles[0]["tags"])
for article in articles[1:]:
article_tags = article["tags"] # Access 'tags' key to match KeyError behavior
if not common_tags:
continue # Skip intersection but maintain KeyError on missing 'tags'
common_tags.intersection_update(article_tags)
return common_tags
def find_common_tags2_4(articles: list[dict[str, list[str]]]) -> set[str]:
if not articles:
return set()
common_tags = set(articles[0]["tags"])
for article in articles[1:]:
if common_tags:
article_tags = article["tags"] # Access 'tags' only if common_tags is not empty
common_tags.intersection_update(article_tags)
else:
# Do not access article["tags"]; no KeyError is raised
pass
return common_tags
def find_common_tags2_5(articles: list[dict[str, list[str]]]) -> set[str]:
if not articles:
return set()
# Initialize with the first article's tags, defaulting to an empty list if "tags" is missing
common_tags = set(articles[0].get("tags", []))
for article in articles[1:]:
# Use .get("tags", []) to safely access tags, defaulting to an empty list if missing
common_tags.intersection_update(article.get("tags", []))
# Early exit if there are no common tags left
if not common_tags:
break
return common_tags
def find_common_tags2_6(articles: list[dict[str, list[str]]]) -> set[str]:
if not articles:
return set()
# Initialize with the first article's tags
common_tags = set(articles[0]["tags"]) # Raises KeyError if "tags" is missing
for article in articles[1:]:
# Directly access "tags", maintaining behavior
common_tags.intersection_update(article["tags"])
# Early exit if no common tags remain
if not common_tags:
break
return common_tags
def find_common_tags2_7(articles: list[dict[str, list[str]]]) -> set[str]:
if not articles:
return set()
# Initialize with the first article's tags (raises KeyError if "tags" is missing)
common_tags = set(articles[0]["tags"])
for article in articles[1:]:
if not common_tags:
# If no common tags remain, no need to process further
break
# Access "tags" directly, maintaining original behavior (raises KeyError if missing)
common_tags.intersection_update(article["tags"])
return common_tags
def find_common_tags2_8(articles: list[dict[str, list[str]]]) -> set[str]:
if not articles:
return set()
# Initialize with the first article's tags (raises KeyError if "tags" is missing)
try:
common_tags = set(articles[0]["tags"])
except KeyError:
raise KeyError("The first article is missing the 'tags' key.")
for index, article in enumerate(articles[1:], start=2):
try:
tags = article["tags"]
except KeyError:
raise KeyError(f"Article at position {index} is missing the 'tags' key.")
# Perform intersection with the current article's tags
common_tags.intersection_update(tags)
return common_tags
def find_common_tags2_9(articles: list[dict[str, list[str]]]) -> set[str]:
if not articles:
return set()
# Initialize with the first article's tags (raises KeyError if "tags" is missing)
common_tags = set(articles[0]["tags"])
for article in articles[1:]:
if not common_tags:
# If no common tags remain, no need to process further
break
# Directly access "tags", allowing KeyError to propagate naturally
common_tags.intersection_update(article["tags"])
return common_tags
# crosshair diffbehavior --max_uninteresting_iterations 64 code_to_optimize.bubble_sort_typed.find_common_tags code_to_optimize.bubble_sort_typed.find_common_tags3
# Given: (articles=[{'tags': ['', '', '', '']}, {'tags': ['', '', '', '']}, {'tags': ['', '', '']}, {'tags': ['', '', '', '']}, {'tags': ['', '', '']}, {}]),
# code_to_optimize.bubble_sort_typed.find_common_tags : raises KeyError()
# code_to_optimize.bubble_sort_typed.find_common_tags3 : returns set()
# Given: (articles=[{'\x00\x00\x00\x00': ['', ''], 'tags': [], '': []}, {}, {'\x00\x00\x00\x00': ['', ''], '': []}, {'': []}, {'\x00\x00\x00\x00': ['', ''], 'tags': [], '': []}]),
# code_to_optimize.bubble_sort_typed.find_common_tags : returns set()
# code_to_optimize.bubble_sort_typed.find_common_tags3 : raises KeyError()
def find_common_tags3(articles: list[dict[str, list[str]]]) -> set[str]:
if not articles:
return set()
common_tags = set(articles[0]["tags"])
for article in articles[1:]:
common_tags.intersection_update(article["tags"])
if not common_tags:
break
return common_tags
# % crosshair diffbehavior --max_uninteresting_iterations 64 code_to_optimize.bubble_sort_typed.find_common_tags code_to_optimize.bubble_sort_typed.find_common_tags4
# Given: (articles=[{'\x00\x00\x00\x00': ['', ''], 'tags': [], '': []}, {}, {'\x00\x00\x00\x00': ['', ''], '': []}, {'': []}, {'\x00\x00\x00\x00': ['', ''], 'tags': [], '': []}]),
# code_to_optimize.bubble_sort_typed.find_common_tags : returns set()
# code_to_optimize.bubble_sort_typed.find_common_tags4 : raises KeyError()
def find_common_tags4(articles: list[dict[str, list[str]]]) -> set[str]:
if not articles:
return set()
common_tags = set(articles[0]["tags"])
for article in articles[1:]:
common_tags &= set(article["tags"])
if not common_tags: # Early exit if no common tags.
break
return common_tags
def with_pattern(pattern: str, regex_group_count: int | None = None) -> Callable:
def decorator(func: Callable) -> Callable:
func.pattern = pattern
func.regex_group_count = regex_group_count
return func
return decorator
def with_pattern2(pattern: str, regex_group_count: int | None = None) -> Callable:
return (
lambda func: setattr(func, "pattern", pattern)
or setattr(func, "regex_group_count", regex_group_count)
or func
)
"""
We have this original code:
```
def find_common_tags(articles: list[dict[str, list[str]]]) -> set[str]:
if not articles:
return set()
common_tags = articles[0]["tags"]
for article in articles[1:]:
common_tags = [tag for tag in common_tags if tag in article["tags"]]
return set(common_tags)
```
We generated optimized code:
```
def find_common_tags2(articles: list[dict[str, list[str]]]) -> set[str]:
if not articles:
return set()
common_tags = set(articles[0]["tags"])
for article in articles[1:]:
common_tags.intersection_update(article["tags"])
return common_tags
```
When analyzed the optimized code for correctness with Concolic Testing (using CrossHair DiffBehavior), we found the following counter-example:
```
crosshair diffbehavior --max_uninteresting_iterations 64 code_to_optimize.bubble_sort_typed.find_common_tags code_to_optimize.bubble_sort_typed.find_common_tags2
Given: (articles=[{'tags': ['', '']}, {'tags': ['', '']}, {'tags': []}, {}]), code_to_optimize.bubble_sort_typed.find_common_tags : returns set() code_to_optimize.bubble_sort_typed.find_common_tags2 : raises KeyError()
```
Fix find_common_tags2 for correctness relative to find_common_tags, making sure it is still optimized for speed.
"""
"""
To fix the `KeyError` in `find_common_tags2` and ensure it behaves like the original `find_common_tags`, we'll modify the code to handle cases where the `'tags'` key might be missing in some articles. In the original code, it seems that articles might not always have the `'tags'` key, and in such cases, the code should default to an empty list.
Here's the corrected and optimized version:
```python
def find_common_tags2(articles: list[dict[str, list[str]]]) -> set[str]:
if not articles:
return set()
common_tags = set(articles[0].get("tags", []))
for article in articles[1:]:
common_tags.intersection_update(article.get("tags", []))
return common_tags
```
**Explanation:**
- **Use `get` with a default value:**
- We replaced `article["tags"]` with `article.get("tags", [])`. This ensures that if the `'tags'` key is missing, it defaults to an empty list instead of raising a `KeyError`.
- **Initialize `common_tags` properly:**
- We initialize `common_tags` with `set(articles[0].get("tags", []))` to handle cases where the first article might not have `'tags'`.
- **Maintain Performance Optimization:**
- By using `set` operations, we keep the optimized performance of the function while ensuring correctness.
This change ensures that the function doesn't raise a `KeyError` when an article is missing the `'tags'` key, and it behaves identically to the original function for all inputs.
"""
"""We have this original code:
```
def find_common_tags(articles: list[dict[str, list[str]]]) -> set[str]:
if not articles:
return set()
common_tags = articles[0]["tags"]
for article in articles[1:]:
common_tags = [tag for tag in common_tags if tag in article["tags"]]
return set(common_tags)
```
We generated optimized code:
```
def find_common_tags2(articles: list[dict[str, list[str]]]) -> set[str]:
if not articles:
return set()
common_tags = set(articles[0]["tags"])
for article in articles[1:]:
common_tags.intersection_update(article["tags"])
return common_tags
```
When analyzed the optimized code for correctness with Concolic Testing (using CrossHair DiffBehavior), we found the following counter-example:
```
crosshair diffbehavior --max_uninteresting_iterations 64 code_to_optimize.bubble_sort_typed.find_common_tags code_to_optimize.bubble_sort_typed.find_common_tags2
Given: (articles=[{'tags': ['', '']}, {'tags': ['', '']}, {'tags': []}, {}]), code_to_optimize.bubble_sort_typed.find_common_tags : returns set() code_to_optimize.bubble_sort_typed.find_common_tags2 : raises KeyError()
```
We attempted to fix this with the following candidate:
```python
def find_common_tags2(articles: list[dict[str, list[str]]]) -> set[str]:
if not articles:
return set()
common_tags = set(articles[0].get("tags", []))
for article in articles[1:]:
common_tags.intersection_update(article.get("tags", []))
return common_tags
```
When analyzing the optimized code for correctness with Concolic Testing (using CrossHair DiffBehavior), we found a different counter-example:
```
Given: (articles=[{'\x00\x00\x00\x00': [], 'tags': ['']}, {'\x00\x00\x00\x00': [], 'tags': ['']}, {'\x00\x00\x00\x00': [], 'tags': ['']}, {'tags': ['']}, {}, {'\x00\x00\x00\x00': [], 'tags': ['']}, {}]),
     code_to_optimize.bubble_sort_typed.find_common_tags : raises KeyError()
  code_to_optimize.bubble_sort_typed.find_common_tags2_1 : returns set()
```
Fix find_common_tags2_1 for correctness relative to find_common_tags, making sure it is still optimized for speed.
"""
"""To fix `find_common_tags2_1` and ensure it behaves identically to `find_common_tags`, we need to address the discrepancy in how the two functions handle the absence of the `'tags'` key and when `common_tags` becomes empty.
**Issue Analysis:**
1. **KeyError Discrepancy:**
- The original function raises a `KeyError` when accessing `article["tags"]` if the `'tags'` key is missing.
- Our optimized version used `article.get("tags", [])`, which avoids the `KeyError`, leading to different behavior.
2. **Empty `common_tags`:**
- In the original function, if `common_tags` becomes empty, the list comprehension doesn't access `article["tags"]`, preventing a `KeyError`.
- In the optimized version, `intersection_update` still accesses `article["tags"]` even if `common_tags` is empty, leading to unnecessary `KeyError`s.
**Solution:**
- **Mimic Original Exception Behavior:**
- We should ensure that `find_common_tags2` raises a `KeyError` in the same situations as `find_common_tags`.
- Avoid accessing `article["tags"]` when `common_tags` is empty, just like the original function.
- **Optimize by Early Exit:**
- If `common_tags` becomes empty, further intersections won't change it.
- We can break out of the loop early, improving performance.
**Corrected and Optimized Version:**
```python
def find_common_tags2(articles: list[dict[str, list[str]]]) -> set[str]:
if not articles:
return set()
common_tags = set(articles[0]["tags"])
for article in articles[1:]:
if not common_tags:
break # No need to continue if common_tags is empty
common_tags.intersection_update(article["tags"])
return common_tags
```
**Explanation:**
- **Accessing `article["tags"]`:**
- We use `article["tags"]` instead of `article.get("tags", [])` to ensure a `KeyError` is raised if `'tags'` is missing, matching the original function's behavior.
- **Early Exit on Empty `common_tags`:**
- The `if not common_tags: break` statement checks if `common_tags` is empty.
- If it is empty, we break out of the loop, avoiding unnecessary accesses to `article["tags"]` that could raise a `KeyError`.
- **Performance Optimization:**
- By breaking early when `common_tags` is empty, we prevent further iterations and accesses.
- Using `set` operations with `intersection_update` retains the performance benefits.
**Validation with Counter-Examples:**
- **First Counter-Example:**
- With `articles=[{'tags': ['', '']}, {'tags': ['', '']}, {'tags': []}, {}]`, the original function returns `set()` without raising `KeyError`.
- Our corrected `find_common_tags2` also returns `set()` without raising `KeyError` because it avoids accessing `article["tags"]` when `common_tags` is empty.
- **Second Counter-Example:**
- With an input where an article lacks `'tags'` and `common_tags` is not empty, the original function raises `KeyError`.
- Our corrected `find_common_tags2` also raises `KeyError` in this situation, matching the original function's behavior.
**Conclusion:**
This corrected version ensures that `find_common_tags2` behaves identically to `find_common_tags` in all cases, raising `KeyError` when the original function does, and avoiding it otherwise. It also maintains the performance optimizations by using set operations and early loop exit.
**Final Optimized Code:**
```python
def find_common_tags2(articles: list[dict[str, list[str]]]) -> set[str]:
if not articles:
return set()
common_tags = set(articles[0]["tags"])
for article in articles[1:]:
if not common_tags:
break
common_tags.intersection_update(article["tags"])
return common_tags
```"""

View file

@ -1,11 +0,0 @@
def sorter(arr):
for i in range(len(arr)):
for j in range(len(arr) - 1):
if arr[j] > arr[j + 1]:
temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
return arr
CACHED_TESTS = "import unittest\ndef sorter(arr):\n for i in range(len(arr)):\n for j in range(len(arr) - 1):\n if arr[j] > arr[j + 1]:\n temp = arr[j]\n arr[j] = arr[j + 1]\n arr[j + 1] = temp\n return arr\nclass SorterTestCase(unittest.TestCase):\n def test_empty_list(self):\n self.assertEqual(sorter([]), [])\n def test_single_element_list(self):\n self.assertEqual(sorter([5]), [5])\n def test_ascending_order_list(self):\n self.assertEqual(sorter([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5])\n def test_descending_order_list(self):\n self.assertEqual(sorter([5, 4, 3, 2, 1]), [1, 2, 3, 4, 5])\n def test_random_order_list(self):\n self.assertEqual(sorter([3, 1, 4, 2, 5]), [1, 2, 3, 4, 5])\n def test_duplicate_elements_list(self):\n self.assertEqual(sorter([3, 1, 4, 2, 2, 5, 1]), [1, 1, 2, 2, 3, 4, 5])\n def test_negative_numbers_list(self):\n self.assertEqual(sorter([-5, -2, -8, -1, -3]), [-8, -5, -3, -2, -1])\n def test_mixed_data_types_list(self):\n self.assertEqual(sorter(['apple', 2, 'banana', 1, 'cherry']), [1, 2, 'apple', 'banana', 'cherry'])\n def test_large_input_list(self):\n self.assertEqual(sorter(list(range(1000, 0, -1))), list(range(1, 1001)))\n def test_list_with_none_values(self):\n self.assertEqual(sorter([None, 2, None, 1, None]), [None, None, None, 1, 2])\n def test_list_with_nan_values(self):\n self.assertEqual(sorter([float('nan'), 2, float('nan'), 1, float('nan')]), [1, 2, float('nan'), float('nan'), float('nan')])\n def test_list_with_complex_numbers(self):\n self.assertEqual(sorter([3 + 2j, 1 + 1j, 4 + 3j, 2 + 1j, 5 + 4j]), [1 + 1j, 2 + 1j, 3 + 2j, 4 + 3j, 5 + 4j])\n def test_list_with_custom_class_objects(self):\n class Person:\n def __init__(self, name, age):\n self.name = name\n self.age = age\n def __repr__(self):\n return f\"Person('{self.name}', {self.age})\"\n input_list = [Person('Alice', 25), Person('Bob', 30), Person('Charlie', 20)]\n expected_output = [Person('Charlie', 20), Person('Alice', 25), Person('Bob', 30)]\n self.assertEqual(sorter(input_list), expected_output)\n def test_list_with_uncomparable_elements(self):\n with self.assertRaises(TypeError):\n sorter([5, 'apple', 3, [1, 2, 3], 2])\n def test_list_with_custom_comparison_function(self):\n input_list = [5, 4, 3, 2, 1]\n expected_output = [5, 4, 3, 2, 1]\n self.assertEqual(sorter(input_list, reverse=True), expected_output)\nif __name__ == '__main__':\n unittest.main()"

View file

@ -1,9 +0,0 @@
def check_user_access(user_ids, check_ids):
"""Check if each ID in check_ids is in the list of user_ids"""
results = []
for id in check_ids:
if id in user_ids:
results.append(True)
else:
results.append(False)
return results

View file

@ -1,65 +0,0 @@
import itertools
from functools import reduce
from typing import List, Set, Tuple
def compare_lists(
li1: List[int],
li2: List[int],
value_func1=None,
value_func2=None,
) -> Tuple[Set[int], Set[int], Set[int]]:
"""Compare *li1* and *li2*, return the results as a list in the following form:
[[data seen in both lists], [data only seen in li1], [data only seen in li2]]
and [data seen in both lists] contains 2 tuple: [(actual items in li1), (actual items in li2)]
* *value_func1* callback function to li1, applied to each item in the list, returning the **logical** value for comparison
* *value_func2* callback function to li2, similarly
If not supplied, lists will be compared as it is.
Usage::
>>> compare_lists([1, 2, 3], [1, 3, 5])
>>> ([(1, 3), (1, 3)], [2], [5])
Or with callback functions specified::
>>> f = lambda x: x['v']
>>>
>>> li1 = [{'v': 1}, {'v': 2}, {'v': 3}]
>>> li2 = [1, 3, 5]
>>>
>>> compare_lists(li1, li2, value_func1=f)
>>> ([({'v': 1}, {'v': 3}), (1, 3)], [{'v': 2}], [5])
"""
if not value_func1:
value_func1 = lambda x: x
if not value_func2:
value_func2 = lambda x: x
def to_dict(li, vfunc):
return {k: list(g) for k, g in itertools.groupby(li, vfunc)}
def flatten(li):
return reduce(list.__add__, li) if li else []
d1 = to_dict(li1, value_func1)
d2 = to_dict(li2, value_func2)
if d1 == d2:
return set(li1), set(), set()
k1 = set(d1.keys())
k2 = set(d2.keys())
elems_left = flatten([d1[k] for k in k1 - k2])
elems_right = flatten([d2[k] for k in k2 - k1])
common_keys = k1 & k2
elems_both = flatten([d2[k] for k in common_keys])
return set(elems_both), set(elems_left), set(elems_right)

View file

@ -1,35 +0,0 @@
def _encodePythonStringToC(value):
"""Encode a string, so that it gives a C string literal.
This doesn't handle limits.
"""
assert type(value) is bytes, type(value)
result = ""
octal = False
for c in value:
if str is bytes:
cv = ord(c)
else:
cv = c
if c in b'\\\t\r\n"?':
result += r"\%03o" % cv
octal = True
elif 32 <= cv <= 127:
if octal and c in b"0123456789":
result += '" "'
result += chr(cv)
octal = False
else:
result += r"\%o" % cv
octal = True
result = result.replace('" "\\', "\\")
return '"%s"' % result

View file

@ -1,5 +0,0 @@
def exponentiation(base, exponent):
result = 1
for _ in range(exponent):
result *= base
return result

View file

@ -1,8 +0,0 @@
def find_common_tags(articles: list[dict[str, list[str]]]) -> set[str]:
if not articles:
return set()
common_tags = articles[0]["tags"]
for article in articles[1:]:
common_tags = [tag for tag in common_tags if tag in article["tags"]]
return set(common_tags)

View file

@ -1,7 +0,0 @@
def find_duplicates(lst):
duplicates = []
for i in range(len(lst)):
for j in range(i + 1, len(lst)):
if lst[i] == lst[j] and lst[i] not in duplicates:
duplicates.append(lst[i])
return duplicates

View file

@ -1,7 +0,0 @@
def find_factors(product):
answers = []
for factor in range(1, product + 1):
if not product % factor:
factor2 = int(product / factor)
answers.append((factor, factor2))
return answers

View file

@ -1,20 +0,0 @@
def find_top_k_elements(arr: list, k):
if k <= 0:
return []
if k >= len(arr):
result = []
for num in arr:
result.append(num)
result.sort(reverse=True)
return result
top_k = []
for num in arr:
top_k.append(num)
top_k.sort(reverse=True)
if len(top_k) > k:
top_k.pop()
return top_k

View file

@ -1,21 +0,0 @@
def is_prime(n):
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
def generate_primes(limit):
primes = []
for num in range(2, limit + 1):
if is_prime(num):
primes.append(num)
return primes

View file

@ -1,10 +0,0 @@
import numpy as np
def gradient(n_features, n_samples, y, X, w, b, subgrad, lambda1, lambda2):
for i in range(n_features):
for n in range(n_samples):
subgrad[i] += (-y[n] * X[n][i]) if y[n] * (np.dot(X[n], w) + b) < 1 else 0
subgrad[i] += lambda1 * (-1 if w[i] < 0 else 1) + 2 * lambda2 * w[i]
return subgrad

View file

@ -1,5 +0,0 @@
import numpy as np
def _hamming_distance(a: np.ndarray, b: np.ndarray) -> np.floating:
return np.mean(a != b)

View file

@ -1,3 +0,0 @@
def indentedCode(codes, count):
"""Indent code, used for generating test codes."""
return "\n".join(" " * count + line if line else "" for line in codes)

View file

@ -1,10 +0,0 @@
def f(x):
return x * (x - 1)
def integrate_f(a, b, N):
s = 0
dx = (b - a) / N
for i in range(N):
s += f(a + i * dx)
return s * dx

View file

@ -1,11 +0,0 @@
def matrix_multiply(A, B):
if len(A[0]) != len(B):
raise ValueError("Matrices A and B cannot be multiplied")
result = [[0 for _ in range(len(B[0]))] for _ in range(len(A))]
for i in range(len(A)):
for j in range(len(B[0])):
for k in range(len(B)):
result[i][j] += A[i][k] * B[k][j]
return result

View file

@ -1,18 +0,0 @@
def translate(word):
vowels = "aeiou"
if word[0] in vowels:
return word + "way"
else:
consonants = ""
for letter in word:
if letter not in vowels:
consonants += letter
else:
break
return word[len(consonants) :] + consonants + "ay"
def pig_latin(text):
words = text.lower().split()
translated_words = [translate(word) for word in words]
return " ".join(translated_words)

View file

@ -1,11 +0,0 @@
def single_name_to_first_last_names(
name: str,
) -> list[tuple[str, str]]:
parts = name.upper().split()
if len(parts) == 2:
return [tuple(parts)]
elif len(parts) == 3:
a, b, c = parts
return [(a, c), (a, f"{b} {c}"), (f"{a} {b}", c)]
else:
return []

View file

@ -1,37 +0,0 @@
def standardize_name(street_name):
standard_street_names = [
"Brattle St",
"Mount Auburn St",
"Massachusetts Ave",
"Cardinal Medeiros Ave",
"Hampshire Street",
"Beacon St",
"Blake St",
"Beech St",
"Garden St",
]
# Exact match:
if street_name in standard_street_names:
return street_name
# Different case:
lower_name = street_name.lower()
for street in standard_street_names:
if lower_name == street.lower():
return street
# "Ave." and "Avenue" are possible synonyms of "Ave":
parts = street_name.split()
if parts[-1].lower() in ("ave.", "avenue"):
parts[-1] = "Ave"
fixed_street_name = " ".join(parts)
return standardize_name(fixed_street_name)
# "St." and "Street" are possible synonyms of "St":
if parts[-1].lower() in ("st.", "street"):
parts[-1] = "St"
fixed_street_name = " ".join(parts)
return standardize_name(fixed_street_name)
raise ValueError(f"Unknown street {street_name}")

View file

@ -1,5 +0,0 @@
def concatenate_strings(n):
result = ""
for i in range(n):
result += str(i) + ", "
return result

View file

@ -1,15 +0,0 @@
from code_to_optimize.final_test_set.bubble_sort import sorter
def test_sort():
input = [5, 4, 3, 2, 1, 0]
output = sorter(input)
assert output == [0, 1, 2, 3, 4, 5]
input = [5.0, 4.0, 3.0, 2.0, 1.0, 0.0]
output = sorter(input)
assert output == [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
input = list(reversed(range(5000)))
output = sorter(input)
assert output == list(range(5000))

View file

@ -1,12 +0,0 @@
from code_to_optimize.final_test_set.check_list import check_user_access
def test_check_user_access():
user_ids = [str(i) for i in range(1000)]
check_ids = [str(i) for i in range(1000)]
res = [True] * 1000
assert check_user_access(user_ids, check_ids) == res
check_ids = [str(i) for i in range(1000, 2000)]
res = [False] * 1000
assert check_user_access(user_ids, check_ids) == res

View file

@ -1,31 +0,0 @@
from code_to_optimize.final_test_set.encode_python_string_to_c import (
_encodePythonStringToC,
)
def test_empty_string():
assert _encodePythonStringToC(b"") == '""'
def test_printable_characters():
assert _encodePythonStringToC(b"hello world") == '"hello world"'
def test_special_characters():
assert _encodePythonStringToC(b'hello\\world"') == '"hello\\134world\\042"'
def test_control_characters():
assert _encodePythonStringToC(b"\t\n\r") == r'"\011\012\015"'
def test_non_printable_characters():
assert _encodePythonStringToC(bytes([0, 1, 255])) == r'"\0\1\377"'
def test_mixed_content():
assert _encodePythonStringToC(b"Line 1\nLine 2") == r'"Line 1\012Line 2"'
def test_adjacent_octal_characters():
assert _encodePythonStringToC(b"\n123") == r'"\012" "123"'

View file

@ -1,9 +0,0 @@
from code_to_optimize.final_test_set.exponentiation import exponentiation
def test_exponentiation():
res = exponentiation(2, 10)
assert res == 1024
res = exponentiation(2, 100)
assert res == 1267650600228229401496703205376

View file

@ -1,62 +0,0 @@
from code_to_optimize.final_test_set.find_common_tags import find_common_tags
def test_common_tags_1():
articles_1 = [
{"title": "Article 1", "tags": ["Python", "AI", "ML"]},
{"title": "Article 2", "tags": ["Python", "Data Science", "AI"]},
{"title": "Article 3", "tags": ["Python", "AI", "Big Data"]},
]
expected = set(["Python", "AI"])
assert find_common_tags(articles_1) == expected
articles_2 = [
{"title": "Article 1", "tags": ["Python", "AI", "ML"]},
{"title": "Article 2", "tags": ["Python", "Data Science", "AI"]},
{"title": "Article 3", "tags": ["Python", "AI", "Big Data"]},
{"title": "Article 4", "tags": ["Python", "AI", "ML"]},
]
assert find_common_tags(articles_2) == expected
def test_empty_article_list():
articles = []
expected = set()
assert (
find_common_tags(articles) == expected
), "Test failed for empty list of articles."
def test_no_common_tags():
articles = [
{"tags": ["python", "coding", "tutorial"]},
{"tags": ["java", "software", "programming"]},
{"tags": ["javascript", "development", "web"]},
]
expected = set()
assert (
find_common_tags(articles) == expected
), "Test failed when no tags are common."
def test_all_common_tags():
articles = [
{"tags": ["tech", "startups", "innovation"]},
{"tags": ["tech", "startups", "innovation"]},
{"tags": ["tech", "startups", "innovation"]},
]
expected = {"tech", "startups", "innovation"}
assert (
find_common_tags(articles) == expected
), "Test failed when all tags are common."
def test_single_article():
articles = [{"tags": ["single", "article", "test"]}]
expected = {"single", "article", "test"}
assert (
find_common_tags(articles) == expected
), "Test failed for a single article input."

View file

@ -1,35 +0,0 @@
from code_to_optimize.final_test_set.find_duplicates import find_duplicates
def test_basic_case():
assert find_duplicates([1, 2, 3, 2, 1, 5, 6, 5]) == [
1,
2,
5,
], "Failed on basic case"
def test_no_duplicates():
assert find_duplicates([1, 2, 3, 4, 5]) == [], "Failed when no duplicates present"
def test_multiple_duplicates():
assert find_duplicates([1, 2, 2, 3, 3, 3, 4]) == [
2,
3,
], "Failed on multiple duplicates of the same item"
def test_empty_list():
assert find_duplicates([]) == [], "Failed on empty list"
def test_all_elements_same():
assert find_duplicates([7, 7, 7, 7]) == [7], "Failed when all elements are the same"
def test_mixed_data_types():
assert find_duplicates(["apple", "banana", "apple", 42, 42]) == [
"apple",
42,
], "Failed on mixed data types"

View file

@ -1,50 +0,0 @@
from code_to_optimize.final_test_set.find_factors import find_factors
def test_small_number():
assert find_factors(12) == [
(1, 12),
(2, 6),
(3, 4),
(4, 3),
(6, 2),
(12, 1),
], "Failed on small number with multiple factors"
def test_prime_number():
assert find_factors(13) == [(1, 13), (13, 1)], "Failed on prime number"
def test_perfect_square():
assert find_factors(16) == [
(1, 16),
(2, 8),
(4, 4),
(8, 2),
(16, 1),
], "Failed on perfect square number"
def test_large_number():
# 120 has factors: 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 24, 30, 40, 60, 120
result = find_factors(120)
expected_factors = 16 # There should be 16 pairs
assert (
len(result) == expected_factors
), "Failed on large number with multiple factors"
def test_one():
assert find_factors(1) == [
(1, 1)
], "Failed on one, which should only have one factor pair"
def test_zero():
assert find_factors(0) == [], "Failed on zero, which should have no factors"
def test_negative_number():
# Expecting an error, or modify the function to handle negative input gracefully
assert find_factors(-1) == [], "Failed on negative number"

View file

@ -1,46 +0,0 @@
import numpy as np
from code_to_optimize.final_test_set.gradient import gradient
def test_simple_case():
# Test case with simple values
n_features = 2
n_samples = 2
y = np.array([1, -1])
X = np.array([[1, 2], [3, 4]])
w = np.array([0.5, -0.5])
b = 0.1
subgrad = np.zeros(n_features)
lambda1 = 0.1
lambda2 = 0.05
# Expected result calculated manually or by a reliable source
expected_subgrad = np.array([2.15, 1.85])
# Perform the function call
result = gradient(n_features, n_samples, y, X, w, b, subgrad, lambda1, lambda2)
# Assert to check if expected result is the actual result
np.testing.assert_array_almost_equal(result, expected_subgrad, decimal=5)
def test_edge_case():
n_features = 2
n_samples = 1
y = np.array([1])
X = np.array([[10, -10]])
w = np.array([1, -1])
b = -100
subgrad = np.zeros(n_features)
lambda1 = 0.1
lambda2 = 0.05
# All examples correctly classified with a large margin
expected_subgrad = np.array([-9.8, 9.8])
# Perform the function call
result = gradient(n_features, n_samples, y, X, w, b, subgrad, lambda1, lambda2)
# Assert
np.testing.assert_array_almost_equal(result, expected_subgrad, decimal=5)

View file

@ -1,15 +0,0 @@
import numpy as np
from code_to_optimize.final_test_set.hamming_distance import _hamming_distance
def test_no_differences():
a = np.array([1, 2, 3, 4])
b = np.array([1, 2, 3, 4])
assert _hamming_distance(a, b) == 0.0
def test_partial_differences():
a = np.array([1, 2, 3, 4])
b = np.array([1, 2, 0, 4])
assert _hamming_distance(a, b) == 0.25

View file

@ -1,42 +0,0 @@
from code_to_optimize.final_test_set.indented_code import indentedCode
def test_basic_indentation():
codes = ["def foo():", " return 42"]
indented = indentedCode(codes, 4)
expected = " def foo():\n return 42"
assert indented == expected, "Basic indentation failed"
def test_zero_indentation():
codes = ["print('hello')", "print('world')"]
indented = indentedCode(codes, 0)
expected = "print('hello')\nprint('world')"
assert indented == expected, "Zero indentation should leave text unchanged"
def test_empty_string_lines():
codes = ["", "print('hello')", ""]
indented = indentedCode(codes, 2)
expected = "\n print('hello')\n"
assert indented == expected, "Empty lines should be handled correctly"
def test_no_lines():
codes = []
indented = indentedCode(codes, 4)
assert indented == "", "Empty code list should return an empty string"
def test_large_indentation():
codes = ["if True:", " pass"]
indented = indentedCode(codes, 8)
expected = " if True:\n pass"
assert indented == expected, "Large indentation failed"
def test_mixed_content():
codes = ["", "def test():", " assert True", ""]
indented = indentedCode(codes, 1)
expected = "\n def test():\n assert True\n"
assert indented == expected, "Mixed content with empty lines failed"

View file

@ -1,35 +0,0 @@
import pytest
from code_to_optimize.final_test_set.integration import integrate_f
def isclose(a, b, rel_tol=1e-5, abs_tol=0.0):
"""
Helper function to compare two floating points for 'closeness'.
Uses a combination of relative and absolute tolerances.
"""
return abs(a - b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
def test_simple_range():
a, b, N = 0, 1, 1000
result = integrate_f(a, b, N)
expected = -1 / 6 # Analytical result
assert isclose(result, expected), f"Expected {expected}, got {result}"
def test_negative_to_positive_range():
a, b, N = -1, 1, 500
result = integrate_f(a, b, N)
expected = 0.6706719 # Analytical result
assert isclose(result, expected), f"Expected {expected}, got {result}"
# Optionally, you can add more detailed information to your pytest output
def test_with_pytest_approx():
a, b, N = 0, 1, 1000
result = integrate_f(a, b, N)
expected = -1 / 6
assert result == pytest.approx(
expected, rel=1e-5
), "Test failed with pytest's approx."

View file

@ -1,37 +0,0 @@
import pytest
from code_to_optimize.final_test_set.matrix_multiplication import matrix_multiply
def test_matrix_multiplication_basic():
A = [[1, 2], [3, 4]]
B = [[2, 0], [1, 2]]
expected = [[4, 4], [10, 8]]
assert matrix_multiply(A, B) == expected
def test_matrix_multiplication_dimension_mismatch():
A = [[1, 2, 3], [4, 5, 6]]
B = [[1, 2], [3, 4]]
with pytest.raises(ValueError):
matrix_multiply(A, B)
def test_zero_matrix_multiplication():
A = [[1, 2], [3, 4]]
B = [[0, 0], [0, 0]]
expected = [[0, 0], [0, 0]]
assert matrix_multiply(A, B) == expected
def test_identity_matrix_multiplication():
A = [[1, 2], [3, 4]]
I = [[1, 0], [0, 1]]
assert matrix_multiply(A, I) == A
def test_large_matrix_multiplication():
A = [[1] * 100 for _ in range(100)]
B = [[2] * 100 for _ in range(100)]
expected = [[200] * 100 for _ in range(100)]
assert matrix_multiply(A, B) == expected

View file

@ -1,94 +0,0 @@
import dill as pickle
from code_to_optimize.final_test_set.pig_latin import pig_latin
def log_test_values(values, test_name):
with open(f"/tmp/test_return_values.bin", "ab") as f:
return_bytes = pickle.dumps(values)
_test_name = f"{test_name}".encode("ascii")
f.write(len(_test_name).to_bytes(4, byteorder="big"))
f.write(_test_name)
f.write(len(return_bytes).to_bytes(4, byteorder="big"))
f.write(return_bytes)
def test_pig_latin_vowel():
global log_test_values
log_test_values(pig_latin("apple"), "pig_latin_test_pig_latin_vowel_0")
log_test_values(pig_latin("elephant"), "pig_latin_test_pig_latin_vowel_1")
def test_pig_latin_single_consonant():
log_test_values(pig_latin("dog"), "pig_latin_test_pig_latin_single_consonant_0")
log_test_values(pig_latin("cat"), "pig_latin_test_pig_latin_single_consonant_1")
def test_pig_latin_multiple_consonants():
log_test_values(
pig_latin("string"), "pig_latin_test_pig_latin_multiple_consonants_0"
)
log_test_values(
pig_latin("glove"), "pig_latin_test_pig_latin_multiple_consonants_1"
)
def test_pig_latin_capital_letters():
log_test_values(pig_latin("Hello"), "pig_latin_test_pig_latin_capital_letters_0")
log_test_values(pig_latin("WoRlD"), "pig_latin_test_pig_latin_capital_letters_1")
def test_pig_latin_multiple_words():
log_test_values(
pig_latin("The quick brown fox"), "pig_latin_test_pig_latin_multiple_words_0"
)
log_test_values(
pig_latin("Python is a fun language"),
"pig_latin_test_pig_latin_multiple_words_1",
)
def test_pig_latin_empty_input():
log_test_values(pig_latin(""), "pig_latin_test_pig_latin_empty_input_0")
def test_pig_latin_spaces_input():
log_test_values(pig_latin(" "), "pig_latin_test_pig_latin_spaces_input_0")
def test_pig_latin_non_alphabetic():
log_test_values(pig_latin("123"), "pig_latin_test_pig_latin_non_alphabetic_0")
log_test_values(
pig_latin("Hello, world!"), "pig_latin_test_pig_latin_non_alphabetic_1"
)
def test_pig_latin_non_ascii():
log_test_values(pig_latin("café"), "pig_latin_test_pig_latin_non_ascii_0")
log_test_values(pig_latin("über"), "pig_latin_test_pig_latin_non_ascii_1")
def test_pig_latin_hyphenated_words():
log_test_values(
pig_latin("sister-in-law"), "pig_latin_test_pig_latin_hyphenated_words_0"
)
log_test_values(
pig_latin("self-driving car"), "pig_latin_test_pig_latin_hyphenated_words_1"
)
def test_pig_latin_contractions():
log_test_values(pig_latin("can't"), "pig_latin_test_pig_latin_contractions_0")
log_test_values(pig_latin("I'm"), "pig_latin_test_pig_latin_contractions_1")
def test_pig_latin_apostrophes():
log_test_values(pig_latin("don't"), "pig_latin_test_pig_latin_apostrophes_0")
log_test_values(
pig_latin("rock 'n' roll"), "pig_latin_test_pig_latin_apostrophes_1"
)
def test_pig_latin_non_letter():
log_test_values(pig_latin("123"), "pig_latin_test_pig_latin_non_letter_0")
log_test_values(pig_latin("Hello, world!"), "pig_latin_test_pig_latin_non_letter_1")

View file

@ -1,12 +0,0 @@
from code_to_optimize.final_test_set.generate_primes import generate_primes
def test_generate_primes():
primes = generate_primes(100)
assert len(primes) == 25
primes = generate_primes(10000)
assert len(primes) == 1229
primes = generate_primes(100000)
assert len(primes) == 9592

View file

@ -1,38 +0,0 @@
from code_to_optimize.final_test_set.single_name_to_first_last_names import (
single_name_to_first_last_names,
)
def test_two_part_name():
name = "John Doe"
expected = [("JOHN", "DOE")]
result = single_name_to_first_last_names(name)
assert result == expected
def test_three_part_name():
name = "John Michael Doe"
expected = [("JOHN", "DOE"), ("JOHN", "MICHAEL DOE"), ("JOHN MICHAEL", "DOE")]
result = single_name_to_first_last_names(name)
assert result == expected
def test_single_part_name():
name = "Prince"
expected = []
result = single_name_to_first_last_names(name)
assert result == expected
def test_more_than_three_parts():
name = "John Michael Andrew Doe"
expected = []
result = single_name_to_first_last_names(name)
assert result == expected
def test_empty_string():
name = ""
expected = []
result = single_name_to_first_last_names(name)
assert result == expected

View file

@ -1,23 +0,0 @@
import pytest
from code_to_optimize.final_test_set.standardize_name import standardize_name
def test_exact_match():
assert standardize_name("Brattle St") == "Brattle St"
def test_case_insensitivity():
assert standardize_name("brattle st") == "Brattle St"
assert standardize_name("MASSACHUSETTS AVE") == "Massachusetts Ave"
def test_handling_abbreviations():
assert standardize_name("Beacon St.") == "Beacon St"
assert standardize_name("Massachusetts Avenue") == "Massachusetts Ave"
def test_error_for_unknown_name():
with pytest.raises(ValueError) as e:
standardize_name("Infinite Loop")
assert "Unknown street Infinite Loop" in str(e.value)

View file

@ -1,21 +0,0 @@
from code_to_optimize.final_test_set.string_concat import concatenate_strings
def test_concatenate_strings_zero():
assert concatenate_strings(0) == "", "Failed: Expected an empty string for input 0"
def test_concatenate_strings_positive():
assert (
concatenate_strings(5) == "0, 1, 2, 3, 4, "
), "Failed: Incorrect string for input 5"
def test_concatenate_strings_large_number():
result = concatenate_strings(1000)
expected_length = sum(
len(str(i)) + 2 for i in range(1000)
) # Each number i + len(", ")
assert (
len(result) == expected_length
), f"Failed: Incorrect length for large input 1000"

View file

@ -1,47 +0,0 @@
from code_to_optimize.final_test_set.find_top_k_elements import find_top_k_elements
def test_negative_k():
assert find_top_k_elements([3, 1, 2, 4], -1) == [], "Failed when k is negative"
def test_zero_k():
assert find_top_k_elements([10, 8, 12, 5], 0) == [], "Failed when k is zero"
def test_k_greater_than_array_length():
array = [4, 1, 5, 6, 2]
k = 10
expected = sorted(array, reverse=True)
assert (
find_top_k_elements(array, k) == expected
), "Failed when k is greater than array length"
def test_normal_case():
array = [20, 1, 15, 3, 30, 10]
k = 3
expected = [30, 20, 15]
assert find_top_k_elements(array, k) == expected, "Failed in normal scenario"
def test_array_with_duplicate_values():
array = [5, 5, 5, 5]
k = 2
expected = [5, 5]
assert (
find_top_k_elements(array, k) == expected
), "Failed when array contains duplicates"
def test_empty_array():
assert find_top_k_elements([], 3) == [], "Failed when array is empty"
def test_single_element_array():
array = [42]
k = 1
expected = [42]
assert (
find_top_k_elements(array, k) == expected
), "Failed when array contains a single element"

View file

@ -1,44 +0,0 @@
from code_to_optimize.final_test_set.topological_sort import Graph
def test_graph_simple():
g = Graph(6)
g.addEdge(5, 2)
g.addEdge(5, 0)
g.addEdge(4, 0)
g.addEdge(4, 1)
g.addEdge(2, 3)
g.addEdge(3, 1)
assert g.topologicalSort() == [5, 4, 2, 3, 1, 0]
def test_tree_graph():
g = Graph(4)
g.addEdge(0, 1)
g.addEdge(0, 2)
g.addEdge(0, 3)
result = g.topologicalSort()
assert result.index(0) < result.index(1)
assert result.index(0) < result.index(2)
assert result.index(0) < result.index(3)
def test_complex_dag():
g = Graph(6)
g.addEdge(5, 2)
g.addEdge(5, 0)
g.addEdge(4, 0)
g.addEdge(4, 1)
g.addEdge(2, 3)
g.addEdge(3, 1)
result = g.topologicalSort()
assert all(
result.index(u) < result.index(v)
for u, v in [(5, 2), (5, 0), (4, 0), (4, 1), (2, 3), (3, 1)]
)
def test_single_node_graph():
g = Graph(1)
assert g.topologicalSort() == [0]

View file

@ -1,25 +0,0 @@
from code_to_optimize.final_test_set.unique_paths import uniquePaths
def test_minimal_grid():
assert uniquePaths(1, 1) == 1, "Failed on the minimal grid 1x1"
def test_single_row():
assert uniquePaths(1, 5) == 1, "Failed on a single row grid 1x5"
def test_single_column():
assert uniquePaths(5, 1) == 1, "Failed on a single column grid 5x1"
def test_square_grid():
assert uniquePaths(3, 3) == 6, "Failed on square grid 3x3"
def test_rectangular_grid():
assert uniquePaths(2, 3) == 3, "Failed on rectangular grid 2x3"
def test_large_grid():
assert uniquePaths(10, 10) == 48620, "Failed on large grid 10x10"

View file

@ -1,30 +0,0 @@
from collections import defaultdict
class Graph:
def __init__(self, vertices):
self.graph = defaultdict(list)
self.V = vertices # No. of vertices
def addEdge(self, u, v):
self.graph[u].append(v)
def topologicalSortUtil(self, v, visited, stack):
visited[v] = True
for i in self.graph[v]:
if visited[i] == False:
self.topologicalSortUtil(i, visited, stack)
stack.insert(0, v)
def topologicalSort(self):
visited = [False] * self.V
stack = []
for i in range(self.V):
if visited[i] == False:
self.topologicalSortUtil(i, visited, stack)
# Print contents of stack
return stack

View file

@ -1,6 +0,0 @@
def uniquePaths(m, n, i=0, j=0):
if i >= m or j >= n:
return 0
if i == m - 1 and j == n - 1:
return 1
return uniquePaths(m, n, i + 1, j) + uniquePaths(m, n, i, j + 1)

View file

@ -1,11 +0,0 @@
from __future__ import annotations
def find_common_tags(articles: list[dict[str, list[str]]]) -> set[str]:
if not articles:
return set()
common_tags = articles[0]["tags"]
for article in articles[1:]:
common_tags = [tag for tag in common_tags if tag in article["tags"]]
return set(common_tags)

View file

@ -1,7 +0,0 @@
def OptimizeMe(a, b, c):
return HelperClass().helper_method(a, b, c)
class HelperClass:
def helper_method(self, a, b, c):
return a + b + c

View file

@ -1,3 +0,0 @@
def mutinator(l):
l.append(0)
return len(l)

View file

@ -1,60 +0,0 @@
"""Math utils."""
from typing import List, Optional, Tuple, Union
import numpy as np
Matrix = Union[List[List[float]], List[np.ndarray], np.ndarray]
def cosine_similarity(X: Matrix, Y: Matrix) -> np.ndarray:
"""Row-wise cosine similarity between two equal-width matrices."""
if len(X) == 0 or len(Y) == 0:
return np.array([])
X = np.array(X)
Y = np.array(Y)
if X.shape[1] != Y.shape[1]:
raise ValueError(
f"Number of columns in X and Y must be the same. X has shape {X.shape} "
f"and Y has shape {Y.shape}.",
)
X_norm = np.linalg.norm(X, axis=1)
Y_norm = np.linalg.norm(Y, axis=1)
similarity = np.dot(X, Y.T) / np.outer(X_norm, Y_norm)
similarity[np.isnan(similarity) | np.isinf(similarity)] = 0.0
return similarity
def cosine_similarity_top_k(
X: Matrix,
Y: Matrix,
top_k: Optional[int] = 5,
score_threshold: Optional[float] = None,
) -> Tuple[List[Tuple[int, int]], List[float]]:
"""Row-wise cosine similarity with optional top-k and score threshold filtering.
Args:
----
X: Matrix.
Y: Matrix, same width as X.
top_k: Max number of results to return.
score_threshold: Minimum cosine similarity of results.
Returns:
-------
Tuple of two lists. First contains two-tuples of indices (X_idx, Y_idx),
second contains corresponding cosine similarities.
"""
if len(X) == 0 or len(Y) == 0:
return [], []
score_array = cosine_similarity(X, Y)
sorted_idxs = score_array.flatten().argsort()[::-1]
top_k = top_k or len(sorted_idxs)
top_idxs = sorted_idxs[:top_k]
score_threshold = score_threshold or -1.0
top_idxs = top_idxs[score_array.flatten()[top_idxs] > score_threshold]
ret_idxs = [(x // score_array.shape[1], x % score_array.shape[1]) for x in top_idxs]
scores = score_array.flatten()[top_idxs].tolist()
return ret_idxs, scores

View file

@ -1,18 +0,0 @@
def translate(word):
vowels = "aeiou"
if word[0] in vowels:
return word + "way"
else:
consonants = ""
for letter in word:
if letter not in vowels:
consonants += letter
else:
break
return word[len(consonants) :] + consonants + "ay"
def pig_latin(text):
words = text.lower().split()
translated_words = [translate(word) for word in words]
return " ".join(translated_words)

View file

@ -1,10 +0,0 @@
import re
class CharacterRemover:
def __init__(self):
self.version = "0.1"
def remove_control_characters(self, s) -> str:
"""Remove control characters from the string."""
return re.sub("[\\x00-\\x1F\\x7F]", "", s) if s else ""

View file

@ -1,10 +0,0 @@
import time
def accurate_sleepfunc(t) -> float:
"""T is in seconds"""
start_time = time.perf_counter_ns()
while True:
if (time.perf_counter_ns() - start_time) / 10e9 >= t:
break
return t

View file

@ -1,39 +0,0 @@
from typing import Generator
import pytest
from sqlalchemy import Engine, create_engine, delete, update
from sqlalchemy.orm import Session, sessionmaker
from code_to_optimize.book_catalog import Author, Book, get_authors
POSTGRES_CONNECTION_STRING = (
"postgresql://cf_developer:XJcbU37MBYeh4dDK6PTV5n@sqlalchemy-experiments.postgres"
".database.azure.com:5432/postgres"
)
@pytest.fixture(scope="module")
def engine() -> Engine:
return create_engine(POSTGRES_CONNECTION_STRING)
@pytest.fixture(scope="module")
def session_factory(engine: Engine) -> sessionmaker[Session]:
return sessionmaker(bind=engine)
@pytest.fixture(scope="function")
def session(session_factory: sessionmaker[Session]) -> Generator[Session, None, None]:
session = session_factory()
yield session
session.rollback()
session.close()
def test_get_authors_basic(session: Session) -> None:
books: list[Book] = session.query(Book).all()
authors = get_authors(books)
assert len(authors) == 50, "Should return 50 authors"
author_names = [author.name for author in authors]
for i in range(50):
assert f"author{i}" in author_names, f"author{i} should be in the list of authors"

View file

@ -1,6 +0,0 @@
from code_to_optimize.book_catalog import get_authors2
def test_get_authors_basic() -> None:
authors = get_authors2(num_authors=10)
assert len(authors) == 10, "Should return 10 authors"

View file

@ -1,22 +0,0 @@
from typing import Generator
import pytest
from sqlalchemy import Engine, create_engine, delete, update
from sqlalchemy.orm import Session, sessionmaker
from code_to_optimize.book_catalog import Author, Book, get_top_author
POSTGRES_CONNECTION_STRING = (
"postgresql://cf_developer:XJcbU37MBYeh4dDK6PTV5n@sqlalchemy-experiments.postgres"
".database.azure.com:5432/postgres"
)
def test_get_top_author():
engine: Engine = create_engine(POSTGRES_CONNECTION_STRING, echo=True)
session_factory: sessionmaker[Session] = sessionmaker(bind=engine)
session: Session = session_factory()
authors = session.query(Author).all()
top_author = get_top_author(authors)
assert top_author.id == 0
assert top_author.name == "author0"

View file

@ -1,15 +0,0 @@
from code_to_optimize.bubble_sort import sorter
def test_sort():
input = [5, 4, 3, 2, 1, 0]
output = sorter(input)
assert output == [0, 1, 2, 3, 4, 5]
input = [5.0, 4.0, 3.0, 2.0, 1.0, 0.0]
output = sorter(input)
assert output == [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
input = list(reversed(range(5000)))
output = sorter(input)
assert output == list(range(5000))

View file

@ -1,15 +0,0 @@
from code_to_optimize.bubble_sort_3 import sorter
def test_sort():
input = [5, 4, 3, 2, 1, 0]
output = sorter(input)
assert output == [0, 1, 2, 3, 4, 5]
input = [5.0, 4.0, 3.0, 2.0, 1.0, 0.0]
output = sorter(input)
assert output == [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
input = list(reversed(range(5000)))
output = sorter(input)
assert output == list(range(5000))

View file

@ -1,7 +0,0 @@
from code_to_optimize.bubble_sort import sorter
def test_sort():
input = [5, 4, 3, 2, 1, 0]
if len(input) > 0:
assert sorter(input) == [0, 1, 2, 3, 4, 5]

View file

@ -1,15 +0,0 @@
from code_to_optimize.bubble_sort import sorter as bubble_sorter
def test_sort():
input = [5, 4, 3, 2, 1, 0]
output = bubble_sorter(input)
assert output == [0, 1, 2, 3, 4, 5]
input = [5.0, 4.0, 3.0, 2.0, 1.0, 0.0]
output = bubble_sorter(input)
assert output == [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
input = list(reversed(range(5000)))
output = bubble_sorter(input)
assert output == list(range(5000))

View file

@ -1,22 +0,0 @@
from code_to_optimize.bubble_sort import sorter
class TestSorter:
def setup_method(self, method):
pass
def teardown_method(self, method):
pass
def test_sort_in_pytest_class(self):
input = [5, 4, 3, 2, 1, 0]
output = sorter(input)
assert output == [0, 1, 2, 3, 4, 5]
input = [5.0, 4.0, 3.0, 2.0, 1.0, 0.0]
output = sorter(input)
assert output == [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
input = list(reversed(range(5000)))
output = sorter(input)
assert output == list(range(5000))

View file

@ -1,16 +0,0 @@
import pytest
from code_to_optimize.bubble_sort import sorter
@pytest.mark.parametrize(
"input, expected_output",
[
([5, 4, 3, 2, 1, 0], [0, 1, 2, 3, 4, 5]),
([5.0, 4.0, 3.0, 2.0, 1.0, 0.0], [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]),
(list(reversed(range(5000))), list(range(5000))),
],
)
def test_sort_parametrized(input, expected_output):
output = sorter(input)
assert output == expected_output

View file

@ -1,17 +0,0 @@
import pytest
from code_to_optimize.bubble_sort import sorter
@pytest.mark.parametrize(
"input, expected_output",
[
([5, 4, 3, 2, 1, 0], [0, 1, 2, 3, 4, 5]),
([5.0, 4.0, 3.0, 2.0, 1.0, 0.0], [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]),
(list(reversed(range(50))), list(range(50))),
],
)
def test_sort_loop_parametrized(input, expected_output):
for i in range(2):
output = sorter(input)
assert output == expected_output

View file

@ -1,94 +0,0 @@
import dill as pickle
from code_to_optimize.pig_latin import pig_latin
def log_test_values(values, test_name):
with open(f"/tmp/test_return_values.bin", "ab") as f:
return_bytes = pickle.dumps(values)
_test_name = f"{test_name}".encode("ascii")
f.write(len(_test_name).to_bytes(4, byteorder="big"))
f.write(_test_name)
f.write(len(return_bytes).to_bytes(4, byteorder="big"))
f.write(return_bytes)
def test_pig_latin_vowel():
global log_test_values
log_test_values(pig_latin("apple"), "pig_latin_test_pig_latin_vowel_0")
log_test_values(pig_latin("elephant"), "pig_latin_test_pig_latin_vowel_1")
def test_pig_latin_single_consonant():
log_test_values(pig_latin("dog"), "pig_latin_test_pig_latin_single_consonant_0")
log_test_values(pig_latin("cat"), "pig_latin_test_pig_latin_single_consonant_1")
def test_pig_latin_multiple_consonants():
log_test_values(
pig_latin("string"), "pig_latin_test_pig_latin_multiple_consonants_0"
)
log_test_values(
pig_latin("glove"), "pig_latin_test_pig_latin_multiple_consonants_1"
)
def test_pig_latin_capital_letters():
log_test_values(pig_latin("Hello"), "pig_latin_test_pig_latin_capital_letters_0")
log_test_values(pig_latin("WoRlD"), "pig_latin_test_pig_latin_capital_letters_1")
def test_pig_latin_multiple_words():
log_test_values(
pig_latin("The quick brown fox"), "pig_latin_test_pig_latin_multiple_words_0"
)
log_test_values(
pig_latin("Python is a fun language"),
"pig_latin_test_pig_latin_multiple_words_1",
)
def test_pig_latin_empty_input():
log_test_values(pig_latin(""), "pig_latin_test_pig_latin_empty_input_0")
def test_pig_latin_spaces_input():
log_test_values(pig_latin(" "), "pig_latin_test_pig_latin_spaces_input_0")
def test_pig_latin_non_alphabetic():
log_test_values(pig_latin("123"), "pig_latin_test_pig_latin_non_alphabetic_0")
log_test_values(
pig_latin("Hello, world!"), "pig_latin_test_pig_latin_non_alphabetic_1"
)
def test_pig_latin_non_ascii():
log_test_values(pig_latin("café"), "pig_latin_test_pig_latin_non_ascii_0")
log_test_values(pig_latin("über"), "pig_latin_test_pig_latin_non_ascii_1")
def test_pig_latin_hyphenated_words():
log_test_values(
pig_latin("sister-in-law"), "pig_latin_test_pig_latin_hyphenated_words_0"
)
log_test_values(
pig_latin("self-driving car"), "pig_latin_test_pig_latin_hyphenated_words_1"
)
def test_pig_latin_contractions():
log_test_values(pig_latin("can't"), "pig_latin_test_pig_latin_contractions_0")
log_test_values(pig_latin("I'm"), "pig_latin_test_pig_latin_contractions_1")
def test_pig_latin_apostrophes():
log_test_values(pig_latin("don't"), "pig_latin_test_pig_latin_apostrophes_0")
log_test_values(
pig_latin("rock 'n' roll"), "pig_latin_test_pig_latin_apostrophes_1"
)
def test_pig_latin_non_letter():
log_test_values(pig_latin("123"), "pig_latin_test_pig_latin_non_letter_0")
log_test_values(pig_latin("Hello, world!"), "pig_latin_test_pig_latin_non_letter_1")

View file

@ -1,41 +0,0 @@
from code_to_optimize.topological_sort import Graph
def test_topological_sort():
g = Graph(6)
g.addEdge(5, 2)
g.addEdge(5, 0)
g.addEdge(4, 0)
g.addEdge(4, 1)
g.addEdge(2, 3)
g.addEdge(3, 1)
assert g.topologicalSort() == [5, 4, 2, 3, 1, 0]
def test_topological_sort_2():
g = Graph(10)
for i in range(10):
for j in range(i + 1, 10):
g.addEdge(i, j)
assert g.topologicalSort() == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
g = Graph(10)
for i in range(10):
for j in range(i + 1, 10):
g.addEdge(i, j)
assert g.topologicalSort() == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
def test_topological_sort_3():
g = Graph(1000)
for i in range(1000):
for j in range(i + 1, 1000):
g.addEdge(j, i)
assert g.topologicalSort() == list(reversed(range(1000)))

Some files were not shown because too many files have changed in this diff Show more