python-5
python-5
Armstrong Number
def is_armstrong(n):
num_str = str(n)
num_digits = len(num_str)
total = 0
return total == n
# Example usage:
if is_armstrong(num):
else:
2. Array rota on
# Example usage:
arr = [1, 2, 3, 4, 5]
k=2
def analyze_string(input_string):
length_of_string = len(modified_string)
char_count = {}
if char in char_count:
char_count[char] += 1
else:
char_count[char] = 1
print(f"'{char}': {count}")
# Example usage:
analyze_string(input_string)
occurance = { }
i=0
item = lst[i]
if item in occurance:
occurance[item] += 1
else:
occurance[item] = 1
if occurance[item] = 2:
lst.pop(i)
else:
i += 1
return lst
# Example usage:
result = delete_second_occurrence(l)
5. Fla en List
def fla en_list(nested_list):
fla ened= [ ]
fla ened.extend(fla en_list(item))
else:
fla ened.append(item)
return fla ened
# Example usage:
print(fla en_list(nested_list))
def interchange_first_last(arr):
if len(arr) > 1:
return arr
# Example usage:
arr = [1, 2, 3, 4, 5]
7. Intersect_without_built_in
intersect = [ ]
for i in list1:
if item in list2 and item not in intersect:
intersect.append(item)
return intersect
# Example usage:
list1 = [1, 2, 3, 4, 5]
list2 = [3, 4, 5, 6, 7]
# Output: [3, 4, 5]
def remove_duplicates(lst):
unique_list = []
return unique_list
# Example usage:
unique_list = remove_duplicates(l)
def is_palindrome(string):
string = string.lower()
if string == string[::-1]:
return True
else:
return False
# Example usage:
input_string = "Madam"
if is_palindrome(input_string):
print(f'"{input_string}" is a palindrome.')
else:
def print_right_angle_triangle():
for j in range(i):
print()
# Example usage:
print_right_angle_triangle()
def print_diamond(n):
# Print spaces
# Print stars
print('*', end='')
print()
# Print spaces
# Print stars
print('*', end='')
print()
# Get user input
n = int(input("Enter the number of rows for the top half of the diamond: "))
print_diamond(n)
n=5
# Print stars
print('* ' * i)
13. Fibanacci
def fibonacci_recursive(n):
if n <= 1:
return n
# Example usage:
result = { }
if value in result:
result[value].append(key)
else:
result[value] = [key]
return result
# Example
my_dict = {'a': 10, 'b': 20, 'c': 10, 'd': 30, 'e': 20}
print(group_by_value(my_dict))
total = 0
total += value
return total
# Example
print(sum_dict_values(my_dict)) # Output: 60
16. find largest value in Array
arr = [3, 1, 4, 1, 5, 9, 2]
largest = arr[0]
largest = num
17. fla en_list
def fla en_list(input_list):
flat_list = []
if isinstance(item, list):
flat_list.extend(fla en_list(item))
else:
flat_list.append(item)
return flat_list
# Example usage:
input_list = [1, 2, [3, 4, 5, 6], 9, 10, 11, 0, [23, 45, 78], [100, 90]]
fla ened = fla en_list(input_list)
print(fla ened)
def find_missing_numbers(lst):
# Find the minimum and maximum values in the list
min_val = min(lst)
max_val = max(lst)
list_set = set(lst)
return sorted(missing_numbers)
# Example usage:
missing_numbers = find_missing_numbers(l)
19. move_zeros_to_end
def move_zeros_to_end(input_list):
for i in input_list:
if i != 0:
else:
for j in range(zero_count):
non_zero_list.append(0)
return non_zero_list
# Example usage:
reordered_list = move_zeros_to_end(input_list)
print(reordered_list)
def is_prime_basic(n):
if n <= 1:
return False
if n % i == 0:
return False
return True
# Example usage:
print(is_prime_basic(29)) # True
print(is_prime_basic(30)) # False
def remove_duplicates(string):
freq = {}
if char in freq:
freq[char] += 1
else:
freq[char] = 1
result = ""
if freq[char] == 1:
result += char
return result
# Example
def reverse_words(sentence):
return ' '.join(sentence.split()[::-1])
# Example
total = 0
total += i**2
return total
# Example usage:
return arr
# Example usage:
arr = [1, 2, 3, 4, 5]
def word_frequency(string):
words = string.split()
return Counter(words)
# Example
26. word2number
def words_to_number(word):
word = word.lower()
num_dict = {
parts = word.split()
number = 0
temp = 0
if part in num_dict:
if part == "hundred":
temp *= num_dict[part]
temp *= num_dict[part]
number += temp
temp = 0
else:
temp += num_dict[part]
output_number = words_to_number(input_text)
print(forma ed_output)