cabbage972/GitChameleon-2.0 huggingface dataset:
Library: torch==1.9.0 (Python 3.7, released 2021-06)
Additional dependencies: scipy==1.7.3 numpy==1.21.6

Problem: Calculate the logarithm of the cumulative distribution function of the standard normal distribution using available functions. If not available in PyTorch, use another library.

Starting code:
import torch
def log_ndtr(input_tensor: torch.Tensor) -> torch.Tensor:


Solution:
    import numpy as np
    from scipy.stats import norm
    output = torch.from_numpy(norm.logcdf(input_tensor.numpy()))
    return output

Test:
from scipy.stats import norm
input_tensor = torch.linspace(-10, 10, steps=20)
expected_result = torch.tensor([-5.3231e+01, -4.3150e+01, -3.4164e+01, -2.6270e+01, -1.9462e+01,
        -1.3734e+01, -9.0731e+00, -5.4610e+00, -2.8617e+00, -1.2062e+00,
        -3.5572e-01, -5.8874e-02, -4.2585e-03, -1.1471e-04, -1.0854e-06,
        -3.5303e-09, -3.9019e-12, -1.4546e-15, -1.8203e-19, -7.6199e-24],
       dtype=torch.float64)
assert torch.allclose(log_ndtr(input_tensor), expected_result, rtol=1e-3, atol=1e-3)
Library: torch==1.9.0 (Python 3.7, released 2021-06)
Additional dependencies: scipy==1.7.3 numpy==1.21.6

Problem: Calculate the natural logarithm of the absolute value of the gamma function using PyTorch's special functions if available in this version, otherwise you may use another library.

Starting code:
import torch
def gamma_ln(input_tensor: torch.Tensor) -> torch.Tensor:


Solution:
    import numpy as np
    from scipy.special import gammaln as scipy_gammaln
    output = torch.from_numpy(scipy_gammaln(input_tensor.numpy()))
    return output

Test:
input_tensor = torch.linspace(0, 10, steps=10)
expected_result = torch.Tensor([float('inf'),-0.0545,0.1092,1.0218,2.3770,4.0476,5.9637,8.0806,10.3675,12.8018])
assert torch.allclose(gamma_ln(input_tensor), expected_result, rtol=1e-3, atol=1e-3)
Library: torch==1.9.0 (Python 3.7, released 2021-06)
Additional dependencies: scipy==1.7.3 numpy==1.21.6

Problem: Calculate the error function using PyTorch's special functions if available in this version, otherwise you may use another library.

Starting code:
import torch
def erf(input_tensor: torch.Tensor) -> torch.Tensor:


Solution:
    import numpy as np
    from scipy.special import erf as scipy_erf
    output = torch.from_numpy(scipy_erf(input_tensor.numpy()))
    return output

Test:
input_tensor = torch.linspace(0, 10, steps=10)
expected_result = torch.Tensor([0.0000,0.8839,0.9983,1.0000,1.0000,1.0000,1.0000,1.0000,1.0000,1.0000])
assert torch.allclose(erf(input_tensor), expected_result, rtol=1e-3, atol=1e-3)
Library: torch==1.9.0 (Python 3.7, released 2021-06)
Additional dependencies: scipy==1.7.3 numpy==1.21.6

Problem: Calculate the complementary error function using PyTorch's special functions if available in this version, otherwise you may use another library.

Starting code:
import torch
def erfc(input_tensor: torch.Tensor) -> torch.Tensor:


Solution:
    import numpy as np
    from scipy.special import erfc as scipy_erfc
    output = torch.from_numpy(scipy_erfc(input_tensor.numpy()))
    return output

Test:
input_tensor = torch.linspace(0, 10, steps=10)
expected_result = torch.Tensor([1.0000e+00,1.1610e-01,1.6740e-03,2.4285e-06,3.2702e-10,3.9425e-15,4.1762e-21,3.8452e-28,3.0566e-36,1.4013e-45])
assert torch.allclose(erfc(input_tensor), expected_result, rtol=1e-3, atol=1e-3)
Library: torch==1.9.0 (Python 3.7, released 2021-06)
Additional dependencies: scipy==1.7.3 numpy==1.21.6

Problem: Calculate the modified Bessel function of the first kind, order 0 using PyTorch's special functions if available in this version, otherwise you may use another library.

Starting code:
import torch
def bessel_i0(input_tensor: torch.Tensor) -> torch.Tensor:


Solution:
    import numpy as np
    from scipy.special import i0 as scipy_i0
    output = torch.from_numpy(scipy_i0(input_tensor.numpy()))
    return output

Test:
input_tensor = torch.linspace(0, 10, steps=10)
expected_result = torch.Tensor([1.0000e+00,1.3333e+00,2.6721e+00,6.4180e+00,1.6648e+01,4.4894e+01,1.2392e+02,3.4740e+02,9.8488e+02,2.8157e+03])
assert torch.allclose(bessel_i0(input_tensor), expected_result, rtol=1e-3, atol=1e-3)
Library: torch==1.9.0 (Python 3.7, released 2021-06)
Additional dependencies: scipy==1.7.3 numpy==1.21.6

Problem: Calculate the modified Bessel function of the first kind, order 1 using PyTorch's special functions if available in this version, otherwise you may use another library.

Starting code:
import torch
def bessel_i1(input_tensor: torch.Tensor) -> torch.Tensor:


Solution:
    import numpy as np
    from scipy.special import i1 as scipy_i1
    output = torch.from_numpy(scipy_i1(input_tensor.numpy()))
    return output

Test:
input_tensor = torch.linspace(0, 10, steps=10)
expected_result = torch.Tensor([0.0000e+00,6.4581e-01,1.9536e+00,5.3391e+00,1.4628e+01,4.0623e+01,1.1420e+02,3.2423e+02,9.2770e+02,2.6710e+03])
assert torch.allclose(bessel_i1(input_tensor), expected_result, rtol=1e-3, atol=1e-3)
Library: torch==1.10.0 (Python 3.7, released 2021-10)

Problem: Calculate the natural logarithm of the absolute value of the gamma function using pytorch's special functions if available in this version, otherwise you may use another library.

Starting code:
import torch
def gamma_ln(input_tensor: torch.Tensor) -> torch.Tensor:
 

Solution:
    return torch.special.gammaln(input_tensor)

Test:
input_tensor = torch.linspace(0, 10, steps=10)
expected_result = torch.Tensor([torch.inf,-0.0545,0.1092,1.0218,2.3770,4.0476,5.9637,8.0806,10.3675,12.8018])
assert torch.allclose(gamma_ln(input_tensor), expected_result, rtol=1e-3, atol=1e-3)
Library: torch==1.10.0 (Python 3.7, released 2021-10)

Problem: Calculate the error function using pytorch's special functions if available in this version, otherwise you may use another library.

Starting code:
import torch
def erf(input_tensor: torch.Tensor) -> torch.Tensor:


Solution:
    return torch.special.erf(input_tensor)

Test:
input_tensor = torch.linspace(0, 10, steps=10)
expected_result = torch.Tensor([0.0000,0.8839,0.9983,1.0000,1.0000,1.0000,1.0000,1.0000,1.0000,1.0000])
assert torch.allclose(erf(input_tensor), expected_result, rtol=1e-3, atol=1e-3)
Library: torch==1.10.0 (Python 3.7, released 2021-10)

Problem: Calculate the complementary error function using pytorch's special functions if available in this version, otherwise you may use another library.

Starting code:
import torch
def erfc(input_tensor: torch.Tensor) -> torch.Tensor:


Solution:
    return torch.special.erfc(input_tensor)

Test:
input_tensor = torch.linspace(0, 10, steps=10)
expected_result = torch.Tensor([1.0000e+00,1.1610e-01,1.6740e-03,2.4285e-06,3.2702e-10,3.9425e-15,4.1762e-21,3.8452e-28,3.0566e-36,1.4013e-45])
assert torch.allclose(erfc(input_tensor), expected_result, rtol=1e-3, atol=1e-3)
Library: torch==1.10.0 (Python 3.7, released 2021-10)

Problem: Calculate the modified Bessel function of the first kind, order 0 using pytorch's special functions if available in this version, otherwise you may use another library.

Starting code:
import torch
def bessel_i0(input_tensor: torch.Tensor) -> torch.Tensor:


Solution:
    return torch.special.i0(input_tensor)

Test:
input_tensor = torch.linspace(0, 10, steps=10)
expected_result = torch.Tensor([1.0000e+00,1.3333e+00,2.6721e+00,6.4180e+00,1.6648e+01,4.4894e+01,1.2392e+02,3.4740e+02,9.8488e+02,2.8157e+03])
assert torch.allclose(bessel_i0(input_tensor), expected_result, rtol=1e-3, atol=1e-3)
Library: torch==1.10.0 (Python 3.7, released 2021-10)

Problem: Calculate the modified Bessel function of the first kind, order 1 using pytorch's special functions if available in this version, otherwise you may use another library.

Starting code:
import torch
def bessel_i1(input_tensor: torch.Tensor) -> torch.Tensor:


Solution:
    return torch.special.i1(input_tensor)

Test:
input_tensor = torch.linspace(0, 10, steps=10)
expected_result = torch.Tensor([0.0000e+00,6.4581e-01,1.9536e+00,5.3391e+00,1.4628e+01,4.0623e+01,1.1420e+02,3.2423e+02,9.2770e+02,2.6710e+03])
assert torch.allclose(bessel_i1(input_tensor), expected_result, rtol=1e-3, atol=1e-3)
Library: torch==1.10.0 (Python 3.7, released 2021-10)

Problem: You are given two tensors, `tensor1` and `tensor2`, both of shape `(n,)`. Your task is to create a boolean mask indicating whether each element of `tensor1` is less than the corresponding element of `tensor2`, and then invert this mask. Store the answer in a variable named mask.

Starting code:
import torch
def invert_mask(tensor1: torch.Tensor, tensor2: torch.Tensor) -> torch.BoolTensor:


Solution:
    return ~(tensor1 < tensor2)

Test:
tensor1 = torch.Tensor([1, 2, 3])
tensor2 = torch.Tensor([3, 1, 2])
expected_mask=torch.Tensor([False, True, True])
assert torch.all(torch.eq(invert_mask(tensor1, tensor2), expected_mask))
Library: torch==1.12.0 (Python 3.10, released 2022-06)

Problem: Calculate the logarithm of the cumulative distribution function of the standard normal distribution using PyTorch's special functions.

Starting code:
import torch
def log_ndtr(input_tensor: torch.Tensor) -> torch.Tensor:


Solution:
    return torch.special.log_ndtr(input_tensor)

Test:
input_tensor = torch.linspace(-10, 10, steps=20)
expected_result = torch.tensor([-5.3231e+01, -4.3150e+01, -3.4164e+01, -2.6270e+01, -1.9462e+01,
        -1.3734e+01, -9.0731e+00, -5.4610e+00, -2.8617e+00, -1.2062e+00,
        -3.5572e-01, -5.8874e-02, -4.2585e-03, -1.1471e-04, -1.0854e-06,
        -3.5303e-09, -3.9019e-12, -1.4546e-15, -1.8203e-19, -7.6199e-24])

assert torch.allclose(log_ndtr(input_tensor), expected_result, rtol=1e-3, atol=1e-3)
Library: torch==1.13 (Python 3.10, released 2022-10)

Problem: You are given two tensors, `tensor1` and `tensor2`, both of shape `(n,)`. Your task is to create a boolean mask indicating whether each element of `tensor1` is less than the corresponding element of `tensor2`, and then invert this mask. Store the answer in a variable named mask.

Starting code:
import torch
def invert_mask(tensor1: torch.Tensor, tensor2: torch.Tensor) -> torch.BoolTensor:


Solution:
    return ~(tensor1 < tensor2).bool()

Test:
tensor1 = torch.Tensor([1, 2, 3])
tensor2 = torch.Tensor([3, 1, 2])
expected_mask=torch.Tensor([False, True, True])
assert torch.all(torch.eq(invert_mask(tensor1, tensor2), expected_mask))
Library: torch==1.13 (Python 3.10, released 2022-10)

Problem: You are given an audio signal represented as a 1D tensor `audio_signal`. Your task is to compute the Short-Time Fourier Transform (STFT) of the signal. Do not return a complex data type.

Starting code:
import torch
def stft(audio_signal: torch.Tensor, n_fft: int) -> torch.Tensor:


Solution:
    return torch.stft(audio_signal, n_fft=n_fft, return_complex=False)

Test:
audio_signal = torch.rand(1024)
n_fft=128
expected_shape = (65, 33, 2)
assert stft(audio_signal, n_fft).shape == expected_shape
Library: torch==2 (Python 3.10, released 2023-05)

Problem: You are given an audio signal represented as a 1D tensor `audio_signal`. Your task is to compute the Short-Time Fourier Transform (STFT) of the signal. Do not return a complex data type.

Starting code:
import torch
def stft(audio_signal: torch.Tensor, n_fft: int) -> torch.Tensor:


Solution:
    return torch.view_as_real(torch.stft(audio_signal, n_fft=n_fft, return_complex=True))

Test:
audio_signal = torch.rand(1024)
n_fft=128
expected_shape = (65, 33, 2)
assert stft(audio_signal, n_fft).shape == expected_shape
Library: torch==1.13 (Python 3.10, released 2022-10)

Problem: You are given a spectrogram represented as a 3D tensor `spectrogram` with dimensions `(65, 33, 2)`, where the first dimension represents the frequency bins, the second dimension represents the time frames, and the third dimension represents the real and imaginary parts of the complex values. Your task is to compute the Inverse Short-Time Fourier Transform (ISTFT) of the spectrogram using PyTorch's `torch.istft` function.

Starting code:
import torch
def istft(spectrogram: torch.Tensor, signal: torch.Tensor, n_fft: int, hop_length: int, win_length: int, normalized=False) -> torch.Tensor:


Solution:
    return torch.istft(spectrogram, n_fft=n_fft, hop_length=hop_length, win_length=win_length, window=torch.hann_window(win_length), length=signal.shape[0], normalized=False)

Test:

# Sample rate (samples per second)
fs = 8000  
# Duration of the signal in seconds
t = 1  
# Time axis for the signal
time = torch.linspace(0, t, steps=int(fs * t))
# Frequency of the sine wave in Hz
frequency = 440  
# Generate a sine wave
signal = torch.sin(2 * torch.pi * frequency * time)
n_fft = 1024  # Number of FFT points
hop_length = 256  # Number of samples between successive frames
win_length = 1024  # Window length
# Compute STFT
spectrogram = torch.stft(signal, n_fft=n_fft, hop_length=hop_length, win_length=win_length, window=torch.hann_window(win_length), normalized=False, return_complex=True)
expected_shape=torch.Size([8000])
assert expected_shape == istft(spectrogram, signal, n_fft, hop_length, win_length).shape
Library: torch==2 (Python 3.10, released 2023-05)

Problem: You are given a spectrogram represented as a 3D tensor `spectrogram` with dimensions `(65, 33, 2)`, where the first dimension represents the frequency bins, the second dimension represents the time frames, and the third dimension represents the real and imaginary parts of the complex values. Your task is to compute the Inverse Short-Time Fourier Transform (ISTFT) of the spectrogram using PyTorch's `torch.istft` function.

Starting code:
import torch
def istft(spectrogram: torch.Tensor, signal: torch.Tensor, n_fft: int, hop_length: int, win_length: int, normalized=False) -> torch.Tensor:


Solution:
    return torch.istft(torch.view_as_complex(spectrogram), n_fft=n_fft, hop_length=hop_length, win_length=win_length, window=torch.hann_window(win_length), length=signal.shape[0], normalized=False)

Test:

# Sample rate (samples per second)
fs = 8000  
# Duration of the signal in seconds
t = 1  
# Time axis for the signal
time = torch.linspace(0, t, steps=int(fs * t))
# Frequency of the sine wave in Hz
frequency = 440  
# Generate a sine wave
signal = torch.sin(2 * torch.pi * frequency * time)
n_fft = 1024  # Number of FFT points
hop_length = 256  # Number of samples between successive frames
win_length = 1024  # Window length
# Compute STFT
spectrogram = torch.stft(signal, n_fft=n_fft, hop_length=hop_length, win_length=win_length, window=torch.hann_window(win_length), normalized=False, return_complex=False)
expected_shape=torch.Size([8000])
assert expected_shape == istft(spectrogram, signal, n_fft, hop_length, win_length).shape
Library: geopandas==0.10.0 (Python 3.10, released 2021-10)
Additional dependencies: rtree==0.9.3

Problem: Write a function that performs a spatial join.

Starting code:
import geopandas as gpd
from shapely.geometry import Point, Polygon

def spatial_join(gdf1 : gpd.GeoDataFrame, gdf2 : gpd.GeoDataFrame) -> gpd.GeoDataFrame:
    return 

Solution:
gpd.sjoin(gdf1, gdf2, predicate='within')

Test:

gdf1 = gpd.GeoDataFrame({'geometry': [Point(1, 1), Point(2, 2), Point(3, 3)]})
polygons = [Polygon([(0, 0), (0, 4), (4, 4), (4, 0)]), Polygon([(4, 4), (4, 8), (8, 8), (8, 4)])]
gdf2 = gpd.GeoDataFrame({'geometry': polygons})
expected = gpd.GeoDataFrame({
    'geometry': [Point(1, 1), Point(2, 2), Point(3, 3)],
    'index_right': [0, 0, 0]
})
assertion_value = spatial_join(gdf1, gdf2).equals(expected)
assert assertion_value
Library: geopandas==0.9.0 (Python 3.10, released 2021-02)
Additional dependencies: rtree==0.9.3 shapely==1.8.5

Problem: Write a function that performs a spatial join.

Starting code:
import geopandas as gpd
from shapely.geometry import Point, Polygon

def spatial_join(gdf1 : gpd.GeoDataFrame, gdf2 : gpd.GeoDataFrame) -> gpd.GeoDataFrame:
    return 

Solution:
gpd.sjoin(gdf1, gdf2, op='within')

Test:

gdf1 = gpd.GeoDataFrame({'geometry': [Point(1, 1), Point(2, 2), Point(3, 3)]})
polygons = [Polygon([(0, 0), (0, 4), (4, 4), (4, 0)]), Polygon([(4, 4), (4, 8), (8, 8), (8, 4)])]
gdf2 = gpd.GeoDataFrame({'geometry': polygons})
expected_result = gpd.GeoDataFrame({
    'geometry': [Point(1, 1), Point(2, 2), Point(3, 3)],
    'index_right': [0, 0, 0]
})
assert spatial_join(gdf1, gdf2).equals(expected_result)
Library: geopandas==0.10.0 (Python 3.10, released 2021-10)

Problem: Write a function that performs a union.

Starting code:
import geopandas as gpd
from shapely.geometry import box

def perform_union(gdf : gpd.GeoDataFrame) -> gpd.GeoSeries:
    return 

Solution:
gdf.geometry.unary_union

Test:

gdf = gpd.GeoDataFrame({'geometry': [box(0, 0, 2, 5), box(0, 0, 2, 1)]})
from shapely.geometry import Polygon
coords = [
    (2, 0),
    (0, 0),
    (0, 1),
    (0, 5),
    (2, 5),
    (2, 1),
    (2, 0)
]
expected_result = Polygon(coords)
assert perform_union(gdf).equals(expected_result)
Library: geopandas==0.9.0 (Python 3.10, released 2021-02)
Additional dependencies: shapely==1.8.5

Problem: Write a function that performs a union.

Starting code:
import geopandas as gpd
from shapely.geometry import box

def perform_union(gdf: gpd.GeoDataFrame) -> gpd.GeoSeries:
    return 

Solution:
gdf.geometry.cascaded_union

Test:

gdf = gpd.GeoDataFrame({'geometry': [box(0, 0, 2, 5), box(0, 0, 2, 1)]})
from shapely.geometry import Polygon
coords = [
    (2, 0),
    (0, 0),
    (0, 1),
    (0, 5),
    (2, 5),
    (2, 1),
    (2, 0)
]
expected_result = Polygon(coords)
assert perform_union(gdf) == expected_result
Library: geopandas==0.10.0 (Python 3.10, released 2021-10)

Problem: Write a function that creates a GeoSeries from x and y coordinates.

Starting code:
import geopandas as gpd
def create_geoseries(x: list[int], y: list[int]) -> gpd.GeoSeries:
    return 

Solution:
gpd.GeoSeries.from_xy(x, y)

Test:

from shapely.geometry import Point
x, y = [1, 2], [3, 4]
expected_result = gpd.GeoSeries([Point(1, 3), Point(2, 4)])
assert list(create_geoseries(x, y)) == list(expected_result)
Library: geopandas==0.9.0 (Python 3.10, released 2021-02)
Additional dependencies: shapely==1.8.5

Problem: Write a function that creates a GeoSeries from x and y coordinates.

Starting code:
import geopandas as gpd
def create_geoseries(x:list[int], y:list[int]) -> gpd.GeoSeries:
    return 

Solution:
gpd.points_from_xy(x, y)

Test:

from shapely.geometry import Point
x, y = [1, 2], [3, 4]
expected_result = gpd.GeoSeries([Point(1, 3), Point(2, 4)])
assert list(create_geoseries(x, y)) == list(expected_result)
Library: geopandas==0.13.0 (Python 3.10, released 2023-05)
Additional dependencies: rtree==1.0.1

Problem: Write a function that performs a spatial query.

Starting code:
import geopandas as gpd
from shapely.geometry import Point, Polygon, box

def spatial_query(gdf:gpd.GeoDataFrame, other:gpd.GeoDataFrame) -> gpd.GeoDataFrame:
    combined_geometry = other.unary_union
    return 

Solution:
gdf.sindex.query(combined_geometry)

Test:

gdf = gpd.GeoDataFrame({'geometry': [Point(1, 2)]})
other = gpd.GeoDataFrame({'geometry': [Point(1,1)]})
result = spatial_query(gdf, other)
expected_result = []
assert (result == expected_result).all()
Library: geopandas==0.10.0 (Python 3.10, released 2021-10)
Additional dependencies: rtree==0.9.3

Problem: Write a function that performs a spatial query.

Starting code:
import geopandas as gpd
from shapely.geometry import Point, Polygon

def spatial_query(gdf:gpd.GeoDataFrame, other:gpd.GeoSeries) -> gpd.GeoDataFrame:
    return 

Solution:
gdf.sindex.query_bulk(other)

Test:

gdf = gpd.GeoDataFrame({'geometry': [Point(1, 1), Point(2, 2), Point(3, 3)]})
other = gpd.GeoSeries([Polygon([(0, 0), (0, 4), (4, 4), (4, 0)])])
result = spatial_query(gdf, other)
import numpy as np
expected_result = np.array([
    [0, 0, 0],
    [0, 1, 2]
])
assert (result == expected_result).all()
Library: nltk==3.6.3 (Python 3.10, released 2021-09)

Problem: Write a function that displays usage information of an object.

Starting code:
import nltk
import io
import contextlib

def show_usage(obj:object) -> str:
    with io.StringIO() as buf, contextlib.redirect_stdout(buf):

Solution:

        nltk.usage(obj)
        return buf.getvalue()

Test:

assert "LazyModule supports the following operations" in show_usage(nltk.corpus)
Library: networkx==2.8 (Python 3.10, released 2022-04)

Problem: Write a function that uses NetworkX's greedy_modularity_communities with the number of communities set at 5.

Starting code:
import networkx as nx
def modularity_communities(G:nx.Graph) -> list:
    return nx.community.greedy_modularity_communities(G,

Solution:
cutoff=5)

Test:
G = nx.karate_club_graph()
result = [
    frozenset({8, 14, 15, 18, 20, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33}),
    frozenset({1, 2, 3, 7, 9, 12, 13, 17, 21}),
    frozenset({0, 16, 4, 5, 6, 10, 11}),
    frozenset({19}),
    frozenset({22})
]
assertion_value = len(modularity_communities(G)) > 0 and len(modularity_communities(G)) == len(result)
assert assertion_value
Library: networkx==2.7 (Python 3.10, released 2022-02)
Additional dependencies: numpy==1.21.2

Problem: Write a function that uses NetworkX's greedy_modularity_communities with the number of communities set at 5.

Starting code:
import networkx as nx
def modularity_communities(G:nx.Graph) -> list:
    return nx.community.greedy_modularity_communities(G,

Solution:
n_communities=5)

Test:
G = nx.karate_club_graph()
result = [
    frozenset({8, 14, 15, 18, 20, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33}),
    frozenset({1, 2, 3, 7, 9, 12, 13, 17, 21}),
    frozenset({0, 16, 4, 5, 6, 10, 11}),
    frozenset({19}),
    frozenset({22})
]
assertion_value = len(modularity_communities(G)) > 0 and len(modularity_communities(G)) == len(result)
assert assertion_value
Library: networkx==2.8 (Python 3.10, released 2022-04)

Problem: Write a function that calculates the diameters' extreme distance of a graph.

Starting code:
import networkx as nx
def bounding_distance(G:nx.Graph) -> int:
    return nx.diameter

Solution:
(G, usebounds=True)

Test:
G = nx.path_graph(5)
result = 4
assertion_value = bounding_distance(G) is not None and result == bounding_distance(G)
assert assertion_value
Library: networkx==2.6 (Python 3.10, released 2021-07)

Problem: Write a function that calculates the diameters' extreme distance of a graph.

Starting code:
import networkx as nx
def bounding_distance(G:nx.Graph) -> int:
    return nx.algorithms.distance_measures.

Solution:
extrema_bounding(G, "diameter")

Test:

G = nx.path_graph(5)
result = 4
assert bounding_distance(G) is not None and result == bounding_distance(G)
Library: networkx==2.5 (Python 3.10, released 2019-10)

Problem: Write a function that returns the naive greedy modularity communities for a graph.

Starting code:
import networkx as nx
def naive_modularity_communities(G:nx.Graph) -> list:
    return nx.community.

Solution:
naive_greedy_modularity_communities(G)

Test:
G = nx.karate_club_graph()
naive_modularity_communities_result = 3
assert len(list(naive_modularity_communities(G))) > 0 and len(list(naive_modularity_communities(G))) == naive_modularity_communities_result
Library: networkx==2.4 (Python 3.10, released 2020-08)

Problem: Write a function that returns the naive greedy modularity communities for a graph.

Starting code:
import networkx as nx
def naive_modularity_communities(G:nx.Graph) -> list:
    return nx.community.

Solution:
_naive_greedy_modularity_communities(G)

Test:
G = nx.karate_club_graph()
naive_modularity_communities_result = 3
assert len(list(naive_modularity_communities(G))) > 0 and len(list(naive_modularity_communities(G))) == naive_modularity_communities_result
Library: networkx==2.5 (Python 3.10, released 2019-10)

Problem: Write a function that returns the nodes as a list of NetworkX graph.

Starting code:
import networkx as nx
def get_nodes(G:nx.Graph) -> list:
   return 

Solution:
list(G.nodes)

Test:

G = nx.karate_club_graph()
nodes_result = 34

assert get_nodes(G) is not None and len(get_nodes(G)) > 0 and len(get_nodes(G)) == nodes_result