Am 18.04.23 um 17:30 schrieb Julian Gilbey:
Would you be able to share the file you were editing when you did this 
screenshot? I'd like to see if I can reproduce it on my machine.


Attached is the file.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 18 18:41:38 2023

@author: amr
"""


def solution(A):
    # Implement your solution here
    for a in range(1, 100001, 1):
        if a not in A:
            if a > 0:
                return a
            else:
                return 1


def puzzle1(a):
    return a.count(19) == 2 and a.count(5) >= 3


# print(puzzle1([19, 19, 15, 5, 3, 5, 5, 2]))
# print(puzzle1([19, 15, 15, 5, 3, 3, 5, 2]))
# print(puzzle1([19, 19, 5, 5, 5, 5, 5]))


def puzzle2(a):
    return len(a) == 8 and a.count(a[4]) == 3


# print(puzzle2([19, 19, 15, 5, 5, 5, 1, 2]))
# print(puzzle2([19, 15, 5, 7, 5, 5, 2]))
# print(puzzle2([11, 12, 14, 13, 14, 13, 15, 14]))
# print(puzzle2([19, 15, 11, 7, 5, 6, 2]))


def puzzle3(a):
    return a > 4**4 and a % 34 == 4


# print(puzzle3(922))


def piles(num_of_piles):
    list_of_piles = [num_of_piles]
    for i in range(num_of_piles - 1):
        list_of_piles.append(num_of_piles + 2)
        num_of_piles += 2
    return list_of_piles


def piles2(num_of_piles):
    return [num_of_piles + 2 * i for i in range(num_of_piles)]


# print(piles2(2))
# print(piles2(10))
# print(piles2(3))
# print(piles2(17))


def strings(list_of_strings):
    return (
        list_of_strings[len(list_of_strings) - 2]
        in list_of_strings[len(list_of_strings) - 1]
        and list_of_strings[len(list_of_strings) - 2]
        != list_of_strings[len(list_of_strings) - 1]
    )


# print(strings(["a", "abb", "sfs", "oo", "de", "sfde"]))
# print(strings(["a", "abb", "sfs", "oo", "ee", "sfde"]))
# print(
#     strings(
#         ["a", "abb", "sad", "ooaaesdfe", "sfsdfde", "sfsd", "sfsdf", "qwrew"]
#     )
# )
# print(
#     strings(
#         [
#             "a",
#             "abb",
#             "sad",
#             "ooaaesdfe",
#             "sfsdfde",
#             "sfsd",
#             "sfsdf",
#             "qwsfsdfrew",
#         ]
#     )
# )


def list_of_ints(a):
    if len(a) == 100:
        for x in a:
            if x in range(0, 1000, 10):
                return True
            else:
                return False
    else:
        return False


# print(list_of_ints([0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, 310, 320, 330, 340, 350, 360, 370, 380, 390, 400, 410, 420, 430, 440, 450, 460, 470, 480, 490,
#                     500, 510, 520, 530, 540, 550, 560, 570, 580, 590, 600, 610, 620, 630, 640, 650, 660, 670, 680, 690, 700, 710, 720, 730, 740, 750, 760, 770, 780, 790, 800, 810, 820, 830, 840, 850, 860, 870, 880, 890, 900, 910, 920, 930, 940, 950, 960, 970, 980, 990]))
# print(list_of_ints([0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240, 260, 280, 300, 320, 340, 360, 380, 400, 420, 440, 460,
#       480, 500, 520, 540, 560, 580, 600, 620, 640, 660, 680, 700, 720, 740, 760, 780, 800, 820, 840, 860, 880, 900, 920, 940, 960, 980]))


def sum_of_ints(a, i):
    return print(sum(a[:i]) == i)


# sum_of_ints([0, 1, 2, 3, 4, 5], 4)
# sum_of_ints([1, 1, 1, 1, 1, 1], 4)
# sum_of_ints([2, 2, 2, 2, 2], 4)


def split_strings(s):
    import re

    words = re.split(r"([ ,]+)", s)
    print(words)


# split_strings("The dance, held in the school gym, ended at midnight.")


def list_of_distinct_ints(nums):
    # return all([nums[i] != nums[i + 1] for i in range(len(nums)-1)]) and len(set(nums)) == 4

    if len(set(nums)) == 4:
        for i in range(len(nums) - 1):
            if nums[i] != nums[i + 1]:
                return True
            else:
                return False
    else:
        return False


# print(list_of_distinct_ints([1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]))
# print(list_of_distinct_ints([1, 2, 3, 3, 1, 2, 3, 3, 1, 2, 3, 3, 1, 2, 3, 3]))
# print(list_of_distinct_ints([1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]))


def test(combined):
    li = []
    st = ""
    for s in combined.replace(" ", ""):
        st += s
        if st.count("(") == st.count(")"):
            li.append(st)
            st = ""
    return li


# test('( ()) ((()()())) (()) ()')


def indices(list_of_nums, thres):
    return [i for i, x in enumerate(list_of_nums) if x < thres]
    # list_of_indices = []
    # for i, x in enumerate(list_of_nums):
    #     if x < thres:
    #         list_of_indices.append(i)
    # return list_of_indices


# print(indices([0, 12, 45, 3, 4923, 322, 105, 29, 15, 39, 55], 100))
# print(indices([0, 12, 4, 3, 49, 9, 1, 5, 3], 10))


def check_palindrome(list_of_strings):
    return [x == x[::-1] for x in list_of_strings]
    # list_of_bool = []
    # for x in list_of_strings:
    #     list_of_bool.append(x == x[::-1])
    # return list_of_bool


# print(check_palindrome(['palindrome', 'madamimadam', '', 'foo', 'eyes']))


def find_strings(list_of_strings, prefix):
    return [x for x in list_of_strings if x.startswith(prefix)]


# print(find_strings(['cat', 'car', 'fear', 'center'], 'ca'))
# print(find_strings(['cat', 'dog', 'shatter', 'donut', 'at', 'todo'], 'do'))


def length_of_strings(list_of_strings):
    # return [len(string) for string in list_of_strings]
    return list(map(len, list_of_strings))


# print(length_of_strings(['cat', 'car', 'fear', 'center']))
# print(length_of_strings(['cat', 'dog', 'shatter', 'donut', 'at', 'todo', '']))


def longest_string(list_of_strings):
    return max(list_of_strings, key=len)


# print(longest_string(['cat', 'car', 'fear', 'center']))
# print(longest_string(['cat', 'dog', 'shatter', 'donut', 'at', 'todo', '']))


def find_4_positive_ints(n):
    for a in range(n, 0, -1):
        if a % 2 != 0:
            continue
        for b in range(n - a, 0, -1):
            if b % 2 != 0:
                continue
            for c in range(n - a - b, 0, -1):
                if c % 2 != 0:
                    continue
                for d in range(n - a - b - c, 0, -1):
                    if d % 2 != 0:
                        continue
                    if a + b + c + d == n:
                        return [a, b, c, d]


# print(find_4_positive_ints(2356))
# print(find_4_positive_ints(5534))
# print(find_4_positive_ints(2412))
# print(find_4_positive_ints(36892))


def strip_string(string):
    return string.replace("-", " ").replace("_", " ").strip()


# print(strip_string('Python-Exercises'))
# print(strip_string('Python_Exercises'))
# print(strip_string('-Hello,_world!__This_is-so-easy!-'))


def find_parens_depth(parens):
    return [len(s.split(")")[0]) for s in parens.split()]


# print('parens_depth', find_parens_depth(' (()) (()) () ((()()())) '))
# print('parens_depth', find_parens_depth('() (()) () () () ()'))
# print('parens_depth', find_parens_depth(
#     ' (((((((()))))))) () (()) ((()()()))'))


def sort_nums(nums):
    if len(nums) < 2:
        return nums
    result = []
    for i in range(len(nums) // 2):
        result.append(min(nums))
        nums.remove(min(nums))
        result.append(max(nums))
        nums.remove(max(nums))
    if len(nums) > 0:
        result.append(nums[0])
    if len(result) < 2 * len(nums):
        result.extend(
            nums[
                len(result) // 2
                + 1 : len(result) // 2
                + 1
                + len(nums)
                - len(result)
            ]
        )
    return result
    # sorted_list = []

    # min_num = min(list_of_nums)
    # max_num = max(list_of_nums)
    # sorted_list.append(min_num)
    # sorted_list.append(max_num)
    # list_of_nums.remove(min_num)
    # list_of_nums.remove(max_num)

    # min_num = min(list_of_nums)
    # max_num = max(list_of_nums)
    # sorted_list.append(min_num)
    # sorted_list.append(max_num)
    # list_of_nums.remove(min_num)
    # list_of_nums.remove(max_num)

    # min_num = min(list_of_nums)
    # max_num = max(list_of_nums)
    # sorted_list.append(min_num)
    # sorted_list.append(max_num)
    # list_of_nums.remove(min_num)
    # list_of_nums.remove(max_num)

    # sorted_list.extend(list_of_nums)
    # return sorted_list


# print(sort_nums([1, 3, 4, 5, 11]))
# print(sort_nums([27, 3, 8, 5, 1, 31]))
# print(sort_nums([1, 2, 7, 3, 4, 5, 6]))


def unique_nums_zero_sum(n):
    """
    Return a list of n random unique integers whose sum equals zero.

    Parameters
    ----------
    n : int
        Number of unique integers (length of list).

    Returns
    -------
    nums : list
        List of unique integers whose sum equals zero.
    """

    def unique_nums():
        """
        Generate n-1 random unique integers without additive inverses.

        Returns
        -------
        nums : list
            List of n-1 unique integers.
        """
        import random as r

        nums = []
        for i in range(n - 1):
            x = r.randint(-2 * n, 2 * n)
            while x in nums or -x in nums:
                x = r.randint(-2 * n, 2 * n)
            else:
                nums.append(x)
        return nums

    nums = unique_nums()  # a list of n-1 unique integers

    while (
        -sum(nums) in nums
        or sum(nums) in nums
        or -sum(nums) not in range(-2 * n, 2 * n + 1)
    ):
        nums = unique_nums()
    else:
        nums.append(-sum(nums))
    return nums


print(unique_nums_zero_sum(10500))

Reply via email to