Question
Hello All, I'm taking a python course at my local community college and just got my first assignment. I haven't taken any python before but
Hello All,
I'm taking a python course at my local community college and just got my first assignment. I haven't taken any python before but the instructor insists on only teaching his curriculum regardless of where the people in the class are with the language. So I'm needing some help. I will post the assignment below. From what I can tell it looks like the lines with dots need to be replaced with code.
CORRECT THE FOLLOWING TEN A to J PYTHON CODE, RUN FOR THE CORRECT OUTPUT AND PROVIDE ONE LINE EXPLANATION
A.# Writing Python code for Checking a given number to be prime or not
num = 1729
# To take input from the user num = int(input("Enter a number: "))
# define a flag variable
flag = False
if num == 1:
print(num, "is not a prime number")
elif num > 1:
# check for factors
for ---------:
if (num % i) == 0:
# if factor is found, set flag to True
flag = True
# break out of loop
break
# check if flag is True
if flag:
print(num, "is not a prime number")
else:
print(num, "is a prime number")
Output: 1729 is not a prime number
B.# Write a Python code for checking a given number to be prime or not, if not what are its factors?
num = 407
# To take input from the user num = int(input("Enter a number: "))
if num == 1:
print(num, "is not a prime number")
elif num > 1:
# check for factors
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
print(--------------------------------------)
break
else:
print(num,"is a prime number")
# if input number is less than or equal to 1, it is not prime
else:
print(num,"is not a prime number")
Output: 407 is not a prime number
11 times 37 is 407
C.# Python program to display all the prime numbers within an interval
lower = 859
upper = 1256
print("Prime numbers between", lower, "and", upper, "are:")
for num in range(lower, upper + 1):
# all prime numbers are greater than 1
if num > 1:
for ----------------:
if (------) == 0:
break
else:
print(num)
# D. Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (----------------))
# change the value for a different result
num = 9
# to take input from the user num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
Output: The factorial of 9 is 362880
# E. Sum of natural numbers up to num
num = 16
if num < 0:
print("Enter a positive number")
else:
sum = 0
# use while loop to iterate until zero
while(-------):
sum += num
num -= 1
print("The sum is", sum)
Output: The sum is 136
#F. Write a Python Code for calculating area of a circle given the radius.
pi=22/7
radian = float(input("Input radians: "))
degree = ----------
print(degree)
Output:
Input radians: 56
3207.2727272727275
# G.Write a Python program to calculate the surface volume and area of a cylinder. Note: A cylinder is one of the most basic curvilinear geometric shapes, the surface formed by the points at a fixed distance from a given straight line, the axis of the cylinder.
#
pi=22/7
height = float(input('Height of cylinder: '))
radian = float(input('Radius of cylinder: '))
volume = pi * radian * radian * height
sur_area = ((-----------) * height) + ((-------------)*2)
print("Volume is: ", volume)
print("Surface Area is: ", sur_area)
Output:
Height of cylinder: 23
Radius of cylinder: 12
Volume is: 10409.142857142857
Surface Area is: 2640.0
>
# H. Printing the current date and time.using Python
import datetime
now = datetime.datetime.now()
print (-----------)
print (now.strftime("%Y-%m-%d %H:%M:%S"))
I. Write a Python program to check whether a given binary tree is a valid binary search tree (BST) or not. Let a binary search tree (BST) is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater than the node's key. Both the left and right subtrees must also be binary search trees.
Example 1:
2
/ \
1 3
Binary tree [2,1,3], return true.
Example 2:
1
/ \
2 3
Binary tree [1,2,3], return false.
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def is_BST(root):
stack = []
prev = None
while root or stack:
while root:
-------
root = root.left
root = stack.pop()
if prev and root.val <= prev.val:
return False
prev = root
--------------
return True
root = TreeNode(2)
root.left = TreeNode(1)
root.right = TreeNode(3)
result = is_BST(root)
print(result)
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
result = is_BST(root)
print(result)
Output:
True
False
>
J. Write a Python program for sequential search. Sequential Search: In computer science, linear search or sequential search is a method for finding a particular value in a list that checks each element in sequence until the desired element is found or the list is exhausted. The list need not be ordered.
Sample Solution:
Python Code:
def Sequential_Search(dlist, item):
pos = 0
found = False
while pos < len(dlist) and not found:
if -------------:
found = True
else:
-------------
return found, pos
print(Sequential_Search([11,23,58,31,56,77,43,12,65,19],31))
Output:
(True, 3)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started