13. Konsultacija ir užduočių aptarimas - MantsSk/CA_PTUA14 GitHub Wiki

Konsultacija ir užduočių aptarimas

War of numbers

There's a great war between the even and odd numbers. Many numbers already lost their lives in this war and it's your task to end this. You have to determine which group sums larger: the evens or the odds. The larger group wins.

Create a function that takes a list of integers, sums the even and odd numbers separately, then returns the difference between the sums of the even and odd numbers.

def war_of_numbers(numbers):
    even_sum = sum(num for num in numbers if num % 2 == 0)
    odd_sum = sum(num for num in numbers if num % 2 != 0)

    difference = even_sum - odd_sum

    if difference > 0:
        print("Evens win by", difference)
    elif difference < 0:
        print("Odds win by", abs(difference))
    else:
        print("It's a tie!")


# Example usage:
numbers_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
numbers_list2 = [1,4,4,2,5,4]
war_of_numbers(numbers_list2)

Bigrams

You are given an input array of bigrams, and an array of words. Write a function that returns True if every single bigram from this array can be found at least once in an array of words.

def can_find(bigrams, words):
    for bigram in bigrams:
        found = False
        for word in words:
            if bigram in word:
                found = True
                break
        if not found:
            return False
    return True

# or

# def can_find(bigrams, words):
#     for bigram in bigrams:
#         found = any(bigram in word for word in words)
#         if not found:
#             return False
#     return True

# Test cases
print(can_find(["at", "be", "th", "au"], ["beautiful", "the", "hat"]))  # True
print(can_find(["ay", "be", "ta", "cu"], ["maybe", "beta", "abet", "course"]))  # False
# "cu" does not exist in any of the words.
print(can_find(["th", "fo", "ma", "or"], ["the", "many", "for", "forest"]))  # True
print(can_find(["oo", "mi", "ki", "la"], ["milk", "chocolate", "cooks"]))  # False

Asmens kodo aptarimas:

Make the program generate a valid personal code (using the previously created function) based on the input of gender, date of birth, and sequence number. Information about the composition of the personal code

Example of a working validator/generator

def grazinti_asmens_kodo_kontrolinį(asmens_kodas):
    kodas = str(asmens_kodas)
    A = int(kodas[0])
    B = int(kodas[1])
    C = int(kodas[2])
    D = int(kodas[3])
    E = int(kodas[4])
    F = int(kodas[5])
    G = int(kodas[6])
    H = int(kodas[7])
    I = int(kodas[8])
    J = int(kodas[9])
    S = A * 1 + B * 2 + C * 3 + D * 4 + E * 5 + F * 6 + G * 7 + H * 8 + I * 9 + J * 1
    if S % 11 != 10:
        return S % 11
    else:
        S = A * 3 + B * 4 + C * 5 + D * 6 + E * 7 + F * 8 + G * 9 + H * 1 + I * 2 + J * 3
        if S % 11 != 10:
            return S % 11
        else:
            return 0


def asmens_kodo_validacija(asmens_kodas):
    paskutinis_sk = int(str(asmens_kodas)[-1])
    return paskutinis_sk == grazinti_asmens_kodo_kontrolinį(asmens_kodas)


def asmens_kodo_generavimas(lytis, gimimo_data, eiles_numeris):
    pirmas_skaicius = ""

    data_split = gimimo_data.split("-")
    metai = int(data_split[0][:2])

    if lytis == "vyras":
        pirmas_skaicius = str((int(metai) - 18) * 2 + 1)
    else:
        pirmas_skaicius = str((int(metai) - 18) * 2 + 2)

    metai = data_split[0][2:]
    menuo = data_split[1]
    diena = data_split[2]

    be_paskutinio = pirmas_skaicius + metai + menuo + diena + eiles_numeris

    return int(be_paskutinio + str(grazinti_asmens_kodo_kontrolinį(be_paskutinio)))


print(asmens_kodo_validacija(45102129987))
print(asmens_kodo_validacija(61907108400))

print(asmens_kodo_generavimas("vyras", "2000-12-12", "512"))

Datų programa

Parašyti programą, kuri:​

Išvestų jūsų kurso dienų sąrašą tokiu formatu:​

  • 2022 - April - 28 - Thursday​
  • 2022 - May - 02 - Monday​
  • 2022 - May - 03 - Tuesday​
  • 2022 - May - 05 - Thursday​
  • 2022 - May - 09 - Monday​

Ir t.t.​

Dienas patalpinti į list (sąrašą)

import datetime

hours = 496
lesson_length = 4
days = int(hours/lesson_length)

course_date_list = []
cancelled_dates = [
    datetime.datetime(2023,12,25),
    datetime.datetime(2023,12,26),
    datetime.datetime(2024,1,1),
    datetime.datetime(2024,3,11),
    datetime.datetime(2024,4,1),
    datetime.datetime(2024,5,1),
    datetime.datetime(2024,6,24),
]

lesson_date = datetime.datetime(2023,12,18)
hours_temp = 0

while len(course_date_list) < days:
    weekday = lesson_date.isoweekday()
    if weekday in (1,2,3,4):
        if lesson_date in cancelled_dates:
            lesson_date = lesson_date + datetime.timedelta(days=1)
        else:
            course_date_list.append(lesson_date)
            lesson_date = lesson_date + datetime.timedelta(days=1)
            hours_temp += 4
    else:
        lesson_date = lesson_date + datetime.timedelta(days=3)



for day in course_date_list:
    print(day.strftime("%Y - %B - %d - %A"))

Homework for next week:

  • Create a github.com account
  • [Optional] Add some code to github.com

Exercises