Certainly! Here are 200 problem-solving examples using list comprehension, along with the expected answers:
-
Generate a list of the first 10 natural numbers.
Expected Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
-
Generate a list of the squares of the first 10 natural numbers.
Expected Output: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
-
Generate a list of the even numbers between 1 and 20.
Expected Output: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
-
Generate a list of the odd numbers between 1 and 20.
Expected Output: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
-
Generate a list of the squares of the odd numbers between 1 and 10.
Expected Output: [1, 9, 25, 49, 81]
-
Generate a list of all the vowels in a given string.
Expected Output: ['a', 'e', 'i', 'o', 'u']
-
Generate a list of the ASCII values of all the characters in a given string.
Expected Output: [97, 98, 99, 100, 101] (if the string is "abcde")
-
Generate a list of the squares of all the even numbers between 1 and 10.
Expected Output: [4, 16, 36, 64, 100]
-
Generate a list of the cubes of the numbers divisible by 3 between 1 and 15.
Expected Output: [27, 216, 729]
-
Generate a list of the lengths of the words in a given sentence.
Expected Output: [5, 7, 4] (if the sentence is "Hello, how are you?")
-
Generate a list of the absolute values of the numbers in a given list.
Expected Output: [1, 2, 3, 4, 5] (if the list is [-1, -2, -3, -4, -5])
-
Generate a list of the squares of all the positive numbers in a given list.
Expected Output: [1, 4, 9, 16, 25] (if the list is [-1, 2, -3, 4, -5])
-
Generate a list of the lengths of all the strings in a given list.
Expected Output: [3, 5, 2, 7] (if the list is ["dog", "cat", "go", "elephant"])
-
Generate a list of the first n Fibonacci numbers.
Expected Output: [0, 1, 1, 2, 3] (if n = 5)
-
Generate a list of the numbers divisible by either 2 or 3 between 1 and 15.
Expected Output: [2, 3, 4, 6, 8, 9, 10, 12, 14]
-
Generate a list of the numbers divisible by both 2 and 3 between 1 and 20.
Expected Output: [6, 12, 18]
-
Generate a list of the powers of 2 up to a given exponent.
Expected Output: [1, 2, 4, 8, 16] (if the exponent is 4)
-
Generate a list of the characters in a given string, excluding the vowels.
Expected Output: ['H', 'l', 'l'] (if the string is "Hello")
-
Generate a list of the uppercase letters in a given string.
Expected Output: ['H', 'E', 'L', 'L', 'O'] (if the string is "Hello")
-
Generate a list of the lengths of all the strings in a given list, excluding the strings of length 3.
Expected Output: [5, 7] (if the list is ["cat", "elephant", "dog"])
-
Generate a list of the positive even numbers in a given list.
Expected Output: [2, 10, 4] (if the list is [-2, 10, -4, -5])
-
Generate a list of the negative numbers in a given list.
Expected Output: [-2, -4, -5] (if the list is [-2, 10, -4, -5])
-
Generate a list of the common elements between two given lists.
Expected Output: [2, 4, 6] (if the lists are [1, 2, 3, 4] and [2, 4, 5, 6])
-
Generate a list of the duplicate elements in a given list.
Expected Output: [2, 4] (if the list is [1, 2, 3, 4, 1, 2])
-
Generate a list of tuples, where each tuple contains an element from two given lists.
Expected Output: [(1, 'A'), (2, 'B'), (3, 'C')] (if the lists are [1, 2, 3] and ['A', 'B', 'C'])
-
Generate a list of the ASCII values of all the uppercase letters.
Expected Output: [65, 66, 67, 68] (if the uppercase letters are 'A', 'B', 'C', 'D')
-
Generate a list of the uppercase vowels in a given string.
Expected Output: ['A', 'E', 'O'] (if the string is "Hello, how are you?")
-
Generate a list of the uppercase letters that are present in both the first and last names of a person.
Expected Output: ['A', 'N'] (if the first name is "Adam" and the last name is "Nilson")
-
Generate a list of the words in a given sentence that have more than 3 letters.
Expected Output: ['Hello', 'there'] (if the sentence is "Hello, how are you there?")
-
Generate a list of the elements at the odd indices of a given list.
Expected Output: [2, 4] (if the list is [1, 2, 3, 4, 5])
-
Generate a list of the elements at the even indices of a given list.
Expected Output: [1, 3, 5] (if the list is [1, 2, 3, 4, 5])
-
Generate a list of the words in a given sentence that start with a vowel.
Expected Output: ['elephant', 'is', 'an', 'animal'] (if the sentence is "The elephant is an animal")
-
Generate a list of the prime numbers up to a given integer.
Expected Output: [2, 3, 5, 7] (if the integer is 10)
-
Generate a list of the characters in a given string along with their ASCII values.
Expected Output: ['H: 72', 'e: 101', 'l: 108', 'l: 108', 'o: 111'] (if the string is "Hello")
-
Generate a list of the numbers that are both divisible by 3 and have a digit sum of 9.
Expected Output: [9, 18, 27, 36, 45, 54, 72, 81, 90]
-
Generate a list of the numbers up to a given integer that have the digit 2 in them.
Expected Output: [2, 12, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 32, 42, 52, 62, 72, 82, 92]
-
Generate a list of the numbers that are palindromes up to a given integer.
Expected Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99]
-
Generate a list of the numbers that are both perfect squares and perfect cubes up to a given integer.
Expected Output: [1, 8, 27, 64, 125] (if the integer is 150)
-
Generate a list of the numbers that have exactly 3 factors up to a given integer.
Expected Output: [4, 9, 25, 49, 121] (if the integer is 150)
-
Generate a list of the numbers that are divisible by the sum of their digits up to a given integer.
Expected Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 14, 16, 18, 24, 27, 28, 36, 48, 56, 64, 72, 80, 84] (if the integer is 100)
-
Generate a list of the numbers that have exactly 4 divisors up to a given integer.
Expected Output: [4, 9, 25, 49, 121] (if the integer is 150)
-
Generate a list of the numbers that fulfill the condition (2^n)+1 is a prime number, up to a given exponent.
Expected Output: [3, 5, 17, 257] (if the exponent is 3)
-
Generate a list of the numbers that are factorial numbers up to a given integer.
Expected Output: [1, 2, 6, 24, 120] (if the integer is 150)
-
Generate a list of the numbers that are both triangular numbers and square numbers up to a given integer.
Expected Output: [0, 1, 36] (if the integer is 40)
-
Generate a list of the numbers that are both perfect numbers and prime numbers up to a given integer.
Expected Output: [6] (if the integer is 10)
-
Generate a list of the numbers that are both abundant numbers and odd numbers up to a given integer.
Expected Output: [945, 1575] (if the integer is 2000)
-
Generate a list of the perfect numbers up to a given integer.
Expected Output: [6, 28, 496] (if the integer is 500)
-
Generate a list of the numbers that are divisible by their reverse up to a given integer.
Expected Output: [0, 4, 5, 9, 11, 24, 28, 44, 49, 55, 64, 77, 88, 99, 309, 448, 495, 550, 572, 660, 825, 968] (if the integer is 1000)
-
Generate a list of the numbers that are both triangular numbers and palindromes up to a given integer.
Expected Output: [0, 1, 3, 6, 10, 15, 21, 28, 36, 55, 66, 78, 91, 105, 120, 136, 171, 190, 210, 231, 253, 276, 300, 325, 351, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990] (if the integer is 1000)
-
Generate a list of the numbers that are both perfect numbers and abundant numbers up to a given integer.
Expected Output: [945] (if the integer is 2000)
-
Generate a list of the square-free numbers up to a given integer.
Expected Output: [1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, 23, 26, 29, 30] (if the integer is 30)
-
Generate a list of the numbers that are both positive and negative up to a given integer.
Expected Output: [-1, -2, -3, -4, -5, -6, -7, -8, -9] (if the integer is 9)
-
Generate a list of the numbers that are both positive and divisible by 3 up to a given integer.
Expected Output: [3, 6, 9] (if the integer is 10)
-
Generate a list of the numbers that are both negative and divisible by 3 up to a given integer.
Expected Output: [-3, -6, -9] (if the integer is -10)
-
Generate a list of the numbers that are the sum of the previous two numbers in the Fibonacci sequence up to a given limit.
Expected Output: [1, 2, 3, 5, 8, 13, 21, 34, 55, 89] (if the limit is 100)
-
Generate a list of the numbers that are both positive and divisible by 2, 3, and 5 up to a given integer.
Expected Output: [30, 60, 90] (if the integer is 100)
-
Generate a list of the powers of 3 up to a given exponent.
Expected Output: [1, 3, 9, 27, 81] (if the exponent is 4)
-
Generate a list of the numbers that are both positive and divisible by 2, 3, 5, 7, 11 up to a given integer.
Expected Output: [2310] (if the integer is 2500)
-
Generate a list of the numbers that are both perfect squares and triangular numbers up to a given limit.
Expected Output: [0, 1, 36, 1225] (if the limit is 2000)
-
Generate a list of the letters in a given string that occur more than once.
Expected Output: ['l'] (if the string is 'Hello')
-
Generate a list of the unique numbers in a given list.
Expected Output: [1, 2, 3, 4, 5] (if the list is [1, 1, 2, 3, 4, 5, 5])
-
Generate a list of the pairs of elements from two given lists where the sum of the elements is even.
Expected Output: [(2, 4), (2, 6), (4, 4), (4, 6)] (if the lists are [1, 2, 3, 4] and [2, 4, 5, 6])
-
Generate a list of the indices of all the occurrences of a given element in a given list.
Expected Output: [1, 4] (if the list is [1, 2, 3, 2, 4] and the element is 2)
-
Generate a list of the indices of all the occurrences of a given element in a given list, starting from 1.
Expected Output: [2, 5] (if the list is [1, 2, 3, 2, 4] and the element is 2)
-
Generate a list of the indices of all the vowels in a given string.
Expected Output: [1, 4, 7] (if the string is "Hello")
-
Generate a list of the indices of all the uppercase letters in a given string.
Expected Output: [0, 7] (if the string is "Hello, Bob")
-
Generate a list of the indices of all the occurrences of a given substring in a given string.
Expected Output: [0, 12] (if the string is "Hello, Hello World" and the substring is "Hello")
-
Generate a list of the elements of a given list in reverse order.
Expected Output: [6, 5, 4, 3, 2, 1] (if the list is [1, 2, 3, 4, 5, 6])
-
Generate a list of the reversed words in a given sentence.
Expected Output: ['world', 'hello'] (if the sentence is "Hello world")
-
Generate a list of the reversed strings in a given list.
Expected Output: ['olleh', 'dlrow'] (if the list is ["hello", "world"])
-
Generate a list of the elements in a given list that occur more than once.
Expected Output: [2, 3, 5] (if the list is [1, 2, 3, 2, 4, 5, 5])
-
Generate a list of the sorted elements in a given list.
Expected Output: [1, 2, 2, 3, 4, 5, 5] (if the list is [2, 3, 1, 5, 4, 2, 5])
-
Generate a list of the first n prime numbers.
Expected Output: `[2, 3