0% found this document useful (0 votes)
7 views2 pages

Essential Python Coding Challenges

The document lists various programming tasks and functions to be implemented, including merging sorted lists, finding non-repeating characters, and calculating the greatest common divisor. It also includes examples of input and expected output for some tasks. The tasks cover a range of topics from string manipulation to mathematical calculations.

Uploaded by

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

Essential Python Coding Challenges

The document lists various programming tasks and functions to be implemented, including merging sorted lists, finding non-repeating characters, and calculating the greatest common divisor. It also includes examples of input and expected output for some tasks. The tasks cover a range of topics from string manipulation to mathematical calculations.

Uploaded by

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

Question Date

Merge Two Sorted Lists 03-04-2025


Find the First Non-Repeating Character
Count Occurrences of Each Character in a String
Check for Anagrams
Find the Factorial of a Number
Reverse a String
Check for Palindrome
Find the Second Largest Number in a List:
Find Missing Number in an Array (1 to n):
Remove Duplicates from a List:
Fibonacci
Count Sequence:
Vowels in a String
Write a function to calculate the sum of the digits of a given integer.
Input:
# Write123a function to determine if a given string consists only of
# Write a digits
numeric program(e.g.,
to "123" → True,
capitalize "12a3"
the first →ofFalse).
letter each 08-04-2025
word
# Writein a
a function
sentenceto(e.g., "hellothe
calculate world" → "Hello
Greatest World").
Common Divisor
# Write a function that takes a nested list and returns a 18 is 6).
(GCD) of two integers using a loop (e.g., GCD of 48 and
single
returnsflattened
all pairs list (e.g., [1, that
of numbers [2, 3],
add[4,up
[5]]]
to → [1, 2, 3, 4, 5])
the target
Write (e.g., that
a function [2, 4,returns
3, 5] and
the target 7 → [(2, in
first character 5), (4, 3)])
a string
from that does
shortest not repeat (e.g., "swiss" → "w").
to longest
(e.g., ["cat", "elephant", "dog"] → ["cat", "dog", "elephant"]).
isdigit(),
isalpha(), we can use
2. title() using builtin method
[Link](i)
max(l)
return k
dic[value] = index
returnreturn
pair i
s[j], s[j+1] = s[j+1],s[j]
return s

Common questions

Powered by AI

Merging two sorted lists involves comparing the elements of both lists one-by-one, starting from the first element in each list. The smaller element is added to the merged list, and the process continues by advancing the index of the list from which the smaller element was taken. This continues until elements from one list are exhausted, after which the remaining elements from the other list are appended. This efficient O(n) process maintains sorted order throughout .

To count occurrences of each character in a string, you can use a dictionary where keys represent characters and values represent counts. Iterate over each character in the string, updating the dictionary by incrementing the count for each character encountered. This approach provides an efficient O(n) way of mapping each character to its count in the string .

Finding the GCD using a loop is significant because it provides an iterative approach to solving the problem, which can be more intuitive for those familiar with basic control structures. Additionally, loops can handle large integers more efficiently in some programming environments compared to recursive approaches, which are limited by recursion depth .

To flatten a nested list, a recursive approach can be used where you iterate over each element of the list, checking if it is itself a list. If it is, recursively flatten it and extend the result to the main list. If it’s not a list, append it. This method ensures a thorough flattening of arbitrarily nested lists into a single-level list .

To check if two strings are anagrams, a common approach is to sort both strings and compare them. If they are identical after sorting, then they are anagrams. Alternatively, you can count the frequency of each character in both strings using a dictionary; if the dictionaries are identical, the strings are anagrams. These methods exploit the property that anagrams contain the same characters in different orders .

To find the first non-repeating character in a string, you can use a two-pass algorithm. First, iterate through the string to count the occurrences of each character and store them in a hash map or dictionary. Next, iterate through the string again, checking the count of each character using the dictionary. The first character with a count of one is the first non-repeating character .

To capitalize the first letter of each word in a sentence, you can use the `title()` method, which automatically converts the first letter of each word to uppercase and the rest to lowercase. For example, 'hello world' would be converted to 'Hello World' .

To remove duplicates from a list, one efficient method is to convert the list into a set, which automatically removes duplicates due to its property of containing only unique elements. Then, convert the set back into a list. This method leverages the unique storage capability of sets and is effective for lists where preserving order doesn't matter or can be reconstructed subsequently .

To determine if a string consists only of numeric digits, the function can use the `isdigit()` method, which returns True if all characters in the string are digits. For example, the string '123' would return True while '12a3' would return False using `isdigit()` .

To find the second largest number in a list, one approach is to traverse the list twice: first to find the maximum value, and second to find the largest value that is smaller than the max. Alternatively, you can maintain two variables in a single pass: one for the maximum and one for the second maximum, updating them accordingly to ensure the second variable always holds the second largest unique number .

You might also like