Python Problem Solving Questions to Boost Your Skills

  • Post author:
  • Post comments:0 Comments
  • Reading time:119 mins read

1. Write a function that takes a list of numbers and returns the sum of the even numbers in the list.

  • The first problem asks you to write a function that takes a list of numbers and returns the sum of the even numbers in the list. This problem tests your understanding of looping through a list and using an if statement to check for even numbers. To solve this problem, you will need to iterate through the list of numbers and check if each number is even by using the modulus operator (%). If the number is even, you should add it to a running total. After looping through the entire list, you should return the total sum of the even numbers.

Here is an example of how the function could be used:

numbers = [1, 2, 3, 4, 5, 6]

result = sum_even_numbers(numbers)

print(result)  # Output: 12

Here is an answer to the first question:


def sum_even_numbers(numbers):
    sum = 0
    for number in numbers:
        if number % 2 == 0:
            sum += number
    return sum

2. Write a function that takes a string and returns the number of vowels in the string

  • The second problem asks you to write a function that takes a string and returns the number of vowels in the string. This problem tests your ability to iterate through a string and check if each character is a vowel. To solve this problem, you will need to define a string containing all the vowels (both uppercase and lowercase) and then loop through the input string, checking if each character is in the vowels string. If a character is a vowel, you should increment a counter. After looping through the entire string, you should return the vowel count.

Here is an example of how the function could be used:


string = "Hello World!"

result = count_vowels(string)

print(result)  # Output: 3

Here is an answer to the second question


def count_vowels(string):
    vowels = 'aeiouAEIOU'
    count = 0
    for char in string:
        if char in vowels:
            count += 1
    return count

3. Write a program that reads a CSV file and prints the sum of the values in the second column

  • The third problem asks you to write a program that reads a CSV file and prints the sum of the values in the second column. This problem tests your understanding of reading and parsing CSV files, as well as your ability to sum a column of values. To solve this problem, you will need to use the CSV module to read the file and parse it into rows, and then loop through the rows, adding the value in the second column to a running total. After looping through the entire file, you should print the total sum.

Here is an example of how the program could be used:


sum_column("data.csv")  # Output: 125

Here is an answer to the third problem


import csv

def sum_column(file):
    with open(file, 'r') as f:
        reader = csv.reader(f)
        sum = 0
        for row in reader:
            sum += int(row[1])
        return sum

4. Write a function that takes a list of strings and returns a new list with all strings in uppercase

  • The fourth problem asks you to write a function that takes a list of strings and returns a new list with all strings in uppercase. This problem tests your ability to use list comprehension to transform a list of strings. To solve this problem, you will need to use a list comprehension to iterate through the input list and return a new list with each string converted to uppercase.

Here is an example of how the function could be used:


strings = ["hello", "world", "Python"]

result = to_uppercase(strings)

print(result)  # Output: ["HELLO", "WORLD", "PYTHON"]

Here is an answer to the fourth question


def to_uppercase(strings):
    return [string.upper() for string in strings]

5. Write a program that reads a file and prints the number of lines in the file

  • The fifth problem asks you to write a program that reads a file and prints the number of lines in the file. This problem tests your ability to read a file and count the number of lines in the file. To solve this problem, you will need to use the readlines() method to read the file and store the lines in a list, and then return the length of the list.

Here is an example of how the program could be used:


count_lines("data.txt")  # Output: 5

Here is an answer to the fifth question:


def count_lines(file):
    with open(file, 'r') as f:
        lines = f.readlines()
        return len(lines)

6. Write a function that takes a list of integers and returns the largest number in the list.

The sixth problem asks you to write a function that takes a list of integers and returns the largest number in the list. This problem tests your understanding of the max() function and your ability to pass a list as an argument to a function. To solve this problem, you will simply need to return the result of calling the max() function on the input list.

Here is an example of how the function could be used:


numbers = [1, 2, 3, 4, 5]

result = find_largest(numbers)

print(result)  # Output: 5

Here is an answer to the sixth question:


def find_largest(numbers):
    return max(numbers)

7. Write a program that reads a file and prints the number of words in the file.

  • The seventh problem asks you to write a program that reads a file and prints the number of words in the file. This problem tests your ability to read a file, split the contents into words, and count the number of words. To solve this problem, you will need to use the read() method to read the contents of the file into a string, and then use the split() method to split the string into a list of words. You can then return the length of the list to get the number of words.

Here is an example of how the program could be used:


count_words("data.txt")  # Output: 20

Here is an answer to this question:


def count_words(file):
    with open(file, 'r') as f:
        words = f.read().split()
        return len(words)

8. Write a function that takes a list of numbers and returns the median value

  • The eighth problem asks you to write a function that takes a list of numbers and returns the median value. This problem tests your understanding of the sort() method and your ability to handle lists of odd and even lengths. To solve this problem, you will need to first sort the input list using the sort() method, and then use an if statement to check if the length of the list is odd or even. If the length is odd, you should return the middle element of the list. If the length is even, you should return the average of the two middle elements.

Here is an example of how the function could be used:


numbers = [3, 5, 1, 2, 4]

result = find_median(numbers)

print(result)  # Output: 3

Here is an answer to this question:


def find_median(numbers):
    numbers.sort()
    if len(numbers) % 2 == 0:
        return (numbers[len(numbers) // 2] + numbers[len(numbers) // 2 - 1]) / 2
    else:
        return numbers[len(numbers) // 2]

9.Write a program that reads a file and prints the longest line in the file

  • The ninth problem asks you to write a program that reads a file and prints the longest line in the file. This problem tests your ability to read a file, split the contents into lines, and find the longest line. To solve this problem, you will need to use the readlines() method to read the file and store the lines in a list, and then loop through the list, keeping track of the longest line so far. After looping through the entire list, you should print the longest line.

Here is an example of how the program could be used:


find_longest_line("data.txt")  # Output: "This is the longest line in the file."

Here is an answer to this question:


def find_longest_line(file):
    with open(file, 'r') as f:
        lines = f.readlines()
        longest_line = ''
        for line in lines:
            if len(line) > len(longest_line):
                longest_line = line
        return longest_line

10. Write a function that takes a list of strings and returns a new list with all strings that have a length greater than 4

  • The tenth problem asks you to write a function that takes a list of strings and returns a new list with all strings that have a length greater than 4. This problem tests your understanding of list comprehensions and your ability to filter a list based on a condition. To solve this problem, you will need to use a list comprehension to iterate through the input list and return a new list with only the strings that have a length greater than 4.

Here is an example of how the function could be used:


strings = ["short", "longer", "longest", "shortest"]

result = filter_long_strings(strings)

print(result)  # Output: ["longer", "longest"]

Here is an answer to this question:


def filter_long_strings(strings):
    return [string for string in strings if len(string) > 4]

In conclusion, the Python problem solving questions presented in this exercise are designed to test your skills in various areas of Python programming, including looping, conditionals, file handling, and list comprehensions. These types of problems are common in coding interviews and can also be useful for practicing and improving your coding skills. By working through these problems and understanding the solutions, you can gain a deeper understanding of Python and become a more confident and proficient programmer.

Publisher

Publisher

Publisher @ideasorblogs

Leave a Reply