benchmarks.wiki / Public workspace

BigCodeBench / BigCodeBench v0.1.0_hf 242c23e7-5a89-5db9-be8e-753f576a8547

Problem

Answer published by the source. Consult the official source to check your work against its answer.

complete prompt

import numpy as np
import secrets
import hashlib
import base64

def task_func(num, from_base, to_base, alphabet):
    """
    Converts a number from one base to another, adds a random salt, hashes the result using SHA-256,
    and then encodes the hash in base64 using a custom alphabet. The function also returns the used salt.

    Parameters:
    num (str): The number to be converted, represented as a string.
    from_base (int): The base of the number to be converted.
    to_base (int): The base to convert the number to.
    alphabet (str): The custom alphabet to be used for base64 encoding. Each character in the provided alphabet
        represents a value in the base64 encoding scheme. For example, the standard base64 alphabet is:
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".
        The function uses this alphabet to encode the hash of the converted number. The length of the alphabet
        determines the possible characters in the resulting base64-encoded hash.

    Returns:
    tuple: A tuple containing the base64-encoded hash of the converted number and the used salt.

    Raises:
    ValueError: If `from_base` or `to_base` is less than 2, indicating an invalid base for conversion.
    ValueError: If the `num` string contains characters not valid in the `from_base` specified, indicating an invalid number format for conversion.

    Requirements:
    - numpy
    - secrets
    - hashlib
    - base64

    Examples:
    Convert a hexadecimal number to octal, hash it using SHA-256, and return the base64-encoded hash and salt using a custom alphabet.
    >>> alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"
    >>> encoded, salt = task_func('A1', 16, 8, alphabet)
    >>> isinstance(encoded, str) and isinstance(salt, str)
    True

    Verify that different invocations produce different results due to the random salt.
    >>> result1, salt1 = task_func('FF', 16, 8, alphabet)
    >>> result2, salt2 = task_func('FF', 16, 8, alphabet)
    >>> result1 != result2
    True
    """

instruct prompt

Converts a number from one base to another, adds a random salt, hashes the result using SHA-256, and then encodes the hash in base64 using a custom alphabet. The function also returns the used salt. Verify that different invocations produce different results due to the random salt. >>> result1, salt1 = task_func('FF', 16, 8, alphabet) >>> result2, salt2 = task_func('FF', 16, 8, alphabet) >>> result1 != result2 True
The function should raise the exception for: ValueError: If `from_base` or `to_base` is less than 2, indicating an invalid base for conversion. ValueError: If the `num` string contains characters not valid in the `from_base` specified, indicating an invalid number format for conversion.
The function should output with:
    tuple: A tuple containing the base64-encoded hash of the converted number and the used salt.
You should write self-contained code starting with:

Code

import numpy as np
import secrets
import hashlib
import base64
def task_func(num, from_base, to_base, alphabet):

code prompt

Code

import numpy as np
import secrets
import hashlib
import base64
def task_func(num, from_base, to_base, alphabet):

entry point

task_func

libs

  • secrets
  • base64
  • hashlib
  • numpy

Discussion

Discussion

No discussion posts on this page yet. Share a minimal failing example, an algorithm with its complexity, or a reproducible command and result. Use the posting template.

See answer Answer published by the source

Artifacts

Code, notes and reproducible work shared by participants. Files are served from a separate origin.

No artifacts on this page yet. Share reproducible code or notes in a contribution. Share a minimal failing example, an algorithm with its complexity, or a reproducible command and result. Use the posting template.

Source and history

Official source

initial import