|
| 1 | +import inspect |
| 2 | +from typing import Tuple, Dict |
| 3 | + |
| 4 | +# 1. custom_power |
| 5 | +# x sadece pozisyonel (positional-only) olduğu için '/' öncesinde yer alır. |
| 6 | +# e pozisyonel veya anahtar kelime olabileceği için '/' sonrasındadır. |
| 7 | +custom_power = lambda x=0, /, e=1: x**e |
| 8 | + |
| 9 | + |
| 10 | +# 2. custom_equation |
| 11 | +# x ve y sadece pozisyonel (/), a ve b her ikisi de olabilir, c sadece anahtar kelime (*) |
| 12 | +def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int = 1) -> float: |
| 13 | + """ |
| 14 | + Belirtilen formülü hesaplar: (x**a + y**b) / c |
| 15 | +
|
| 16 | + :param x: İlk taban değeri, varsayılan 0. |
| 17 | + :type x: int |
| 18 | + :param y: İkinci taban değeri, varsayılan 0. |
| 19 | + :type y: int |
| 20 | + :param a: x için üs, varsayılan 1. |
| 21 | + :type a: int |
| 22 | + :param b: y için üs, varsayılan 1. |
| 23 | + :type b: int |
| 24 | + :param c: Bölen, varsayılan 1. |
| 25 | + :type c: int |
| 26 | + :return: Hesaplamanın sonucu. |
| 27 | + :rtype: float |
| 28 | + """ |
| 29 | + return float((x**a + y**b) / c) |
| 30 | + |
| 31 | + |
| 32 | +# 3. fn_w_counter |
| 33 | +# Fonksiyonun çağrılma sayısını ve çağıran modülün bilgisini tutar. |
| 34 | +def fn_w_counter() -> Tuple[int, Dict[str, int]]: |
| 35 | + # Fonksiyonun kendi üzerinde değişkenler (state) oluşturarak sayaçları tutuyoruz. |
| 36 | + if not hasattr(fn_w_counter, 'total_calls'): |
| 37 | + fn_w_counter.total_calls = 0 |
| 38 | + fn_w_counter.caller_info = {} |
| 39 | + |
| 40 | + # Toplam çağrı sayısını artır |
| 41 | + fn_w_counter.total_calls += 1 |
| 42 | + |
| 43 | + # Çağıranın (caller) __name__ bilgisini al (örn: '__main__') |
| 44 | + frame = inspect.currentframe().f_back |
| 45 | + caller_name = frame.f_globals.get('__name__', 'unknown') |
| 46 | + |
| 47 | + # Çağıran modülün sayacını güncelle |
| 48 | + fn_w_counter.caller_info[caller_name] = fn_w_counter.caller_info.get(caller_name, 0) + 1 |
| 49 | + |
| 50 | + return fn_w_counter.total_calls, fn_w_counter.caller_info |
| 51 | + |
| 52 | + |
| 53 | +# Görseldeki Test Senaryoları |
| 54 | +if __name__ == '__main__': |
| 55 | + # custom_power testleri |
| 56 | + assert custom_power(2) == 2 |
| 57 | + assert custom_power(2, 3) == 8 |
| 58 | + assert custom_power(2, e=2) == 4 |
| 59 | + |
| 60 | + # custom_equation testleri |
| 61 | + assert custom_equation(2, 3) == 5.0 |
| 62 | + assert custom_equation(2, 3, 2) == 7.0 |
| 63 | + assert custom_equation(2, 3, 2, 3) == 31.0 |
| 64 | + assert custom_equation(3, 5, a=2, b=3, c=4) == 33.5 |
| 65 | + assert custom_equation(3, 5, 2, b=3, c=4) == 33.5 |
| 66 | + assert custom_equation(3, 5, 2, 3, c=4) == 33.5 |
| 67 | + |
| 68 | + # fn_w_counter testleri |
| 69 | + for i in range(10): |
| 70 | + fn_w_counter() |
| 71 | + |
| 72 | + assert fn_w_counter() == (11, {'__main__': 11}) |
| 73 | + print("Tüm testler başarıyla geçildi!") |
0 commit comments