Python Basics: 50 Code Examples
Python Basics: 50 Code Examples
The bubble sort algorithm is demonstrated by iteratively comparing adjacent elements and swapping them if needed, until the entire list is sorted . On the other hand, binary search is shown as a more efficient searching method that works on a sorted list, repeatedly dividing the search interval in half to find a target value . Thus, bubble sort is a sorting algorithm, while binary search is a searching algorithm designed for sorted data.
The document illustrates that a stack uses a LIFO (Last In First Out) approach where elements are pushed onto the stack and popped from the top, as demonstrated by appending and popping operations . A queue, however, uses a FIFO (First In First Out) method where elements are appended at the rear and popped from the front, as shown in queue operations .
The document describes merging dictionaries by using syntax that combines the key-value pairs of two dictionaries, resolving key conflicts by choosing the value from the second dictionary . This approach implies that when keys overlap, the latter dictionary’s entries will overwrite the former’s, which can be useful for updating dictionaries but may result in data loss for overlapped keys.
The document uses a stack to check for balanced parentheses by pushing '(' onto the stack and popping it upon encountering ')'. If a closing parenthesis is encountered and the stack is empty, it indicates an imbalance . The algorithm ensures all opening parentheses are matched with closing ones and handles situations where extra closing parentheses or unclosed opening parentheses cause imbalances.
The document demonstrates using the modulus operation to determine if a number is even or odd by checking if it is divisible by 2 (i.e., num % 2 == 0 for even, else odd). This operation is fundamental because it provides a simple and efficient method for evaluating parity, which is useful for conditions and algorithms that depend on numeric properties.
The document uses a stack to reverse a string by pushing each character onto the stack and then popping them off in reverse order. This method takes advantage of the LIFO (Last In First Out) property of stacks, effectively reversing the order of the characters in the string .
The document performs matrix addition by iterating through each element of two matrices simultaneously and summing corresponding elements to form a new matrix . The approach efficiently handles matrix operations that require matching dimensions and correctly applies element-wise addition, which is critical in many mathematical computations involving matrices.
The document checks for an Armstrong number by summing the cubes of its digits and comparing the sum to the number itself. An example given in the document is the number 153, which is determined to be an Armstrong number because the sum of its digits raised to the power of three equals the number itself .
The document employs a dictionary to keep track of how often each character appears in a string. As each character is encountered, its count is incremented in the dictionary. This technique provides insights into character distribution within the text which could be vital for cryptographic analysis or text compression .
The document represents a graph using an adjacency list, where each vertex key maps to a list of adjacent vertices. For example, vertex 'A' maps to['B', 'C'], indicating an edge between A and both B and C . This representation is space-efficient, particularly for sparse graphs, because it only stores edges that are present and allows for efficient traversal of a node’s connections.