diff --git a/README.md b/README.md index 644ef36..001ed8e 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,39 @@ # اصول طراحی الگوریتم + +📅 ترم بهمن ۱۴۰۲ +
+ +### لیست دانلود +
+ + + + + + + + + + + + + + + + +
عنوانسوالاتپاسخلینک‌های مربوطهتوضیحات
تمرین سری یک + دانلود + + به زودی آپلود می‌شود + + لینک مربوط به سوال 3
+ لینک مربوط به سوال 4 +
برگزار شده در ۲۷ فروردین
+ + +

+ 📅 ترم مهر ۱۴۰۲
diff --git a/src/1402-2/practices/1/q3.py b/src/1402-2/practices/1/q3.py new file mode 100644 index 0000000..150f558 --- /dev/null +++ b/src/1402-2/practices/1/q3.py @@ -0,0 +1,24 @@ +import math + +c = 2 + +def multiply(x, y): + if y == 0: + return 0 + else: + return multiply(c * x, math.floor(y / c)) + x * (y % c) + +# def multiply(x, y, counter): +# if y == 0: +# return 0 +# else: +# counter[0] += 1 +# print(f"x={x}, y={y}, [y/c]={ math.floor(y / c)}, x * (y % c) = {x * (y % c)}") +# return multiply(c * x, math.floor(y / c), counter) + x * (y % c) + +a = 4 +b = 31 +# counter = [0] +# print(multiply(a, b, counter)) +print(multiply(a, b)) +# print(counter[0]) \ No newline at end of file diff --git a/src/1402-2/practices/1/q4.py b/src/1402-2/practices/1/q4.py new file mode 100644 index 0000000..3d6d8d6 --- /dev/null +++ b/src/1402-2/practices/1/q4.py @@ -0,0 +1,29 @@ +def find_majority_element(nums): + def majority_element_in_range(low, high): + if low == high: + return nums[low] + + mid = (low + high) // 2 + + left_majority = majority_element_in_range(low, mid) + right_majority = majority_element_in_range(mid + 1, high) + + if left_majority == right_majority: + return left_majority + + left_count = sum(1 for i in range(low, high + 1) if nums[i] == left_majority) + right_count = sum(1 for i in range(low, high + 1) if nums[i] == right_majority) + + if left_count > (high - low + 1) // 2: + return left_majority + elif right_count > (high - low + 1) // 2: + return right_majority + else: + return None + + return majority_element_in_range(0, len(nums) - 1) + +arr = [3, 3, 4, 2, 4, 4, 2, 4, 4] +print("Majority element:", find_majority_element(arr)) + +# time complexity: O(n log n) \ No newline at end of file