Starting from:

$34.99

CS104 Lab 5 Solution



1. Write a method named “isPalindrome” which takes as parameter a string and returns True if input string is a palindrome; False otherwise. Capitalization can be ignored by using ‘lower()’ method of a given string.

2. In number theory, a “perfect number” is a positive integer that is equal to the sum of its positive divisors (excluding the number itself). If sum of its positive divisors is greater than the number itself, such number is called “abundant number”. Lastly, number is called a “deficient number” when the sum of its positive divisors is less than the number itself.

Write a program that prints out all the perfect numbers between 0 and inputted range. Additionally, it prints out the number of abundant and deficient numbers.

3. Write a program that gets two positive numbers as input and returns a list containing all common divisors of n1 and n2.

4. Write a method named transpose that gets a table as a multidimensional array, and returns the copy of table with rows and columns swapped.

Example:
1 2 1 3 5
3 4 => 2 4 6
5 6

5. You will build a simplified, one-player version of the classic board game Battleship! In this version of the game, there will be a single ship hidden in a random location on a 5x5 grid. The player will have 3 guesses to try to sink the ship. At each iteration, you will show user the missed location by x symbol in a grid. You can use “randint” method we have seen in previous labs to generate random locations. Its sample execution will look like below:

Let's play Battleship!
O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
Guess Row:1
Guess Col:2
You missed my battleship!
Turn: 1
O O O O O
O O X O O
O O O O O
O O O O O
O O O O O
Guess Row:3 Guess Col:2 You missed my battleship!
Turn: 2
O O O O O
O O X O O
O O O O O
O O X O O
O O O O O
Guess Row:3
Guess Col:3
Game Over
Turn: 3
O O O O O
O O X O O
O O O O O
O O X O O
O O O O O



More products