# Complex Python Programs

Python programming language is a flexible and strong programming language. It is well-known for being easy to learn and understand. It is widely utilized in many different fields, including web development, data analysis, machine learning, and automation. Python is simple to learn and is used because of its clear syntax and robust library support. It supports multiple platforms and provides an engaging coding experience.

The community surrounding Python is vibrant and helpful, offering a wealth of tools and fostering teamwork. Python provides a friendly environment for developing, whether you are a beginner or an experienced developer. In this article, we will see some different important complex Python programs.

## Reversing a linked list using Python

The main goal of this Python program is to reverse a linked list. Each node (element) in a linked list has a value and a pointer to the subsequent node. A linked list can be reversed by switching the pointers such that the elements appear in the opposite order. This program will show you how to reverse a linked list using the syntax and built-in features of Python. This program offers an easy and useful introduction to reversing linked lists in Python, whether you are new to linked lists or looking to improve your understanding.

Let’s see the code for this program.

**Example:**

class Node: # Initialization constructor for the node object def __init__(self, data): self.data = data self.next = None class LinkedList: # Function to start the head def __init__(self): self.head = None # Function to reverse the linked list def reverse(self): prev = None current = self.head while(current is not None): next = current.next current.next = prev prev = current current = next self.head = prev # Function to insert a new node at the beginning def push(self, new_data): new_node = Node(new_data) new_node.next = self.head self.head = new_node # Printing the LinkedList with a utility function def printList(self): temp = self.head while(temp): print (temp.data,end=" ") temp = temp.next # Driver program llist = LinkedList() llist.push(40) llist.push(14) llist.push(35) llist.push(95) print ("Given Linked List") llist.printList() llist.reverse() print (" Reversed Linked List") llist.printList()

**The output of this code is:**

**Output:**

Given Linked List 95 35 14 40 Reversed Linked List 40 14 35 95

This Python code is an example of implementing a linked list and has a function to make the linked list reversed. A single node with a value and a reference to the following node is represented by the Node class. The LinkedList class includes methods to print the list, reverse the order of the nodes using a while loop, insert nodes at the beginning of the list, and initialize the list. A linked list is created, elements are added, the original list is printed, it is then reversed, and finally, the reversed list is printed. This code essentially shows how to create, modify, and reverse a linked list in Python.

## Finding Minimum Sum of factors of a Number using Python

In this program, we'll determine the minimum sum of factors for a given number. Using Python's mathematical operations and control structures, we will investigate various strategies and techniques. It will help us to quickly calculate a number's factors and determine their minimum sum.

Here is the code for this problem statement.

**Example:**

def find_min_factor_sum(num): # Initialize the minimum sum with the number itself min_sum = num # Check factors up to the square root of the number for i in range(2, int(num**0.5) + 1): if num % i == 0: # Found a factor factor = num // i # Update the minimum sum if the current sum is smaller min_sum = min(min_sum, i + factor) return min_sum # driver code number = 16 # Call the function and print the result min_factor_sum = find_min_factor_sum(number) print("The minimum sum of factors for", number, "is", min_factor_sum)

**The output of this code is:**

**Output:**

The minimum sum of factors for 16 is 8

In this program, the function find_min_factor_sum accepts a number as input. It determines the corresponding factor by going through all of the factors of a number up to its square root. If a smaller quantity is discovered, it adjusts the minimal amount. The function returns the smallest sum of the elements. A test case is established in the driver code by allocating a number. When the function is called with the test case, the minimum sum of the factors is displayed.

## Check if Binary representation is Palindrome in Python

In this example, we will determine whether a number's binary representation is a palindrome or not. We'll look at a technique and strategy that makes use of Python's text manipulation and logical operations. This example clarifies how to use Python to convert a number to binary, represent it in reverse, and check to see if it's a palindrome.

Let’s see the code for this example.

**Example:**

def binarypalindrome(num): # Convert number into binary binary = bin(num) # Skip the first two characters of the string since the bin function appends '0b' as the prefix in binary form of a number binary = binary[2:] # Now compare the original binary string to the reversed version. return binary == binary[-1::-1] #driver code num = 45 if (binarypalindrome(num)): print("Binary representation of " + str(num) + " is a palindrome.") else: print("Binary representation of " + str(num) + " is not a palindrome.")

**The output of this program is:**

**Output:**

Binary representation of 45 is a palindrome.

The program defines a function named binarypalindrome that determines whether a given number's binary representation is a palindrome or not. The binary number is converted, the prefix is removed, and the binary string's original and reversed versions are compared. It returns True, signifying a palindrome if they match, and False otherwise. The function is then used by the code to determine whether the binary form of the number 45 is a palindrome, and the result is then printed.

We can also solve this question using iteration. Let’s see the code for this approach.

**Example:**

def binarypalindrome(num): # Convert number into binary binary = bin(num)[2:] # Initialize variables for loop left = 0 right = len(binary) - 1 # Compare the left and right characters while iterating through the binary string while left < right: if binary[left] != binary[right]: return False left += 1 right -= 1 # if the loop completes, the binary string must be a palindrome return True #driver code num = 16 if (binarypalindrome(num)): print("Binary representation of " + str(num) + " is a palindrome.") else: print("Binary representation of " + str(num) + " is not a palindrome.")

**The output of this code is:**

**Output:**

Binary representation of 16 is not a palindrome.

This code defines a function named binarypalindrome that determines whether the binary representation of a given number is a palindrome using an iteration approach. The binary string is iterated over while comparing characters from both ends until the middle is reached. The binary string is returned as False if a mismatch is discovered, proving that it is not a palindrome. The binary string is a palindrome if the loop ends without locating any mismatches and returns True. The function is then used by the code to determine whether the binary form of the number 16 is a palindrome, and the result is then printed.

## Find the common character and its frequency in a String

This Python program looks for the character that appears the most frequentlyin each string and counts its occurrence. We may determine which character(s) in the string have the highest count by calculating the number of times each character appears in the string. The distribution and occurrence of characters in the input string are revealed by this program.

**Here is the code for this example.**

**Example:**

# Python code to find the most occurring character in a string and its count string1="tutorialandexample" list1=[] list2=[] for i in string1: if i not in list1: list1.append(i)#appending unique characters of the string import operator list2.append(operator.countOf(string1,i)) #finding the maximum in the list ct=max(list2) element=list1[list2.index(ct)] print("Most occurring element = " + element) print("Frequency of this character = " + str(ct))

**The output of this code is:**

**Output:**

Most occurring element = a Frequency of this character = 3

The program examines a given string to identify the character that appears the most frequently and counts how many times it appears in the string. It generates two lists to keep track of unique characters and their associated counts. It uses the count() method to count the instances of each character as it traverses the string. The relevant character is then retrieved from the list of unique characters when the maximum count has been determined. Finally, it displays the character with the highest frequency of occurrence.

## Conclusion

Python is a flexible and user-friendly programming language appreciated for its ease of use and wide range of library support. In this article, we looked at some complex Python programs, such as reversing a linked list, calculating the minimal sum of factors, determining whether a binary representation of a palindrome exists, and figuring out which character in a string is used the most frequently. These examples demonstrate Python's capacity to manage challenging jobs in a variety of fields. Python is the perfect choice for developing reliable software solutions because of its simplicity, vast library environment, and strong community support.