├── DAYS ├── Day1 │ ├── Display_Fibonacci_Series.py │ ├── Factorial_Of_Number.py │ ├── HCF_Of_Number.py │ ├── LCM_Of_Number.py │ └── Number_is_Fibonacci_or_not.py ├── Day10 │ ├── Kth_Element_in_Matrix.py │ ├── String_Palindrome.py │ ├── Symmetric_or_Palindrome.py │ └── Vertical_Concatenation_of_Matrix.py ├── Day100 │ └── Stock_Span_Problem.py ├── Day11 │ ├── Remove_Duplicates_from_List.py │ ├── Remove_Particular_Character.py │ ├── Reverse_Words_of_String.py │ ├── Snake_Case_to_Pascal_Case.py │ └── Word_with_Max_Frequency_in_String.py ├── Day12 │ ├── Even_Length_Strings.py │ ├── Least_Frequency_Character.py │ ├── Matching_Characters_in_String.py │ └── String_with_Vowels.py ├── Day13 │ ├── Removing_ith_Character_from_String.py │ ├── Special_Characters_in_String.py │ ├── String_is_Binary_or_not.py │ └── Words_Greater_than_Given_Length.py ├── Day14 │ ├── Permutation_of_String.py │ ├── Replace_Duplicate_Occurrence_in_String.py │ ├── Replace_Multiple_Words_in_String.py │ └── Uncommon_Words_from_String.py ├── Day15 │ ├── Execute_Code_in_String.py │ ├── Find_URL_from_String.py │ ├── Rotate_a_String.py │ └── String_can_become_Empty_or_not.py ├── Day16 │ ├── Check_String_for_Specific_Characters.py │ ├── Duplicate_Characters_in_String.py │ ├── Last_name_Sorting.py │ └── Letter_not_Common_in_Two_String.py ├── Day17 │ ├── Maximum_Uppercase_run.py │ ├── Move_Numbers_to_the_End.py │ ├── Replace_String_by_SubString.py │ └── Strings_Based_on_Prefix.py ├── Day18 │ ├── Extract_Unique_Dictionary_Values.py │ ├── Remove_Keys_from_Dictionary.py │ ├── Sorting_Dictionary_Using_Itemgetter.py │ └── Sum_of_Items_in_Dictionary.py ├── Day19 │ ├── Check_Order_of_Character.py │ ├── Convert_Key_Values_to_Flatten_list.py │ ├── Merge_Two_Dictionaries.py │ └── Sort_Dictionary_Using_lambda_function.py ├── Day2 │ ├── Armstrong_Number.py │ ├── Array_Rotation.py │ ├── Max_and_Min_in_List.py │ ├── Palindrome_Number.py │ └── Prime_Using_Sieve_of_Eratosthenes.py ├── Day20 │ ├── Append_Keys_and_Values.py │ ├── Counter_in_Dictionary.py │ ├── Sort_Dictionary_Keys_and_Values_List.py │ └── Sorting_Using_Keys_and_Values.py ├── Day21 │ ├── Anagram_Using_list_Dictionary.py │ ├── Binary_Representation_of_Two_Numbers_is_Anagram_or_not.py │ ├── Handling_Missing_Keys_in_Dictionary.py │ └── Kth_Non_Repeating_Character.py ├── Day22 │ ├── Counting_the_Frequency.py │ ├── Mirror_Characters_in_string.py │ ├── Remove_Duplicates_in_Dictionary.py │ └── Size_of_Largest_Subset_in_Anagram.py ├── Day23 │ ├── Dictionary_Intersection.py │ ├── Frequency_can_become_same.py │ ├── Key_Associated_with_Values.py │ └── Possible_Words_Using_Given_Characters.py ├── Day24 │ ├── Assigne_Keys_with_Maximum_Element_Index.py │ ├── Index_Directory_of_Elements.py │ ├── Minimum_Value_key_Assignment.py │ └── Shuffling_of_Values.py ├── Day25 │ ├── Convert_Dictionary_to_list.py │ ├── Filter_Dictionaries_by_Values.py │ ├── Remove_Duplicity_from_Dictionary.py │ └── Unique_Values_Count_of_each_key.py ├── Day26 │ ├── Convert_each_list_element_to_key_value_pair.py │ ├── Resize_Keys_in_Dictionary.py │ ├── Rotate_Dictionary_by_K.py │ └── Value_Sum_is_greater_than_Keys_Sum.py ├── Day27 │ ├── Extract_digit_from_tuple_list.py │ ├── Join_Tuples_if_Similar_initial_elements.py │ ├── Maximum_Minimum_K_Elements.py │ └── Size_of_Tuple.py ├── Day28 │ ├── Occurrences_of_Characters.py │ ├── Pair_Combinations_of_two_tuples.py │ ├── Remove_Tuple_of_length_k.py │ └── Sort_list_by_second_items.py ├── Day29 │ ├── Inverted_Number_Pattern.py │ ├── Inverted_Star_Pattern.py │ ├── Leap_Year.py │ └── Rabbits_Chicken_Heads_and_Legs.py ├── Day3 │ ├── Monotonic_Or_Not.py │ ├── Reversing_a_list.py │ ├── Searching_Element_in_List.py │ ├── Second_Largest_in_List.py │ └── Swap_First_With_Last.py ├── Day30 │ ├── Flatten_tuple_of_list.py │ ├── Nested_Tuple_to_Custom_key_Dictionary.py │ ├── Order_Tuple_by_List.py │ └── Remove_duplicates_tuple.py ├── Day31 │ ├── Extract_tuple_having_k_digit_elements.py │ ├── Remove_None_Elements_from_Tuple.py │ ├── Sort_Tuple_by_Total_digits.py │ └── Sort_tuple_by_last_element.py ├── Day32 │ ├── Sort_tuple_by_Maximum_Values.py │ └── Tuple_Elements_divisble_by_k.py ├── Day33 │ ├── Binary_Search.py │ └── Linear_Search.py ├── Day34 │ ├── Medical_Speciality.py │ └── Translate_Lang.py ├── Day35 │ ├── Insertion_Sort.py │ └── Insertion_Sort_Recursive.py ├── Day36 │ ├── Gem_Qty.py │ └── Quick_Sort.py ├── Day37 │ ├── Bubble_Sort.py │ └── Selection_Sort.py ├── Day38 │ ├── Inverted_Star_Pattern.py │ └── Merge_Sort.py ├── Day39 │ ├── Current_Time.py │ └── Current_Time_and_Date.py ├── Day4 │ ├── Cloning_or_Copying_list.py │ ├── N_Largest_Elements_in_List.py │ ├── Occurence_of_Elements.py │ ├── Remove_Empty_List.py │ └── Remove_Multiple_Elements.py ├── Day40 │ ├── 12_to_24_hour_format.py │ └── Today_Yesterday_Tommorow_Date.py ├── Day41 │ ├── Difference_bw_Times.py │ └── String_to_Timestamp.py ├── Day42 │ ├── Day_Occurs_in_a_Year.py │ └── Timestamp_to_datetime.py ├── Day43 │ ├── Count_Uppercase_Lowercase.py │ └── Defined_Characters_Using_Regex.py ├── Day44 │ ├── Extract_Maximum_Using_Regex.py │ └── Most_Occurring_number_Using_Regex.py ├── Day45 │ ├── Put_Spaces_bw_Words.py │ └── Starts_End_with_same_Character.py ├── Day46 │ ├── Remove_Duplicates_from_string.py │ └── Upper_Case_followed_by_Lower_Case.py ├── Day47 │ ├── Remove_all_except_numbers_and_letters.py │ └── String_ending_wth_numbers.py ├── Day48 │ ├── String_Starting_with_Vowel.py │ └── String_start_with_substring.py ├── Day49 │ ├── Parsing_and_Processing_URL.py │ └── URL_Validation.py ├── Day5 │ ├── Break_into_Chunks.py │ ├── Cumulative_Sum_of_List.py │ ├── Duplicates_in_List.py │ └── Flatten_List.py ├── Day50 │ ├── Extension_Checking_using_Regex.py │ └── Validate_email.py ├── Day51 │ ├── Validity_of_Password.py │ └── Weak_or_Strong_Pass.py ├── Day52 │ ├── Number_is_a_power_of_2.py │ └── Number_is_power_of_3.py ├── Day53 │ ├── Integer_is_power_of_another_Integer.py │ └── Perfect_Square.py ├── Day54 │ ├── Missing_Number_in_list.py │ └── Missing_Numbers_in_list.py ├── Day55 │ ├── Triplet_Sum_is_given_number.py │ └── Triplets_Sum.py ├── Day56 │ ├── Add_until_single_digit.py │ └── Not_Occur_Twice.py ├── Day57 │ └── Additive_Sequence.py ├── Day58 │ └── Reverse_bits_of_integers.py ├── Day59 │ └── Arithmetic_Progression_or_not.py ├── Day6 │ ├── Increament_Numeric_String.py │ ├── Maximum_of_Each_key_in_List.py │ ├── Sort_list1_by_list2.py │ └── Words_Reverse_Pairs_in_List.py ├── Day60 │ └── Geometric_Progression.py ├── Day61 │ ├── Collatz_Sequence.py │ └── Reverse_Sum.py ├── Day62 │ ├── Hamming_Number.py │ └── Hamming_Sequence.py ├── Day63 │ ├── Anagram_or_not.py │ ├── Push_fisrt_number_to_end.py │ └── Ugly_Number.py ├── Day64 │ ├── Odd_Occurrences_of_elements.py │ └── Sum_of_Even_Valued_Terms_Fibonacci_Series.py ├── Day65 │ ├── Largest_Palindrome.py │ └── Largest_Prime_Factor.py ├── Day66 │ └── Small_Positive_Number_Divisble.py ├── Day67 │ ├── Sum_of_Square.py │ └── nth_Prime_number.py ├── Day68 │ └── Amicable_Pairs.py ├── Day69 │ └── Triangular_Number.py ├── Day7 │ ├── Remove_Row_with_Custom_Element.py │ ├── Remove_Specific_Digit_from_Every_Element.py │ ├── String_Based_on_Prefix.py │ └── Swap_ith_with_jth_Element.py ├── Day70 │ ├── Expand_string.py │ └── Index_of_term_in_Fibonacci.py ├── Day71 │ └── Distance_bw_two_strings.py ├── Day72 │ └── List_product.py ├── Day73 │ └── Intinerary_List.py ├── Day74 │ └── Gray_Code.py ├── Day75 │ └── Reverse_Linked_List.py ├── Day76 │ └── Tower_of_Hanoi.py ├── Day77 │ └── Diff_bw_Odd_and_even.py ├── Day78 │ └── Z_Matrix.py ├── Day79 │ └── Insert_operation_on_Linked_List.py ├── Day8 │ ├── Element_Multiple_of_Element_in_List.py │ ├── Element_with_Maximum_Vowel_from_List.py │ ├── Occurence_of_i_before_first_j_in_List.py │ └── Sort_row_by_Frequency_of_Key.py ├── Day80 │ └── Delete_Linked_List.py ├── Day81 │ └── Sorting_List.py ├── Day82 │ └── Swap_nodes.py ├── Day83 │ └── Merge_two_sorted_Lists.py ├── Day84 │ └── Merge_Sort_Linked_List.py ├── Day85 │ └── Reverse_list_in_groups.py ├── Day86 │ └── Detect_Remove_loop.py ├── Day87 │ └── Add_two_Linked_List.py ├── Day88 │ └── Rotate_Linked_List.py ├── Day89 │ └── Circular_Linked_List_Insertions.py ├── Day9 │ ├── Add_Two_Matrix.py │ ├── Multiply_Matrix.py │ ├── Product_of_All_Elements_in_Matrix.py │ └── Transpose_Matrix.py ├── Day90 │ └── Split_Circular_Linked_List.py ├── Day91 │ └── Sorted_Inserted_Linked_List.py ├── Day92 │ └── Doubly_Linked_List_Insertions.py ├── Day93 │ ├── Binary_Insertion_Sort.py │ └── Linear_Insertion_Sort.py ├── Day94 │ └── Deletion_Doubly_Linked_List.py ├── Day95 │ └── Reverse_Doubly_Linked_List.py ├── Day96 │ └── Reverse_Doubly_Linked_List_Using_Stack.py ├── Day97 │ └── Infix_to_Prefix.py ├── Day98 │ └── Postfix_Evaluate.py └── Day99 │ └── Balanced_Brackets.py └── README.md /DAYS/Day1/Display_Fibonacci_Series.py: -------------------------------------------------------------------------------- 1 | def fibo(a): 2 | m, n = 0, 1 3 | count = 0 4 | if a <= 0: 5 | print("Alert! Wrong Input") 6 | elif a == 1: 7 | print(f"Fibonacci Series: {m}") 8 | else: 9 | print(f"Fibonacci Series: ", end="") 10 | while count < a: 11 | count += 1 12 | print(m, end=" ") 13 | nth = m+n 14 | m = n 15 | n = nth 16 | 17 | a = int(input("How Many Terms do you want to Display: ")) 18 | fibo(a) 19 | -------------------------------------------------------------------------------- /DAYS/Day1/Factorial_Of_Number.py: -------------------------------------------------------------------------------- 1 | def factorial(n): 2 | return 1 if (n == 0 or n == 1) else n*factorial(n-1) 3 | 4 | 5 | n = int(input("Enter a Number: ")) 6 | print(f"{n}! = {factorial(n)}") 7 | -------------------------------------------------------------------------------- /DAYS/Day1/HCF_Of_Number.py: -------------------------------------------------------------------------------- 1 | def hcf(a, b): 2 | if a < b: 3 | small = a 4 | else: 5 | small = b 6 | for i in range(1, small + 1): 7 | print(i) 8 | if (a % i == 0) and (b % i == 0): 9 | hcf = i 10 | return hcf 11 | 12 | 13 | a = int(input("Enter 1st Number: ")) 14 | b = int(input("Enter 2nd Number: ")) 15 | print(f"HCF: {hcf(a,b)}") 16 | -------------------------------------------------------------------------------- /DAYS/Day1/LCM_Of_Number.py: -------------------------------------------------------------------------------- 1 | def lcm(a, b): 2 | if a > b: 3 | greater = a 4 | else: 5 | greater = b 6 | while(True): 7 | if(greater % a == 0) and (greater % b == 0): 8 | result = greater 9 | break 10 | else: 11 | greater += 1 12 | return result 13 | 14 | 15 | a = int(input("Enter 1st Number: ")) 16 | b = int(input("Enter 2nd Number: ")) 17 | print(f"LCM of {a} and {b}: {lcm(a,b)}") 18 | -------------------------------------------------------------------------------- /DAYS/Day1/Number_is_Fibonacci_or_not.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | def check_Perfect_Square(m): 4 | semifinal = int(math.sqrt(m)) 5 | if pow(semifinal, 2) == m: 6 | return True 7 | return False 8 | 9 | 10 | def fibo(n): 11 | t1 = 5*n*n+4 12 | t2 = 5*n*n-4 13 | if check_Perfect_Square(t1) or check_Perfect_Square(t2): 14 | return True 15 | else: 16 | return False 17 | 18 | 19 | a = int(input("Enter a Number: ")) 20 | if fibo(a): 21 | print("Yup! It's a Fibinacci Number") 22 | else: 23 | print("It's not a Fibonacci Number") 24 | -------------------------------------------------------------------------------- /DAYS/Day10/Kth_Element_in_Matrix.py: -------------------------------------------------------------------------------- 1 | # Approach 1: 2 | 3 | def Kth_Element(Test_list, k): 4 | return [ele[k-1] for ele in Test_list] 5 | 6 | 7 | Test_list = [[4, 5, 6], 8 | [8, 1, 10], 9 | [7, 12, 5]] 10 | k = 2 11 | print(Kth_Element(Test_list, k)) 12 | 13 | # Approach 2: 14 | 15 | 16 | def Kth_Element(Test_list, k): 17 | Result = list(zip(*Test_list))[k-1] 18 | return Result 19 | 20 | 21 | Test_list = [[4, 5, 6], 22 | [8, 1, 10], 23 | [7, 12, 5]] 24 | k = 2 25 | print(list(Kth_Element(Test_list, k))) 26 | -------------------------------------------------------------------------------- /DAYS/Day10/String_Palindrome.py: -------------------------------------------------------------------------------- 1 | def Palindrome(Test_String): 2 | if Test_String == Test_String[::-1]: 3 | return True 4 | else: 5 | return False 6 | 7 | 8 | Test_String = input("Enter a String: ") 9 | print(Palindrome(Test_String)) 10 | -------------------------------------------------------------------------------- /DAYS/Day10/Symmetric_or_Palindrome.py: -------------------------------------------------------------------------------- 1 | def Symmetric(Test_String): 2 | length = len(Test_String)//2 3 | if len(Test_String) % 2 == 0 and str(Test_String[0:length]) == str(Test_String[length:]): 4 | return "Symmetric" 5 | else: 6 | return "Unsymmetric" 7 | 8 | 9 | def Palindrome(Test_String): 10 | if Test_String == Test_String[::-1]: 11 | return "Palindrome" 12 | else: 13 | return "Not Plaindrome" 14 | 15 | 16 | Test_String = input("Enter a String: ") 17 | print(Symmetric(Test_String)) 18 | print(Palindrome(Test_String)) 19 | -------------------------------------------------------------------------------- /DAYS/Day10/Vertical_Concatenation_of_Matrix.py: -------------------------------------------------------------------------------- 1 | def Vertical_Concatenation(Test_list): 2 | Result = [] 3 | n = 0 4 | while n != len(Test_list): 5 | temp = '' 6 | for indexes in Test_list: 7 | try: 8 | temp += indexes[n] 9 | except IndexError: 10 | pass 11 | n += 1 12 | Result.append(temp) 13 | return Result 14 | 15 | 16 | Test_list = [["Gfg", "good"], ["is", "for"], ["Best"]] 17 | print(Vertical_Concatenation(Test_list)) -------------------------------------------------------------------------------- /DAYS/Day100/Stock_Span_Problem.py: -------------------------------------------------------------------------------- 1 | def span(price): 2 | hey = [1]*len(price) 3 | for i in range(1, len(price)): 4 | a = i 5 | while price[a] > price[i-1] and i > 0: 6 | hey[a] += 1 7 | i -= 1 8 | print(hey) 9 | 10 | 11 | price = [100, 80, 60, 70, 60, 75, 85] 12 | span(price) -------------------------------------------------------------------------------- /DAYS/Day11/Remove_Duplicates_from_List.py: -------------------------------------------------------------------------------- 1 | def Remove_Duplicates(Test_list): 2 | Result = [] 3 | for element in Test_list: 4 | if element not in Result: 5 | Result.append(element) 6 | return Result 7 | 8 | 9 | Test_list = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6] 10 | print(Remove_Duplicates(Test_list)) 11 | -------------------------------------------------------------------------------- /DAYS/Day11/Remove_Particular_Character.py: -------------------------------------------------------------------------------- 1 | # Approach 1: 2 | 3 | def Word_Reverse(Test_String, Test_Word): 4 | Final_String = "" 5 | for words in Test_String: 6 | if words != Test_Word: 7 | Final_String += words 8 | return Final_String 9 | 10 | 11 | Test_String = input("Enter a String: ") 12 | Test_Word = input("Enter a Word: ") 13 | print(Word_Reverse(Test_String, Test_Word)) 14 | 15 | # Approach 2: 16 | 17 | def Word_Reverse(Test_String, Test_Word): 18 | return ''.join([ele for ele in Test_String if ele != Test_Word]) 19 | 20 | 21 | Test_String = input("Enter a String: ") 22 | Test_Word = input("Enter a Word: ") 23 | print(Word_Reverse(Test_String, Test_Word)) 24 | -------------------------------------------------------------------------------- /DAYS/Day11/Reverse_Words_of_String.py: -------------------------------------------------------------------------------- 1 | def Word_Reverse(Test_String): 2 | return f"Reversed: {' '.join(reversed(Test_String.split(' ')))}" 3 | 4 | 5 | Test_String = input("Enter a String: ") 6 | print(Word_Reverse(Test_String)) 7 | -------------------------------------------------------------------------------- /DAYS/Day11/Snake_Case_to_Pascal_Case.py: -------------------------------------------------------------------------------- 1 | def Snake_to_Pascal(Test_String): 2 | return Test_String.replace("_", " ").title().replace(" ", "") 3 | 4 | 5 | Test_String = input("Enter a String: ") 6 | print(Snake_to_Pascal(Test_String)) -------------------------------------------------------------------------------- /DAYS/Day11/Word_with_Max_Frequency_in_String.py: -------------------------------------------------------------------------------- 1 | def Words_Frequency(Test_String): 2 | hey = {keys: Test_String.count(keys) for keys in Test_String.split()} 3 | return max(hey, key=hey.get) 4 | 5 | 6 | Test_String = input("Enter a String: ") 7 | print(Words_Frequency(Test_String)) 8 | -------------------------------------------------------------------------------- /DAYS/Day12/Even_Length_Strings.py: -------------------------------------------------------------------------------- 1 | def Even_Length(Test_string): 2 | return "\n".join([words for words in Test_string.split() if len(words) % 2 == 0]) 3 | 4 | 5 | Test_string = input("Enter a String: ") 6 | print(f"Output: {Even_Length(Test_string)}") -------------------------------------------------------------------------------- /DAYS/Day12/Least_Frequency_Character.py: -------------------------------------------------------------------------------- 1 | def Least_Frequency(Test_string): 2 | Result = {} 3 | for words in Test_string: 4 | if words in Result: 5 | Result[words] += 1 6 | else: 7 | Result[words] = 1 8 | Final_Result = min(Result, key=Result.get) 9 | return Final_Result 10 | 11 | 12 | Test_string = input("Enter a String: ") 13 | print(Least_Frequency(Test_string)) -------------------------------------------------------------------------------- /DAYS/Day12/Matching_Characters_in_String.py: -------------------------------------------------------------------------------- 1 | def Matching_Characters_in_String(String1, String2): 2 | Result = [] 3 | for words in String1: 4 | if words in String2: 5 | Result.append(words) 6 | return Result 7 | 8 | 9 | String1 = input("Enter a String: ") 10 | String2 = input("Enter a String: ") 11 | print(*(Matching_Characters_in_String(String1, String2))) 12 | -------------------------------------------------------------------------------- /DAYS/Day12/String_with_Vowels.py: -------------------------------------------------------------------------------- 1 | def String_with_Vowels(Test_string): 2 | return all([words in "aeiouAEIOU" for words in Test_string]) 3 | 4 | 5 | Test_string = input("Enter a String: ") 6 | if String_with_Vowels(Test_string): 7 | print("Accepted") 8 | else: 9 | print("Not Accepted") 10 | -------------------------------------------------------------------------------- /DAYS/Day13/Removing_ith_Character_from_String.py: -------------------------------------------------------------------------------- 1 | def Remove_ith_Character(Test_string, Character): 2 | return Test_string[:Character]+Test_string[Character+1:] 3 | 4 | 5 | Test_string = input("Enter a String: ") 6 | Character = int(input("Enter ith Character: ")) 7 | print(Remove_ith_Character(Test_string, Character)) -------------------------------------------------------------------------------- /DAYS/Day13/Special_Characters_in_String.py: -------------------------------------------------------------------------------- 1 | import string 2 | def Special_Characters_String(Test_string): 3 | Special_Characters = string.punctuation 4 | if any(elements in Special_Characters for elements in Test_string): 5 | return "Not Accepted" 6 | else: 7 | return "Accepted" 8 | 9 | 10 | Test_string = input("Enter a String: ") 11 | print(Special_Characters_String(Test_string)) -------------------------------------------------------------------------------- /DAYS/Day13/String_is_Binary_or_not.py: -------------------------------------------------------------------------------- 1 | def Binary_or_not(Test_string): 2 | Binary = "01" 3 | return all([numbers in Binary for numbers in Test_string]) 4 | 5 | 6 | Test_string = input("Enter a String: ") 7 | if Binary_or_not(Test_string): 8 | print("Binary") 9 | else: 10 | print("Not a Binary") 11 | -------------------------------------------------------------------------------- /DAYS/Day13/Words_Greater_than_Given_Length.py: -------------------------------------------------------------------------------- 1 | def Words_Greater_than_length(Test_string, length): 2 | return [words for words in Test_string.split() if len(words) > length] 3 | 4 | 5 | Test_string = input("Enter a String: ") 6 | length = int(input("Enter a Length: ")) 7 | print(*(Words_Greater_than_length(Test_string, length))) -------------------------------------------------------------------------------- /DAYS/Day14/Permutation_of_String.py: -------------------------------------------------------------------------------- 1 | from itertools import permutations 2 | 3 | 4 | def Permutation_of_String(Test_string): 5 | Perm_list = permutations(Test_string) 6 | return "\n".join(["".join(perms) for perms in list(Perm_list)]) 7 | 8 | 9 | Test_string = input("Enter a String: ") 10 | print(Permutation_of_String(Test_string)) 11 | -------------------------------------------------------------------------------- /DAYS/Day14/Replace_Duplicate_Occurrence_in_String.py: -------------------------------------------------------------------------------- 1 | def Replace_Duplicates(Test_string, replace_dict): 2 | Test_list = Test_string.split() 3 | return [replace_dict.get(Value) if Value in replace_dict.keys() and Test_list.index(Value) != Index else Value for Index, Value in enumerate(Test_list)] 4 | 5 | 6 | Test_string = 'Gfg is best . Gfg also has Classes now. Classes help understand better . ' 7 | replace_dict = {'Gfg': 'It', 'Classes': 'They'} 8 | print(*(Replace_Duplicates(Test_string, replace_dict))) 9 | -------------------------------------------------------------------------------- /DAYS/Day14/Replace_Multiple_Words_in_String.py: -------------------------------------------------------------------------------- 1 | def Replace_Multiple_Words(Test_string, Word_list, Replace_word): 2 | return " ".join([Replace_word if words in Word_list else words for words in Test_string.split()]) 3 | 4 | 5 | Test_string = "Geeksforgeeks is best for geeks and CS" 6 | Word_list = ["best", 'CS', 'for'] 7 | Replace_word = 'gfg' 8 | print(Replace_Multiple_Words(Test_string, Word_list, Replace_word)) 9 | -------------------------------------------------------------------------------- /DAYS/Day14/Uncommon_Words_from_String.py: -------------------------------------------------------------------------------- 1 | def Uncommon_Words(String1, String2): 2 | Result = "" 3 | for words in String1.split(): 4 | if words not in String2.split(): 5 | Result += words+" " 6 | for words in String2.split(): 7 | if words not in String1.split(): 8 | Result += words+" " 9 | return Result 10 | 11 | 12 | String1 = input("Enter 1st String: ") 13 | String2 = input("Enter 2nd String: ") 14 | print(Uncommon_Words(String1, String2)) -------------------------------------------------------------------------------- /DAYS/Day15/Execute_Code_in_String.py: -------------------------------------------------------------------------------- 1 | def Execute_Code(Test_string): 2 | return exec(Test_string) 3 | 4 | 5 | Test_string = """ 6 | def factorial(num): 7 | fact=1 8 | for i in range(1,num+1): 9 | fact = fact*i 10 | return fact 11 | print(factorial(5)) 12 | """ 13 | Execute_Code(Test_string) 14 | -------------------------------------------------------------------------------- /DAYS/Day15/Find_URL_from_String.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | 4 | def URL_from_string(Test_string): 5 | return re.findall("(https?://[^\s]+)", Test_string) 6 | 7 | 8 | Test_string = "These are the links http://www.google.com and http://stackoverflow.com/questions/839994/extracting-a-url-in-python" 9 | print("\n".join(URL_from_string(Test_string))) 10 | -------------------------------------------------------------------------------- /DAYS/Day15/Rotate_a_String.py: -------------------------------------------------------------------------------- 1 | def Rotate_String(Test_string, n): 2 | Left_First = Test_string[0:n] 3 | Left_Second = Test_string[n:] 4 | Right_First = Test_string[0:len(Test_string)-n] 5 | Right_Second = Test_string[len(Test_string)-n:] 6 | return f"Left Rotation: {Left_Second+Left_First}\nRight Rotation: {Right_Second+Right_First}" 7 | 8 | 9 | Test_string = input("Enter a String: ") 10 | n = int(input("Upto: ")) 11 | print(Rotate_String(Test_string, n)) 12 | -------------------------------------------------------------------------------- /DAYS/Day15/String_can_become_Empty_or_not.py: -------------------------------------------------------------------------------- 1 | def Empty_or_not(Test_string, Sub_String): 2 | if len(Test_string) == 0 and len(Sub_String) == 0: 3 | return 'True' 4 | if len(Sub_String) == 0: 5 | return "True" 6 | while(len(Test_string) != 0): 7 | index = Test_string.find(Sub_String) 8 | if index == -1: 9 | return "False" 10 | else: 11 | Test_string = Test_string[0:index] + \ 12 | Test_string[index+len(Sub_String):] 13 | return "True" 14 | 15 | 16 | Test_string = input("Enter a String: ") 17 | Sub_String = input("Enter a Sub-String: ") 18 | print(Empty_or_not(Test_string, Sub_String)) 19 | -------------------------------------------------------------------------------- /DAYS/Day16/Check_String_for_Specific_Characters.py: -------------------------------------------------------------------------------- 1 | def Check_String_for_Specific_Characters(Test_string, Test_list): 2 | Result = [] 3 | for char in Test_list: 4 | if char in Test_string: 5 | Result.append('True') 6 | else: 7 | Result.append('False') 8 | 9 | return Result 10 | 11 | 12 | Test_string = "@geeksfor#geeks123%^&*" 13 | Test_list = ['#', '%', '8', '@', ')'] 14 | print(Check_String_for_Specific_Characters(Test_string, Test_list)) 15 | 16 | 17 | def Check_String_for_Specific_Characters(Test_string, Test_list): 18 | return [char in Test_string for char in Test_list] 19 | 20 | 21 | Test_string = "@geeksfor#geeks123%^&*" 22 | Test_list = ['#', '%', '8', '@', ')'] 23 | print(Check_String_for_Specific_Characters(Test_string, Test_list)) 24 | -------------------------------------------------------------------------------- /DAYS/Day16/Duplicate_Characters_in_String.py: -------------------------------------------------------------------------------- 1 | def Duplicate_Charaters(Test_String): 2 | Duplicates = [] 3 | for char in Test_String: 4 | if Test_String.count(char) > 1 and char not in Duplicates: 5 | Duplicates.append(char) 6 | return Duplicates 7 | 8 | 9 | Test_String = input("Enter a String: ") 10 | print(*(Duplicate_Charaters(Test_String))) 11 | -------------------------------------------------------------------------------- /DAYS/Day16/Last_name_Sorting.py: -------------------------------------------------------------------------------- 1 | def Sort_by_Last_Name(Test_list): 2 | Test_list.sort(key=lambda names: names.split()[-1]) 3 | return Test_list 4 | 5 | 6 | Test_list = ['John Wick', 'Jason Voorhees', 'Freddy Krueger', 7 | 'Keyser Soze', 'Mohinder Singh Pandher'] 8 | print(Sort_by_Last_Name(Test_list)) 9 | -------------------------------------------------------------------------------- /DAYS/Day16/Letter_not_Common_in_Two_String.py: -------------------------------------------------------------------------------- 1 | def Letters_not_common(Test_String_1, Test_String_2): 2 | return list(set(Test_String_1) ^ set(Test_String_2)) 3 | 4 | 5 | Test_String_1 = input("Enter 1st String: ") 6 | Test_String_2 = input("Enter 2nd String: ") 7 | print(*(Letters_not_common(Test_String_1, Test_String_2))) 8 | -------------------------------------------------------------------------------- /DAYS/Day17/Maximum_Uppercase_run.py: -------------------------------------------------------------------------------- 1 | def Maximum_Uppercase(Test_string): 2 | count = 0 3 | Result = 0 4 | Store_1 = '' 5 | Store_2 = '' 6 | for indexes in range(0, len(Test_String)): 7 | if Test_String[indexes].isupper(): 8 | Store_1 += Test_String[indexes] 9 | count += 1 10 | else: 11 | Result = count 12 | Store_2 = Store_1 13 | Store_1 = '' 14 | count = 0 15 | if Test_String[len(Test_String)-1].isupper(): 16 | Result = count 17 | Store_2 = Store_1 18 | 19 | return Store_2, Result 20 | 21 | 22 | Test_String = input("Enter a String: ") 23 | print(*(Maximum_Uppercase(Test_String))) 24 | -------------------------------------------------------------------------------- /DAYS/Day17/Move_Numbers_to_the_End.py: -------------------------------------------------------------------------------- 1 | def Move_at_end(Test_String): 2 | one_Half = ''.join([ele for ele in Test_String if ele.isdigit()]) 3 | second_half = ''.join([ele for ele in Test_String if not ele.isdigit()]) 4 | second_half += one_Half 5 | return second_half 6 | 7 | 8 | Test_String = input("Enter a String: ") 9 | print(Move_at_end(Test_String)) 10 | -------------------------------------------------------------------------------- /DAYS/Day17/Replace_String_by_SubString.py: -------------------------------------------------------------------------------- 1 | def Replace_Words(Test_string, Sub_1, Sub_2): 2 | return Test_String.replace(Sub_1, Sub_2) 3 | 4 | 5 | Test_String = input("Enter a String: ") 6 | Sub_1 = input("Which: ") 7 | Sub_2 = input("To: ") 8 | print(Replace_Words(Test_String, Sub_1, Sub_2)) 9 | -------------------------------------------------------------------------------- /DAYS/Day17/Strings_Based_on_Prefix.py: -------------------------------------------------------------------------------- 1 | def Prefix_Strings(Test_list, Pref_list): 2 | return [ele for ele in Test_list if any(ele.startswith(el) for el in Pref_list)] 3 | 4 | 5 | Test_list = ["geeks", "peeks", "meeks", "leeks", "mean"] 6 | Pref_list = ["ge", "ne", "me", "re"] 7 | print(*(Prefix_Strings(Test_list, Pref_list))) 8 | -------------------------------------------------------------------------------- /DAYS/Day18/Extract_Unique_Dictionary_Values.py: -------------------------------------------------------------------------------- 1 | def Extract_Dictionary_Values(Test_dict): 2 | return [sorted({numbers for ele in Test_dict.values() for numbers in ele})] 3 | 4 | 5 | Test_dict = {'Challenges': [5, 6, 7, 8], 6 | 'are': [10, 11, 7, 5], 7 | 'best': [6, 12, 10, 8], 8 | 'for': [1, 2, 5]} 9 | print(*(Extract_Dictionary_Values(Test_dict))) 10 | -------------------------------------------------------------------------------- /DAYS/Day18/Remove_Keys_from_Dictionary.py: -------------------------------------------------------------------------------- 1 | def Remove_Keys(Test_dict, Remove_Value): 2 | return {key: value for key, value in Test_dict.items() if key != Remove_Value} 3 | 4 | 5 | N_value = int(input("Enter n: ")) 6 | Test_dict = {} 7 | for i in range(N_value): 8 | key = input("Key: ") 9 | Value = int(input("Value: ")) 10 | Test_dict[key] = Value 11 | Remove_Value = input("Which Key to Delete: ") 12 | print(Remove_Keys(Test_dict, Remove_Value)) -------------------------------------------------------------------------------- /DAYS/Day18/Sorting_Dictionary_Using_Itemgetter.py: -------------------------------------------------------------------------------- 1 | from operator import itemgetter 2 | 3 | 4 | def Sort_list_Name_and_Age(Test_list): 5 | Test_list.sort(key=itemgetter("Age", "Name")) 6 | return Test_list 7 | 8 | 9 | def Sort_list_Age(Test_list): 10 | Test_list.sort(key=itemgetter('Age')) 11 | return Test_list 12 | 13 | 14 | Test_list = [{"Name": "Tushar", "Age": 20}, 15 | {"Name": "Aditya", "Age": 19}, 16 | {"Name": "Satyam", "Age": 21}] 17 | print(f"Age Sorting: {Sort_list_Age(Test_list)}") 18 | print(f"Name and Age Sorting: {Sort_list_Name_and_Age(Test_list)}") 19 | -------------------------------------------------------------------------------- /DAYS/Day18/Sum_of_Items_in_Dictionary.py: -------------------------------------------------------------------------------- 1 | def Sum_of_Values(Test_dict): 2 | count = 0 3 | for ele in Test_dict.values(): 4 | count += ele 5 | return count 6 | 7 | 8 | Test_dict = {'a': 100, 'b': 200, 'c': 300, 'd': 400} 9 | print(f'Total Sum: {Sum_of_Values(Test_dict)}') 10 | -------------------------------------------------------------------------------- /DAYS/Day19/Check_Order_of_Character.py: -------------------------------------------------------------------------------- 1 | from collections import OrderedDict 2 | 3 | 4 | def Order_of_Character(Test_string, Pattern): 5 | Test_dict = OrderedDict.fromkeys(Test_string) 6 | count = 0 7 | for key, _ in Test_dict.items(): 8 | if key == Pattern[count]: 9 | count += 1 10 | if count == len(Pattern): 11 | return 'True' 12 | return 'False' 13 | 14 | 15 | Test_string = input("Enter String: ") 16 | Pattern = input("Enter Pattern: ") 17 | print(Order_of_Character(Test_string, Pattern)) -------------------------------------------------------------------------------- /DAYS/Day19/Convert_Key_Values_to_Flatten_list.py: -------------------------------------------------------------------------------- 1 | def Convert_Dict(Test_dict): 2 | return dict(zip(Test_dict['Month'], Test_dict['Name'])) 3 | 4 | 5 | Test_dict = {'Month': [1, 2, 3], 6 | 'Name': ['Jan', 'Feb', 'March']} 7 | print(Convert_Dict(Test_dict)) 8 | -------------------------------------------------------------------------------- /DAYS/Day19/Merge_Two_Dictionaries.py: -------------------------------------------------------------------------------- 1 | def Merge_Dictionaries(Test_dict_1, Test_dict_2): 2 | return Test_dict_1.update(Test_dict_2) 3 | 4 | 5 | Test_dict_1 = {'A': 10, 'B': 8} 6 | Test_dict_2 = {'D': 6, 'C': 4} 7 | Merge_Dictionaries(Test_dict_1, Test_dict_2) 8 | print(Test_dict_1) -------------------------------------------------------------------------------- /DAYS/Day19/Sort_Dictionary_Using_lambda_function.py: -------------------------------------------------------------------------------- 1 | def Sort_Dictionary(Test_list): 2 | return sorted(Test_list, key=lambda dic: dic["Age"]) 3 | 4 | 5 | Test_list = [{"Name": "Satyam", "Age": 20}, 6 | {"Name": "Aditya", "Age": 20}, 7 | {"Name": "Tushar", "Age": 19}] 8 | print(Sort_Dictionary(Test_list)) -------------------------------------------------------------------------------- /DAYS/Day2/Armstrong_Number.py: -------------------------------------------------------------------------------- 1 | def Armstrong(a): 2 | temp = a 3 | result = 0 4 | order = len(str(a)) 5 | while temp > 0: 6 | digit = temp % 10 7 | result += digit**order 8 | temp //= 10 9 | return "It's an Armstrong Number" if (result == a) else "No, It's not an Armstrong Number" 10 | 11 | 12 | a = int(input("Enter a Number: ")) 13 | print(f"{Armstrong(a)}") 14 | -------------------------------------------------------------------------------- /DAYS/Day2/Array_Rotation.py: -------------------------------------------------------------------------------- 1 | # Approach #1: 2 | 3 | a, b = [], [] 4 | n = int(input("Enter the Number of Elements: ")) 5 | for i in range(0, n): 6 | element = int(input()) 7 | a.append(element) 8 | 9 | c = int(input("From Which: ")) 10 | b.append(a[:c]) 11 | d = [k for k in a if k not in b[0]] 12 | d.extend(b[0]) 13 | print(d) 14 | 15 | # Approach #2: 16 | 17 | def Array_Rotate(arr, n): 18 | for i in range(0, n): 19 | first = arr[0] 20 | for j in range(0, len(arr)-1): 21 | arr[j] = arr[j+1] 22 | arr[len(arr)-1] = first 23 | 24 | print(arr) 25 | 26 | 27 | a, b = [], [] 28 | n = int(input("Enter the Number of Elements: ")) 29 | for i in range(0, n): 30 | element = int(input()) 31 | a.append(element) 32 | c = int(input("From Which: ")) 33 | Array_Rotate(a, c) 34 | -------------------------------------------------------------------------------- /DAYS/Day2/Max_and_Min_in_List.py: -------------------------------------------------------------------------------- 1 | a = [] 2 | n = int(input("Enter the Number of Elements: ")) 3 | for i in range(0, n): 4 | element = int(input()) 5 | a.append(element) 6 | max = a[0] 7 | min = a[0] 8 | for i in range(1, n): 9 | if a[i] > max: 10 | max = a[i] 11 | if a[i] < min: 12 | min = a[i] 13 | print(f"Max: {max}, MIN: {min}") 14 | -------------------------------------------------------------------------------- /DAYS/Day2/Palindrome_Number.py: -------------------------------------------------------------------------------- 1 | # Approach #1: 2 | 3 | def Palindrome(a): 4 | result = 0 5 | temp = a 6 | while temp > 0: 7 | digit = temp % 10 8 | result = result*10+digit 9 | temp //= 10 10 | 11 | return "It's an Palindrome Number" if (result == a) else "No, It's not an Palindrome Number" 12 | 13 | 14 | a = int(input("Enter a Number: ")) 15 | print(f"{Palindrome(a)}") 16 | 17 | # Approach #2: 18 | 19 | def Palindrome(a): 20 | b = "".join(reversed(str(a))) 21 | return "It's an Palindrome Number" if (int(b) == a) else "No, It's not an Palindrome Number" 22 | 23 | 24 | a = int(input("Enter a Number: ")) 25 | print(f"{Palindrome(a)}") 26 | 27 | # Approach #3: 28 | 29 | def Palindrome(a): 30 | b = str(a)[::-1] 31 | return "It's an Palindrome Number" if (int(b) == a) else "No, It's not an Palindrome Number" 32 | 33 | 34 | a = int(input("Enter a Number: ")) 35 | print(f"{Palindrome(a)}") 36 | -------------------------------------------------------------------------------- /DAYS/Day2/Prime_Using_Sieve_of_Eratosthenes.py: -------------------------------------------------------------------------------- 1 | def Sieve_of_Eratosthenes(n): 2 | prime = [True for i in range(n+1)] 3 | p = 2 4 | while p <= n: 5 | if prime[p] == True: 6 | for i in range(p*2, n+1, p): 7 | prime[i] = False 8 | p += 1 9 | 10 | prime[0] = False 11 | prime[1] = False 12 | for i in range(n+1): 13 | if prime[i]: 14 | print(i, end=" ") 15 | 16 | 17 | a = int(input("Upto How Many Terms: ")) 18 | Sieve_of_Eratosthenes(a) 19 | -------------------------------------------------------------------------------- /DAYS/Day20/Append_Keys_and_Values.py: -------------------------------------------------------------------------------- 1 | def Append_Keys_and_Values(Test_list): 2 | return list(Test_list.keys())+list(Test_list.values()) 3 | 4 | 5 | Test_list = {"Code": 1, "Challenge": 10, "is": 100, "Best": 1000} 6 | print(Append_Keys_and_Values(Test_list)) -------------------------------------------------------------------------------- /DAYS/Day20/Counter_in_Dictionary.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | 4 | def Counter_Dict(Test_list): 5 | Dict = Counter(Test_list) 6 | Maximum = max(Dict.values()) 7 | return sorted([ele for ele in Dict.keys() if Dict[ele] == Maximum])[0] 8 | 9 | 10 | Test_list = ['john', 'johnny', 'jackie', 'johnny', 'john', 'jackie', 11 | 'jamie', 'jamie', 'john', 'johnny', 'jamie', 'johnny', 'john'] 12 | print(Counter_Dict(Test_list)) -------------------------------------------------------------------------------- /DAYS/Day20/Sort_Dictionary_Keys_and_Values_List.py: -------------------------------------------------------------------------------- 1 | def Sort_Keys_and_Values(Test_dict): 2 | return {key: sorted(Test_dict[key]) for key in sorted(Test_dict)} 3 | 4 | 5 | Test_dict = {'Code': [7, 6, 3], 6 | 'Challenge': [2, 10, 3], 7 | 'is': [19, 4], 'Best': [18, 20, 7]} 8 | print(Sort_Keys_and_Values(Test_dict)) 9 | -------------------------------------------------------------------------------- /DAYS/Day20/Sorting_Using_Keys_and_Values.py: -------------------------------------------------------------------------------- 1 | def Sorting_by_Keys(Key_Values): 2 | return {key: value for key, value in sorted(Key_Values.items())} 3 | 4 | 5 | def Sorting_by_Values(Key_Values): 6 | return {key: value for key, value in sorted(Key_Values.items(), key=lambda elements: elements[1])} 7 | 8 | 9 | Key_Values = {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323} 10 | print(f"Sorting by Keys: {Sorting_by_Keys(Key_Values)}") 11 | print(f"Sorting by Values: {Sorting_by_Values(Key_Values)}") 12 | 13 | 14 | # from operator import itemgetter 15 | # def Sorting_by_Values(Key_Values): 16 | # return {key: value for key, value in sorted(Key_Values.items(), key=itemgetter(1))} 17 | 18 | 19 | # Key_Values = {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323} 20 | # print(Sorting_by_Values(Key_Values)) 21 | -------------------------------------------------------------------------------- /DAYS/Day21/Anagram_Using_list_Dictionary.py: -------------------------------------------------------------------------------- 1 | def Anagram(Test_list): 2 | Dict = {} 3 | for elements in Test_list: 4 | Key = ''.join(sorted(elements)) 5 | if Key in Dict.keys(): 6 | Dict[Key].append(elements) 7 | else: 8 | Dict[Key] = [] 9 | Dict[Key].append(elements) 10 | Result="" 11 | for keys, values in Dict.items(): 12 | Result+=' '.join(values)+" " 13 | return Result 14 | 15 | 16 | 17 | if __name__ == "__main__": 18 | Test_list = ['cat', 'dog', 'tac', 'god', 'act'] 19 | print(Anagram(Test_list)) 20 | -------------------------------------------------------------------------------- /DAYS/Day21/Binary_Representation_of_Two_Numbers_is_Anagram_or_not.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | 4 | def Check_Binary_Representation(Num_1, Num_2): 5 | Bin_1 = bin(Num_1)[2:] 6 | Bin_2 = bin(Num_2)[2:] 7 | Zeroes = abs(len(Bin_1)-len(Bin_2)) 8 | if len(Bin_2) > len(Bin_1): 9 | Bin_1 = Zeroes*'0'+Bin_1 10 | else: 11 | Bin_2 = Zeroes*'0'+Bin_2 12 | Dict_1 = Counter(Bin_1) 13 | Dict_2 = Counter(Bin_2) 14 | if Dict_1 == Dict_2: 15 | return "True" 16 | else: 17 | return "False" 18 | 19 | 20 | Num_1 = int(input("1st Number: ")) 21 | Num_2 = int(input("2nd Number: ")) 22 | print(Check_Binary_Representation(Num_1, Num_2)) 23 | -------------------------------------------------------------------------------- /DAYS/Day21/Handling_Missing_Keys_in_Dictionary.py: -------------------------------------------------------------------------------- 1 | from collections import defaultdict 2 | Test_dict = defaultdict(lambda: "Key Not Found") 3 | Test_dict['a'] = 1 4 | Test_dict['b'] = 2 5 | print(Test_dict['d']) 6 | print(Test_dict['a']) 7 | -------------------------------------------------------------------------------- /DAYS/Day21/Kth_Non_Repeating_Character.py: -------------------------------------------------------------------------------- 1 | from collections import OrderedDict 2 | 3 | 4 | def Non_Repeating_Character(Test_string, K_Value): 5 | Test_dict = OrderedDict.fromkeys(Test_string, 0) 6 | for characters in Test_string: 7 | Test_dict[characters] += 1 8 | Non_Repeating_Char = [key for key, value in Test_dict.items() if value == 1] 9 | if len(Non_Repeating_Char) < K_Value: 10 | return "Alert! Wrong" 11 | else: 12 | return Non_Repeating_Char[K_Value-1] 13 | 14 | 15 | Test_string = input("Enter a String: ") 16 | K_Value = int(input("K: ")) 17 | print(Non_Repeating_Character(Test_string, K_Value)) 18 | -------------------------------------------------------------------------------- /DAYS/Day22/Counting_the_Frequency.py: -------------------------------------------------------------------------------- 1 | def Counting_the_frequency(Test_list): 2 | Test_dict = {} 3 | for items in Test_list: 4 | Test_dict[items] = Test_list.count(items) 5 | for key, value in Test_dict.items(): 6 | print(f"{key} : {value}") 7 | 8 | 9 | Test_list = [1, 1, 1, 5, 5, 3, 1, 3, 3, 1, 4, 4, 4, 2, 2, 2, 2] 10 | Counting_the_frequency(Test_list) -------------------------------------------------------------------------------- /DAYS/Day22/Mirror_Characters_in_string.py: -------------------------------------------------------------------------------- 1 | import string 2 | 3 | def Mirror_Characters(Test_string, N): 4 | Test_Alpha = string.ascii_lowercase 5 | Test_Rever_Alpha = ''.join(list(reversed(Test_Alpha))) 6 | Dict_Alpha = dict(zip(Test_Alpha, Test_Rever_Alpha)) 7 | Prefix = Test_string[:N-1] 8 | Suffix = Test_string[N-1:] 9 | Result = "" 10 | for elements in range(0, len(Test_string)): 11 | Result += Dict_Alpha[Test_string[elements]] 12 | return Result 13 | 14 | 15 | Test_string = input("Enter a String: ") 16 | n = int(input("N: ")) 17 | print(Mirror_Characters(Test_string, n)) 18 | -------------------------------------------------------------------------------- /DAYS/Day22/Remove_Duplicates_in_Dictionary.py: -------------------------------------------------------------------------------- 1 | def Remove_Duplicates(Test_string): 2 | Test_list = [] 3 | for elements in Test_string.split(" "): 4 | if ((Test_string.count(elements) > 1 or Test_string.count(elements) == 1) and elements not in Test_list): 5 | Test_list.append(elements) 6 | return Test_list 7 | 8 | 9 | Test_string = input("Enter a String: ") 10 | print(*(Remove_Duplicates(Test_string))) -------------------------------------------------------------------------------- /DAYS/Day22/Size_of_Largest_Subset_in_Anagram.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | 4 | def Max_Anagram(Test_string): 5 | for i in range(0, len(Test_string.split(" "))): 6 | Test_string[i] = ''.join(sorted(Test_string[i])) 7 | Dict_Frequency = Counter(Test_string) 8 | return max(Dict_Frequency.values()) 9 | 10 | 11 | if __name__ == "__main__": 12 | Test_string = 'ant magenta magnate tan gnamate' 13 | print(Max_Anagram(Test_string)) 14 | -------------------------------------------------------------------------------- /DAYS/Day23/Dictionary_Intersection.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | 4 | def Dict_Intersection(Test_string_1, Test_string_2): 5 | Test_Dict_1 = Counter(Test_string_1) 6 | Test_Dict_2 = Counter(Test_string_2) 7 | Result = Test_Dict_1 & Test_Dict_2 8 | return Test_Dict_1 == Result 9 | 10 | 11 | if __name__ == "__main__": 12 | Test_string_1 = 'ABHISHEKsinGH' 13 | Test_string_2 = 'gfhfBHkooIHnfndSHEKsiAnG' 14 | if Dict_Intersection(Test_string_1, Test_string_2): 15 | print("Possible") 16 | else: 17 | print("Not Possible") 18 | -------------------------------------------------------------------------------- /DAYS/Day23/Frequency_can_become_same.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | 4 | def Check_Frequency(Test_string): 5 | Test_dict = Counter(Test_string) 6 | Result = list(set(Test_dict.values())) 7 | if len(Result) > 2: 8 | return False 9 | elif len(Result) == 2 and Result[1]-Result[0] > 1: 10 | return False 11 | else: 12 | return True 13 | 14 | 15 | Test_string = "xxxyyzz" 16 | if Check_Frequency(Test_string): 17 | print("Yes") 18 | else: 19 | print("No") 20 | -------------------------------------------------------------------------------- /DAYS/Day23/Key_Associated_with_Values.py: -------------------------------------------------------------------------------- 1 | from collections import defaultdict 2 | 3 | 4 | def Key_Associated(Test_dict): 5 | Result = defaultdict(list) 6 | for key, value in Test_dict.items(): 7 | for ele in value: 8 | Result[ele].append(key) 9 | 10 | return dict(Result) 11 | 12 | 13 | Test_dict = {'Code': [1, 2, 3], "Challenge": [ 14 | 3, 4, 5], 'is': [1, 4], 'best': [4, 2]} 15 | print(Key_Associated(Test_dict)) 16 | -------------------------------------------------------------------------------- /DAYS/Day23/Possible_Words_Using_Given_Characters.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | 4 | def Possible_Words(Test_list, Test_chars): 5 | for words in Test_list: 6 | count = 1 7 | Char = dict(Counter(words)) 8 | for key in words: 9 | if key not in Test_chars: 10 | count = 0 11 | else: 12 | if Test_chars.count(key) != Char[key]: 13 | count = 0 14 | if count == 1: 15 | print(words) 16 | 17 | 18 | if __name__ == "__main__": 19 | Test_list = ['go', 'bat', 'me', 'eat', 'goal', 'boy', 'run'] 20 | Test_chars = ['e', 'o', 'b', 'a', 'm', 'g', 'l'] 21 | Possible_Words(Test_list, Test_chars) 22 | -------------------------------------------------------------------------------- /DAYS/Day24/Assigne_Keys_with_Maximum_Element_Index.py: -------------------------------------------------------------------------------- 1 | def Maximum_Index(Test_dict): 2 | return {key: Test_dict[key].index(max(Test_dict[key])) for key in Test_dict} 3 | 4 | 5 | Test_dict = {"Challenges": [5, 3, 6, 3], "are": [1, 7, 5, 3], "Best": [9, 1, 3, 5]} 6 | print(Maximum_Index(Test_dict)) 7 | -------------------------------------------------------------------------------- /DAYS/Day24/Index_Directory_of_Elements.py: -------------------------------------------------------------------------------- 1 | # Approach 1: 2 | 3 | from collections import defaultdict 4 | 5 | 6 | def Index_Directory(Test_list): 7 | Result = defaultdict(list) 8 | for key in set(Test_list): 9 | for index, value in enumerate(Test_list): 10 | if key == value: 11 | Result[key].append(index) 12 | return dict(Result) 13 | 14 | 15 | Test_list = [7, 6, 3, 7, 8, 3, 6, 7, 8] 16 | print(Index_Directory(Test_list)) 17 | 18 | # Approach 2: 19 | 20 | def Index_Directory(Test_list): 21 | return {key: [index for index, value in enumerate(Test_list) if value == key] for key in set(Test_list)} 22 | 23 | 24 | Test_list = [7, 6, 3, 7, 8, 3, 6, 7, 8] 25 | print(Index_Directory(Test_list)) 26 | -------------------------------------------------------------------------------- /DAYS/Day24/Minimum_Value_key_Assignment.py: -------------------------------------------------------------------------------- 1 | def Minimum_Values_Key(Test_dict_1, Test_dict_2): 2 | return {key: min(value, Test_dict_2[key]) for key, value in Test_dict_1.items()} 3 | 4 | 5 | Test_dict_1 = {"Code": 1, "Challenge": 7, "is": 8, "Best": 10} 6 | Test_dict_2 = {"Code": 5, "Challenge": 5, "is": 7, "Best": 14} 7 | print(Minimum_Values_Key(Test_dict_1, Test_dict_2)) 8 | -------------------------------------------------------------------------------- /DAYS/Day24/Shuffling_of_Values.py: -------------------------------------------------------------------------------- 1 | # Approach 1: 2 | 3 | import random 4 | 5 | 6 | def Shuffling_of_Value(Test_dict): 7 | Temp_dict = list(Test_dict.values()) 8 | random.shuffle(Temp_dict) 9 | return dict(zip(Test_dict, Temp_dict)) 10 | 11 | 12 | Test_dict = {"Code": 1, "Challenge": 7, "is": 8, "Best": 10} 13 | print(Shuffling_of_Value(Test_dict)) 14 | 15 | 16 | from random import sample 17 | 18 | # Approach 2: 19 | 20 | def Shuffling_of_Value(Test_dict): 21 | return dict(zip(Test_dict, sample(list(Test_dict.values()), len(Test_dict)))) 22 | 23 | 24 | Test_dict = {"Code": 1, "Challenge": 7, "is": 8, "Best": 10} 25 | print(Shuffling_of_Value(Test_dict)) 26 | -------------------------------------------------------------------------------- /DAYS/Day25/Convert_Dictionary_to_list.py: -------------------------------------------------------------------------------- 1 | def Dict_to_List(Test_dict): 2 | Result = [[key]*value for key, value in Test_dict.items()] 3 | return [items for elements in Result for items in elements] 4 | 5 | 6 | Test_dict = {'S': 2, 'a': 3, 'm': 1, 't': 4, 'y': 1, 'y': 4, 't': 3} 7 | print(Dict_to_List(Test_dict)) 8 | -------------------------------------------------------------------------------- /DAYS/Day25/Filter_Dictionaries_by_Values.py: -------------------------------------------------------------------------------- 1 | def Filter_Dictionaries(Test_list, Search_list, Element): 2 | return [Sub_dict for Sub_dict in Test_list if Sub_dict[Element] in Search_list] 3 | 4 | 5 | Test_list = [{"Gfg": 3, "is": 5, "best": 10}, 6 | {"Gfg": 5, "is": 1, "best": 1}, 7 | {"Gfg": 8, "is": 3, "best": 9}, 8 | {"Gfg": 9, "is": 9, "best": 8}, 9 | {"Gfg": 4, "is": 10, "best": 7}] 10 | 11 | Search_list = [1, 9, 8, 4, 5] 12 | Element = "best" 13 | print(Filter_Dictionaries(Test_list, Search_list, Element)) 14 | -------------------------------------------------------------------------------- /DAYS/Day25/Remove_Duplicity_from_Dictionary.py: -------------------------------------------------------------------------------- 1 | def Remove_Duplicity(Test_dict): 2 | Result = {} 3 | for key, value in Test_dict.items(): 4 | Flag = True 5 | for Sub_key, Sub_Value in Result.items(): 6 | if key in Sub_Value: 7 | Flag = False 8 | if Flag == True: 9 | Result[key] = value 10 | return Result 11 | 12 | 13 | Test_dict = {'All': ['All', 'Time', 'Favourite'], 14 | 'Favourite': ['All'], 'Code': ['Challenge']} 15 | print(Remove_Duplicity(Test_dict)) 16 | -------------------------------------------------------------------------------- /DAYS/Day25/Unique_Values_Count_of_each_key.py: -------------------------------------------------------------------------------- 1 | def Unique_Values_Count(Test_dict): 2 | return {key: len(set([elements[key] for elements in Test_dict])) for key in Test_dict[0].keys()} 3 | 4 | 5 | Test_dict = [{"Challenges": 1, "are": 3, "Best": 2}, { 6 | "Challenges": 1, "are": 3, "Best": 6}, {"Challenges": 7, "are": 3, "Best": 10}] 7 | print(Unique_Values_Count(Test_dict)) 8 | -------------------------------------------------------------------------------- /DAYS/Day26/Convert_each_list_element_to_key_value_pair.py: -------------------------------------------------------------------------------- 1 | def Key_Value_Pair(Test_list): 2 | Result = dict() 3 | for elements in Test_list: 4 | Mid = len(str(elements))//2 5 | Key = int(str(elements)[:Mid]) 6 | Value = int(str(elements)[Mid:]) 7 | Result[Key] = Value 8 | return Result 9 | 10 | 11 | Test_list = [2323, 82, 129388, 234, 95] 12 | print(Key_Value_Pair(Test_list)) 13 | -------------------------------------------------------------------------------- /DAYS/Day26/Resize_Keys_in_Dictionary.py: -------------------------------------------------------------------------------- 1 | def Resize_Dict(Test_Dict, K): 2 | return {key[:k]: Test_Dict[key] for key in Test_Dict} 3 | 4 | 5 | Test_Dict = {"geeksforgeeks": 3, "best": 3, "coding": 4, "practice": 3} 6 | k = int(input("Enter K: ")) 7 | print(Resize_Dict(Test_Dict, k)) 8 | -------------------------------------------------------------------------------- /DAYS/Day26/Rotate_Dictionary_by_K.py: -------------------------------------------------------------------------------- 1 | def Rotate_Dict(Test_Dict, K): 2 | Test_Dict = list(Test_Dict.items()) 3 | c = Test_Dict[len(Test_Dict)-K:]+Test_Dict[:-K] 4 | return {sub[0]: sub[1] for sub in c} 5 | 6 | 7 | Test_Dict = {1: 6, 8: 1, 9: 3, 10: 8, 12: 6, 4: 9} 8 | K = int(input("Enter K: ")) 9 | print(Rotate_Dict(Test_Dict, K)) 10 | -------------------------------------------------------------------------------- /DAYS/Day26/Value_Sum_is_greater_than_Keys_Sum.py: -------------------------------------------------------------------------------- 1 | def Values_Sum_Greater(Test_Dict): 2 | return sum(list(Test_Dict.keys())) < sum(list(Test_Dict.values())) 3 | 4 | 5 | Test_Dict = {5: 3, 1: 3, 10: 4, 7: 3, 8: 1, 9: 5} 6 | print(Values_Sum_Greater(Test_Dict)) 7 | -------------------------------------------------------------------------------- /DAYS/Day27/Extract_digit_from_tuple_list.py: -------------------------------------------------------------------------------- 1 | def Extract_digit(Test_list): 2 | Result = "" 3 | for key, value in Test_list: 4 | Result += str(key)+str(value) 5 | return [int(ele) for ele in set(Result)] 6 | 7 | 8 | Test_list = [(15, 3), (3, 9), (1, 10), (99, 2)] 9 | print(Extract_digit(Test_list)) 10 | -------------------------------------------------------------------------------- /DAYS/Day27/Join_Tuples_if_Similar_initial_elements.py: -------------------------------------------------------------------------------- 1 | from collections import defaultdict 2 | 3 | 4 | def Join_Tuples(Test_list): 5 | Result = defaultdict(list) 6 | for key, value in Test_list: 7 | Result[key].append(value) 8 | return [(key, *value) for key, value in Result.items()] 9 | 10 | 11 | Test_list = [(5, 6), (5, 7), (6, 8), (6, 10), (7, 13)] 12 | print(Join_Tuples(Test_list)) 13 | -------------------------------------------------------------------------------- /DAYS/Day27/Maximum_Minimum_K_Elements.py: -------------------------------------------------------------------------------- 1 | def Max_Min_Elements(Test_tup, K): 2 | Test_tup = list(Test_tup) 3 | Temp_Tup = sorted(Test_tup) 4 | return tuple(Temp_Tup[:K]+Temp_Tup[-K:]) 5 | 6 | 7 | Test_tup = (5, 20, 3, 7, 6, 8, 2, 87, 9, 14) 8 | K = int(input("K: ")) 9 | print(Max_Min_Elements(Test_tup, K)) -------------------------------------------------------------------------------- /DAYS/Day27/Size_of_Tuple.py: -------------------------------------------------------------------------------- 1 | import sys 2 | Tuple_1 = ("A", 1, "B", 2, "C", 3) 3 | Tuple_2 = ("Geek1", "Satyam", "hey you yeah you", "Tushar", "Geek3", "Aditya") 4 | Tuple_3 = ((1, "Lion"), (2, "Tiger"), (3, "Fox"), (4, "Wolf")) 5 | print(f"Size of Tuple_1: {str(sys.getsizeof(Tuple_1))} bytes") 6 | print(f"Size of Tuple_2: {str(sys.getsizeof(Tuple_2))} bytes") 7 | print(f"Size of Tuple_3: {str(sys.getsizeof(Tuple_3))} bytes") 8 | -------------------------------------------------------------------------------- /DAYS/Day28/Occurrences_of_Characters.py: -------------------------------------------------------------------------------- 1 | def encode(Test_string): 2 | count = 1 3 | Result = "" 4 | for i in range(len(Test_string)): 5 | if (i+1) < len(Test_string) and (Test_string[i] == Test_string[i+1]): 6 | count += 1 7 | else: 8 | Result += str(count)+Test_string[i] 9 | count = 1 10 | return Result 11 | 12 | 13 | print(encode("ABBBBCCCCCCCCAB")) 14 | -------------------------------------------------------------------------------- /DAYS/Day28/Pair_Combinations_of_two_tuples.py: -------------------------------------------------------------------------------- 1 | def Pair_Combinations(Test_tuple_1, Test_tuple_2): 2 | return [(a, b) for a in Test_tuple_1 for b in Test_tuple_2] +\ 3 | [(a, b) for a in Test_tuple_2 for b in Test_tuple_1] 4 | 5 | 6 | Test_tuple_1 = (4, 5) 7 | Test_tuple_2 = (7, 8) 8 | print(Pair_Combinations(Test_tuple_1, Test_tuple_2)) 9 | -------------------------------------------------------------------------------- /DAYS/Day28/Remove_Tuple_of_length_k.py: -------------------------------------------------------------------------------- 1 | def Remove_Tuple(Test_list, K): 2 | return [elements for elements in Test_list if len(elements) != K] 3 | 4 | 5 | Test_list = [(4, 5), (4, ), (8, 6, 7), (1, ), (3, 4, 6, 7)] 6 | K = int(input("Enter K: ")) 7 | print(Remove_Tuple(Test_list, K)) 8 | -------------------------------------------------------------------------------- /DAYS/Day28/Sort_list_by_second_items.py: -------------------------------------------------------------------------------- 1 | # Approach 1: 2 | 3 | from operator import itemgetter 4 | 5 | 6 | def Sort_by_second_items(Test_tup): 7 | Test_tup.sort(key=lambda x: x[1]) 8 | return Test_tup 9 | 10 | 11 | Test_tup = [('for', 24), ('is', 10), ('Geeks', 28), 12 | ('Geeksforgeeks', 5), ('portal', 20), ('a', 15)] 13 | print(Sort_by_second_items(Test_tup)) 14 | 15 | # Approach 2: 16 | 17 | 18 | def Sort_by_second_items(Test_tup): 19 | Test_tup.sort(key=itemgetter(1)) 20 | return Test_tup 21 | 22 | 23 | Test_tup = [('for', 24), ('is', 10), ('Geeks', 28), 24 | ('Geeksforgeeks', 5), ('portal', 20), ('a', 15)] 25 | print(Sort_by_second_items(Test_tup)) 26 | -------------------------------------------------------------------------------- /DAYS/Day29/Inverted_Number_Pattern.py: -------------------------------------------------------------------------------- 1 | def Inverted_Star(Test_input): 2 | for i in range(Test_input+1, 0, -1): 3 | for j in range(1, i): 4 | print(j, end=" ") 5 | print(" ") 6 | 7 | 8 | Test_input = int(input("Enter N: ")) 9 | Inverted_Star(Test_input) 10 | -------------------------------------------------------------------------------- /DAYS/Day29/Inverted_Star_Pattern.py: -------------------------------------------------------------------------------- 1 | def Inverted_Star(Test_input): 2 | for i in range(Test_input+1, 0, -1): 3 | for j in range(0, i-1): 4 | print("*", end=" ") 5 | print(" ") 6 | 7 | 8 | Test_input = int(input("Enter N: ")) 9 | Inverted_Star(Test_input) 10 | -------------------------------------------------------------------------------- /DAYS/Day29/Leap_Year.py: -------------------------------------------------------------------------------- 1 | import calendar 2 | 3 | 4 | def Leap_Year(Test_input): 5 | count = 0 6 | Result = [] 7 | while count < 15: 8 | if calendar.isleap(Test_input): 9 | Result.append(Test_input) 10 | count += 1 11 | Test_input += 1 12 | return Result 13 | 14 | Test_input = int(input("Enter Year: ")) 15 | print(Leap_Year(Test_input)) 16 | -------------------------------------------------------------------------------- /DAYS/Day29/Rabbits_Chicken_Heads_and_Legs.py: -------------------------------------------------------------------------------- 1 | def solve(heads, legs): 2 | error_msg = "No solution" 3 | chicken_count = 0 4 | rabbit_count = 0 5 | if legs % 2 != 0: 6 | print("No Solution") 7 | else: 8 | rabbit_count = (legs-2*heads)/2 9 | chicken_count = (4*heads-legs)/2 10 | print(int(chicken_count)) 11 | print(int(rabbit_count)) 12 | 13 | 14 | solve(35, 94) -------------------------------------------------------------------------------- /DAYS/Day3/Monotonic_Or_Not.py: -------------------------------------------------------------------------------- 1 | def Monotonic_List(Arr): 2 | return (all(Arr[i] <= Arr[i+1] for i in range(len(Arr)-1)) or all(Arr[i] >= Arr[i+1] for i in range(len(Arr)-1))) 3 | 4 | 5 | a = [] 6 | n = int(input("How Many Elements: ")) 7 | print("Enter The Elements: ") 8 | for i in range(0, n): 9 | element = int(input()) 10 | a.append(element) 11 | if Monotonic_List(a): 12 | print("Yup, It's an Monotonic List") 13 | else: 14 | print("No, It's not an Monotonic List") 15 | -------------------------------------------------------------------------------- /DAYS/Day3/Reversing_a_list.py: -------------------------------------------------------------------------------- 1 | # Approach 1: 2 | 3 | def Reversing(Arr): 4 | return [ele for ele in reversed(Arr)] 5 | 6 | 7 | a = [] 8 | n = int(input("How Many Elements: ")) 9 | print("Enter The Elements: ") 10 | for i in range(0, n): 11 | element = int(input()) 12 | a.append(element) 13 | 14 | print(f"Reversed List: {Reversing(a)}") 15 | 16 | # Approach 2: 17 | 18 | def Reversing(Arr): 19 | return Arr[::-1] 20 | 21 | a = [] 22 | n = int(input("How Many Elements: ")) 23 | print("Enter The Elements: ") 24 | for i in range(0, n): 25 | element = int(input()) 26 | a.append(element) 27 | 28 | print(f"Reversed List: {Reversing(a)}") 29 | -------------------------------------------------------------------------------- /DAYS/Day3/Searching_Element_in_List.py: -------------------------------------------------------------------------------- 1 | # Approach 1: 2 | 3 | def Searching(Arr, ele): 4 | count = 0 5 | for i in Arr: 6 | if i == ele: 7 | print("Present") 8 | count = 1 9 | break 10 | if count == 0: 11 | print("Absent") 12 | 13 | 14 | a = [] 15 | n = int(input("How Many Elements: ")) 16 | print("Enter The Elements: ") 17 | for i in range(0, n): 18 | element = int(input()) 19 | a.append(element) 20 | b = int(input("Element to be Search: ")) 21 | Searching(a, b) 22 | 23 | # Approach 2: 24 | 25 | def Searching(Arr, ele): 26 | count = 0 27 | if ele in Arr: 28 | print("Present") 29 | else: 30 | print("Absent") 31 | 32 | 33 | a = [] 34 | n = int(input("How Many Elements: ")) 35 | print("Enter The Elements: ") 36 | for i in range(0, n): 37 | element = int(input()) 38 | a.append(element) 39 | b = int(input("Element to be Search: ")) 40 | Searching(a, b) 41 | -------------------------------------------------------------------------------- /DAYS/Day3/Second_Largest_in_List.py: -------------------------------------------------------------------------------- 1 | # Approach 1: 2 | 3 | def Second_Largest(Arr): 4 | Arr.remove(max(Arr)) 5 | return max(Arr) 6 | 7 | 8 | a = [] 9 | n = int(input("How Many Elements: ")) 10 | print("Enter The Elements: ") 11 | for i in range(0, n): 12 | element = int(input()) 13 | a.append(element) 14 | 15 | print(f"Second Largest: {Second_Largest(a)}") 16 | 17 | # Approach 2: 18 | 19 | def Second_Largest(Arr): 20 | return sorted(Arr)[-2] 21 | 22 | 23 | a = [] 24 | n = int(input("How Many Elements: ")) 25 | print("Enter The Elements: ") 26 | for i in range(0, n): 27 | element = int(input()) 28 | a.append(element) 29 | 30 | print(f"Second Largest: {Second_Largest(a)}") 31 | -------------------------------------------------------------------------------- /DAYS/Day3/Swap_First_With_Last.py: -------------------------------------------------------------------------------- 1 | # Approach 1: 2 | 3 | def Swap_Last_with_First(Arr): 4 | temp = Arr[0] 5 | Arr[0] = Arr[len(Arr)-1] 6 | Arr[len(Arr)-1] = temp 7 | return Arr 8 | 9 | 10 | a = [] 11 | n = int(input("How Many Elements: ")) 12 | print("Enter The Elements: ") 13 | for i in range(0, n): 14 | element = int(input()) 15 | a.append(element) 16 | 17 | print(f"List After Swapping: {Swap_Last_with_First(a)}") 18 | 19 | # Approach 2: 20 | 21 | def Swap_Last_with_First(Arr): 22 | Arr[0], Arr[-1] = Arr[-1], Arr[0] 23 | return Arr 24 | 25 | 26 | a = [] 27 | n = int(input("How Many Elements: ")) 28 | print("Enter The Elements: ") 29 | for i in range(0, n): 30 | element = int(input()) 31 | a.append(element) 32 | 33 | print(f"List After Swapping: {Swap_Last_with_First(a)}") 34 | -------------------------------------------------------------------------------- /DAYS/Day30/Flatten_tuple_of_list.py: -------------------------------------------------------------------------------- 1 | def Flatten_Tuple(Test_tuple): 2 | return tuple(sum(Test_tuple, [])) 3 | 4 | 5 | Test_tuple = ([5, 6], [6, 7, 8, 9], [3]) 6 | print(Flatten_Tuple(Test_tuple)) 7 | -------------------------------------------------------------------------------- /DAYS/Day30/Nested_Tuple_to_Custom_key_Dictionary.py: -------------------------------------------------------------------------------- 1 | def Nested_Tuple(Test_tuple, Test_keys): 2 | return [{key: value for key, value in zip(Test_keys, tuples)} for tuples in Test_tuple] 3 | 4 | 5 | Test_tuple = ((4, 'Gfg', 10), (3, 'is', 8), (6, 'Best', 10)) 6 | Test_keys = ['key', 'value', 'id'] 7 | print(Nested_Tuple(Test_tuple, Test_keys)) 8 | -------------------------------------------------------------------------------- /DAYS/Day30/Order_Tuple_by_List.py: -------------------------------------------------------------------------------- 1 | def Order_Tuple(Test_list, Ord_list): 2 | temp = dict(Test_list) 3 | return [(key, temp[key]) for key in Ord_list] 4 | 5 | 6 | Test_list = [('Gfg', 3), ('best', 9), ('CS', 10), ('Geeks', 2)] 7 | Ord_list = ['Geeks', 'best', 'CS', 'Gfg'] 8 | print(Order_Tuple(Test_list, Ord_list)) 9 | -------------------------------------------------------------------------------- /DAYS/Day30/Remove_duplicates_tuple.py: -------------------------------------------------------------------------------- 1 | def Remove_Duplicates(Test_list): 2 | Result = [tuple(sorted(tuples)) for tuples in Test_list] 3 | return list(set(Result)) 4 | 5 | 6 | Test_list = [(4, 6), (1, 2), (9, 2), (2, 1), (5, 7), (6, 4), (9, 2)] 7 | print(Remove_Duplicates(Test_list)) 8 | -------------------------------------------------------------------------------- /DAYS/Day31/Extract_tuple_having_k_digit_elements.py: -------------------------------------------------------------------------------- 1 | def Extract_tuple(Test_list, K): 2 | return [sub for sub in Test_list if all(len(str(ele)) == K for ele in sub)] 3 | 4 | 5 | Test_list = [(54, 2), (34, 55), (222, 23), (12, 45), (78, )] 6 | K = int(input("Enter K: ")) 7 | print(Extract_tuple(Test_list, K)) 8 | -------------------------------------------------------------------------------- /DAYS/Day31/Remove_None_Elements_from_Tuple.py: -------------------------------------------------------------------------------- 1 | def Remove_Elements(Test_list): 2 | return [sub for sub in Test_list if not all(ele == None for ele in sub)] 3 | 4 | 5 | Test_list = [(None, 2), (None, None), (3, 4), (12, 3), (None, )] 6 | print(Remove_Elements(Test_list)) 7 | -------------------------------------------------------------------------------- /DAYS/Day31/Sort_Tuple_by_Total_digits.py: -------------------------------------------------------------------------------- 1 | def Sort_Tuple(Test_list): 2 | return sorted(Test_list, key= lambda Test_Tuple: sum(len(str(ele)) for ele in Test_Tuple)) 3 | 4 | Test_list = [(3, 4, 6, 723), (1, 2), (12345,), (134, 234, 34)] 5 | print(Sort_Tuple(Test_list)) -------------------------------------------------------------------------------- /DAYS/Day31/Sort_tuple_by_last_element.py: -------------------------------------------------------------------------------- 1 | def Sort_by_last_element(Test_tuple): 2 | return sorted(Test_tuple, key=lambda x: x[-1]) 3 | 4 | 5 | Test_tuple = [(1, 3), (3, 2), (2, 1)] 6 | print(Sort_by_last_element(Test_tuple)) 7 | -------------------------------------------------------------------------------- /DAYS/Day32/Sort_tuple_by_Maximum_Values.py: -------------------------------------------------------------------------------- 1 | def Sort_Tuple(Test_list): 2 | Test_list.sort(key=lambda Sub: max(Sub), reverse=True) 3 | return Test_list 4 | 5 | 6 | Test_list = [(4, 5, 5, 7), (1, 3, 7, 4), (19, 4, 5, 3), (1, 2)] 7 | print(Sort_Tuple(Test_list)) 8 | -------------------------------------------------------------------------------- /DAYS/Day32/Tuple_Elements_divisble_by_k.py: -------------------------------------------------------------------------------- 1 | def Tuple_Elements_divisble(Test_list, k): 2 | return [sub for sub in Test_list if all(ele % k == 0 for ele in sub)] 3 | 4 | 5 | Test_list = [(6, 24, 12), (7, 9, 6), (12, 18, 21)] 6 | k = int(input("Enter Divisor: ")) 7 | print(Tuple_Elements_divisble(Test_list, k)) 8 | -------------------------------------------------------------------------------- /DAYS/Day33/Binary_Search.py: -------------------------------------------------------------------------------- 1 | def Binary_Search(Test_list, low, high, k): 2 | if high >= low: 3 | Mid = (low+high)//2 4 | if Test_list[Mid] == k: 5 | return Mid 6 | elif Test_list[Mid] > k: 7 | return Binary_Search(Test_list, low, Mid-1, k) 8 | else: 9 | return Binary_Search(Test_list, Mid+1, high, k) 10 | else: 11 | return -1 12 | 13 | 14 | Test_list = [2, 3, 4, 10, 40] 15 | k = int(input("Which Element: ")) 16 | Result = Binary_Search(Test_list, 0, len(Test_list)-1, k) 17 | if Result != -1: 18 | print(f"Found at {Result}") 19 | else: 20 | print(f"Not Found") 21 | -------------------------------------------------------------------------------- /DAYS/Day33/Linear_Search.py: -------------------------------------------------------------------------------- 1 | def Linear_Search(Test_list, k): 2 | for ele in range(len(Test_list)): 3 | if Test_list[ele] == k: 4 | return ele 5 | 6 | return -1 7 | 8 | 9 | Test_list = [2, 3, 4, 10, 40] 10 | k = int(input("Which Element: ")) 11 | Result = Linear_Search(Test_list, k) 12 | if Result != -1: 13 | print(f"Found at {Result}") 14 | else: 15 | print(f"Not Found") 16 | -------------------------------------------------------------------------------- /DAYS/Day34/Medical_Speciality.py: -------------------------------------------------------------------------------- 1 | Test_dict = {"P": "Pediatrics", "O": "Orthopedics", "E": "ENT"} 2 | Test_list = [101, 'P', 102, 'E', 302, 'P', 305, 3 | 'P', 401, 'E', 656, 'O', 987, 'E', 999, 'E'] 4 | Result = {} 5 | for items in Test_list: 6 | if isinstance(items, str): 7 | Result[items] = Test_list.count(items) 8 | print(max(Result, key=Result.get)) 9 | print(Test_dict[max(Result, key=Result.get)]) 10 | -------------------------------------------------------------------------------- /DAYS/Day34/Translate_Lang.py: -------------------------------------------------------------------------------- 1 | def translate(bilingual_dict, english_words_list): 2 | swedish_words_list = [] 3 | for key in english_words_list: 4 | swedish_words_list.append(bilingual_dict[key]) 5 | return swedish_words_list 6 | 7 | 8 | bilingual_dict = {"merry": "god", "christmas": "jul", 9 | "and": "och", "happy": "gott", "new": "nytt", "year": "ar"} 10 | english_words_list = ["merry", "christmas"] 11 | print("The bilingual dict is:", bilingual_dict) 12 | print("The english words are:", english_words_list) 13 | 14 | swedish_words_list = translate(bilingual_dict, english_words_list) 15 | print("The equivalent swedish words are:", swedish_words_list) 16 | -------------------------------------------------------------------------------- /DAYS/Day35/Insertion_Sort.py: -------------------------------------------------------------------------------- 1 | def Insertion_Sorting(Test_list): 2 | for i in range(len(Test_list)): 3 | key = Test_list[i] 4 | j = i-1 5 | while(j >= 0 and key < Test_list[j]): 6 | Test_list[j+1] = Test_list[j] 7 | j -= 1 8 | Test_list[j+1] = key 9 | return Test_list 10 | 11 | 12 | Test_list = [14, 18, 11, 56, 12, 32, 5] 13 | print(Insertion_Sorting(Test_list)) -------------------------------------------------------------------------------- /DAYS/Day35/Insertion_Sort_Recursive.py: -------------------------------------------------------------------------------- 1 | def Insertion_Sorting_Recur(Test_list, n): 2 | if n <= 1: 3 | return 4 | Insertion_Sorting_Recur(Test_list, n-1) 5 | last = Test_list[n-1] 6 | j = n-2 7 | while(j >= 0 and last < Test_list[j]): 8 | Test_list[j+1] = Test_list[j] 9 | j -= 1 10 | Test_list[j+1] = last 11 | 12 | 13 | Test_list = [14, 18, 11, 56, 12, 32, 5] 14 | Insertion_Sorting_Recur(Test_list, len(Test_list)) 15 | print(Test_list) 16 | -------------------------------------------------------------------------------- /DAYS/Day36/Gem_Qty.py: -------------------------------------------------------------------------------- 1 | Gem_Qty = {"ruby": 25, "diamond": 30, 2 | "emrald": 15, "topaz": 18, "sapphire": 20} 3 | Gem_Price = {"ruby": 2000, "diamond": 4000, 4 | "emrald": 1900, "topaz": 500, "sapphire": 2500} 5 | Gem_Name = input("Enter Gem Names: ").split(",") 6 | Gem_Num = input("Enter Gem Quantities: ").split(",") 7 | Total_Cost = 0 8 | for items in range(len(Gem_Name)): 9 | Total_Cost = Total_Cost+int(Gem_Num[items])*Gem_Price[Gem_Name[items]] 10 | Gem_Qty[Gem_Name[items]] = Gem_Qty[Gem_Name[items]]-int(Gem_Num[items]) 11 | 12 | print(f"Total Cost: {Total_Cost}") 13 | print(f"Gem_Qty: {Gem_Qty}") 14 | -------------------------------------------------------------------------------- /DAYS/Day36/Quick_Sort.py: -------------------------------------------------------------------------------- 1 | def partition(Test_list, low, high): 2 | i = low-1 3 | pivot = Test_list[high] 4 | for j in range(low, high): 5 | if Test_list[j] < pivot: 6 | i += 1 7 | Test_list[i], Test_list[j] = Test_list[j], Test_list[i] 8 | Test_list[i+1], Test_list[high] = Test_list[high], Test_list[i+1] 9 | return (i+1) 10 | 11 | 12 | def Quick_Sorting(Test_list, low, high): 13 | if len(Test_list) == 1: 14 | return Test_list 15 | if low < high: 16 | Part = partition(Test_list, low, high) 17 | Quick_Sorting(Test_list, low, Part-1) 18 | Quick_Sorting(Test_list, Part+1, high) 19 | 20 | Test_list = [11, 35, 12, 36, 10, 6, 25] 21 | Quick_Sorting(Test_list, 0, len(Test_list)-1) 22 | print(Test_list) -------------------------------------------------------------------------------- /DAYS/Day37/Bubble_Sort.py: -------------------------------------------------------------------------------- 1 | def Bubble_Sorting(Test_list): 2 | for i in range(len(Test_list)-1): 3 | for j in range(len(Test_list)-i-1): 4 | if Test_list[j] > Test_list[j+1]: 5 | Test_list[j], Test_list[j+1] = Test_list[j+1], Test_list[j] 6 | return Test_list 7 | 8 | 9 | Test_list = [64, 34, 25, 12, 22, 11, 90] 10 | print(Bubble_Sorting(Test_list)) 11 | -------------------------------------------------------------------------------- /DAYS/Day37/Selection_Sort.py: -------------------------------------------------------------------------------- 1 | def Selection_Sorting(Test_list): 2 | for i in range(len(Test_list)): 3 | min = i 4 | for j in range(i+1, len(Test_list)): 5 | if Test_list[min] > Test_list[j]: 6 | min = j 7 | Test_list[i], Test_list[min] = Test_list[min], Test_list[i] 8 | return Test_list 9 | 10 | Test_list = [64, 25, 12, 22, 11] 11 | print(Selection_Sorting(Test_list)) -------------------------------------------------------------------------------- /DAYS/Day38/Inverted_Star_Pattern.py: -------------------------------------------------------------------------------- 1 | def Inverted_Star(n): 2 | for i in range(n, 0, -1): 3 | print((n-i)*' ' + i * '*') 4 | 5 | 6 | n = int(input("Enter N: ")) 7 | Inverted_Star(n) 8 | -------------------------------------------------------------------------------- /DAYS/Day38/Merge_Sort.py: -------------------------------------------------------------------------------- 1 | def Merging(Test_list, low, mid, high): 2 | n1 = mid-low+1 3 | n2 = high-mid 4 | 5 | left = [0]*n1 6 | right = [0]*n2 7 | 8 | for i in range(0, n1): 9 | left[i] = Test_list[low+i] 10 | 11 | for j in range(0, n2): 12 | right[j] = Test_list[mid+1+j] 13 | 14 | i = 0 15 | j = 0 16 | k = low 17 | while i < n1 and j < n2: 18 | if left[i] < right[j]: 19 | Test_list[k] = left[i] 20 | i += 1 21 | else: 22 | Test_list[k] = right[j] 23 | j += 1 24 | k += 1 25 | 26 | while i < n1: 27 | Test_list[k] = left[i] 28 | i += 1 29 | k += 1 30 | 31 | while j < n2: 32 | Test_list[k] = right[j] 33 | j += 1 34 | k += 1 35 | 36 | 37 | def Merge_Sorting(Test_list, low, high): 38 | if low < high: 39 | mid = (low+(high-1))//2 40 | Merge_Sorting(Test_list, low, mid) 41 | Merge_Sorting(Test_list, mid+1, high) 42 | Merging(Test_list, low, mid, high) 43 | 44 | 45 | if __name__ == "__main__": 46 | Test_list = [12, 11, 13, 5, 6, 7] 47 | Merge_Sorting(Test_list, 0, len(Test_list)-1) 48 | for ele in range(len(Test_list)): 49 | print(Test_list[ele], end=" ") 50 | print("\n") 51 | 52 | -------------------------------------------------------------------------------- /DAYS/Day39/Current_Time.py: -------------------------------------------------------------------------------- 1 | from datetime import datetime 2 | Time_now = datetime.now() 3 | Result_time = Time_now.strftime("%H:%M:%S") 4 | print(Result_time) 5 | -------------------------------------------------------------------------------- /DAYS/Day39/Current_Time_and_Date.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | Result = datetime.datetime.now() 3 | print(f"{Result.day}-{Result.month}-{Result.year}") 4 | print(f"{Result.hour}:{Result.minute}:{Result.second}") 5 | -------------------------------------------------------------------------------- /DAYS/Day4/Cloning_or_Copying_list.py: -------------------------------------------------------------------------------- 1 | # Approach 1: 2 | 3 | def Cloning_List(Given_List): 4 | Result = Given_List[:] 5 | return Result 6 | 7 | 8 | Given_List = [4, 5, 7, 8, 9, 6, 10, 15] 9 | print(Cloning_List(Given_List)) 10 | 11 | # Approach 2: 12 | 13 | 14 | def Cloning_List(Given_List): 15 | Result = [] 16 | Result.extend(Given_List) 17 | return Result 18 | 19 | 20 | Given_List = [4, 5, 7, 8, 9, 6, 10, 15] 21 | print(Cloning_List(Given_List)) 22 | 23 | # Approach 3: 24 | 25 | 26 | def Cloning_List(Given_List): 27 | Result = list(Given_List) 28 | return Result 29 | 30 | 31 | Given_List = [4, 5, 7, 8, 9, 6, 10, 15] 32 | print(Cloning_List(Given_List)) 33 | 34 | # Approach 4: 35 | 36 | 37 | def Cloning_List(Given_List): 38 | Result = Given_List 39 | return Result 40 | 41 | 42 | Given_List = [4, 5, 7, 8, 9, 6, 10, 15] 43 | print(Cloning_List(Given_List)) 44 | -------------------------------------------------------------------------------- /DAYS/Day4/N_Largest_Elements_in_List.py: -------------------------------------------------------------------------------- 1 | # Approach 1: 2 | 3 | def N_Largest_Elements(Arr, Upto): 4 | result = [] 5 | for i in range(0, Upto): 6 | max = 0 7 | for j in range(0, len(Arr)): 8 | if Arr[j] > max: 9 | max = Arr[j] 10 | Arr.remove(max) 11 | result.append(max) 12 | return result 13 | 14 | 15 | Given_List = [] 16 | n = int(input("How Many Elements: ")) 17 | print("Enter The Elements: ") 18 | for i in range(0, n): 19 | element = int(input()) 20 | Given_List.append(element) 21 | Upto = int(input("Upto Which Number: ")) 22 | print(N_Largest_Elements(Given_List, Upto)) 23 | 24 | 25 | # Approach 2: 26 | 27 | def N_Largest_Elemnts(Arr, Upto): 28 | return sorted(Arr)[-Upto:] 29 | 30 | 31 | Given_List = [] 32 | n = int(input("How Many Elements: ")) 33 | print("Enter The Elements: ") 34 | for i in range(0, n): 35 | element = int(input()) 36 | Given_List.append(element) 37 | Upto = int(input("Upto Which Number: ")) 38 | print(N_Largest_Elemnts(Given_List, Upto)) 39 | -------------------------------------------------------------------------------- /DAYS/Day4/Occurence_of_Elements.py: -------------------------------------------------------------------------------- 1 | # Approach 1: 2 | 3 | def Occurence_of_Element(Given_List, Element): 4 | count = 0 5 | for ele in Given_List: 6 | if ele == Element: 7 | count += 1 8 | return count 9 | 10 | 11 | Given_List = [14, 25, 16, 23, 10, 5, 6, 8, 7, 9, 10, 25, 14] 12 | Element = 14 13 | print(Occurence_of_Element(Given_List, Element)) 14 | 15 | # Approach 2: 16 | 17 | def Occurence_of_Element(Given_List, Element): 18 | return Given_List.count(Element) 19 | 20 | 21 | Given_List = [14, 25, 16, 23, 10, 5, 6, 8, 7, 9, 10, 25, 14] 22 | Element = 14 23 | print(Occurence_of_Element(Given_List, Element)) 24 | -------------------------------------------------------------------------------- /DAYS/Day4/Remove_Empty_List.py: -------------------------------------------------------------------------------- 1 | # Approach 1: 2 | 3 | def Remove_Empty_List(Given_List): 4 | Result = [ele for ele in Given_List if ele != []] 5 | return Result 6 | 7 | 8 | Given_List = [5, 6, [], 7, 8, 9, [], 12, [], 4,[]] 9 | print(Remove_Empty_List(Given_List)) 10 | 11 | # Approach 2: 12 | 13 | Given_List = [5, 6, [], 7, 8, 9, [], 12, [], 4, []] 14 | result = list(filter(None, Given_List)) 15 | print(result) 16 | 17 | -------------------------------------------------------------------------------- /DAYS/Day4/Remove_Multiple_Elements.py: -------------------------------------------------------------------------------- 1 | # Approach 1: 2 | 3 | def Remove_Multiple(Arr1, Arr2): 4 | Final = [ele for ele in Arr1 if ele not in Arr2] 5 | return Final 6 | 7 | 8 | a = [1, 2, 3, 4, 5] 9 | b = [3, 4] 10 | print(Remove_Multiple(a, b)) 11 | 12 | # Approach 2: 13 | 14 | def Remove_Multiple(Arr1, Arr2): 15 | for ele in Arr2: 16 | Arr1.remove(ele) 17 | return Arr1 18 | 19 | 20 | a = [1, 2, 3, 4, 5] 21 | b = [3, 4] 22 | print(Remove_Multiple(a, b)) 23 | -------------------------------------------------------------------------------- /DAYS/Day40/12_to_24_hour_format.py: -------------------------------------------------------------------------------- 1 | from time import time 2 | 3 | 4 | def Convert_Format(Time): 5 | if Time[:2] == "12" and Time[-2:] == "AM": 6 | return "00"+Time[2:-2] 7 | elif Time[-2:] == "AM": 8 | return Time[:-2] 9 | elif Time[:2] == "12" and Time[-2:] == "PM": 10 | return Time[:-2] 11 | else: 12 | return str(int(Time[:2])+12)+Time[2:] 13 | 14 | 15 | Time = "09:05:52 PM" 16 | print(Convert_Format(Time)) 17 | -------------------------------------------------------------------------------- /DAYS/Day40/Today_Yesterday_Tommorow_Date.py: -------------------------------------------------------------------------------- 1 | from datetime import date, datetime, timedelta 2 | 3 | Present_Day = datetime.now() 4 | Yesterday = Present_Day-timedelta(1) 5 | Tommorow = Present_Day+timedelta(1) 6 | 7 | print(f"Today Date: {Present_Day.strftime('%d-%m-%Y')}") 8 | print(f"Yesterday Date: {Yesterday.strftime('%d-%m-%Y')}") 9 | print(f"Tommorow Date: {Tommorow.strftime('%d-%m-%Y')}") 10 | -------------------------------------------------------------------------------- /DAYS/Day41/Difference_bw_Times.py: -------------------------------------------------------------------------------- 1 | # Approach 1: 2 | 3 | def Time_Difference(h1, m1, h2, m2): 4 | Time_1 = h1*60+m1 5 | Time_2 = h2*60+m2 6 | if Time_1 == Time_2: 7 | print("Both are Same") 8 | return 9 | else: 10 | Diff = Time_2-Time_1 11 | 12 | Hour = int(Diff/60) 13 | Minutes = Diff % 60 14 | print(f"Difference: {Hour} : {Minutes}") 15 | 16 | 17 | if __name__ == "__main__": 18 | Time_Difference(7, 20, 9, 45) 19 | Time_Difference(15, 23, 18, 54) 20 | Time_Difference(16, 20, 16, 20) 21 | 22 | 23 | # Approach 2: 24 | 25 | from datetime import datetime 26 | Time_1 = '10:33:26' 27 | Time_2 = '11:15:49' 28 | FMT = '%H:%M:%S' 29 | Diff = datetime.strptime(Time_2, FMT) - datetime.strptime(Time_1, FMT) 30 | print(Diff) 31 | -------------------------------------------------------------------------------- /DAYS/Day41/String_to_Timestamp.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | 3 | Given_time = "02/04/2021" 4 | 5 | Semi_Result = datetime.datetime.strptime(Given_time, "%d/%m/%Y") 6 | Result = datetime.datetime.timestamp(Semi_Result) 7 | print(Result) 8 | -------------------------------------------------------------------------------- /DAYS/Day42/Day_Occurs_in_a_Year.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | import calendar 3 | 4 | 5 | def Day_Occur(Year): 6 | Days_list = ['Monday', 'Tuesday', 'Wednesday', 7 | 'Thursday', 'Friday', 'Saturday', 'Sunday'] 8 | 9 | Days_Count = [52 for i in range(7)] 10 | pos = 0 11 | First_day = datetime.datetime(Year, month=1, day=1).strftime("%A") 12 | pos = Days_list.index(First_day) 13 | if calendar.isleap(Year): 14 | Days_Count[pos] += 1 15 | Days_Count[(pos+1) % 7] += 1 16 | else: 17 | Days_Count[pos] += 1 18 | 19 | for days in range(7): 20 | print(f"{Days_list[days]} : {Days_Count[days]}") 21 | 22 | 23 | Year = int(input("Enter a Year: ")) 24 | Day_Occur(Year) 25 | -------------------------------------------------------------------------------- /DAYS/Day42/Timestamp_to_datetime.py: -------------------------------------------------------------------------------- 1 | from datetime import datetime 2 | 3 | Given_time = 1617301800 4 | 5 | Result = datetime.fromtimestamp(Given_time).strftime("%d-%m-%Y") 6 | print(Result) 7 | -------------------------------------------------------------------------------- /DAYS/Day43/Count_Uppercase_Lowercase.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | 4 | def Upper_Lower_Count(Test_string): 5 | Upper_Case = re.findall(r"[A-Z]", Test_string) 6 | Lower_Case = re.findall(r"[a-z]", Test_string) 7 | Numeric_Char = re.findall(r"[0-9]", Test_string) 8 | Special_Char = re.findall(r"[ ,.!?]", Test_string) 9 | print(f"Upper Case - {len(Upper_Case)}") 10 | print(f"Lower Case - {len(Lower_Case)}") 11 | print(f"Numeric Characters - {len(Numeric_Char)}") 12 | print(f"Special Characters Case - {len(Special_Char)}") 13 | 14 | 15 | Test_string = "ThisIs100DaysofCodeChallenge !, 123" 16 | Upper_Lower_Count(Test_string) 17 | -------------------------------------------------------------------------------- /DAYS/Day43/Defined_Characters_Using_Regex.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | 4 | def Check_Defined_Characters(Test_String, pattern): 5 | if re.search(pattern, Test_String): 6 | print("Valid String...") 7 | else: 8 | print("Invalid String...") 9 | 10 | 11 | pattern = re.compile("^[1234]+$") 12 | Check_Defined_Characters('2134', pattern) 13 | Check_Defined_Characters('349', pattern) 14 | -------------------------------------------------------------------------------- /DAYS/Day44/Extract_Maximum_Using_Regex.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | 4 | def Extract_Maximum(Test_string): 5 | # Result=re.findall(r"[0-9]+", Test_string) 6 | Result=re.findall("\d+", Test_string) 7 | return max(Result) 8 | 9 | Test_string = '100klh564abc365bg' 10 | print(Extract_Maximum(Test_string)) -------------------------------------------------------------------------------- /DAYS/Day44/Most_Occurring_number_Using_Regex.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | 4 | def Most_Occur(Test_string): 5 | Result = re.findall(r"[0-9]+", Test_string) 6 | return max(Result, key=Result.count) 7 | 8 | 9 | Test_string = 'geek55of55gee4ksabc3dr2x' 10 | print(Most_Occur(Test_string)) 11 | -------------------------------------------------------------------------------- /DAYS/Day45/Put_Spaces_bw_Words.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | def Put_Spaces(Test_string): 4 | words = re.findall('[A-Z][a-z]*', Test_string) 5 | Result = [word.lower() for word in words] 6 | print(" ".join(Result)) 7 | 8 | 9 | if __name__ == "__main__": 10 | Test_string = 'BruceWayneIsBatman' 11 | Put_Spaces(Test_string) 12 | -------------------------------------------------------------------------------- /DAYS/Day45/Starts_End_with_same_Character.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | 4 | def Starts_End(Pattern, Test_string): 5 | if re.search(Pattern, Test_string): 6 | print("Valid...") 7 | else: 8 | print("Invalid...") 9 | 10 | 11 | if __name__ == "__main__": 12 | Pattern = r'^[a-z]$|^([a-z]).*\1$' 13 | Test_string = "abba" 14 | Starts_End(Pattern, Test_string) 15 | -------------------------------------------------------------------------------- /DAYS/Day46/Remove_Duplicates_from_string.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | 4 | def Remove_Duplicates(Test_string): 5 | Pattern = r"\b(\w+)(?:\W\1\b)+" 6 | return re.sub(Pattern, r"\1", Test_string, flags=re.IGNORECASE) 7 | 8 | 9 | Test_string1 = "Good bye bye world world" 10 | Test_string2 = "Ram went went to to his home" 11 | Test_string3 = "Hello hello world world" 12 | print(Remove_Duplicates(Test_string1)) 13 | print(Remove_Duplicates(Test_string2)) 14 | print(Remove_Duplicates(Test_string3)) 15 | -------------------------------------------------------------------------------- /DAYS/Day46/Upper_Case_followed_by_Lower_Case.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | 4 | def Match_Chars(Test_string): 5 | Pattern = "[A-Z]+[a-z]+$" 6 | if re.search(Pattern, Test_string): 7 | print("Yes") 8 | else: 9 | print("No") 10 | 11 | 12 | Test_string1 = "CodeChallenge" 13 | Test_string2 = "codechallenge" 14 | Test_string3 = "codeChallenge" 15 | Match_Chars(Test_string1) 16 | Match_Chars(Test_string2) 17 | Match_Chars(Test_string3) 18 | -------------------------------------------------------------------------------- /DAYS/Day47/Remove_all_except_numbers_and_letters.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | 4 | def Remove_all(Test_string): 5 | return re.sub(r"[\W_]+", "", Test_string) 6 | 7 | 8 | Test_string = "123abcjw:, .@! eiw" 9 | print(Remove_all(Test_string)) 10 | -------------------------------------------------------------------------------- /DAYS/Day47/String_ending_wth_numbers.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | 4 | def string_ending(Test_string): 5 | pattern = "[a-zA-Z0-9]$" 6 | if re.search(pattern, Test_string): 7 | print("Accepted") 8 | else: 9 | print("Discarded") 10 | 11 | 12 | Test_string1 = "satyam@" 13 | Test_string2 = "satyamempire5623" 14 | Test_string3 = "satyam." 15 | Test_string4 = "heysatyamhere" 16 | string_ending(Test_string1) 17 | string_ending(Test_string2) 18 | string_ending(Test_string3) 19 | string_ending(Test_string4) 20 | -------------------------------------------------------------------------------- /DAYS/Day48/String_Starting_with_Vowel.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | 4 | def Vowel_String(Test_string): 5 | pattern = "^[aeiouAEIOU][a-zA-Z0-9]*" 6 | if re.search(pattern, Test_string): 7 | print("Accepted") 8 | else: 9 | print("Discarded") 10 | 11 | 12 | Test_string1 = "code" 13 | Test_string2 = "expression20" 14 | Vowel_String(Test_string1) 15 | Vowel_String(Test_string2) 16 | -------------------------------------------------------------------------------- /DAYS/Day48/String_start_with_substring.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | 4 | def String_start_with_Substring(Test_string, Test_sample): 5 | Result = "^"+Test_sample 6 | if re.search(Result, Test_string): 7 | return "True" 8 | else: 9 | return "False" 10 | 11 | 12 | Test_string = "100 Days of Code Challenge makes your basic clear" 13 | Test_sample = "100" 14 | print(String_start_with_Substring(Test_string, Test_sample)) 15 | -------------------------------------------------------------------------------- /DAYS/Day49/Parsing_and_Processing_URL.py: -------------------------------------------------------------------------------- 1 | from itertools import permutations 2 | import re 3 | Test_URL = 'https://www.geeksforgeeks.org/' 4 | Sample1 = "(\w+)://" 5 | Sample2 = "://www.([\w\-\.]+)" 6 | print("Protocol: " + "".join(re.findall(Sample1, Test_URL))) 7 | print("Host Name: " + "".join(re.findall(Sample2, Test_URL))) 8 | -------------------------------------------------------------------------------- /DAYS/Day49/URL_Validation.py: -------------------------------------------------------------------------------- 1 | import validators 2 | valid = validators.url("https://github.com/Iamtripathisatyam") 3 | if valid: 4 | print("Valid URL") 5 | else: 6 | print("Invalid URL") 7 | -------------------------------------------------------------------------------- /DAYS/Day5/Break_into_Chunks.py: -------------------------------------------------------------------------------- 1 | def Break_Chunks(Given_List, n): 2 | return [Given_List[ele:ele+n] for ele in range(1, len(Given_List), n)] 3 | 4 | 5 | Given_List = [10, 45, 20, 62, 47, 85, 12, 63, 24, 78, 10] 6 | n = 2 7 | print(Break_Chunks(Given_List, n)) 8 | -------------------------------------------------------------------------------- /DAYS/Day5/Cumulative_Sum_of_List.py: -------------------------------------------------------------------------------- 1 | def Cumulative_Sum(Given_List): 2 | Result = [] 3 | sum = 0 4 | for i in range(0, len(Given_List)): 5 | sum += Given_List[i] 6 | Result.append(sum) 7 | return Result 8 | 9 | 10 | Given_List = [10, 20, 30, 40, 50, 60] 11 | print(Cumulative_Sum(Given_List)) 12 | -------------------------------------------------------------------------------- /DAYS/Day5/Duplicates_in_List.py: -------------------------------------------------------------------------------- 1 | def Find_Duplicates(Given_List): 2 | Duplicates = [] 3 | for i in range(0, len(Given_List)): 4 | for j in range(i+1, len(Given_List)): 5 | if Given_List[i] == Given_List[j] and Given_List[i] not in Duplicates: 6 | Duplicates.append(Given_List[i]) 7 | return Duplicates 8 | 9 | 10 | Given_List = [4, 5, 1, 2, 6, 5, 2] 11 | print(Find_Duplicates(Given_List)) 12 | -------------------------------------------------------------------------------- /DAYS/Day5/Flatten_List.py: -------------------------------------------------------------------------------- 1 | def Flatten_List(Given_List): 2 | stack = [Given_List] 3 | Result = [] 4 | while stack: 5 | current = stack.pop(-1) 6 | if isinstance(current, list): 7 | stack.extend(current) 8 | else: 9 | Result.append(current) 10 | Result.reverse() 11 | return Result 12 | 13 | 14 | Given_List = [1, 2, [3, 4, [5, 6], 7], [[[8, 9], 10]], [11, [12, 13]]] 15 | print(f"Flatten list: {Flatten_List(Given_List)}") 16 | -------------------------------------------------------------------------------- /DAYS/Day50/Extension_Checking_using_Regex.py: -------------------------------------------------------------------------------- 1 | import re 2 | File_names = ["gfg.html", "geeks.xml", 3 | "computer.txt", "geeksforgeeks.jpg", "hey.png"] 4 | Extension = input("Enter Extension: ") 5 | Final = "\."+Extension+"$" 6 | for file in File_names: 7 | match = re.search(Final, file) 8 | if match: 9 | print("File is:", file) 10 | -------------------------------------------------------------------------------- /DAYS/Day50/Validate_email.py: -------------------------------------------------------------------------------- 1 | import re 2 | Regex = r"[^@]+@[^@]+\.[^@]+" 3 | Email = input("Enter Email: ") 4 | if not re.match(Regex, Email): 5 | print("Invalid Email...") 6 | else: 7 | print("Valid Email") 8 | -------------------------------------------------------------------------------- /DAYS/Day51/Validity_of_Password.py: -------------------------------------------------------------------------------- 1 | import re 2 | password = "R@m@_f0rtu9e$" 3 | flag = 0 4 | while True: 5 | if len(password) < 8: 6 | flag = -1 7 | break 8 | elif not re.search("[A-Z]", password): 9 | flag = -1 10 | break 11 | elif not re.search("[a-z]", password): 12 | flag = -1 13 | break 14 | elif not re.search("[0-9]", password): 15 | flag = -1 16 | break 17 | elif not re.search("[_@$]", password): 18 | flag = -1 19 | break 20 | elif re.search("\s", password): 21 | flag = -1 22 | break 23 | else: 24 | print("It's a Valid Password") 25 | break 26 | 27 | if flag == -1: 28 | print("Invalid Password") 29 | 30 | -------------------------------------------------------------------------------- /DAYS/Day51/Weak_or_Strong_Pass.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | 4 | def password(Given_Pass): 5 | 6 | if Given_Pass == "\n" or Given_Pass == " ": 7 | return "Password cannot be a newline or space" 8 | 9 | if 9 <= len(Given_Pass) <= 20: 10 | 11 | if re.search(r'(.)\1\1', Given_Pass): 12 | return "Sorry, Weak Password - Same character repeats three or more times in a row" 13 | 14 | if re.search(r'(..)(.*?)\1', Given_Pass): 15 | return "Sorry, Weak Password - Same string pattern repetition" 16 | 17 | else: 18 | return "Awesome, Strong Password" 19 | 20 | else: 21 | return "Password length must be 9-20 characters" 22 | 23 | 24 | if __name__ == "__main__": 25 | print(password("Qggf!@ghf3")) 26 | print(password("Gggksforgeeks")) 27 | print(password("aaabnil1gu")) 28 | print(password("Aasd!feasn")) 29 | print(password("772*hd897")) 30 | print(password(" ")) 31 | -------------------------------------------------------------------------------- /DAYS/Day52/Number_is_a_power_of_2.py: -------------------------------------------------------------------------------- 1 | def power(Num1): 2 | while Num1 % 2 == 0: 3 | Num1 /= 2 4 | return Num1 == 1 5 | 6 | 7 | Num1 = int(input("Enter a Number: ")) 8 | print(power(Num1)) 9 | -------------------------------------------------------------------------------- /DAYS/Day52/Number_is_power_of_3.py: -------------------------------------------------------------------------------- 1 | def power(Num1): 2 | while Num1 % 3 == 0: 3 | Num1 /= 3 4 | return Num1 == 1 5 | 6 | 7 | Num1 = int(input("Enter a Number: ")) 8 | print(power(Num1)) 9 | -------------------------------------------------------------------------------- /DAYS/Day53/Integer_is_power_of_another_Integer.py: -------------------------------------------------------------------------------- 1 | def power(num1, num2): 2 | while(num1 % num2 == 0): 3 | num1 /= num2 4 | return num1 == 1 5 | 6 | 7 | num1 = int(input("Enter 1st Number: ")) 8 | num2 = int(input("Enter 2nd Number: ")) 9 | print(power(num1, num2)) 10 | -------------------------------------------------------------------------------- /DAYS/Day53/Perfect_Square.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | 4 | def perfect_square(num): 5 | if num > 0: 6 | root = math.sqrt(num) 7 | return root*root == num 8 | return False 9 | 10 | 11 | num = int(input("Enter a Number: ")) 12 | print(perfect_square(num)) 13 | -------------------------------------------------------------------------------- /DAYS/Day54/Missing_Number_in_list.py: -------------------------------------------------------------------------------- 1 | def missing_number(Test_list): 2 | return sum(range(Test_list[0], Test_list[-1]+1)) - sum(Test_list) 3 | 4 | 5 | Test_list_1 = [1, 2, 3, 5, 6, 7, 8] 6 | Test_list_2 = [10, 11, 12, 14, 15, 16, 17] 7 | print(missing_number(Test_list_1)) 8 | print(missing_number(Test_list_2)) 9 | -------------------------------------------------------------------------------- /DAYS/Day54/Missing_Numbers_in_list.py: -------------------------------------------------------------------------------- 1 | def missing_numbers(Test_list): 2 | Result = [x for x in range(Test_list[0], Test_list[-1] + 1)] 3 | return (list(set(Test_list) ^ set(Result))) 4 | 5 | 6 | print(missing_numbers([1, 2, 3, 4, 6, 7, 10])) 7 | print(missing_numbers([10, 11, 12, 14, 17])) 8 | -------------------------------------------------------------------------------- /DAYS/Day55/Triplet_Sum_is_given_number.py: -------------------------------------------------------------------------------- 1 | def Find_triplets(Test_list, length, target): 2 | found = False 3 | for i in range(0, length-1): 4 | s = set() 5 | for j in range(i+1, length): 6 | Add = -(Test_list[i]+Test_list[j]-target) 7 | if Add in s: 8 | print(list(sorted([Add, Test_list[i], Test_list[j]]))) 9 | found = True 10 | else: 11 | s.add(Test_list[j]) 12 | if not found: 13 | print("No Triplets Found") 14 | 15 | 16 | Test_list = [-25, -10, -7, -3, 2, 4, 8, 10] 17 | target = int(input("Enter a Value: ")) 18 | Find_triplets(Test_list, len(Test_list), target) -------------------------------------------------------------------------------- /DAYS/Day55/Triplets_Sum.py: -------------------------------------------------------------------------------- 1 | def Find_triplets(Test_list, length): 2 | found = False 3 | for i in range(0, length-1): 4 | s = set() 5 | for j in range(i+1, length): 6 | Add = -(Test_list[i]+Test_list[j]) 7 | if Add in s: 8 | print(list(sorted([Add, Test_list[i], Test_list[j]]))) 9 | found = True 10 | else: 11 | s.add(Test_list[j]) 12 | if not found: 13 | print("No Triplets Found") 14 | 15 | 16 | Test_list = [-25, -10, -7, -3, 2, 4, 8, 10] 17 | Find_triplets(Test_list, len(Test_list)) 18 | -------------------------------------------------------------------------------- /DAYS/Day56/Add_until_single_digit.py: -------------------------------------------------------------------------------- 1 | def single_digit(Num): 2 | Result = map(int, str(Num)) 3 | Total = sum(list(Result)) 4 | if Total < 10: 5 | print(Total) 6 | else: 7 | single_digit(Total) 8 | 9 | 10 | Num = int(input("Enter a Number: ")) 11 | single_digit(Num) 12 | -------------------------------------------------------------------------------- /DAYS/Day56/Not_Occur_Twice.py: -------------------------------------------------------------------------------- 1 | def Occur_twice(Test_list): 2 | Result = 0 3 | for i in Test_list: 4 | Result ^= i 5 | return Result 6 | 7 | 8 | Test_list = [5, 3, 4, 3, 4] 9 | print(Occur_twice(Test_list)) 10 | -------------------------------------------------------------------------------- /DAYS/Day57/Additive_Sequence.py: -------------------------------------------------------------------------------- 1 | def Valid_string(first, second, remain): 2 | Sum_str = str(int(first)+int(second)) 3 | if Sum_str == remain: 4 | return True 5 | elif remain.startswith(Sum_str): 6 | return Valid_string(second, Sum_str, remain[len(Sum_str):]) 7 | else: 8 | return False 9 | 10 | 11 | def Additive_Sequence(Test_string): 12 | length = len(Test_string) 13 | for i in range(1, int(length/2)+1): 14 | for j in range(1, int(length-i/2)+1): # int(length/2) 15 | first, second, remain = Test_string[:i], Test_string[i:i + 16 | j], Test_string[i+j:] 17 | if Valid_string(first, second, remain): 18 | return True 19 | return False 20 | 21 | 22 | if __name__ == "__main__": 23 | Test_string = input("Enter String of Numbers: ") 24 | print(Additive_Sequence(Test_string)) 25 | -------------------------------------------------------------------------------- /DAYS/Day58/Reverse_bits_of_integers.py: -------------------------------------------------------------------------------- 1 | def Reverse_bits(Num, Bit_size): 2 | binary = bin(Num) 3 | reverse = binary[:1:-1] 4 | Result = reverse+(Bit_size-len(reverse))*"0" 5 | return int(Result, 2) 6 | 7 | 8 | if __name__ == "__main__": 9 | Num = int(input("Enter a Number: ")) 10 | Bit_size = int(input("Enter Bit Size: ")) 11 | print(Reverse_bits(Num, Bit_size)) 12 | -------------------------------------------------------------------------------- /DAYS/Day59/Arithmetic_Progression_or_not.py: -------------------------------------------------------------------------------- 1 | def Arithmetic_Prog(Test_list): 2 | first = Test_list[1]-Test_list[0] 3 | for ele in range(len(Test_list)-1): 4 | if not Test_list[ele+1]-Test_list[ele] == first: 5 | return False 6 | return True 7 | 8 | 9 | if __name__ == "__main__": 10 | Test_list1 = [5, 7, 9, 11] 11 | Test_list2 = [5, 8, 9, 11] 12 | print(Arithmetic_Prog(Test_list1)) 13 | print(Arithmetic_Prog(Test_list2)) 14 | -------------------------------------------------------------------------------- /DAYS/Day6/Increament_Numeric_String.py: -------------------------------------------------------------------------------- 1 | def Increment_Numeric_String(Test_list, n): 2 | Result = [] 3 | for ele in Test_list: 4 | if ele.isdigit(): 5 | Result.append(str(int(ele)+n)) 6 | else: 7 | Result.append(ele) 8 | return Result 9 | 10 | 11 | Given_list = ["100", "Days", "of", "CodeChallenge", 12 | "234", "is", "98", "123", "best", "4"] 13 | n = 10 14 | print(Increment_Numeric_String(Given_list, n)) -------------------------------------------------------------------------------- /DAYS/Day6/Maximum_of_Each_key_in_List.py: -------------------------------------------------------------------------------- 1 | def Maximum_Key(Test_list): 2 | Result = {} 3 | for dictionary in Test_list: 4 | for key, value in dictionary.items(): 5 | if key in Result: 6 | Result[key] = max(Result[key], value) 7 | else: 8 | Result[key] = value 9 | return Result 10 | 11 | 12 | Test_list = [{"Days": 8, "Code": 1, "Challenge": 9}, 13 | {"Days": 2, "Code": 9, "Challenge": 1}, 14 | {"Days": 5, "Code": 10, "Challenge": 7}] 15 | print(Maximum_Key(Test_list)) 16 | -------------------------------------------------------------------------------- /DAYS/Day6/Sort_list1_by_list2.py: -------------------------------------------------------------------------------- 1 | def Sort_One_by_Other(Arr1, Arr2): 2 | Result = zip(Arr2, Arr1) 3 | Result = [ele for _, ele in sorted(Result)] 4 | print(Result) 5 | 6 | 7 | x = ["a", "b", "c", "d", "e", "f", "g", "h", "i"] 8 | y = [0, 1, 1, 0, 1, 2, 2, 0, 1] 9 | Sort_One_by_Other(x, y) 10 | -------------------------------------------------------------------------------- /DAYS/Day6/Words_Reverse_Pairs_in_List.py: -------------------------------------------------------------------------------- 1 | def Words_Pair(Test_list): 2 | count = 0 3 | reverse_words = [] 4 | for word in Test_list: 5 | if word[::-1] in Test_list: 6 | Test_list.remove(word[::-1]) 7 | Test_list.remove(word) 8 | count += 1 9 | return count 10 | 11 | 12 | Given_list = ["skeeg", "best", "tseb", 13 | "for", "skeeg", "skeeg", "best", "geeks", "tseb"] 14 | print(Words_Pair(Given_list)) 15 | -------------------------------------------------------------------------------- /DAYS/Day60/Geometric_Progression.py: -------------------------------------------------------------------------------- 1 | def Geometric(Test_list): 2 | ratio = Test_list[1]/Test_list[0] 3 | for ele in range(1, len(Test_list)-1): 4 | if not Test_list[ele+1]/Test_list[ele] == ratio: 5 | return False 6 | return True 7 | 8 | 9 | if __name__ == "__main__": 10 | Test_list1 = [2, 6, 18, 54] 11 | Test_list2 = [10, 5, 2.5, 1.25] 12 | Test_list3 = [5, 8, 9, 11] 13 | print(Geometric(Test_list1)) 14 | print(Geometric(Test_list2)) 15 | print(Geometric(Test_list3)) 16 | -------------------------------------------------------------------------------- /DAYS/Day61/Collatz_Sequence.py: -------------------------------------------------------------------------------- 1 | def Collatz_Sequence(num): 2 | Result = [num] 3 | while num != 1: 4 | if num % 2 == 0: 5 | num = num/2 6 | else: 7 | num = 3*num+1 8 | Result.append(num) 9 | return Result 10 | 11 | 12 | if __name__ == "__main__": 13 | num = int(input("Enter a Number: ")) 14 | print(Collatz_Sequence(num)) -------------------------------------------------------------------------------- /DAYS/Day61/Reverse_Sum.py: -------------------------------------------------------------------------------- 1 | def Reverse_Sum(num1, num2): 2 | return int(str(int(str(num1)[::-1]) + int(str(num2)[::-1]))[::-1]) 3 | 4 | 5 | if __name__ == "__main__": 6 | print(Reverse_Sum(13, 14)) 7 | print(Reverse_Sum(130, 1)) 8 | print(Reverse_Sum(305, 794)) 9 | 10 | 11 | -------------------------------------------------------------------------------- /DAYS/Day62/Hamming_Number.py: -------------------------------------------------------------------------------- 1 | def is_Hamming(num): 2 | if num == 1: 3 | return 1 4 | if num % 2 == 0: 5 | return is_Hamming(num/2) 6 | if num % 3 == 0: 7 | return is_Hamming(num/3) 8 | if num % 5 == 0: 9 | return is_Hamming(num/5) 10 | return 0 11 | 12 | 13 | if __name__ == "__main__": 14 | num = int(input("Enter a Number: ")) 15 | if is_Hamming(num) == 1: 16 | print("It's a Hamming Number") 17 | else: 18 | print("It's not a Hamming Number") 19 | -------------------------------------------------------------------------------- /DAYS/Day62/Hamming_Sequence.py: -------------------------------------------------------------------------------- 1 | def is_Hamming(num): 2 | if num == 1: 3 | return 1 4 | if num % 2 == 0: 5 | return is_Hamming(num/2) 6 | if num % 3 == 0: 7 | return is_Hamming(num/3) 8 | if num % 5 == 0: 9 | return is_Hamming(num/5) 10 | return 0 11 | 12 | 13 | def Hamming_sequence(num): 14 | if num == 1: 15 | return 1 16 | Hamming_sequence(num-1) 17 | if is_Hamming(num) == True: 18 | print(num, end=" ") 19 | 20 | 21 | if __name__ == "__main__": 22 | print("Hamming Sequence: ") 23 | Hamming_sequence(24) 24 | -------------------------------------------------------------------------------- /DAYS/Day63/Anagram_or_not.py: -------------------------------------------------------------------------------- 1 | def is_anagram(str1, str2): 2 | first = list(str1) 3 | first.sort() 4 | second = list(str2) 5 | second.sort() 6 | return first == second 7 | 8 | 9 | print(is_anagram('anagram', 'nagaram')) 10 | print(is_anagram('cat', 'rat')) 11 | print(is_anagram('education', 'uaconitde')) 12 | -------------------------------------------------------------------------------- /DAYS/Day63/Push_fisrt_number_to_end.py: -------------------------------------------------------------------------------- 1 | def move_zero_end(Test_list): 2 | Zero = [Test_list[0] for i in range(Test_list.count(Test_list[0]))] 3 | Result = [i for i in Test_list if i != Test_list[0]] 4 | Result.extend(Zero) 5 | return Result 6 | 7 | 8 | print(move_zero_end([0, 2, 3, 4, 6, 7, 10])) 9 | print(move_zero_end([10, 0, 11, 10, 12, 0, 14, 17])) 10 | -------------------------------------------------------------------------------- /DAYS/Day63/Ugly_Number.py: -------------------------------------------------------------------------------- 1 | def Ugly_number(num): 2 | for ele in [2, 3, 5]: 3 | while num % ele == 0: 4 | num /= ele 5 | return num == 1 6 | 7 | 8 | if __name__ == "__main__": 9 | num = int(input("Enter a Number: ")) 10 | print(Ugly_number(num)) 11 | -------------------------------------------------------------------------------- /DAYS/Day64/Odd_Occurrences_of_elements.py: -------------------------------------------------------------------------------- 1 | # Approach 1: 2 | 3 | 4 | def get_Odd_Occurrence(Test_list): 5 | a = set(Test_list) 6 | hey = {i: Test_list.count(i) for i in a} 7 | return [key for key, values in hey.items() if values % 2 != 0] 8 | 9 | 10 | Test_list = [4, 5, 4, 5, 2, 2, 3, 3, 2, 4, 4] 11 | 12 | print(*get_Odd_Occurrence(Test_list)) 13 | 14 | 15 | # Approach 2: 16 | 17 | 18 | def get_Odd_Occurrence(Test_list): 19 | Result = 0 20 | for ele in Test_list: 21 | Result ^= ele 22 | return Result 23 | 24 | 25 | Test_list = [4, 5, 4, 5, 2, 2, 3, 3, 2, 4, 4] 26 | print(get_Odd_Occurrence(Test_list)) 27 | -------------------------------------------------------------------------------- /DAYS/Day64/Sum_of_Even_Valued_Terms_Fibonacci_Series.py: -------------------------------------------------------------------------------- 1 | def Even_Fib_Sum(limit): 2 | if limit < 2: 3 | return 0 4 | first = 1 5 | sec = 2 6 | sum = first+sec-1 7 | while sec <= limit: 8 | if first == 1: 9 | third = 4*sec+first-1 10 | else: 11 | third = 4*sec+first 12 | if third > limit: 13 | break 14 | first = sec 15 | sec = third 16 | sum = sum+sec 17 | return sum 18 | 19 | 20 | # Driver Code 21 | limit = int(input("Enter The Limit: ")) 22 | print(Even_Fib_Sum(limit)) 23 | -------------------------------------------------------------------------------- /DAYS/Day65/Largest_Palindrome.py: -------------------------------------------------------------------------------- 1 | def Largest_Prime(num): 2 | Upper_limit = (10**(num))-1 3 | Lower_limit = 1+Upper_limit//10 4 | Low_range = Upper_limit-Lower_limit 5 | Max_product = 0 6 | for i in range(Upper_limit, Low_range, -1): 7 | for j in range(i, Low_range, -1): 8 | number = str(i*j) 9 | if number == number[::-1]: 10 | Max_product = i*j 11 | return Max_product 12 | 13 | 14 | if __name__ == "__main__": 15 | num = int(input("Enter a Number: ")) 16 | print(Largest_Prime(num)) 17 | -------------------------------------------------------------------------------- /DAYS/Day65/Largest_Prime_Factor.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | 4 | def Largest_Prime_Factor(num): 5 | count = 2 6 | Prime_Factor = 1 7 | while count <= math.sqrt(num): 8 | if not num % count == 0: 9 | Prime_Factor = count 10 | count += 1 11 | else: 12 | num /= count 13 | 14 | if Prime_Factor < num: 15 | Prime_Factor = num 16 | return int(Prime_Factor) 17 | 18 | 19 | if __name__ == "__main__": 20 | num = int(input("Enter a Number: ")) 21 | print(Largest_Prime_Factor(num)) 22 | -------------------------------------------------------------------------------- /DAYS/Day66/Small_Positive_Number_Divisble.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | 4 | def small_positive(num): 5 | Result = 1 6 | for i in range(1, num+1): 7 | Result = int((Result*i)/math.gcd(Result, i)) 8 | return Result 9 | 10 | 11 | # Driver Function 12 | if __name__ == "__main__": 13 | num = int(input("Enter a Number: ")) 14 | print(small_positive(num)) 15 | -------------------------------------------------------------------------------- /DAYS/Day67/Sum_of_Square.py: -------------------------------------------------------------------------------- 1 | def Difference(num): 2 | first = range(1, num+1) 3 | square_of_sum = sum(first) 4 | return (square_of_sum*square_of_sum - sum(i*i for i in first)) 5 | 6 | 7 | if __name__ == "__main__": 8 | num = int(input("Enter Upper Limit: ")) 9 | print(Difference(num)) 10 | -------------------------------------------------------------------------------- /DAYS/Day67/nth_Prime_number.py: -------------------------------------------------------------------------------- 1 | def nth_Prime(num): 2 | Semi = num*num 3 | Res_1 = [True for i in range(Semi+1)] 4 | prime = 2 5 | while prime*prime <= Semi: 6 | if Res_1[prime] == True: 7 | for i in range(prime*prime, Semi+1, prime): 8 | Res_1[i] = False 9 | prime += 1 10 | Res_2 = [] 11 | for i in range(2, Semi+1): 12 | if Res_1[i]: 13 | Res_2.append(i) 14 | return Res_2[num-1] 15 | 16 | 17 | if __name__ == "__main__": 18 | num = int(input("Enter nth Number: ")) 19 | print(nth_Prime(num)) 20 | -------------------------------------------------------------------------------- /DAYS/Day68/Amicable_Pairs.py: -------------------------------------------------------------------------------- 1 | def sum_factors(num): 2 | Result = [] 3 | for i in range(1, num): 4 | if num % i == 0: 5 | Result.append(i) 6 | return sum(Result) 7 | 8 | 9 | def amicable_pairs(num): 10 | Result1 = [] 11 | Result2 = [] 12 | 13 | for x in range(1, num+1): 14 | y = sum_factors(x) 15 | if sum_factors(y) == x and x != y: 16 | Result1.append(tuple(sorted((x, y)))) 17 | Result2.append(x+y) 18 | 19 | x += 1 20 | return set(Result1), sum(set(Result2)) 21 | 22 | 23 | if __name__ == "__main__": 24 | num = int(input("Enter n: ")) 25 | a, b = amicable_pairs(num) 26 | print(f"Amicable Pairs: {a}") 27 | print(f"Sum of Amicable Pairs: {b}") 28 | -------------------------------------------------------------------------------- /DAYS/Day69/Triangular_Number.py: -------------------------------------------------------------------------------- 1 | def divisor(num): 2 | Result = [] 3 | for i in range(1, int(num)): 4 | if num % i == 0: 5 | Result.append(i) 6 | return Result 7 | 8 | 9 | def triangular_number(num): 10 | count = True 11 | natural = 1 12 | while count: 13 | ctr = (natural*(natural+1))/2 14 | if len(divisor(ctr)) >= num: 15 | return int(ctr) 16 | natural += 1 17 | 18 | 19 | if __name__ == "__main__": 20 | num = int(input("Enter n: ")) 21 | print(triangular_number(num)) 22 | -------------------------------------------------------------------------------- /DAYS/Day7/Remove_Row_with_Custom_Element.py: -------------------------------------------------------------------------------- 1 | # Approach 1: 2 | 3 | def Remove_Row_with_Custom_Element(Test_list, Check_list): 4 | for row in Test_list: 5 | for ele in Check_list: 6 | if ele in row: 7 | Test_list.remove(row) 8 | return Test_list 9 | 10 | 11 | Test_list = [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]] 12 | Check_list = [3, 10, 19, 29, 20, 15] 13 | print(Remove_Row_with_Custom_Element(Test_list, Check_list)) 14 | 15 | 16 | 17 | 18 | # Approach 2: 19 | 20 | def Remove_Row_with_Custom_Element(Test_list, Check_list): 21 | return [row for row in Test_list if not any(ele in row for ele in Check_list)] 22 | 23 | 24 | Test_list = [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]] 25 | Check_list = [3, 10, 19, 29, 20, 15] 26 | print(Remove_Row_with_Custom_Element(Test_list, Check_list)) 27 | -------------------------------------------------------------------------------- /DAYS/Day7/Remove_Specific_Digit_from_Every_Element.py: -------------------------------------------------------------------------------- 1 | def Remove_Specific_Digit(Test_list, n): 2 | Result = [] 3 | for ele in Test_list: 4 | Result.append(''.join([elem for elem in str(ele) if int(elem) != n])) 5 | return list(filter(None, Result)) 6 | 7 | 8 | Test_list = [343, 893, 1948, 3433333, 2346] 9 | n = int(input("Specific Character: ")) 10 | print(Remove_Specific_Digit(Test_list, n)) 11 | -------------------------------------------------------------------------------- /DAYS/Day7/String_Based_on_Prefix.py: -------------------------------------------------------------------------------- 1 | def String_Prefix(Test_list, Prefix_list): 2 | return [ele for ele in Test_list if any(ele.startswith(element) for element in Prefix_list)] 3 | 4 | 5 | Test_list = ["geeks", "peeks", "meeks", "leeks", "mean"] 6 | Prefix_list = ["ge", "ne", "me", "re"] 7 | print(String_Prefix(Test_list, Prefix_list)) 8 | -------------------------------------------------------------------------------- /DAYS/Day7/Swap_ith_with_jth_Element.py: -------------------------------------------------------------------------------- 1 | def Swap_ith_with_jth(Test_list, i, j): 2 | for ele in range(len(Test_list)): 3 | if Test_list[ele] is i: 4 | Test_list[ele] = j 5 | elif Test_list[ele] is j: 6 | Test_list[ele] = i 7 | return Test_list 8 | 9 | 10 | Test_list = [4, 7, 8, 0, 8, 4, 2, 9, 4, 8, 4] 11 | i, j = 4, 8 12 | print(Swap_ith_with_jth(Test_list, i, j)) -------------------------------------------------------------------------------- /DAYS/Day70/Expand_string.py: -------------------------------------------------------------------------------- 1 | import string 2 | 3 | 4 | def letter_string(Test_string): 5 | digs = "" 6 | letters = "" 7 | Result = "" 8 | for i in Test_string: 9 | if i in string.digits: 10 | digs += i 11 | else: 12 | letters += i 13 | for index, num in enumerate(digs): 14 | Result += int(num)*letters[index] 15 | return Result 16 | 17 | 18 | if __name__ == "__main__": 19 | Test_string = input("Enter String: ") 20 | print(letter_string(Test_string)) 21 | -------------------------------------------------------------------------------- /DAYS/Day70/Index_of_term_in_Fibonacci.py: -------------------------------------------------------------------------------- 1 | import itertools 2 | 3 | 4 | def Fibo_index(num): 5 | Prev_term = 0 6 | Curr_term = 1 7 | for i in itertools.count(1): 8 | if len(str(Curr_term)) == num: 9 | return i, Curr_term 10 | Prev_term, Curr_term = Curr_term, Prev_term+Curr_term 11 | 12 | 13 | if __name__ == "__main__": 14 | num = int(input("Enter n: ")) 15 | Index, number = Fibo_index(num) 16 | print(f"Number is {number}") 17 | print(f"Index is: {Index}") 18 | -------------------------------------------------------------------------------- /DAYS/Day71/Distance_bw_two_strings.py: -------------------------------------------------------------------------------- 1 | def Edit_distance(Str1, Str2): 2 | if len(Str1) > len(Str2): 3 | distance = len(Str1)-len(Str2) 4 | elif len(Str2) > len(Str1): 5 | distance = len(Str2)-len(Str1) 6 | else: 7 | distance = 0 8 | for i in range(len(Str1)): 9 | if Str1[i] != Str2[i]: 10 | print(f"Replace: {Str1[i]} <--> {Str2[i]}") 11 | distance += 1 12 | return f"Replacement Required: {distance}\n" 13 | 14 | 15 | if __name__ == "__main__": 16 | print(Edit_distance("Kitten", "Sitting")) 17 | print(Edit_distance("Medium", "Median")) 18 | -------------------------------------------------------------------------------- /DAYS/Day72/List_product.py: -------------------------------------------------------------------------------- 1 | def List_prod(Test_list): 2 | Result = [] 3 | for i in Test_list: 4 | product = 1 5 | for j in Test_list: 6 | if j != i: 7 | product *= j 8 | Result.append(product) 9 | return Result 10 | 11 | 12 | if __name__ == "__main__": 13 | print(List_prod([10, 20, 30, 40, 50])) 14 | print(List_prod([1, 2, 0, 4])) 15 | print(List_prod([1, 2, 3, -4])) 16 | -------------------------------------------------------------------------------- /DAYS/Day73/Intinerary_List.py: -------------------------------------------------------------------------------- 1 | def Intinerary(Test_list, start): 2 | Result = [start] 3 | while len(Test_list) >= 1: 4 | Semi_dict = {} 5 | for i in Test_list: 6 | if i[0] == start: 7 | Semi_dict[i[0]] = i[1] 8 | try: 9 | current = min(Semi_dict.values()) 10 | Test_list.remove((start, current)) 11 | Result.append(current) 12 | start = current 13 | except: 14 | return "Invalid intinerary, Unable to process..." 15 | return Result 16 | 17 | 18 | if __name__ == "__main__": 19 | Test_list1 = [('SFO', 'HKO'), ('YYZ', 'SFO'), 20 | ('YUL', 'YYZ'), ('HKO', 'ORD')] 21 | start1 = 'YUL' 22 | 23 | Test_list2 = [('A', 'B'), ('A', 'C'), ('B', 'C'), ('C', 'A')] 24 | start2 = 'A' 25 | 26 | Test_list3 = [('SFO', 'COM'), ('COM', 'YYZ')] 27 | start3 = 'COM' 28 | print(Intinerary(Test_list1, start1)) 29 | print(Intinerary(Test_list2, start2)) 30 | print(Intinerary(Test_list3, start3)) 31 | -------------------------------------------------------------------------------- /DAYS/Day74/Gray_Code.py: -------------------------------------------------------------------------------- 1 | def Generate_gray(bits): 2 | hey = ["0", "1"] 3 | hey1, hey2 = [], [] 4 | if bits <= 0: 5 | return ["0"] 6 | if bits == 1: 7 | return ["0", "1"] 8 | while bits != 1: 9 | hey1, hey2 = [], [] 10 | for i in range(len(hey)): 11 | hey1.append("0"+hey[i]) 12 | for i in range(len(hey)): 13 | hey2.append("1"+hey[i]) 14 | hey = [] 15 | hey = hey1+hey2 16 | bits -= 1 17 | return hey 18 | 19 | 20 | if __name__ == "__main__": 21 | bits = int(input("How many Bits: ")) 22 | Res = Generate_gray(bits) 23 | for code in Res: 24 | print(" ".join(code)) 25 | -------------------------------------------------------------------------------- /DAYS/Day75/Reverse_Linked_List.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data): 3 | self.data = data 4 | self.next = None 5 | 6 | 7 | class Reverse_Linked_List: 8 | def __init__(self): 9 | self.head = None 10 | 11 | def push(self, new_data): 12 | new_node = Node(new_data) 13 | if not self.head: 14 | self.head = new_node 15 | else: 16 | current = self.head 17 | while(current.next): 18 | current = current.next 19 | current.next = new_node 20 | 21 | def Reverse_list(self): 22 | previous = None 23 | current = self.head 24 | while current is not None: 25 | following = current.next 26 | current.next = previous 27 | previous = current 28 | current = following 29 | self.head = previous 30 | 31 | def Display(self): 32 | temp = self.head 33 | while temp is not None: 34 | print(temp.data, end=" ") 35 | temp = temp.next 36 | 37 | 38 | if __name__ == "__main__": 39 | Lin_list = Reverse_Linked_List() 40 | num = int(input("How many elements: ")) 41 | for i in range(num): 42 | ele = int(input()) 43 | Lin_list.push(ele) 44 | print("Linked List:") 45 | Lin_list.Display() 46 | Lin_list.Reverse_list() 47 | print("\nReverse Linked List: ") 48 | Lin_list.Display() 49 | -------------------------------------------------------------------------------- /DAYS/Day76/Tower_of_Hanoi.py: -------------------------------------------------------------------------------- 1 | def TOH(disks, source, aux, to): 2 | if disks == 1: 3 | print(f"Move Disk from {source} to {to}") 4 | return 5 | else: 6 | TOH(disks-1, source, to, aux) 7 | print(f"Move Disk from {source} to {to}") 8 | TOH(disks-1, aux, source, to) 9 | 10 | 11 | if __name__ == "__main__": 12 | num = int(input("How many Disks: ")) 13 | TOH(num, 'A', 'B', 'C') 14 | -------------------------------------------------------------------------------- /DAYS/Day77/Diff_bw_Odd_and_even.py: -------------------------------------------------------------------------------- 1 | 2 | # Approach 1: 3 | 4 | def Difference(num): 5 | Even = 0 6 | Odd = 0 7 | Result = str(num) 8 | for i in range(len(Result)): 9 | if i % 2 == 0: 10 | Even += int(Result[i]) 11 | else: 12 | Odd += int(Result[i]) 13 | return Odd-Even == 0 14 | 15 | 16 | if __name__ == "__main__": 17 | num = int(input("Enter Number: ")) 18 | if Difference(num): 19 | print("Yes") 20 | else: 21 | print("No") 22 | 23 | 24 | # Approach 2: 25 | 26 | def Difference(num): 27 | return (num % 11 == 0) 28 | 29 | 30 | if __name__ == "__main__": 31 | num = int(input("Enter Number: ")) 32 | if Difference(num): 33 | print("Yes") 34 | else: 35 | print("No") 36 | -------------------------------------------------------------------------------- /DAYS/Day78/Z_Matrix.py: -------------------------------------------------------------------------------- 1 | def Z_print(Test_list): 2 | Result = [] 3 | diff = len(Test_list)-len(Test_list[0]) 4 | for i in range(len(Test_list)): 5 | if i == 0 or i == len(Test_list)-1: 6 | Result.append(Test_list[i]) 7 | Result = Result[0] 8 | print(*Result) 9 | Result = [] 10 | else: 11 | Result.append(Test_list[i][len(Test_list)-i-1-diff]) 12 | a = Result[0] 13 | print(" " * (len(Test_list)-i-1-diff) + str(a)) 14 | Result = [] 15 | 16 | return Result 17 | 18 | 19 | if __name__ == "__main__": 20 | Test_list1 = [[4, 5, 6, 8, 5], 21 | [1, 2, 3, 1, 4], 22 | [7, 8, 9, 4, 7], 23 | [1, 8, 7, 5, 2], 24 | [7, 9, 5, 6, 9], 25 | [9, 4, 5, 6, 6]] 26 | Test_list2 = [[4, 5, 6, 8], 27 | [1, 2, 3, 1], 28 | [7, 8, 9, 4], 29 | [1, 8, 7, 5]] 30 | print("1st: ") 31 | Z_print(Test_list1) 32 | print("\n") 33 | print("2nd: ") 34 | Z_print(Test_list2) 35 | -------------------------------------------------------------------------------- /DAYS/Day79/Insert_operation_on_Linked_List.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data): 3 | self.data = data 4 | self.next = None 5 | 6 | 7 | class Linked_List: 8 | def __init__(self): 9 | self.head = None 10 | 11 | def Insert_At_Beginning(self, new_data): 12 | new_node = Node(new_data) 13 | new_node.next = self.head 14 | self.head = new_node 15 | 16 | def Insert_After(self, node, new_data): 17 | if node is None: 18 | return "Alert!, Node must be in Linked List" 19 | new_node = Node(new_data) 20 | new_node.next = node.next 21 | node.next = new_node 22 | 23 | def Insert_At_End(self, new_data): 24 | new_node = Node(new_data) 25 | if self.head is None: 26 | self.head = new_node 27 | return 28 | current = self.head 29 | while(current.next): 30 | current = current.next 31 | current.next = new_node 32 | 33 | def Display(self): 34 | temp = self.head 35 | while(temp): 36 | print(temp.data, "->", end=" ") 37 | temp = temp.next 38 | print("None") 39 | 40 | 41 | if __name__ == "__main__": 42 | L_list = Linked_List() 43 | L_list.Insert_At_Beginning(1) 44 | L_list.Display() 45 | L_list.Insert_At_Beginning(2) 46 | L_list.Display() 47 | L_list.Insert_At_Beginning(3) 48 | L_list.Display() 49 | L_list.Insert_At_End(4) 50 | L_list.Display() 51 | L_list.Insert_At_End(5) 52 | L_list.Display() 53 | L_list.Insert_At_End(6) 54 | L_list.Display() 55 | L_list.Insert_After(L_list.head.next, 10) 56 | L_list.Display() -------------------------------------------------------------------------------- /DAYS/Day8/Element_Multiple_of_Element_in_List.py: -------------------------------------------------------------------------------- 1 | # Approach 1: 2 | 3 | def Element_Multiple(Test_list, Check_list): 4 | Result = [] 5 | for ele in Test_list: 6 | count = 0 7 | for element in Check_list: 8 | if ele % element == 0: 9 | count += 1 10 | if count == len(Check_list): 11 | Result.append(ele) 12 | return Result 13 | 14 | 15 | Test_list = [4, 24, 8, 10, 12, 23] 16 | Check_list = [6, 4] 17 | print(Element_Multiple(Test_list, Check_list)) 18 | 19 | # Approach 2: 20 | 21 | def Element_Multiple(Test_list, Check_list): 22 | return [ele for ele in Test_list if all(ele % element == 0 for element in Check_list)] 23 | 24 | 25 | Test_list = [4, 24, 8, 10, 12, 23] 26 | Check_list = [6, 4] 27 | print(Element_Multiple(Test_list, Check_list)) 28 | -------------------------------------------------------------------------------- /DAYS/Day8/Element_with_Maximum_Vowel_from_List.py: -------------------------------------------------------------------------------- 1 | def Maximum_Vowel(Test_list): 2 | Max_String = 0 3 | Result = "" 4 | for ele in Test_list: 5 | count = 0 6 | for element in ele: 7 | if element in "aeiouAEIOU": 8 | count += 1 9 | if count > Max_String: 10 | Max_String = count 11 | Result = ele 12 | return Result 13 | 14 | 15 | Test_list = ["gfg", "beaeioust", "for", "geeks"] 16 | print(Maximum_Vowel(Test_list)) -------------------------------------------------------------------------------- /DAYS/Day8/Occurence_of_i_before_first_j_in_List.py: -------------------------------------------------------------------------------- 1 | # Approach 1: 2 | 3 | def Occurence(Test_list, i, j): 4 | count = 0 5 | for ele in Test_list: 6 | if ele == j: 7 | break 8 | if ele == i: 9 | count += 1 10 | return count 11 | 12 | 13 | Test_list = [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9] 14 | i, j = 4, 8 15 | print(Occurence(Test_list, i, j)) 16 | 17 | # Approach 2: 18 | 19 | def Occurence(Test_list, i, j): 20 | Index = Test_list.index(j) 21 | Temp = Test_list[:Index] 22 | return Temp.count(i) 23 | 24 | 25 | Test_list = [4, 5, 6, 4, 1, 4, 8, 5, 4, 3, 4, 9] 26 | i, j = 4, 8 27 | print(Occurence(Test_list, i, j)) 28 | -------------------------------------------------------------------------------- /DAYS/Day8/Sort_row_by_Frequency_of_Key.py: -------------------------------------------------------------------------------- 1 | # Approach 1: 2 | 3 | def length(row): 4 | return row.count(n) 5 | 6 | 7 | def Sort_Row(Test_list, n): 8 | Test_list.sort(key=length) 9 | return Test_list 10 | 11 | Test_list = [[10, 2, 3, 2, 3], [5, 5, 4, 7, 7, 4], [1, 2], [1, 1, 2, 2, 2]] 12 | n = 2 13 | print(Sort_Row(Test_list, n)) 14 | 15 | 16 | # Approach 2: 17 | 18 | def Sort_Row(Test_list, n): 19 | Result = sorted(Test_list, key=lambda row: row.count(n)) 20 | return Result 21 | 22 | 23 | Test_list = [[10, 2, 3, 2, 3], [5, 5, 4, 7, 7, 4], [1, 2], [1, 1, 2, 2, 2]] 24 | n = 2 25 | print(Sort_Row(Test_list, n)) 26 | -------------------------------------------------------------------------------- /DAYS/Day80/Delete_Linked_List.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data): 3 | self.data = data 4 | self.next = None 5 | 6 | 7 | class Linked_List: 8 | def __init__(self): 9 | self.head = None 10 | 11 | def Insert_At_End(self, new_data): 12 | new_node = Node(new_data) 13 | if self.head is None: 14 | self.head = new_node 15 | return 16 | current = self.head 17 | while(current.next): 18 | current = current.next 19 | current.next = new_node 20 | 21 | def Delete(self, key): 22 | temp = self.head 23 | if temp is None: 24 | return "Can't Delete!" 25 | else: 26 | if temp.data == key: 27 | self.head = temp.next 28 | temp = None 29 | while temp is not None: 30 | prev = temp 31 | temp = temp.next 32 | curr = temp.next 33 | if temp.data == key: 34 | prev.next = curr 35 | return 36 | 37 | def Display(self): 38 | temp = self.head 39 | while(temp): 40 | print(temp.data, "->", end=" ") 41 | temp = temp.next 42 | print("None") 43 | 44 | 45 | if __name__ == "__main__": 46 | L_list = Linked_List() 47 | L_list.Insert_At_End(1) 48 | L_list.Insert_At_End(2) 49 | L_list.Insert_At_End(3) 50 | L_list.Insert_At_End(4) 51 | L_list.Insert_At_End(5) 52 | L_list.Insert_At_End(6) 53 | L_list.Insert_At_End(7) 54 | print("Linked List: ") 55 | L_list.Display() 56 | print("Deleted Linked List: ") 57 | L_list.Delete(3) 58 | L_list.Display() 59 | -------------------------------------------------------------------------------- /DAYS/Day81/Sorting_List.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data): 3 | self.data = data 4 | self.next = None 5 | 6 | 7 | class Linked_List: 8 | def __init__(self): 9 | self.head = None 10 | 11 | def Insert_At_End(self, new_data): 12 | new_node = Node(new_data) 13 | if self.head is None: 14 | self.head = new_node 15 | return 16 | current = self.head 17 | while(current.next): 18 | current = current.next 19 | current.next = new_node 20 | 21 | def Sort(self): 22 | temp = self.head 23 | while(temp): 24 | minn = temp 25 | after = temp.next 26 | while(after): 27 | if minn.data > after.data: 28 | minn = after 29 | after = after.next 30 | key = temp.data 31 | temp.data = minn.data 32 | minn.data = key 33 | temp = temp.next 34 | 35 | def Display(self): 36 | temp = self.head 37 | while(temp): 38 | print(temp.data, "->", end=" ") 39 | temp = temp.next 40 | print("None") 41 | 42 | 43 | if __name__ == "__main__": 44 | L_list = Linked_List() 45 | L_list.Insert_At_End(8) 46 | L_list.Insert_At_End(5) 47 | L_list.Insert_At_End(10) 48 | L_list.Insert_At_End(7) 49 | L_list.Insert_At_End(6) 50 | L_list.Insert_At_End(11) 51 | L_list.Insert_At_End(9) 52 | print("Linked List: ") 53 | L_list.Display() 54 | print("Sorted Linked List: ") 55 | L_list.Sort() 56 | L_list.Display() 57 | -------------------------------------------------------------------------------- /DAYS/Day82/Swap_nodes.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data): 3 | self.data = data 4 | self.next = None 5 | 6 | 7 | class Linked_List: 8 | def __init__(self): 9 | self.head = None 10 | 11 | def Insert_At_End(self, new_data): 12 | new_node = Node(new_data) 13 | if self.head is None: 14 | self.head = new_node 15 | return 16 | current = self.head 17 | while(current.next): 18 | current = current.next 19 | current.next = new_node 20 | 21 | def Swap(self, key1, key2): 22 | Prev_node_1 = None 23 | Prev_node_2 = None 24 | Node_1 = self.head 25 | Node_2 = self.head 26 | if self.head == None: 27 | return 28 | if key1 == key2: 29 | return 30 | while(Node_1.data != key1): 31 | Prev_node_1 = Node_1 32 | Node_1 = Node_1.next 33 | while(Node_2.data != key2): 34 | Prev_node_2 = Node_2 35 | Node_2 = Node_2.next 36 | if Prev_node_1 is not None: 37 | Prev_node_1.next = Node_2 38 | else: 39 | self.head = Node_2 40 | if Prev_node_2 is not None: 41 | Prev_node_2.next = Node_1 42 | else: 43 | self.head = Node_1 44 | temp = Node_1.next 45 | Node_1.next = Node_2.next 46 | Node_2.next = temp 47 | 48 | def Display(self): 49 | temp = self.head 50 | while(temp): 51 | print(temp.data, "->", end=" ") 52 | temp = temp.next 53 | print("None") 54 | 55 | 56 | if __name__ == "__main__": 57 | L_list = Linked_List() 58 | L_list.Insert_At_End(8) 59 | L_list.Insert_At_End(5) 60 | L_list.Insert_At_End(10) 61 | L_list.Insert_At_End(7) 62 | L_list.Insert_At_End(6) 63 | L_list.Insert_At_End(11) 64 | L_list.Insert_At_End(9) 65 | print("Linked List: ") 66 | L_list.Display() 67 | print("Swap List: ") 68 | L_list.Swap(8, 5) 69 | L_list.Display() 70 | -------------------------------------------------------------------------------- /DAYS/Day83/Merge_two_sorted_Lists.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data): 3 | self.data = data 4 | self.next = None 5 | 6 | 7 | class Linked_List: 8 | def __init__(self): 9 | self.head = None 10 | 11 | def Insert_At_End(self, new_data): 12 | new_node = Node(new_data) 13 | if self.head is None: 14 | self.head = new_node 15 | return 16 | current = self.head 17 | while(current.next): 18 | current = current.next 19 | current.next = new_node 20 | 21 | def Merge(self, List_1, List_2): 22 | temp = head = Node(0) 23 | while True: 24 | if List_1 is None: 25 | temp.next = List_2 26 | break 27 | if List_2 is None: 28 | temp.next = List_1 29 | break 30 | if List_1.data <= List_2.data: 31 | temp.next = List_1 32 | List_1 = List_1.next 33 | else: 34 | temp.next = List_2 35 | List_2 = List_2.next 36 | temp = temp.next 37 | return head.next 38 | 39 | def Display(self): 40 | temp = self.head 41 | while(temp): 42 | print(temp.data, "->", end=" ") 43 | temp = temp.next 44 | print("None") 45 | 46 | 47 | if __name__ == "__main__": 48 | L_list_1 = Linked_List() 49 | L_list_2 = Linked_List() 50 | 51 | L_list_1.Insert_At_End(5) 52 | L_list_1.Insert_At_End(8) 53 | L_list_1.Insert_At_End(10) 54 | 55 | L_list_2.Insert_At_End(7) 56 | L_list_2.Insert_At_End(9) 57 | L_list_2.Insert_At_End(11) 58 | L_list_2.Insert_At_End(12) 59 | 60 | L_list_3 = Linked_List() 61 | L_list_3.head = L_list_3.Merge(L_list_1.head, L_list_2.head) 62 | 63 | L_list_3.Display() 64 | -------------------------------------------------------------------------------- /DAYS/Day84/Merge_Sort_Linked_List.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data): 3 | self.data = data 4 | self.next = None 5 | 6 | 7 | class Linked_List: 8 | def __init__(self): 9 | self.head = None 10 | 11 | def Insert_At_End(self, new_data): 12 | new_node = Node(new_data) 13 | if self.head is None: 14 | self.head = new_node 15 | return 16 | current = self.head 17 | while(current.next): 18 | current = current.next 19 | current.next = new_node 20 | 21 | def Sorted_Merging(self, left, right): 22 | Result = None 23 | if left == None: 24 | return right 25 | if right == None: 26 | return left 27 | if left.data <= right.data: 28 | Result = left 29 | Result.next = self.Sorted_Merging(left.next, right) 30 | else: 31 | Result = right 32 | Result.next = self.Sorted_Merging(right.next, left) 33 | return Result 34 | 35 | def Merge_Sort(self, link): 36 | if link == None or link.next == None: 37 | return link 38 | middle = self.get_Middle(link) 39 | next_to_middle = middle.next 40 | middle.next = None 41 | left = self.Merge_Sort(link) 42 | right = self.Merge_Sort(next_to_middle) 43 | Result = self.Sorted_Merging(left, right) 44 | return Result 45 | 46 | def get_Middle(self, link): 47 | if link == None: 48 | return link 49 | prev = link 50 | while(link.next != None and link.next.next != None): 51 | prev = prev.next 52 | link = link.next.next 53 | return prev 54 | 55 | def Display(self): 56 | temp = self.head 57 | while(temp): 58 | print(temp.data, "->", end=" ") 59 | temp = temp.next 60 | print("None") 61 | 62 | 63 | if __name__ == "__main__": 64 | L_list_1 = Linked_List() 65 | 66 | L_list_1.Insert_At_End(5) 67 | L_list_1.Insert_At_End(8) 68 | L_list_1.Insert_At_End(10) 69 | 70 | L_list_1.Insert_At_End(7) 71 | L_list_1.Insert_At_End(9) 72 | L_list_1.Insert_At_End(11) 73 | L_list_1.Insert_At_End(12) 74 | 75 | L_list_1.Merge_Sort(L_list_1.head) 76 | 77 | L_list_1.Display() 78 | -------------------------------------------------------------------------------- /DAYS/Day85/Reverse_list_in_groups.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data): 3 | self.data = data 4 | self.next = None 5 | 6 | 7 | class Reverse_Linked_List: 8 | def __init__(self): 9 | self.head = None 10 | 11 | def Insert_At_End(self, new_data): 12 | new_node = Node(new_data) 13 | if self.head is None: 14 | self.head = new_node 15 | return 16 | current = self.head 17 | while(current.next): 18 | current = current.next 19 | current.next = new_node 20 | 21 | def Reverse_list_Groups(self, head, k): 22 | count = 0 23 | previous = None 24 | current = head 25 | while (current is not None and count < k): 26 | following = current.next 27 | current.next = previous 28 | previous = current 29 | current = following 30 | count += 1 31 | if following is not None: 32 | head.next = self.Reverse_list_Groups(following, k) 33 | return previous 34 | 35 | def Display(self): 36 | temp = self.head 37 | while(temp): 38 | print(temp.data, "->", end=" ") 39 | temp = temp.next 40 | print("None") 41 | 42 | 43 | if __name__ == "__main__": 44 | L_list = Reverse_Linked_List() 45 | L_list.Insert_At_End(1) 46 | L_list.Insert_At_End(2) 47 | L_list.Insert_At_End(3) 48 | L_list.Insert_At_End(4) 49 | L_list.Insert_At_End(5) 50 | L_list.Insert_At_End(6) 51 | L_list.Insert_At_End(7) 52 | L_list.Display() 53 | L_list.head = L_list.Reverse_list_Groups(L_list.head, 2) 54 | print("\nReverse Linked List: ") 55 | L_list.Display() -------------------------------------------------------------------------------- /DAYS/Day86/Detect_Remove_loop.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data): 3 | self.data = data 4 | self.next = None 5 | 6 | 7 | class Linked_List: 8 | def __init__(self): 9 | self.head = None 10 | 11 | def Insert_At_End(self, new_data): 12 | new_node = Node(new_data) 13 | if self.head is None: 14 | self.head = new_node 15 | return 16 | current = self.head 17 | while(current.next): 18 | current = current.next 19 | current.next = new_node 20 | 21 | def Detect_and_Remove_Loop(self): 22 | slow = fast = self.head 23 | while(slow and fast and fast.next): 24 | slow = slow.next 25 | fast = fast.next.next 26 | if slow == fast: 27 | self.Remove_loop(slow) 28 | print("Loop Found") 29 | return 1 30 | return 0 31 | 32 | def Remove_loop(self, Loop_node): 33 | ptr1 = self.head 34 | while(1): 35 | ptr2 = Loop_node 36 | while(ptr2.next != Loop_node and ptr2.next != ptr1): 37 | ptr2 = ptr2.next 38 | if ptr2.next == ptr1: 39 | break 40 | ptr1 = ptr1.next 41 | ptr2.next = None 42 | 43 | def Display(self): 44 | temp = self.head 45 | while(temp): 46 | print(temp.data, "->", end=" ") 47 | temp = temp.next 48 | print("None") 49 | 50 | 51 | if __name__ == "__main__": 52 | L_list = Linked_List() 53 | L_list.Insert_At_End(8) 54 | L_list.Insert_At_End(5) 55 | L_list.Insert_At_End(10) 56 | L_list.Insert_At_End(7) 57 | L_list.Insert_At_End(6) 58 | L_list.Insert_At_End(11) 59 | L_list.Insert_At_End(9) 60 | print("Linked List with Loop: ") 61 | L_list.Display() 62 | print("Linked List without Loop: ") 63 | L_list.head.next.next.next.next.next.next.next = L_list.head.next.next 64 | L_list.Detect_and_Remove_Loop() 65 | L_list.Display() 66 | -------------------------------------------------------------------------------- /DAYS/Day87/Add_two_Linked_List.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data): 3 | self.data = data 4 | self.next = None 5 | 6 | 7 | class Linked_List: 8 | def __init__(self): 9 | self.head = None 10 | 11 | def Insert_At_Beginning(self, new_data): 12 | new_node = Node(new_data) 13 | if self.head is None: 14 | self.head = new_node 15 | return 16 | new_node.next = self.head 17 | self.head = new_node 18 | 19 | def Add_two_no(self, First, Second): 20 | prev = None 21 | temp = None 22 | carry = 0 23 | while First is not None or Second is not None: 24 | first_data = 0 if First is None else First.data 25 | second_data = 0 if Second is None else Second.data 26 | Sum = carry+first_data+second_data 27 | carry = 1 if Sum >= 10 else 0 28 | Sum = Sum if Sum < 10 else Sum % 10 29 | temp = Node(Sum) 30 | if self.head is None: 31 | self.head = temp 32 | else: 33 | prev.next = temp 34 | prev = temp 35 | if First is not None: 36 | First = First.next 37 | if Second is not None: 38 | Second = Second.next 39 | if carry > 0: 40 | temp.next = Node(carry) 41 | 42 | def Display(self): 43 | temp = self.head 44 | while(temp): 45 | print(temp.data, "->", end=" ") 46 | temp = temp.next 47 | print("None") 48 | 49 | 50 | if __name__ == "__main__": 51 | First = Linked_List() 52 | Second = Linked_List() 53 | First.Insert_At_Beginning(6) 54 | First.Insert_At_Beginning(4) 55 | First.Insert_At_Beginning(9) 56 | 57 | Second.Insert_At_Beginning(2) 58 | Second.Insert_At_Beginning(2) 59 | 60 | print("First Linked List: ") 61 | First.Display() 62 | print("Second Linked List: ") 63 | Second.Display() 64 | 65 | Result = Linked_List() 66 | Result.Add_two_no(First.head, Second.head) 67 | print("Final Result: ") 68 | Result.Display() 69 | -------------------------------------------------------------------------------- /DAYS/Day88/Rotate_Linked_List.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data): 3 | self.data = data 4 | self.next = None 5 | 6 | 7 | class Linked_List: 8 | def __init__(self): 9 | self.head = None 10 | 11 | def Insert_At_Beginning(self, new_data): 12 | new_node = Node(new_data) 13 | if self.head is None: 14 | self.head = new_node 15 | return 16 | new_node.next = self.head 17 | self.head = new_node 18 | 19 | def Rotation(self, key): 20 | if key == 0: 21 | return 22 | current = self.head 23 | count = 1 24 | while count < key and current is not None: 25 | current = current.next 26 | count += 1 27 | if current is None: 28 | return 29 | Kth_Node = current 30 | while current.next is not None: 31 | current = current.next 32 | current.next = self.head 33 | self.head = Kth_Node.next 34 | Kth_Node.next = None 35 | 36 | def Display(self): 37 | temp = self.head 38 | while(temp): 39 | print(temp.data, "->", end=" ") 40 | temp = temp.next 41 | print("None") 42 | 43 | 44 | if __name__ == "__main__": 45 | L_list = Linked_List() 46 | L_list.Insert_At_Beginning(8) 47 | L_list.Insert_At_Beginning(5) 48 | L_list.Insert_At_Beginning(10) 49 | L_list.Insert_At_Beginning(7) 50 | L_list.Insert_At_Beginning(6) 51 | L_list.Insert_At_Beginning(11) 52 | L_list.Insert_At_Beginning(9) 53 | print("Linked List Before Rotation: ") 54 | L_list.Display() 55 | print("Linked List After Rotation: ") 56 | L_list.Rotation(4) 57 | L_list.Display() -------------------------------------------------------------------------------- /DAYS/Day89/Circular_Linked_List_Insertions.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data): 3 | self.data = data 4 | self.next = None 5 | 6 | 7 | class Circular_Linked_List: 8 | def __init__(self): 9 | self.last = None 10 | 11 | def Add_to_Empty(self, data): 12 | if self.last is not None: 13 | return self.last 14 | temp = Node(data) 15 | self.last = temp 16 | self.last.next = self.last 17 | return self.last 18 | 19 | def Add_to_Begin(self, data): 20 | if self.last is None: 21 | return self.Add_to_Empty(data) 22 | temp = Node(data) 23 | temp.next = self.last.next 24 | self.last.next = temp 25 | return self.last 26 | 27 | def Add_to_End(self, data): 28 | if self.last is None: 29 | return self.Add_to_Empty(data) 30 | temp = Node(data) 31 | temp.next = self.last.next 32 | self.last.next = temp 33 | self.last = temp 34 | return self.last 35 | 36 | def Add_After(self, data, key): 37 | if self.last == None: 38 | return None 39 | temp = Node(data) 40 | temp1 = self.last.next 41 | while temp1: 42 | if temp1.data == key: 43 | temp.next = temp1.next 44 | temp1.next = temp 45 | if temp1 == self.last: 46 | self.last = temp 47 | return self.last 48 | else: 49 | return self.last 50 | temp1 = temp1.next 51 | if temp1 == self.last.next: 52 | print(data, "Not present in a list!") 53 | break 54 | 55 | def Display(self): 56 | if self.last == None: 57 | print("List is Empty!") 58 | return 59 | temp = self.last.next 60 | temp1 = temp 61 | while(temp): 62 | print(temp.data, "->", end=" ") 63 | temp = temp.next 64 | if temp == self.last.next: 65 | print(temp1.data) 66 | break 67 | 68 | 69 | if __name__ == "__main__": 70 | 71 | L_list = Circular_Linked_List() 72 | L_list.Add_to_Empty(6) 73 | L_list.Add_to_Begin(4) 74 | L_list.Add_to_Begin(2) 75 | L_list.Add_to_End(8) 76 | L_list.Add_to_End(12) 77 | L_list.Add_After(10, 8) 78 | L_list.Display() 79 | -------------------------------------------------------------------------------- /DAYS/Day9/Add_Two_Matrix.py: -------------------------------------------------------------------------------- 1 | def Add_Matrix(X, Y): 2 | return [X[i][j]+Y[i][j] for i in range(len(X)) for j in range(len(X[0]))] 3 | 4 | 5 | X = [[1, 2, 3], 6 | [4, 5, 6], 7 | [7, 8, 9]] 8 | 9 | Y = [[9, 8, 7], 10 | [6, 5, 4], 11 | [3, 2, 1]] 12 | print(Add_Matrix(X, Y)) 13 | -------------------------------------------------------------------------------- /DAYS/Day9/Multiply_Matrix.py: -------------------------------------------------------------------------------- 1 | def Multiply_Matrix(A, B): 2 | Result = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] 3 | for i in range(len(A)): 4 | for j in range(len(B[0])): 5 | for k in range(len(B)): 6 | Result[i][j] += A[i][k]*B[k][j] 7 | return Result 8 | 9 | 10 | A = [[12, 7, 3], [4, 5, 6], [7, 8, 9]] 11 | B = [[5, 8, 1, 2], [6, 7, 3, 0], [4, 5, 9, 1]] 12 | print(Multiply_Matrix(A, B)) 13 | -------------------------------------------------------------------------------- /DAYS/Day9/Product_of_All_Elements_in_Matrix.py: -------------------------------------------------------------------------------- 1 | def Final_Product(Check_list): 2 | Prod = 1 3 | for ele in Check_list: 4 | Prod *= ele 5 | return Prod 6 | 7 | 8 | def Product_Matrix(Test_list): 9 | Semi_Result = Final_Product( 10 | [element for ele in Test_list for element in ele]) 11 | return Semi_Result 12 | 13 | 14 | Test_list = [[1, 4, 5], [7, 3], [4], [46, 7, 3]] 15 | print(Product_Matrix(Test_list)) 16 | -------------------------------------------------------------------------------- /DAYS/Day9/Transpose_Matrix.py: -------------------------------------------------------------------------------- 1 | def Tranpose_Matrix(Check_list): 2 | Result = [[Check_list[i][j] 3 | for i in range(len(Check_list))] for j in range(len(Check_list[0]))] 4 | return Result 5 | 6 | 7 | X = [[12, 7], [4, 5], [3, 8]] 8 | Final = Tranpose_Matrix(X) 9 | for items in Final: 10 | print(items) 11 | -------------------------------------------------------------------------------- /DAYS/Day90/Split_Circular_Linked_List.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data): 3 | self.data = data 4 | self.next = None 5 | 6 | 7 | class Circular_Linked_List: 8 | def __init__(self): 9 | self.head = None 10 | 11 | def Push(self, data): 12 | temp = Node(data) 13 | temp.next = self.head 14 | temp1 = self.head 15 | if self.head is not None: 16 | while temp1.next is not None: 17 | temp1 = temp1.next 18 | temp1.next = temp 19 | else: 20 | temp.next = temp 21 | self.head = temp 22 | 23 | def Split_List(self, head1, head2): 24 | if self.head is None: 25 | return 26 | slow_ptr = self.head 27 | fast_ptr = self.head 28 | while fast_ptr.next != self.head and fast_ptr.next.next != self.head: 29 | fast_ptr = fast_ptr.next.next 30 | slow_ptr = slow_ptr.next.next 31 | if fast_ptr.next.next == self.head: 32 | fast_ptr = fast_ptr.next 33 | head1 = self.head 34 | slow_ptr.next = head1 35 | if self.head.next != self.head: 36 | head2.head = slow_ptr.next 37 | fast_ptr.next = slow_ptr.next 38 | 39 | def Display(self): 40 | temp = self.head 41 | if self.head is not None: 42 | while(temp): 43 | print(temp.data, "->", end=" ") 44 | temp = temp.next 45 | if temp == self.head: 46 | print(temp.data) 47 | break 48 | 49 | 50 | if __name__ == "__main__": 51 | 52 | L_list = Circular_Linked_List() 53 | head1 = Circular_Linked_List() 54 | head2 = Circular_Linked_List() 55 | L_list.Push(6) 56 | L_list.Push(4) 57 | L_list.Push(2) 58 | L_list.Push(8) 59 | L_list.Push(12) 60 | L_list.Push(10) 61 | L_list.Split_List(head1, head2) 62 | print("Circular Linked List: ") 63 | L_list.Display() 64 | print("Firts Split Linked List: ") 65 | head1.Display() 66 | print("Second Split Linked List: ") 67 | head2.Display() 68 | -------------------------------------------------------------------------------- /DAYS/Day91/Sorted_Inserted_Linked_List.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data): 3 | self.data = data 4 | self.next = None 5 | 6 | 7 | class Circular_Linked_List: 8 | def __init__(self): 9 | self.head = None 10 | 11 | def Sorted_Insert(self, new_node): 12 | current = self.head 13 | if current is None: 14 | new_node.next = new_node 15 | self.head = new_node 16 | elif current.data >= new_node.data: 17 | while current.next != self.head: 18 | current = current.next 19 | current.next = new_node 20 | new_node.next = self.head 21 | self.head = new_node 22 | else: 23 | while current.next != self.head and current.next.data < new_node.data: 24 | current = current.next 25 | new_node.next = current.next 26 | current.next = new_node 27 | 28 | def Display(self): 29 | temp = self.head 30 | if self.head is not None: 31 | while(temp): 32 | print(temp.data, "->", end=" ") 33 | temp = temp.next 34 | if temp == self.head: 35 | print(temp.data) 36 | break 37 | 38 | 39 | if __name__ == "__main__": 40 | L_list = Circular_Linked_List() 41 | Test_list = [12, 56, 2, 11, 1, 90] 42 | for keys in Test_list: 43 | temp = Node(keys) 44 | L_list.Sorted_Insert(temp) 45 | print("Sorted Inserted Circular Linked List: ") 46 | L_list.Display() 47 | -------------------------------------------------------------------------------- /DAYS/Day92/Doubly_Linked_List_Insertions.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data): 3 | self.prev = None 4 | self.data = data 5 | self.next = None 6 | 7 | 8 | class Doubly_Linked_List: 9 | def __init__(self): 10 | self.head = None 11 | 12 | def Add_at_front(self, new_data): 13 | new_node = Node(new_data) 14 | new_node.next = self.head 15 | new_node.prev = None 16 | if self.head is not None: 17 | self.head.prev = new_node 18 | self.head = new_node 19 | 20 | def Add_After(self, prev_node, new_data): 21 | if prev_node is None: 22 | return 23 | new_node = Node(new_data) 24 | new_node.next = prev_node.next 25 | prev_node.next = new_node 26 | new_node.prev = prev_node 27 | if new_node.next is not None: 28 | new_node.next.prev = new_node 29 | 30 | def Add_End(self, new_data): 31 | new_node = Node(new_data) 32 | temp = self.head 33 | new_node.next = None 34 | if self.head is None: 35 | new_node.prev = None 36 | self.head = new_node 37 | return 38 | while(temp.next is not None): 39 | temp = temp.next 40 | temp.next = new_node 41 | new_node.prev = temp 42 | 43 | def Display(self): 44 | temp = self.head 45 | print("\nTraversal in Forward Direction: ") 46 | while temp: 47 | print(temp.data, end=" ") 48 | last = temp 49 | temp = temp.next 50 | print("\nTraversal in Reverse Direction: ") 51 | while last: 52 | print(last.data, end=" ") 53 | last = last.prev 54 | print("\n") 55 | 56 | 57 | if __name__ == "__main__": 58 | L_list = Doubly_Linked_List() 59 | L_list.Add_at_front(5) 60 | L_list.Add_at_front(7) 61 | L_list.Add_at_front(9) 62 | L_list.Add_at_front(8) 63 | L_list.Add_End(2) 64 | L_list.Add_End(6) 65 | L_list.Add_After(L_list.head.next, 1) 66 | L_list.Display() -------------------------------------------------------------------------------- /DAYS/Day93/Binary_Insertion_Sort.py: -------------------------------------------------------------------------------- 1 | def Binary_Search(Test_arr, low, high, k): 2 | if high >= low: 3 | Mid = (low+high)//2 4 | if Test_arr[Mid] < k: 5 | return Binary_Search(Test_arr, Mid+1, high, k) 6 | elif Test_arr[Mid] > k: 7 | return Binary_Search(Test_arr, low, Mid-1, k) 8 | else: 9 | return Mid 10 | else: 11 | return low 12 | 13 | 14 | def Insertion_Sort(Test_arr): 15 | for i in range(1, len(Test_arr)): 16 | val = Test_arr[i] 17 | j = Binary_Search(Test_arr[:i], 0, len(Test_arr[:i])-1, val) 18 | Test_arr.pop(i) 19 | Test_arr.insert(j, val) 20 | return Test_arr 21 | 22 | 23 | if __name__ == "__main__": 24 | Test_list = input("Enter the list of Numbers: ").split() 25 | Test_list = [int(i) for i in Test_list] 26 | print(f"Binary Insertion Sort: {Insertion_Sort(Test_list)}") -------------------------------------------------------------------------------- /DAYS/Day93/Linear_Insertion_Sort.py: -------------------------------------------------------------------------------- 1 | def Linear_Search(Test_arr, val): 2 | index = 0 3 | for i in range(len(Test_arr)): 4 | if val > Test_arr[i]: 5 | index = i+1 6 | return index 7 | 8 | 9 | def Insertion_Sort(Test_arr): 10 | for i in range(1, len(Test_arr)): 11 | val = Test_arr[i] 12 | j = Linear_Search(Test_arr[:i], val) 13 | Test_arr.pop(i) 14 | Test_arr.insert(j, val) 15 | return Test_arr 16 | 17 | 18 | if __name__ == "__main__": 19 | Test_list = input("Enter the list of Numbers: ").split() 20 | Test_list = [int(i) for i in Test_list] 21 | print(f"Binary Insertion Sort: {Insertion_Sort(Test_list)}") -------------------------------------------------------------------------------- /DAYS/Day94/Deletion_Doubly_Linked_List.py: -------------------------------------------------------------------------------- 1 | import gc 2 | 3 | 4 | class Node: 5 | def __init__(self, data): 6 | self.prev = None 7 | self.data = data 8 | self.next = None 9 | 10 | 11 | class Doubly_Linked_List: 12 | def __init__(self): 13 | self.head = None 14 | 15 | def Add_at_front(self, new_data): 16 | new_node = Node(new_data) 17 | new_node.next = self.head 18 | new_node.prev = None 19 | if self.head is not None: 20 | self.head.prev = new_node 21 | self.head = new_node 22 | 23 | def Delete_Node(self, node): 24 | if self.head is None or node is None: 25 | return 26 | if self.head == node: 27 | self.head = node.next 28 | if node.next is not None: 29 | node.next.prev = node.prev 30 | if node.prev is not None: 31 | node.prev.next = node.next 32 | gc.collect() 33 | 34 | def Display(self): 35 | temp = self.head 36 | while temp: 37 | print(temp.data, end=" ") 38 | temp = temp.next 39 | 40 | 41 | if __name__ == "__main__": 42 | L_list = Doubly_Linked_List() 43 | L_list.Add_at_front(5) 44 | L_list.Add_at_front(7) 45 | L_list.Add_at_front(9) 46 | L_list.Add_at_front(8) 47 | L_list.Add_at_front(1) 48 | L_list.Add_at_front(2) 49 | print("Original Linked List: ") 50 | L_list.Display() 51 | L_list.Delete_Node(L_list.head) 52 | L_list.Delete_Node(L_list.head.next) 53 | L_list.Delete_Node(L_list.head.next) 54 | print("\nDelete Linked List: ") 55 | L_list.Display() -------------------------------------------------------------------------------- /DAYS/Day95/Reverse_Doubly_Linked_List.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data): 3 | self.prev = None 4 | self.data = data 5 | self.next = None 6 | 7 | 8 | class Doubly_Linked_List: 9 | def __init__(self): 10 | self.head = None 11 | 12 | def Add_at_front(self, new_data): 13 | new_node = Node(new_data) 14 | new_node.next = self.head 15 | new_node.prev = None 16 | if self.head is not None: 17 | self.head.prev = new_node 18 | self.head = new_node 19 | 20 | def Reverse_Linked_List(self): 21 | temp = None 22 | current = self.head 23 | if current.next is None: 24 | return 25 | while current is not None: 26 | temp = current.prev 27 | current.prev = current.next 28 | current.next = temp 29 | current = current.prev 30 | self.head = temp.prev 31 | 32 | def Display(self): 33 | temp = self.head 34 | while temp: 35 | print(temp.data, end=" ") 36 | temp = temp.next 37 | 38 | 39 | if __name__ == "__main__": 40 | L_list = Doubly_Linked_List() 41 | L_list.Add_at_front(5) 42 | L_list.Add_at_front(7) 43 | L_list.Add_at_front(9) 44 | L_list.Add_at_front(8) 45 | L_list.Add_at_front(1) 46 | L_list.Add_at_front(2) 47 | print("\nOriginal Linked List: ") 48 | L_list.Display() 49 | print("\nReverse Linked List: ") 50 | L_list.Reverse_Linked_List() 51 | L_list.Display() -------------------------------------------------------------------------------- /DAYS/Day96/Reverse_Doubly_Linked_List_Using_Stack.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data): 3 | self.prev = None 4 | self.data = data 5 | self.next = None 6 | 7 | 8 | class Doubly_Linked_List: 9 | def __init__(self): 10 | self.head = None 11 | 12 | def Add_at_front(self, new_data): 13 | new_node = Node(new_data) 14 | new_node.next = self.head 15 | new_node.prev = None 16 | if self.head is not None: 17 | self.head.prev = new_node 18 | self.head = new_node 19 | 20 | def Reverse_Linked_List(self): 21 | temp = self.head 22 | stack = [] 23 | while temp is not None: 24 | stack.append(temp.data) 25 | temp = temp.next 26 | temp = self.head 27 | while temp is not None: 28 | temp.data = stack.pop() 29 | temp = temp.next 30 | 31 | def Display(self): 32 | temp = self.head 33 | while temp: 34 | print(temp.data, end=" ") 35 | temp = temp.next 36 | 37 | 38 | if __name__ == "__main__": 39 | L_list = Doubly_Linked_List() 40 | L_list.Add_at_front(5) 41 | L_list.Add_at_front(7) 42 | L_list.Add_at_front(9) 43 | L_list.Add_at_front(8) 44 | L_list.Add_at_front(1) 45 | L_list.Add_at_front(2) 46 | print("\nOriginal Linked List: ") 47 | L_list.Display() 48 | print("\nReverse Linked List: ") 49 | L_list.Reverse_Linked_List() 50 | L_list.Display() 51 | -------------------------------------------------------------------------------- /DAYS/Day97/Infix_to_Prefix.py: -------------------------------------------------------------------------------- 1 | class Coversion_Infi_Prefi: 2 | 3 | def __init__(self, size): 4 | self.top = -1 5 | self.size = size 6 | self.garbage = [] 7 | self.Result = [] 8 | self.precedence = {"+": 1, "-": 1, "*": 2, "/": 2, "^": 3} 9 | 10 | def Is_Empty(self): 11 | return True if self.top == -1 else False 12 | 13 | def Peek(self): 14 | return self.garbage[-1] 15 | 16 | def Pop(self): 17 | if not self.Is_Empty(): 18 | self.top -= 1 19 | return self.garbage.pop() 20 | else: 21 | return "$" 22 | 23 | def Push(self, data): 24 | self.top += 1 25 | self.garbage.append(data) 26 | 27 | def Is_Operand(self, ch): 28 | return ch.isalpha() 29 | 30 | def Is_Greater(self, ele): 31 | try: 32 | a = self.precedence[ele] 33 | b = self.precedence[self.Peek()] 34 | return True if a <= b else False 35 | except KeyError: 36 | return False 37 | 38 | def Infix_to_Prefix(self, Expres): 39 | for i in Expres: 40 | if self.Is_Operand(i): 41 | self.Result.append(i) 42 | elif i == "(": 43 | self.Push(i) 44 | elif i == ")": 45 | while (not self.Is_Empty() and self.Peek() != "("): 46 | ele = self.Pop() 47 | self.Result.append(ele) 48 | if (not self.Is_Empty() and self.Peek() != "("): 49 | return -1 50 | else: 51 | self.Pop() 52 | else: 53 | while(not self.Is_Empty() and self.Is_Greater(i)): 54 | self.Result.append(self.Pop()) 55 | self.Push(i) 56 | while (not self.Is_Empty()): 57 | self.Result.append(self.Pop()) 58 | print("".join(self.Result)) 59 | 60 | 61 | if __name__ == "__main__": 62 | Expr = "a+b*(c^d-e)^(f+g*h)-i" 63 | Stack_Expr = Coversion_Infi_Prefi(len(Expr)) 64 | Stack_Expr.Infix_to_Prefix(Expr) -------------------------------------------------------------------------------- /DAYS/Day98/Postfix_Evaluate.py: -------------------------------------------------------------------------------- 1 | class Evaluate_Postfix: 2 | 3 | def __init__(self, size): 4 | self.top = -1 5 | self.size = size 6 | self.garbage = [] 7 | 8 | def Is_Empty(self): 9 | return True if self.top == -1 else False 10 | 11 | def Peek(self): 12 | return self.garbage[-1] 13 | 14 | def Pop(self): 15 | if not self.Is_Empty(): 16 | self.top -= 1 17 | return self.garbage.pop() 18 | else: 19 | return "$" 20 | 21 | def Push(self, data): 22 | self.top += 1 23 | self.garbage.append(data) 24 | 25 | def Postfix_Eval(self, Expres): 26 | for i in Expres: 27 | if i.isdigit(): 28 | self.Push(i) 29 | else: 30 | Val_1 = self.Pop() 31 | Val_2 = self.Pop() 32 | self.Push(str(eval(Val_2+i+Val_1))) 33 | return int(self.Pop()) 34 | 35 | 36 | if __name__ == "__main__": 37 | Expr = "231*+9-" 38 | Stack_Expr = Evaluate_Postfix(len(Expr)) 39 | print("%d" % (Stack_Expr.Postfix_Eval(Expr))) 40 | -------------------------------------------------------------------------------- /DAYS/Day99/Balanced_Brackets.py: -------------------------------------------------------------------------------- 1 | def Balanced_Brackets(Expr): 2 | stack = [] 3 | for Brkt in Expr: 4 | if Brkt in ["(", "{", "["]: 5 | stack.append(Brkt) 6 | else: 7 | if not stack: 8 | return False 9 | Current_Brkt = stack.pop() 10 | if Current_Brkt == "(": 11 | if Brkt != ")": 12 | return False 13 | if Current_Brkt == "{": 14 | if Brkt != "}": 15 | return False 16 | if Current_Brkt == "[": 17 | if Brkt != "]": 18 | return False 19 | if stack: 20 | return False 21 | return True 22 | 23 | 24 | if __name__ == "__main__": 25 | Expr = "{()}[]" 26 | if Balanced_Brackets(Expr): 27 | print("Balanced Brackets") 28 | else: 29 | print("UnBalanced Brackets") 30 | --------------------------------------------------------------------------------