Duplicate characters

Duplicate characters

EasyFunction Programs · 23 of 39

Write a Python function that returns the characters that appear more than once.

Input
"Programming"
Output
['g', 'm', 'r']

Approach

  1. Count with Counter and keep counts above 1.

Solution

Python
from collections import Counter def duplicates(text):    counts = Counter(text.lower())    return sorted(ch for ch, n in counts.items() if n > 1) print(duplicates("Programming"))
Output
['g', 'm', 'r']

Learn the concept

New to this topic? Read the lesson, then come back.

More function programs

← All 39 Function Programs

Unique values with dict.fromkeys

Unique values with dict.fromkeys

EasyFunction Programs · 22 of 39

Write a Python function to get the unique values of a list in order.

Input
[4, 6, 1, 7, 6, 1, 5]
Output
[4, 6, 1, 7, 5]

Approach

  1. list(dict.fromkeys(items)).

Solution

Python
def unique(items):    return list(dict.fromkeys(items)) print(unique([4, 6, 1, 7, 6, 1, 5]))
Output
[4, 6, 1, 7, 5]

Learn the concept

New to this topic? Read the lesson, then come back.

More function programs

← All 39 Function Programs

Does any pair sum to 10?

Does any pair sum to 10?

EasyFunction Programs · 21 of 39

Write a Python function that checks whether any two numbers in a list add up to 10.

Input
[2, 5, 6, 4, 7, 3, 8, 9, 1]
Output
True False

Approach

  1. Keep a set of numbers seen; for each number check whether 10 - n was seen.

Solution

Python
def has_pair(numbers, target=10):    seen = set()    for n in numbers:        if target - n in seen:            return True        seen.add(n)    return False print(has_pair([2, 5, 6, 4, 7, 3, 8, 9, 1]), has_pair([1, 2, 3]))
Output
True False

Line by line

Line 4

If the partner value was already seen, a pair exists. This needs one pass instead of checking every pair.

Learn the concept

New to this topic? Read the lesson, then come back.

More function programs

← All 39 Function Programs

Fibonacci terms up to n terms

Fibonacci terms up to n terms

EasyFunction Programs · 20 of 39

Write a Python function that returns the first n Fibonacci numbers starting 1, 1.

Input
9
Output
1 1 2 3 5 8 13 21 34

Approach

  1. Build a list in a loop.

Solution

Python
def fibonacci(n):    series = []    a, b = 1, 1    for _ in range(n):        series.append(a)        a, b = b, a + b    return series print(*fibonacci(9))
Output
1 1 2 3 5 8 13 21 34

Learn the concept

New to this topic? Read the lesson, then come back.

More function programs

← All 39 Function Programs

Factorial function (recursive)

Factorial function (recursive)

EasyFunction Programs · 19 of 39

Write a Python function to get the factorial of a number.

Input
5
Output
120

Approach

  1. n! = n × (n − 1)!, with 0! = 1.

Solution

Python
def factorial(n):    if n <= 1:        return 1    return n * factorial(n - 1) print(factorial(5))
Output
120

Learn the concept

New to this topic? Read the lesson, then come back.

More function programs

← All 39 Function Programs

Function with *args

Function with *args

EasyFunction Programs · 18 of 39

Write a Python function that accepts any number of numbers with *args and prints their cubes.

Input
5, 6, 8, 7
Output
tuple (5, 6, 8, 7)
125 216 512 343

Approach

  1. *args collects the values into a tuple.

Solution

Python
def cubes(*args):    print(type(args).__name__, args)    print(*(n ** 3 for n in args)) cubes(5, 6, 8, 7)
Output
tuple (5, 6, 8, 7)
125 216 512 343

Learn the concept

New to this topic? Read the lesson, then come back.

More function programs

← All 39 Function Programs

HCF function

HCF function

EasyFunction Programs · 17 of 39

Write a Python function to find the HCF of two numbers.

Input
24, 54
Output
6

Approach

  1. Euclid’s algorithm.

Solution

Python
def hcf(a, b):    while b:        a, b = b, a % b    return a print(hcf(24, 54))
Output
6

Line by line

Line 3

(24, 54) → (54, 24) → (24, 6) → (6, 0), so the HCF is 6.

Learn the concept

New to this topic? Read the lesson, then come back.

More function programs

← All 39 Function Programs

Sum from 0 to n

Sum from 0 to n

EasyFunction Programs · 16 of 39

Write a Python function to calculate the sum of numbers from 0 to 10.

Output
55

Approach

  1. sum(range(n + 1)).

Solution

Python
def sum_to(n=10):    return sum(range(n + 1)) print(sum_to())
Output
55

Learn the concept

New to this topic? Read the lesson, then come back.

More function programs

← All 39 Function Programs

LCM function

LCM function

EasyFunction Programs · 15 of 39

Write a Python function to find the LCM of two numbers.

Input
12, 20
Output
60

Approach

  1. LCM = a × b // HCF.

Solution

Python
def hcf(a, b):    while b:        a, b = b, a % b    return a def lcm(a, b):    return a * b // hcf(a, b) print(lcm(12, 20))
Output
60

Learn the concept

New to this topic? Read the lesson, then come back.

More function programs

← All 39 Function Programs

Function inside a function

Function inside a function

EasyFunction Programs · 14 of 39

Write a Python program that defines and uses a function inside another function.

Output
Hello, Asha!

Approach

  1. The inner function is only visible inside the outer one; it can read the outer variables.

Solution

Python
def greet(name):    def make_message():        return f"Hello, {name}!"    return make_message() print(greet("Asha"))
Output
Hello, Asha!

Line by line

Lines 2-3

make_message reads name from the enclosing function.

Learn the concept

New to this topic? Read the lesson, then come back.

More function programs

← All 39 Function Programs