Computer Science Homework Help

Page 1

Computer Science Homework Help Visit: www.computersciencehomeworkhelper.com Email: support@computersciencehomeworkhelper.com WhatsApp/ Call: +1(315)557-6473

Question 1: Write a Python function to compute the factorial of a non-negative integer using recursion.

Answer: Here's an implementation of a recursive function to compute the factorial of a non-negative integer: def factorial(n):

if n == 0: return 1

else: return n * factorial(n-1)

This function takes in a non-negative integer n and returns the factorial of n. If n is 0, the function returns 1 (since 0! is defined to be 1). Otherwise, the function recursively calls itself with n-1 as the argument and multiplies the result by n. This continues until n reaches 0 and the function starts returning values.

Here's an example of how to use the function:

>>> factorial(5) 120 >>> factorial(0) 1 >>> factorial(10)

3628800

Visit: www.computersciencehomeworkhelper.com

Email: support@computersciencehomeworkhelper.com WhatsApp/ Call: +1(315)557-6473

Note that the recursive approach used here can be computationally expensive for very large values of n, as the function will continue to call itself until it reaches the base case. In practice, it's often more efficient to use an iterative approach to compute factorials.

Question: Write a Python program that accepts a string and calculates the number of digits and letters in the string.

Answer: Here's an implementation of a Python program that accepts a string and calculates the number of digits and letters in the string:

def count_digits_letters(string):

num_digits = 0

num_letters = 0 for char in string: if char.isdigit():

num_digits += 1 elif char.isalpha():

num_letters += 1

return num_digits, num_letters

Visit: www.computersciencehomeworkhelper.com

Email: support@computersciencehomeworkhelper.com

WhatsApp/ Call: +1(315)557-6473

This program takes in a string as input and initializes two variables, num_digits and num_letters, to 0. It then iterates over each character in the string and uses the isdigit() and isalpha() methods to check if the character is a digit or a letter, respectively. If the character is a digit, num_digits is incremented. If the character is a letter, num_letters is incremented. Finally, the program returns a tuple containing the counts of digits and letters. Here's an example of how to use the program:

>>> count_digits_letters("Hello, world! 123")

(3, 10)

>>> count_digits_letters("Python is awesome 42")

(2, 16)

>>> count_digits_letters("")

(0, 0)

In the first example, the string contains 3 digits and 10 letters. In the second example, the string contains 2 digits and 16 letters. In the third example, the string is empty and contains no digits or letters.

Visit: www.computersciencehomeworkhelper.com
support@computersciencehomeworkhelper.com WhatsApp/ Call: +1(315)557-6473
Email:

Question: Write a Python function that takes in a list of integers and returns a new list that contains only the even numbers from the original list.

Answer: Here's an implementation of a Python function that takes in a list of integers and returns a new list that contains only the even numbers from the original list: def get_even_numbers(numbers): even_numbers = [] for number in numbers: if number % 2 == 0:

even_numbers.append(number)

return even_numbers

This function takes in a list of integers as input and initializes an empty list called even_numbers. It then iterates over each number in the input list and uses the modulo operator (%) to check if the number is even (i.e., divisible by 2). If the number is even, it is added to the even_numbers list using the append() method. Finally, the function returns the even_numbers list.

Here's an example of how to use the function:

Visit: www.computersciencehomeworkhelper.com

Email: support@computersciencehomeworkhelper.com

WhatsApp/ Call: +1(315)557-6473

>>> get_even_numbers([1, 2, 3, 4, 5, 6]) [2, 4, 6]

>>> get_even_numbers([10, 20, 30, 40, 50]) [10, 20, 30, 40, 50]

>>> get_even_numbers([])

In the first example, the function returns a list containing the even numbers (2, 4, and 6) from the input list. In the second example, the function returns the entire input list since all the numbers are even. In the third example, the input list is empty and the function returns an empty list.

Visit: www.computersciencehomeworkhelper.com Email: support@computersciencehomeworkhelper.com

WhatsApp/ Call: +1(315)557-6473

[]

Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.