Exercise Set 4

Comprehension Questions

Programming Questions

1. Factor Counting Program

Create a program that counts the number of factors of the given input.

2. String Cleaning Program

Write a function that removes non-alphabetical characters from the given string and returns it as a lowercase version of the cleaned string.

cleaner("H E L L 0O!") -> "hello"

Function:

  • 1 argument string

Returns:

  • 1 string object

3. Palindrome Program

Write a function that returns True if the given string argument is a palindrome. Assume that the argument will only contain alphabetical characters.

Example ā†’ 'tacocat' is a palindrome. 'tacodog' is not a palindrome

4. String Consonant Counter

Write a function that counts the number of consonants in the given string argument. Assume that for simplicity, ā€œaeiouā€ are the only set of vowels in English. The function should return an integer

Example ā†’ "blueberry" ā†’ 6 consonants (including y)

5. String Pattern Creator

Write a function that takes an N integer greater than 0 and outputs/prints the following pattern.

N = 1

1

N = 2

1

10

N = 3

1

10

101

N = 4

1

10

101

1010

N = 5

1

10

101

1010

10101

6. Anagram Checker

Create a function that checks if the two strings arguments are anagrams. The function should return True if the two strings are anagrams.

Example:

bored

robed

anagrams

elbow

below

anagrams

jake

jasper

Not anagrams

7. Duplicates between Two Strings

Create a function that takes two string inputs, return a single sorted list of characters that are found in each string.

Example:

hello

goodbye

[ā€˜eā€™, ā€˜oā€™]

jake

jasper

[ā€˜aā€™, ā€˜eā€™, ā€˜jā€™]

8. Mean & Median

Create a function for each statistical analysis tool: mean, and median. Each function should take a single list of integers as an argument, and then output the result.

9. Comma Separated Value to a List & List of Random Integers

Create a function that takes a string argument with a comma separating different integer values. Convert the argument to a list of integers.

Example: "1,2,3,4,5" ā†’ [1,2,3,4,5]

Create another function that takes 3 arguments: (start, end, frequency).

The function should return a list of random [frequency] many integers from [start, end].

10. Removing Duplicates

Create a function that removes duplicates from the given list argument.

Example: [ā€œaā€, ā€bā€, ā€cā€, ā€œcā€, ā€œbā€, ā€œcā€, ā€œaā€, ā€œaā€, ā€œdā€] ā†’ [ā€œaā€, ā€œbā€, ā€œcā€, ā€œdā€]

11. Factor List & Prime Checker

Create a function that returns a list of the integer argument's factor.

Example: 12 returns [1, 2, 3, 4, 6, 12]

Create a function that returns True if the given integer argument is a prime number.

12. String Compression

Implement a function that performs basic string compression using the counts of repeated characters. Return the string if the compression is longer or the same length as the argument.

For example, "aabcccccaaa" would become "a2b1c5a3"

Last updated