Question

You must use recursion to solve each problem. You cannot use loops in this homework. You...

You must use recursion to solve each problem. You cannot use loops in this homework. You cannot import any module. A couple of the tasks have individual restrictions; note them, as we will remove points for any task that does not follow the requirements.

def factorial_evens(num): Implement a function to calculate and return the product of all even numbers from 1 up to num (inclusive if num is even).
o Assumption: num will be an integer greater than or equal to 1.
o Examples:
▪ factorial_evens(4) → 8 # 2*4 = 8
▪ factorial_evens(7) → 48 # 2*4*6 = 48
▪ factorial_evens(1) → 1

def power_of_three(num): Implement a function that determines whether or not a number is a power of three, and returns True or False accordingly
o Assumption: num will be an integer greater than or equal to 1.
o Examples:
▪ power_of_three(1) → True # 3 ** 0
▪ power_of_three(5) → False # not a power of 3
▪ power_of_three(9) → True # 3 ** 2

def snake(grid, row, col): Implement a function to calculate the longest "snake" of ones that can be made from a starting position in a grid. A snake is made by moving right and down from a starting position as far as possible.
o Assumption: grid will be at least 1x1, row and col will each be valid indexes in grid
o Restrictions: You may not use the max() or min() functions.
o Examples: (cells is a 2D list below)

▪ snake(cells,0,0) → 1 # can't move in either direction
▪ snake(cells,0,2) → 2 # can go down one, then no more moves
▪ snake(cells,1,1) → 5 # all the way down, and then right

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question, here are the first two functions in python, in the 3rd question the question does not states what is the initial dimension of the cells 2D array, hencce its not very clear to write the logic.

The other two functions is working fine.
===========================================================================
def factorial_evens(num):
    if num<=1:
        return 1
    else:
        if num%2==1:
            return factorial_evens(num-1)
        else:
            return num*factorial_evens(num-1)

a=factorial_evens(1)
print(a)

===========================================================================

import math
def power_of_three(num):
    exp=math.log(num,3)
    exp_int=int(exp)
    return exp-exp_int==0


print(power_of_three(5))

==========================================================================

Add a comment
Know the answer?
Add Answer to:
You must use recursion to solve each problem. You cannot use loops in this homework. You...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Def snake (grid, row, co1): Implement a function to calculate the longest "snake" of ones that ca...

    def snake (grid, row, co1): Implement a function to calculate the longest "snake" of ones that can be made from a starting position in a grid. A snake is made by moving right and down from a starting position as far as possible. o Assumption: grid will be at least 1x1, row and col will each be valid indexes in grid o Restrictions: You may not use the max() or min) functions. o Examples: (cells is a 2D list below)...

  • I need help with the following and written in c++ thank you!: 1) replace the 2D...

    I need help with the following and written in c++ thank you!: 1) replace the 2D arrays with vectors 2) add a constructor to the Sudoku class that reads the initial configuration from a file 3) adds a function to the Sudoku class that writes the final Sudoku grid to a file or to the standard output device, cout. Sudoku.h #pragma once /* notes sudoku() default constructor precondition : none postcondition: grid is initialized to 0 sudoku(g[][9]) 1-parameter constructor precondition...

  • Hi. Please help me solve this in python using only while loops, if statements. Can't use...

    Hi. Please help me solve this in python using only while loops, if statements. Can't use for loops and lists. In this programming assignment, you will be writing a program that prints out several shapes via text. In this PA, you will be required to use functions and parameters. This is both to reduce redundancy and to make your code clean and well-structured. Name your program shaper.py Your program should have the capability to print out three different shapes: An...

  • You are going to be implementing the classic computer science simulation, Conway's Game of Life. Conway's...

    You are going to be implementing the classic computer science simulation, Conway's Game of Life. Conway's Life is played on a matrix of cells, kind of like a chess board but theoretically extending infinitely in every direction. Each individual cell in the matrix can either be alive or dead. A live cell in the matrix is shown in our simulation by printing an asterisk (*) to the screen. A dead cell is shown by leaving that area of the matrix...

  • In this part, you will complete the code to solve a maze.

    - Complete the code to solve a maze- Discuss related data structures topicsProgramming-----------In this part, you will complete the code to solve a maze.Begin with the "solveMaze.py" starter file.This file contains comment instructions that tell you where to add your code.Each maze resides in a text file (with a .txt extension).The following symbols are used in the mazes:BARRIER = '-' # barrierFINISH = 'F' # finish (goal)OPEN = 'O' # open stepSTART = 'S' # start stepVISITED = '#' #...

  • This is my code for my game called Reversi, I need to you to make the...

    This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...

  • In this puzzle, you can bounce between the two 3’s, but you cannot reach any other...

    In this puzzle, you can bounce between the two 3’s, but you cannot reach any other squares. Write a function bool Solvable(int start, int[] squares) that takes a starting position of the marker along with the array of squares. The function should return true if it is possible to solve the puzzle from the starting configuration, and false if it is impossible. You may assume all the integers in the array are positive except for the last entry, the goal...

  • Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’,...

    Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’, the goal is to assign digits (from 1 to 9) to the empty cells so that every row, column, and subgrid of size 3×3 contains exactly one instance of the digits from 1 to 9. I have posted 3 input files: sudoku1.txt, sudoku2.txt and sudoku3.txt Problem Analysis & Design - Think about how you will need to solve this problem. You should do an...

  • use python to solve this problem Problem 3- contains.py: Sub-list containment You will write a function...

    use python to solve this problem Problem 3- contains.py: Sub-list containment You will write a function to determine whether a given sub-list" s appears within another given list 1. The problem will guide you through the problem breakdown. Essentially, first you can solve the easier problem if the "sub-list" s appears within 1 at a specific position i. Then Practice goal: Guided practice with modular thinking and programming. Problem 3a Partial equality [5 points] Write a function contains_at that accepts...

  • I only need the "functions" NOT the header file nor the main implementation file JUST the impleme...

    I only need the "functions" NOT the header file nor the main implementation file JUST the implementations for the functions Please help, if its difficult to do the complete program I would appreciate if you could do as much functions as you can especially for the derived class. I am a beginer so I am only using classes and pointers while implementing everything using simple c++ commands thank you in advanced Design and implement two C++ classes to provide matrix...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT