1
+ # 10 more Python code snippets for common programming tasks
2
+ # Calculate Factorial:
3
+ # Calculate the factorial of a number using a recursive function.
4
+ def factorial (n ):
5
+ if n == 0 :
6
+ return 1 # Base case: factorial of 0 is 1
7
+ else :
8
+ return n * factorial (n - 1 ) # Recursive case: n! = n * (n-1)!
9
+
10
+ # Reverse a String:
11
+ # Reverse a string using slicing.
12
+ def reverse_string (s ):
13
+ return s [::- 1 ] # Slice the string with a step of -1 to reverse it
14
+
15
+ # Find the Intersection of Two Lists:
16
+ # Find the common elements between two lists.
17
+
18
+ def intersection (list1 , list2 ):
19
+ return list (set (list1 ) & set (list2 )) # Convert lists to sets, then use set intersection
20
+
21
+ # Calculate Fibonacci Series:
22
+ # Generate the Fibonacci series up to a specified number of terms.
23
+ def fibonacci (n ):
24
+ a , b = 0 , 1
25
+ result = []
26
+ while len (result ) < n :
27
+ result .append (a )
28
+ a , b = b , a + b # Generate the next Fibonacci number
29
+ return result
30
+
31
+ # Check for a Palindrome:
32
+ # Determine whether a given string is a palindrome.
33
+ def is_palindrome (s ):
34
+ s = s .lower ().replace (" " , "" ) # Remove spaces and convert to lowercase
35
+ return s == s [::- 1 ] # Compare the string to its reverse
36
+
37
+ # Calculate Square Root:
38
+ # Calculate the square root of a number.
39
+ import math
40
+
41
+ def square_root (x ):
42
+ return math .sqrt (x ) # Use the math module's sqrt function to calculate the square root
43
+
44
+ # Sum of Digits:
45
+ # Calculate the sum of the digits of a number.
46
+ def sum_of_digits (n ):
47
+ return sum (map (int , str (n ))) # Convert the number to a string, map digits to integers, and sum them
48
+
49
+ # Count Vowels in a String:
50
+ # Count the number of vowels in a string.
51
+ def count_vowels (s ):
52
+ vowels = "AEIOUaeiou" # Define a string containing vowels
53
+ return sum (1 for char in s if char in vowels ) # Count vowels in the input string
54
+
55
+ # Generate Random Password:
56
+ # Generate a random password of a specified length.
57
+ import random
58
+ import string
59
+
60
+ def generate_password (length ):
61
+ characters = string .ascii_letters + string .digits + string .punctuation # Define characters for the password
62
+ password = '' .join (random .choice (characters ) for _ in range (length )) # Generate a random password
63
+ return password
64
+
65
+ # Check for Prime Number:
66
+ # Determine whether a given number is prime.
67
+ def is_prime (n ):
68
+ if n <= 1 :
69
+ return False # Numbers less than or equal to 1 are not prime
70
+ if n <= 3 :
71
+ return True # 2 and 3 are prime
72
+ if n % 2 == 0 or n % 3 == 0 :
73
+ return False # Numbers divisible by 2 or 3 are not prime
74
+ i = 5
75
+ while i * i <= n :
76
+ if n % i == 0 or n % (i + 2 ) == 0 :
77
+ return False # Check for divisibility by numbers in the form 6k +/- 1
78
+ i += 6
79
+ return True # The number is prime
0 commit comments