From 13bc98fe8bc443d38e73ac15fd6932101c29741d Mon Sep 17 00:00:00 2001
From: HiyaaT <120657191+HiyaaT@users.noreply.github.com>
Date: Tue, 7 Jan 2025 16:30:56 +0530
Subject: [PATCH 01/12] Create process_mixed_data_types.md
---
process_mixed_data_types.md | 192 ++++++++++++++++++++++++++++++++++++
1 file changed, 192 insertions(+)
create mode 100644 process_mixed_data_types.md
diff --git a/process_mixed_data_types.md b/process_mixed_data_types.md
new file mode 100644
index 0000000..9264339
--- /dev/null
+++ b/process_mixed_data_types.md
@@ -0,0 +1,192 @@
+---
+title: Process Mixed Data Types
+---
+
+# Problem Statement
+
+Write a function process_data(data: dict) -> dict that takes a dictionary with the following keys and performs the described operations:
+'int': Add 15 to the integer value.
+'float': Multiply the float value by 1.5.
+'str': Convert the string to uppercase.
+'list': Reverse the list.
+'set': Add the number 100 to the set.
+'dict': Add a new key-value pair 'hello': 'haha'.
+
+**Example**
+```py3
+data = {
+ 'int': 5,
+ 'float': 2.5,
+ 'str': 'hello',
+ 'list': [1, 2, 3],
+ 'set': {1, 2, 3},
+ 'dict': {'a': 1, 'b': 2}
+}
+process_data(data)
+# Output:
+{
+ 'int': 20,
+ 'float': 3.75,
+ 'str': 'HELLO',
+ 'list': [3, 2, 1],
+ 'set': {1, 2, 3, 100},
+ 'dict': {'a': 1, 'b': 2, 'hello': 'haha'}
+}
+
+```
+
+# Solution
+
+```py3 test.py -r 'python test.py'
+
+# some prefix
+
+
+def process_data(data: dict) -> dict:
+ '''
+ Perform operations on mixed data types within a dictionary.
+
+ Arguments:
+ data: dict - a dictionary containing keys: 'int', 'float', 'str', 'list', 'set', and 'dict'.
+
+ Return:
+ dict - the updated dictionary after processing.
+ '''
+ ...
+
+ data['int'] += 15
+ data['float'] *= 1.5
+ data['str'] = data['str'].upper()
+ data['list'] = data['list'][::-1]
+ data['set'].add(100)
+ data['dict']['hello'] = 'haha'
+ return data
+
+ test = ...'test' #tests
+
+
+# some suffix
+
+
+{% include '../function_type_and_modify_check_suffix.py.jinja' %}
+
+
+```
+
+# Public Test Cases
+
+## Input 1
+
+```
+data = {
+ 'int': 10,
+ 'float': 4.0,
+ 'str': 'python',
+ 'list': [5, 6, 7],
+ 'set': {8, 9},
+ 'dict': {'x': 1}
+}
+is_equal(
+ process_data(data),
+ {
+ 'int': 25,
+ 'float': 6.0,
+ 'str': 'PYTHON',
+ 'list': [7, 6, 5],
+ 'set': {8, 9, 100},
+ 'dict': {'x': 1, 'hello': 'haha'}
+ }
+)
+
+```
+
+## Output 1
+
+```
+ {
+ 'int': 25,
+ 'float': 6.0,
+ 'str': 'PYTHON',
+ 'list': [7, 6, 5],
+ 'set': {8, 9, 100},
+ 'dict': {'x': 1, 'hello': 'haha'}
+ }
+```
+
+## Input 2
+
+```
+data = {
+ 'int': 0,
+ 'float': 1.0,
+ 'str': 'test',
+ 'list': [],
+ 'set': set(),
+ 'dict': {}
+}
+is_equal(
+ process_data(data),
+ {
+ 'int': 15,
+ 'float': 1.5,
+ 'str': 'TEST',
+ 'list': [],
+ 'set': {100},
+ 'dict': {'hello': 'haha'}
+ }
+)
+
+```
+
+## Output 2
+
+```
+ {
+ 'int': 15,
+ 'float': 1.5,
+ 'str': 'TEST',
+ 'list': [],
+ 'set': {100},
+ 'dict': {'hello': 'haha'}
+ }
+```
+
+# Private Test Cases
+
+## Input 1
+
+```
+data = {
+ 'int': -5,
+ 'float': 0.0,
+ 'str': 'abcd',
+ 'list': [1],
+ 'set': {42},
+ 'dict': {'k1': 'v1'}
+}
+is_equal(
+ process_data(data),
+ {
+ 'int': 10,
+ 'float': 0.0,
+ 'str': 'ABCD',
+ 'list': [1],
+ 'set': {42, 100},
+ 'dict': {'k1': 'v1', 'hello': 'haha'}
+ }
+)
+
+```
+
+## Output 1
+
+```
+ {
+ 'int': 10,
+ 'float': 0.0,
+ 'str': 'ABCD',
+ 'list': [1],
+ 'set': {42, 100},
+ 'dict': {'k1': 'v1', 'hello': 'haha'}
+ }
+```
From 6c49aa76f5397f7fb0d612e0115af9926c02a0f3 Mon Sep 17 00:00:00 2001
From: HiyaaT <120657191+HiyaaT@users.noreply.github.com>
Date: Tue, 7 Jan 2025 16:50:39 +0530
Subject: [PATCH 02/12] Create group_words_by_length.md
---
group_words_by_length.md | 103 +++++++++++++++++++++++++++++++++++++++
1 file changed, 103 insertions(+)
create mode 100644 group_words_by_length.md
diff --git a/group_words_by_length.md b/group_words_by_length.md
new file mode 100644
index 0000000..b7f0be0
--- /dev/null
+++ b/group_words_by_length.md
@@ -0,0 +1,103 @@
+---
+title: Group Words by Length
+---
+
+# Problem Statement
+
+Given a list of words, group them by their lengths and return a dictionary where the keys are the lengths, and the values are lists of words with that length.
+**Example**
+```
+words = ["apple", "bat", "car", "door", "ant", "elephant"]
+
+```
+The output should be:
+{3: ["bat", "car", "ant"], 5: ["apple"], 4: ["door"], 8: ["elephant"]}
+
+
+# Solution
+
+```py3 test.py -r 'python test.py'
+
+def group_words_by_length(words: list) -> dict:
+ '''
+ Groups words by their lengths.
+
+ Arguments:
+ words: list - a list of strings.
+
+ Return:
+ dict - a dictionary with lengths as keys and lists of words as values.
+ '''
+ ...
+
+ from collections import defaultdict
+ length_dict = defaultdict(list)
+ for word in words:
+ length_dict[len(word)].append(word)
+ return dict(length_dict)
+
+
+
+
+{% include '../function_type_and_is_equal_suffix.py.jinja' %}
+
+
+```
+
+# Public Test Cases
+
+## Input 1
+
+```
+words = ["cat", "dog", "bird", "fish", "horse"]
+is_equal(
+ group_words_by_length(words),
+ {3: ["cat", "dog"], 4: ["bird", "fish"], 5: ["horse"]}
+)
+
+```
+
+## Output 1
+
+```
+{3: ["cat", "dog"], 4: ["bird", "fish"], 5: ["horse"]}
+
+```
+
+## Input 2
+
+```
+words = ["a", "to", "the", "quick", "brown", "fox"]
+is_equal(
+ group_words_by_length(words),
+ {1: ["a"], 2: ["to"], 3: ["the"], 5: ["quick"], 4: ["brown", "fox"]}
+)
+
+```
+
+## Output 2
+
+```
+{1: ["a"], 2: ["to"], 3: ["the"], 5: ["quick"], 4: ["brown", "fox"]}
+
+```
+
+# Private Test Cases
+
+## Input 1
+
+```
+words = ["blue", "red", "yellow", "pink", "orange"]
+is_equal(
+ group_words_by_length(words),
+ {4: ["blue", "pink"], 3: ["red"], 6: ["yellow"], 6: ["orange"]}
+)
+
+```
+
+## Output 1
+
+```
+{4: ["blue", "pink"], 3: ["red"], 6: ["yellow", "orange"]}
+
+```
From 1be1ff635c0dd1c89f496f339bc7037083ec8e19 Mon Sep 17 00:00:00 2001
From: HiyaaT <120657191+HiyaaT@users.noreply.github.com>
Date: Tue, 7 Jan 2025 16:51:58 +0530
Subject: [PATCH 03/12] Rename group_words_by_length.md to
Q1_group_words_by_length.md
---
group_words_by_length.md => Q1_group_words_by_length.md | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename group_words_by_length.md => Q1_group_words_by_length.md (100%)
diff --git a/group_words_by_length.md b/Q1_group_words_by_length.md
similarity index 100%
rename from group_words_by_length.md
rename to Q1_group_words_by_length.md
From 20321000b1c439363947a1050f3f5fae3b351afd Mon Sep 17 00:00:00 2001
From: HiyaaT <120657191+HiyaaT@users.noreply.github.com>
Date: Tue, 7 Jan 2025 16:52:27 +0530
Subject: [PATCH 04/12] Rename process_mixed_data_types.md to
Q2_process_mixed_data_types.md
---
process_mixed_data_types.md => Q2_process_mixed_data_types.md | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename process_mixed_data_types.md => Q2_process_mixed_data_types.md (100%)
diff --git a/process_mixed_data_types.md b/Q2_process_mixed_data_types.md
similarity index 100%
rename from process_mixed_data_types.md
rename to Q2_process_mixed_data_types.md
From 9dfddcbec35eb7809862509da21104e104655841 Mon Sep 17 00:00:00 2001
From: HiyaaT <120657191+HiyaaT@users.noreply.github.com>
Date: Tue, 7 Jan 2025 17:22:41 +0530
Subject: [PATCH 05/12] Create Q3_unique_length_of3_palindrome_subsequence.md
---
...nique_length_of3_palindrome_subsequence.md | 137 ++++++++++++++++++
1 file changed, 137 insertions(+)
create mode 100644 Q3_unique_length_of3_palindrome_subsequence.md
diff --git a/Q3_unique_length_of3_palindrome_subsequence.md b/Q3_unique_length_of3_palindrome_subsequence.md
new file mode 100644
index 0000000..a0b5e59
--- /dev/null
+++ b/Q3_unique_length_of3_palindrome_subsequence.md
@@ -0,0 +1,137 @@
+---
+title: Unique Length-3 Palindromic Subsequences
+---
+
+# Problem Statement
+
+Given a string s, find the number of unique palindromic subsequences of length 3 in the string. A subsequence is defined as a sequence derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
+
+**Note:**
+
+**Input Format:** A single string s (1 ≤ |s| ≤ 10^4) consisting of lowercase English letters.
+
+**Output Format:** he count of unique palindromic subsequences of length 3.
+
+**Sample Input**
+```
+aabca
+```
+**Sample Output**
+```
+3
+```
+
+# Solution
+```py test.py -r 'python test.py'
+
+def count_unique_palindromes(s):
+ """
+ Count the number of unique length-3 palindromic subsequences in the string.
+
+ Args:
+ s (str): The input string.
+
+ Returns:
+ int: The count of unique palindromic subsequences of length 3.
+ """
+ # Initialize a set to store unique characters
+ unique_chars = set(s)
+ count = 0
+
+ # Iterate through all unique characters
+ for letter in unique_chars:
+ first_index = -1
+ last_index = -1
+
+ # Find the first and last occurrence of the letter
+ for i, char in enumerate(s):
+ if char == letter:
+ if first_index == -1:
+ first_index = i
+ last_index = i
+
+ # Collect all characters between first and last occurrences
+ between_elements = set(s[first_index + 1:last_index])
+
+ # Add the count of unique characters between first and last
+ count += len(between_elements)
+
+ return count
+
+
+# Input parsing
+s = input().strip()
+
+# Output the result
+print(count_unique_palindromes(s))
+
+
+
+```
+
+# Public Test Cases
+
+## Input 1
+
+```
+aabca
+```
+
+## Output 1
+
+```
+3
+```
+
+
+## Input 2
+
+```
+aaa
+```
+
+## Output 2
+
+```
+1
+```
+
+
+## Input 3
+
+```
+abc
+```
+
+## Output 3
+
+```
+0
+```
+
+
+# Private Test Cases
+
+## Input 1
+
+```
+abccba
+```
+
+## Output 1
+
+```
+3
+```
+
+## Input 2
+
+```
+xyzzyx
+```
+
+## Output 2
+
+```
+3
+```
From a53bec86f52cb200a72f1a38b1712287ba0cabcf Mon Sep 17 00:00:00 2001
From: HiyaaT <120657191+HiyaaT@users.noreply.github.com>
Date: Tue, 7 Jan 2025 17:43:14 +0530
Subject: [PATCH 06/12] Create Q4_peak_element_in_mountain_array.md
---
Q4_peak_element_in_mountain_array.md | 107 +++++++++++++++++++++++++++
1 file changed, 107 insertions(+)
create mode 100644 Q4_peak_element_in_mountain_array.md
diff --git a/Q4_peak_element_in_mountain_array.md b/Q4_peak_element_in_mountain_array.md
new file mode 100644
index 0000000..87540e9
--- /dev/null
+++ b/Q4_peak_element_in_mountain_array.md
@@ -0,0 +1,107 @@
+---
+title:Peak Element in a Mountain Array
+---
+
+# Problem Statement
+
+A mountain array is defined as an array where:
+
+1. The elements initially increase to a single peak element.
+2. The elements then strictly decrease.
+You are given a mountain array arr. Your task is to find the index of the peak element using binary search to achieve efficient performance.
+
+3. The peak element is the one that is greater than its neighbors.
+
+**Example**
+```
+input: arr = [1, 2, 3, 1]
+```
+Output : 2 which is index of element 3
+
+
+# Solution
+
+```py3 test.py -r 'python test.py'
+
+def peak_index_in_mountain_array(arr: list[int]) -> int:
+ """
+ Find the peak element in a mountain array using binary search.
+
+ Args:
+ arr (list[int]): A list of integers representing the mountain array.
+
+ Returns:
+ int: The index of the peak element.
+ """
+ left, right = 0, len(arr) - 1
+
+ while left < right:
+ mid = left + (right - left) // 2
+ if arr[mid] > arr[mid + 1]:
+ right = mid
+ else:
+ left = mid + 1
+
+ return left
+
+
+
+# Input handling (for testing purposes only)
+if __name__ == "__main__":
+ arr = list(map(int, input().strip().split()))
+ print(peak_index_in_mountain_array(arr))
+
+
+```
+
+# Public Test Cases
+
+## Input 1
+
+```
+arr = [1, 2, 1, 3, 5, 6, 4, 2]
+```
+
+## Output 1
+
+```
+5
+```
+
+## Input 2
+
+```
+arr = [10, 20, 30, 40, 50, 60, 50, 40, 30, 20, 10]
+```
+
+## Output 2
+
+```
+5
+```
+
+## Input 3
+
+```
+arr = [1, 2, 3, 4, 5, 4, 3, 2, 1]
+```
+
+## Output 3
+
+```
+4
+```
+
+# Private Test Cases
+
+## Input 1
+
+```
+arr = [5, 6, 7, 8, 9, 5, 3, 1]
+```
+
+## Output 1
+
+```
+4
+```
From d114d3149c72242c7429843f4613dc9be5010b69 Mon Sep 17 00:00:00 2001
From: HiyaaT <120657191+HiyaaT@users.noreply.github.com>
Date: Wed, 8 Jan 2025 16:02:39 +0530
Subject: [PATCH 07/12] Rename Q1_group_words_by_length.md to
Q2_group_words_by_length.md
---
Q1_group_words_by_length.md => Q2_group_words_by_length.md | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename Q1_group_words_by_length.md => Q2_group_words_by_length.md (100%)
diff --git a/Q1_group_words_by_length.md b/Q2_group_words_by_length.md
similarity index 100%
rename from Q1_group_words_by_length.md
rename to Q2_group_words_by_length.md
From d3155e6686621209d3e4037ab2568671a3b859d6 Mon Sep 17 00:00:00 2001
From: HiyaaT <120657191+HiyaaT@users.noreply.github.com>
Date: Wed, 8 Jan 2025 16:03:09 +0530
Subject: [PATCH 08/12] Rename Q2_process_mixed_data_types.md to
Q1_process_mixed_data_types.md
---
Q2_process_mixed_data_types.md => Q1_process_mixed_data_types.md | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename Q2_process_mixed_data_types.md => Q1_process_mixed_data_types.md (100%)
diff --git a/Q2_process_mixed_data_types.md b/Q1_process_mixed_data_types.md
similarity index 100%
rename from Q2_process_mixed_data_types.md
rename to Q1_process_mixed_data_types.md
From 59c2912120c13ebb9494ebf0855bcc7668efcb19 Mon Sep 17 00:00:00 2001
From: HiyaaT <120657191+HiyaaT@users.noreply.github.com>
Date: Wed, 8 Jan 2025 16:21:49 +0530
Subject: [PATCH 09/12] Update and rename Q4_peak_element_in_mountain_array.md
to Q4_crptography.md
---
Q4_crptography.md | 140 +++++++++++++++++++++++++++
Q4_peak_element_in_mountain_array.md | 107 --------------------
2 files changed, 140 insertions(+), 107 deletions(-)
create mode 100644 Q4_crptography.md
delete mode 100644 Q4_peak_element_in_mountain_array.md
diff --git a/Q4_crptography.md b/Q4_crptography.md
new file mode 100644
index 0000000..90e9bb3
--- /dev/null
+++ b/Q4_crptography.md
@@ -0,0 +1,140 @@
+---
+title:Cryptographic Shift Decoder
+---
+
+# Problem Statement
+
+You are tasked with building a cryptographic decoder that deciphers a string encoded with a shift cipher. In a shift cipher, each letter in the plaintext is shifted by a fixed number of positions in the alphabet. For example, with a shift of 3, A becomes D, B becomes E, and so on. The decryption reverses this process.
+
+Your task is to:
+
+->Identify the most common letter in the encoded text (assuming this corresponds to E in plaintext, as it is the most frequent letter in English).
+->Deduce the shift value based on this information.
+->Decode the entire string using the deduced shift value.
+
+# Functions to Implement
+
+**find_most_frequent_letter(encoded: str) -> str**
+
+Input: Encoded string containing uppercase English letters and spaces.
+Output: The most frequent letter in the string.
+**deduce_shift(most_frequent: str) -> int**
+
+Input: Most frequent letter from the encoded text.
+Output: The shift value used to encode the text.
+
+**decrypt(encoded: str, shift: int) -> str**
+Input: Encoded string and the deduced shift value.
+Output: Decoded plaintext string.
+
+
+**Example**
+```
+Input: encoded = "KHOOR ZRUOG"
+
+```
+Output : HELLO WORLD
+
+
+# Solution
+
+```py3 test.py -r 'python test.py'
+
+from collections import Counter
+
+def find_most_frequent_letter(encoded: str) -> str:
+ """
+ Identify the most frequent letter in the encoded text.
+ Ignore spaces.
+ """
+ freq = Counter([char for char in encoded if char.isalpha()])
+ return freq.most_common(1)[0][0]
+
+def deduce_shift(most_frequent: str) -> int:
+ """
+ Deduce the shift value based on the most frequent letter.
+ Assuming the most frequent letter corresponds to 'E'.
+ """
+ return (ord(most_frequent) - ord('E')) % 26
+
+def decrypt(encoded: str, shift: int) -> str:
+ """
+ Decrypt the encoded text using the deduced shift value.
+ """
+ decoded = []
+ for char in encoded:
+ if char.isalpha():
+ shifted_char = chr((ord(char) - ord('A') - shift) % 26 + ord('A'))
+ decoded.append(shifted_char)
+ else:
+ decoded.append(char)
+ return ''.join(decoded)
+
+# Input handling (for testing purposes only)
+if __name__ == "__main__":
+ encoded = input("Enter encoded text: ").strip().upper()
+ most_frequent = find_most_frequent_letter(encoded)
+ shift = deduce_shift(most_frequent)
+ print("Decoded Text:", decrypt(encoded, shift))
+
+
+```
+
+# Public Test Cases
+
+## Input 1
+
+```
+encoded = "XLMW MW XLI SRHITXMSR"
+
+```
+
+## Output 1
+
+```
+Decoded Text: THIS IS THE ENCRYPTION
+
+```
+
+## Input 2
+
+```
+encoded = "GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT"
+```
+
+## Output 2
+
+```
+Decoded Text: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
+
+```
+
+## Input 3
+
+```
+encoded = "WKH FDW LV RQ WKH URRI"
+
+```
+
+## Output 3
+
+```
+Decoded Text: THE CAT IS ON THE ROOF
+
+```
+
+# Private Test Cases
+
+## Input 1
+
+```
+encoded = "ZHOFRPH WR WKH FUBSWRJUDSKB FKDOOHQJH"
+
+```
+
+## Output 1
+
+```
+Decoded Text: WELCOME TO THE CRYPTOGRAPHY CHALLENGE
+
+```
diff --git a/Q4_peak_element_in_mountain_array.md b/Q4_peak_element_in_mountain_array.md
deleted file mode 100644
index 87540e9..0000000
--- a/Q4_peak_element_in_mountain_array.md
+++ /dev/null
@@ -1,107 +0,0 @@
----
-title:Peak Element in a Mountain Array
----
-
-# Problem Statement
-
-A mountain array is defined as an array where:
-
-1. The elements initially increase to a single peak element.
-2. The elements then strictly decrease.
-You are given a mountain array arr. Your task is to find the index of the peak element using binary search to achieve efficient performance.
-
-3. The peak element is the one that is greater than its neighbors.
-
-**Example**
-```
-input: arr = [1, 2, 3, 1]
-```
-Output : 2 which is index of element 3
-
-
-# Solution
-
-```py3 test.py -r 'python test.py'
-
-def peak_index_in_mountain_array(arr: list[int]) -> int:
- """
- Find the peak element in a mountain array using binary search.
-
- Args:
- arr (list[int]): A list of integers representing the mountain array.
-
- Returns:
- int: The index of the peak element.
- """
- left, right = 0, len(arr) - 1
-
- while left < right:
- mid = left + (right - left) // 2
- if arr[mid] > arr[mid + 1]:
- right = mid
- else:
- left = mid + 1
-
- return left
-
-
-
-# Input handling (for testing purposes only)
-if __name__ == "__main__":
- arr = list(map(int, input().strip().split()))
- print(peak_index_in_mountain_array(arr))
-
-
-```
-
-# Public Test Cases
-
-## Input 1
-
-```
-arr = [1, 2, 1, 3, 5, 6, 4, 2]
-```
-
-## Output 1
-
-```
-5
-```
-
-## Input 2
-
-```
-arr = [10, 20, 30, 40, 50, 60, 50, 40, 30, 20, 10]
-```
-
-## Output 2
-
-```
-5
-```
-
-## Input 3
-
-```
-arr = [1, 2, 3, 4, 5, 4, 3, 2, 1]
-```
-
-## Output 3
-
-```
-4
-```
-
-# Private Test Cases
-
-## Input 1
-
-```
-arr = [5, 6, 7, 8, 9, 5, 3, 1]
-```
-
-## Output 1
-
-```
-4
-```
From 021f5a5f27be33c809d67dd5f632ef6f6f66ba2e Mon Sep 17 00:00:00 2001
From: HiyaaT <120657191+HiyaaT@users.noreply.github.com>
Date: Wed, 8 Jan 2025 18:35:33 +0530
Subject: [PATCH 10/12] Update and rename Q4_crptography.md to
Q4_magic_matrix.md
---
Q4_crptography.md | 140 ------------------------------------
Q4_magic_matrix.md | 174 +++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 174 insertions(+), 140 deletions(-)
delete mode 100644 Q4_crptography.md
create mode 100644 Q4_magic_matrix.md
diff --git a/Q4_crptography.md b/Q4_crptography.md
deleted file mode 100644
index 90e9bb3..0000000
--- a/Q4_crptography.md
+++ /dev/null
@@ -1,140 +0,0 @@
----
-title:Cryptographic Shift Decoder
----
-
-# Problem Statement
-
-You are tasked with building a cryptographic decoder that deciphers a string encoded with a shift cipher. In a shift cipher, each letter in the plaintext is shifted by a fixed number of positions in the alphabet. For example, with a shift of 3, A becomes D, B becomes E, and so on. The decryption reverses this process.
-
-Your task is to:
-
-->Identify the most common letter in the encoded text (assuming this corresponds to E in plaintext, as it is the most frequent letter in English).
-->Deduce the shift value based on this information.
-->Decode the entire string using the deduced shift value.
-
-# Functions to Implement
-
-**find_most_frequent_letter(encoded: str) -> str**
-
-Input: Encoded string containing uppercase English letters and spaces.
-Output: The most frequent letter in the string.
-**deduce_shift(most_frequent: str) -> int**
-
-Input: Most frequent letter from the encoded text.
-Output: The shift value used to encode the text.
-
-**decrypt(encoded: str, shift: int) -> str**
-Input: Encoded string and the deduced shift value.
-Output: Decoded plaintext string.
-
-
-**Example**
-```
-Input: encoded = "KHOOR ZRUOG"
-
-```
-Output : HELLO WORLD
-
-
-# Solution
-
-```py3 test.py -r 'python test.py'
-
-from collections import Counter
-
-def find_most_frequent_letter(encoded: str) -> str:
- """
- Identify the most frequent letter in the encoded text.
- Ignore spaces.
- """
- freq = Counter([char for char in encoded if char.isalpha()])
- return freq.most_common(1)[0][0]
-
-def deduce_shift(most_frequent: str) -> int:
- """
- Deduce the shift value based on the most frequent letter.
- Assuming the most frequent letter corresponds to 'E'.
- """
- return (ord(most_frequent) - ord('E')) % 26
-
-def decrypt(encoded: str, shift: int) -> str:
- """
- Decrypt the encoded text using the deduced shift value.
- """
- decoded = []
- for char in encoded:
- if char.isalpha():
- shifted_char = chr((ord(char) - ord('A') - shift) % 26 + ord('A'))
- decoded.append(shifted_char)
- else:
- decoded.append(char)
- return ''.join(decoded)
-
-# Input handling (for testing purposes only)
-if __name__ == "__main__":
- encoded = input("Enter encoded text: ").strip().upper()
- most_frequent = find_most_frequent_letter(encoded)
- shift = deduce_shift(most_frequent)
- print("Decoded Text:", decrypt(encoded, shift))
-
-
-```
-
-# Public Test Cases
-
-## Input 1
-
-```
-encoded = "XLMW MW XLI SRHITXMSR"
-
-```
-
-## Output 1
-
-```
-Decoded Text: THIS IS THE ENCRYPTION
-
-```
-
-## Input 2
-
-```
-encoded = "GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT"
-```
-
-## Output 2
-
-```
-Decoded Text: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
-
-```
-
-## Input 3
-
-```
-encoded = "WKH FDW LV RQ WKH URRI"
-
-```
-
-## Output 3
-
-```
-Decoded Text: THE CAT IS ON THE ROOF
-
-```
-
-# Private Test Cases
-
-## Input 1
-
-```
-encoded = "ZHOFRPH WR WKH FUBSWRJUDSKB FKDOOHQJH"
-
-```
-
-## Output 1
-
-```
-Decoded Text: WELCOME TO THE CRYPTOGRAPHY CHALLENGE
-
-```
diff --git a/Q4_magic_matrix.md b/Q4_magic_matrix.md
new file mode 100644
index 0000000..837a76c
--- /dev/null
+++ b/Q4_magic_matrix.md
@@ -0,0 +1,174 @@
+---
+title:Magic Matrix Finder
+---
+
+# Problem Statement
+A magic matrix is a square matrix where the sum of every row, column, and both diagonals is the same. Your task is to write a program that determines if the given matrix is a magic matrix.
+
+You need to implement the following functions:
+
+# Functions to Implement
+
+**is_square_matrix(matrix: List[List[int]]) -> bool**
+
+Input: A list of lists matrix, where each sublist represents a row of the matrix.
+Output: Returns True if the matrix is square (i.e., the number of rows equals the number of columns), otherwise False.
+
+**is_magic_matrix(matrix: List[List[int]]) -> bool**
+
+Input: A list of lists matrix, where each sublist represents a row of the matrix.
+Output: Returns True if the matrix is a magic matrix, otherwise False. A magic matrix is a square matrix where the sum of each row, column, and both diagonals is the same.
+
+**Example**
+```
+Input: matrix = [
+ [8, 1, 6],
+ [3, 5, 7],
+ [4, 9, 2]
+]
+
+
+```
+Output : True
+
+
+# Solution
+
+
+from typing import List
+
+def is_square_matrix(matrix: List[List[int]]) -> bool:
+ """
+ Checks if the given matrix is square.
+ """
+ return len(matrix) == len(matrix[0])
+
+def is_magic_matrix(matrix: List[List[int]]) -> bool:
+ """
+ Determines if the given matrix is a magic matrix.
+ """
+ if not is_square_matrix(matrix):
+ return False
+
+ n = len(matrix)
+
+ # Calculate the sum of the first row (this will be the magic sum)
+ magic_sum = sum(matrix[0])
+
+ # Check sum of rows
+ for row in matrix:
+ if sum(row) != magic_sum:
+ return False
+
+ # Check sum of columns
+ for col in range(n):
+ col_sum = sum(matrix[row][col] for row in range(n))
+ if col_sum != magic_sum:
+ return False
+
+ # Check sum of diagonals
+ diag1_sum = sum(matrix[i][i] for i in range(n))
+ diag2_sum = sum(matrix[i][n - 1 - i] for i in range(n))
+
+ if diag1_sum != magic_sum or diag2_sum != magic_sum:
+ return False
+
+ return True
+
+# Input handling (for testing purposes only)
+if __name__ == "__main__":
+ # Read the matrix input
+ n = int(input("Enter the size of the matrix: "))
+ matrix = []
+ for _ in range(n):
+ row = list(map(int, input().split()))
+ matrix.append(row)
+
+ # Check if the matrix is magic
+ if is_magic_matrix(matrix):
+ print("True")
+ else:
+ print("False")
+
+
+
+```
+
+# Public Test Cases
+
+## Input 1
+
+```
+matrix = [
+ [8, 1, 6],
+ [9, 5, 7],
+ [4, 9, 2]
+]
+
+
+```
+
+## Output 1
+
+```
+False
+
+```
+
+## Input 2
+
+```
+matrix = [
+ [2, 7, 6],
+ [9, 5, 1],
+ [4, 3, 8]
+]
+
+```
+
+## Output 2
+
+```
+True
+
+```
+
+## Input 3
+
+```
+matrix = [
+ [8, 1, 6],
+ [3, 5, 7]
+]
+
+
+```
+
+## Output 3
+
+```
+False
+
+```
+
+# Private Test Cases
+
+## Input 1
+
+```
+matrix = [
+ [4, 9, 2],
+ [3, 5, 7],
+ [8, 1, 6]
+]
+
+
+```
+
+## Output 1
+True
+
+```
+Decoded Text: WELCOME TO THE CRYPTOGRAPHY CHALLENGE
+
+```
From 9f1f69050c0f21a6e0e335dec550d2dc353ef742 Mon Sep 17 00:00:00 2001
From: HiyaaT <120657191+HiyaaT@users.noreply.github.com>
Date: Wed, 8 Jan 2025 18:37:12 +0530
Subject: [PATCH 11/12] Update Q4_magic_matrix.md
---
Q4_magic_matrix.md | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/Q4_magic_matrix.md b/Q4_magic_matrix.md
index 837a76c..9ff30d6 100644
--- a/Q4_magic_matrix.md
+++ b/Q4_magic_matrix.md
@@ -166,9 +166,8 @@ matrix = [
```
## Output 1
-True
```
-Decoded Text: WELCOME TO THE CRYPTOGRAPHY CHALLENGE
+True
```
From 3857fd138df5ffe3a51e2a3659f97659a7549cf9 Mon Sep 17 00:00:00 2001
From: HiyaaT <120657191+HiyaaT@users.noreply.github.com>
Date: Wed, 8 Jan 2025 18:51:27 +0530
Subject: [PATCH 12/12] Update Q4_magic_matrix.md
---
Q4_magic_matrix.md | 70 ++++++++++++++++++++--------------------------
1 file changed, 30 insertions(+), 40 deletions(-)
diff --git a/Q4_magic_matrix.md b/Q4_magic_matrix.md
index 9ff30d6..e44403d 100644
--- a/Q4_magic_matrix.md
+++ b/Q4_magic_matrix.md
@@ -1,39 +1,34 @@
---
-title:Magic Matrix Finder
+title: Magic Matrix Finder
---
# Problem Statement
-A magic matrix is a square matrix where the sum of every row, column, and both diagonals is the same. Your task is to write a program that determines if the given matrix is a magic matrix.
-You need to implement the following functions:
+A magic matrix is a square matrix where the sum of every row, column, and both diagonals is the same. Your task is to write a program that determines if the given matrix is a magic matrix.
-# Functions to Implement
+**Functions to Implement**
**is_square_matrix(matrix: List[List[int]]) -> bool**
-Input: A list of lists matrix, where each sublist represents a row of the matrix.
+Input: A list of lists matrix, where each sublist represents a row of the matrix.
Output: Returns True if the matrix is square (i.e., the number of rows equals the number of columns), otherwise False.
**is_magic_matrix(matrix: List[List[int]]) -> bool**
-Input: A list of lists matrix, where each sublist represents a row of the matrix.
+Input: A list of lists matrix, where each sublist represents a row of the matrix.
Output: Returns True if the matrix is a magic matrix, otherwise False. A magic matrix is a square matrix where the sum of each row, column, and both diagonals is the same.
-**Example**
+**Sample Input**
```
-Input: matrix = [
- [8, 1, 6],
- [3, 5, 7],
- [4, 9, 2]
-]
-
-
+matrix = [ [8, 1, 6], [3, 5, 7], [4, 9, 2] ]
+```
+**Sample Output**
+```
+True
```
-Output : True
-
# Solution
-
+```py test.py -r 'python test.py'
from typing import List
@@ -74,22 +69,21 @@ def is_magic_matrix(matrix: List[List[int]]) -> bool:
return False
return True
-
-# Input handling (for testing purposes only)
-if __name__ == "__main__":
- # Read the matrix input
- n = int(input("Enter the size of the matrix: "))
- matrix = []
- for _ in range(n):
- row = list(map(int, input().split()))
- matrix.append(row)
-
- # Check if the matrix is magic
- if is_magic_matrix(matrix):
- print("True")
- else:
- print("False")
+
+# Input parsing
+n = int(input("Enter the size of the matrix: "))
+matrix = []
+for _ in range(n):
+ row = list(map(int, input().split()))
+ matrix.append(row)
+
+# Output the result
+if is_magic_matrix(matrix):
+ print("True")
+else:
+ print("False")
+
```
@@ -105,16 +99,15 @@ matrix = [
[4, 9, 2]
]
-
```
-## Output 1
+## Output 1
```
False
-
```
+
## Input 2
```
@@ -130,9 +123,9 @@ matrix = [
```
True
-
```
+
## Input 3
```
@@ -141,16 +134,15 @@ matrix = [
[3, 5, 7]
]
-
```
## Output 3
```
False
-
```
+
# Private Test Cases
## Input 1
@@ -162,12 +154,10 @@ matrix = [
[8, 1, 6]
]
-
```
## Output 1
```
True
-
```