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
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))
==========================================================================
You must use recursion to solve each problem. You cannot use loops in this homework. You...
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 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 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 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...
- 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 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 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...
Assignment 2 In this assignment, you will write two short programs to solve problems using recursion. 1. Initial Setup Log in to Unix. Run the setup script for Assignment 2 by typing: setup 2 2. Towers of Hanoi Legend has it that in a temple in the Far East, priests are attempting to move a stack of disks from one peg to another. The initial stack had 64 disks threaded onto one peg and arranged from bottom to top by...
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...
Assignment overview In this assignment, you will implement a simple game of Battleship. If you are unfamiliar with the game Battleship, there are tutorials online describing the game. In short, there are two players that each have a 10 by 10 grid where ships are placed. The players take turns taking shots at each other’s ships. A player wins when they have shot at all spaces on their opponent’s grid that are occupied by ship. To sink a ship, you...