0% found this document useful (0 votes)
4 views4 pages

Python Functions for Common Problems

Uploaded by

username191827
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Python Functions for Common Problems

Uploaded by

username191827
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Find Pair with Target Sum

Given a list of integers, return True if there exists a pair of numbers that sum to
a specific target sum. For example, if the list is [10, 15, 3, 7] and the target is
17, the function should return True because 10 + 7 = 17.

Count Character Occurrences in a String


Write a function that takes a string and returns a dictionary where the keys are
characters and the values are the number of occurrences of that character in
the string. For example, for the string "programming", the function should
return {'p': 1, 'r': 2, 'o': 1, 'g': 2, 'a': 1, 'm': 2, 'i': 1, 'n': 1}.

Check If All Elements in List Are Unique


Write a function that checks if all elements in a list are unique. For example, if
the list is [1, 2, 3, 4, 5], the function should return True. If the list is [1, 2, 2, 3,
4], the function should return False.

Find Common Elements in Two Lists


Write a function that takes two lists and returns a new list containing the
common elements between them. For example, if list1 = [1, 2, 3, 4, 5] and list2
= [4, 5, 6, 7, 8], the function should return [4, 5].

Find Missing Number in List


Given a list of consecutive numbers starting from 1, find the missing number in
the list. For example, if the list is [1, 2, 3, 5, 6], the function should return 4.

Find the Longest Substring Without Repeating Characters


Write a function that returns the length of the longest substring without
repeating characters. For example, for the string "abcabcbb", the function
should return 3 because the longest substring without repeating characters is
"abc".

Implement a Function to Merge Two Dictionaries


Write a function that merges two dictionaries. If a key exists in both
dictionaries, sum their values. For example, if dict1 = {'a': 1, 'b': 2} and dict2 =
{'b': 3, 'c': 4}, the function should return {'a': 1, 'b': 5, 'c': 4}.

Flatten a Nested List


Write a function that flattens a nested list into a single list. For example, if the
list is [1, [2, 3], [4, [5, 6]], 7], the function should return [1, 2, 3, 4, 5, 6, 7].

Create a Function to Count the Occurrences of a Specific Element in a Set


Write a function that counts how many times a specific item appears in a set.
For example, if the set is {1, 2, 3, 4, 5} and the item is 3, the function should
return 1.

Sum of Digits of a Number


Write a function that returns the sum of digits of a given number. For example,
for the number 12345, the function should return 15 because 1 + 2 + 3 + 4 + 5
= 15.

Perfect Number
A perfect number is a number that is equal to the sum of its proper divisors
(excluding the number itself). Write a function to check if a given number is a
perfect number. For example, the number 6 is perfect because its divisors
(excluding 6) are 1, 2, 3, and 1 + 2 + 3 = 6.

Armstrong Number
An Armstrong number (or narcissistic number) is a number that is equal to the
sum of its own digits each raised to the power of the number of digits. Write a
function to check if a number is an Armstrong number. For example, 153 is an
Armstrong number because 1^3 + 5^3 + 3^3 = 153.

Strong Number
A strong number is a number where the sum of the factorials of its digits is
equal to the number itself. Write a function to check if a number is a strong
number. For example, 145 is a strong number because 1! + 4! + 5! = 145.

Number Pyramid Pattern


Write a program to print the following number pyramid pattern for a given
number n:
1
121
12321
1234321
123454321

Floyd's Triangle
Floyd’s triangle is a right-angled triangular arrangement of natural numbers,
starting from 1. Write a function to print Floyd’s triangle for a given number
rows n. For example:
1
23
456
7 8 9 10

Diamond Pattern
Write a program to print the following diamond pattern:
N=5

You might also like