20240505

I left "Fluent Pythoin" at my parents' house. I should have brought it here; it's super useful for a Python programmer like me.

References should be checked when reading Wikipedia, but I don't have a plenty of time, so won't do it for now.

Duck test

The duck test is a form of abductive reasoning. This is its usual expression: If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.

The test implies that a person can identify an unknown subject by observing that subject's habitual characteristics. It is sometimes used to counter abstruse arguments that something is not what it appears to be.

Dock typing

In computer programming, duck typing is an application of the duck test, "If it walks like a duck and it quacks like a duck, then it must be a duck", to determine whether an object can be used for a particular purpose. With nominative typing, an object is of a given type if it is declared as such (or if a type's association with the object is inferred through mechanisms such as object inheritance). With duck typing, an object is of a given type if it has all methods and properties required by that type. Duck typing may be viewed as a usage-based structural equivalence between a given object and the requirements of a type.

My understanding

Duck typing in Pythoon is a concept where the type of class of an object is less important than the methods it defines.

Instead of explicitly checking an object's type, you check if it has the necessary methods or attributes to perform a certain action. This allows for more flexible and adaptable code.

The advantages of that in Python is polymorphism.

Duck typing facilitates polymorphism, where we can use objects of different types interchangeably as long as they have the required functionality. The flexibility of types suits well with dynamic typing in Python; the type of a varialbe is determined at runtime instead of at compile time.

class SpottedSeal:
    def bark(self):
        print("wawawa")


class RingedSeal:
    def bark(self):
        print("wooooo")


class Human:
    pass


animals = [SpottedSeal(), RingedSeal(), Human()]
for animal in animals:
    try:
        animal.bark()  # types are not related as long as they implement bark()
    except AttributeError:
        print(f"{animal} does not bark.")

# OUTPUT
# wawawa
# wooooo
# <__main__.Human object at 0x10415e420> does not bark.

Codeforces Round 943 (Div. 3)

1968A - Maximize?

1 <= y < x return y that maximizes gcd(x, y) + y

After testing with various input, I noticed that y = x - 1 is always one of the optimal answers.

I tried to rationalize this, but since I'm bad at math, it's not possible.

for _ in range(int(input())):
    x = int(input())
    print(x - 1)

Editorial

It is a well-known fact that gcd(a, b) = gcd(a - b, b) for a >= b

Applying it to our formula, we get gcd(x - y, y) + y

Using the fact that gcd(x - y, y) <= x - y, we get gcd(x - y, y) + y <= x - y + y = x. # This is the upper bound

Hence, gcd(x, y) + y <= x for 1 <= y < x.

For y = x - 1, we have gcd(x, x - 1) + x - 1 = 1 + x - 1 = x, which is the maximal possible value.

Things to remember

Both are obvious after consideration, but better keep them in the accessible part of my brain.

how to tackle this problem

  1. Find the upper bound of the target formula
  2. Find a value that results in the upper bound

Is this a common approach? I better keep that in mind.

1968B. Prefiquence

Two pointers problem. I thought about what I would do if I needed to do that manually, and implemented it as a program.

from collections import defaultdict


for _ in range(int(input())):
    n, m = map(int, input().split())
    a = input()
    b = input()

    a_i = b_i = 0
    while a_i < len(a) and b_i < len(b):
        if a[a_i] == b[b_i]:
            a_i += 1
        b_i += 1
    print(a_i)

1968C. Assembly via Remain

x_i = a_i mod a_(i-1)

possible a_i ->

There's a constraint on a

1 <= a_i <= 10 ^ 9 for all 1 <= i <= n

but even with a_i = x_i + a_(i-1), the values of a stays in the scope.

Also, if a_i becomes too small to make the remainder x (i.e., a_(i-1) <= x), it will be impossible to construct the rest elements of the answer. So, if a is too small, I add the minimal value to make the remainder x for all elements.

The first element of a is (x_0 + 1), which is the smallest and can be inferred by looking at the sample output

# there is a constraint on the max(a), but thinking about n <= 500, x <= 500, this algorith, won't exceed 250000 < 10 ^ 9

for _ in range(int(input())):
    _ = int(input())
    x_from_2 = list(map(int, input().split()))

    # O(n)
    minimal_divisor = max(x_from_2) + 1

    a = [x_from_2[0] + 1]
    for x in x_from_2:
        val = x + a[-1]
        while val < minimal_divisor:
            val += a[-1]
        a.append(val)
    print(" ".join([str(e) for e in a]))

Avocado rolls 300 Seafood salad 300 Salmon donburi 800 Yogurt 200 Kombucha 100 Proteim chips 150 Bubble tea 600

Total 2450 kcal is this due to stress???


index 20240504 20240506