You are on page 1of 6

chapter04 - 飞桨AI Studio - 人工智能学习实训社区 https://aistudio.baidu.

com/aistudio/projectdetail/3140925

Projects Datasets Courses Competitions Certification More Forum PaddlePaddle 中 | En

Education > My Course > Python Programming > [作业]chapter04

chapter04
提交作业
学生: AL THAMARANI GHADIR ALI MOHAMMED 课程: Python Programming 提交次数: 1 /10 上次提交时间: 2021-11-28 17:31
学生提交截止时间: 2021-11-28 23:59(剩余0天6时30分)

1 of 6 11/28/2021, 5:30 PM
chapter04 - 飞桨AI Studio - 人工智能学习实训社区 https://aistudio.baidu.com/aistudio/projectdetail/3140925

作业说明 作业内容 Projects Bg Task


Datasets Deployment
Courses Competitions Certification More Enter Forum
Stop Running Create Service
PaddlePaddle 中 | En

HOMEWORK 2021-11-28 17:30:31 新版Notebook- BML CodeLab上线,点击项目修改可切换为新版进行体验

查看教师发布的版本

1. Code to calculate the greatest common divisor of 2 integer (user input).


请选择预览文件

In [ ] # Recursive function to return gcd of a and b


当前Notebook没有标题
def gcd(a, b):

# Everything divides 0
if (a == 0):
return b
if (b == 0):
return a

# base case
if (a == b):
return a

# a is greater
if (a > b):
return gcd(a-b, b)
return gcd(a, b-a)

# Driver program to test above function


a = int(input("Enter 1st number: "))
b = int(input("Enter 2nd number: "))
if(gcd(a, b)):
print('GCD of', a, 'and', b, 'is', gcd(a, b))
else:
print('not found')

2. Find out the first power of 7 larger than 50, output both the power and product.

In [ ] base = 7
exponent = 50

result = pow(base, exponent)

print("Answer = " + str(result))

3. Calculate the sum of 1 to 100,and the odd and even respectively

In [ ] even_Sum=0
odd_Sum=0
for num in range(1,100):
if (num%2==0):
even_Sum=even_Sum+num
else:
odd_Sum=odd_Sum+num
print("The sum of Even numbers 1 to 100",even_Sum)
print("The sum of odd numbers 1 to {0} = {1}",odd_Sum)

4. Calculate the factorial of n

In [ ] import math
n = int(input("Enter the n number: "))
print ("The factorial of n umber ",n )
print (math.factorial(n))

5. Calculate the class average with unknown student’s amount. stop the input while user key in a
negative score.

In [ ] NUMBER_OF_GRADES = 3

file =open('grades1.in.txt').read().split('\n')
for scores in file:
name_numbers = (scores.split('_'))

def averages ():


for numbers in file:
sum=0
numbers = split("\n")

2 of 6 11/28/2021, 5:30 PM
chapter04 - 飞桨AI Studio - 人工智能学习实训社区 https://aistudio.baidu.com/aistudio/projectdetail/3140925

Projects Datasets Courses Competitions Certification More Forum PaddlePaddle 中 | En

for num in numbers:


sum = sum + int(num)
averages = sum/NUMBER_OF_GRADES
print ('% has an average grade of %.1') %(name, averages)

6. Use the following approximate formula to evaluate the base e of the natural logarithm until the
absolute value of the last term is less than

In [ ] import math

x = 2
e_to_2 = x**0/math.factorial(0) + x**1/math.factorial(1) + x**2/math.factorial(
print(e_to_2)

7. Use Newton’s method to computing square root

In [ ] # Python3 implementation of the approach

# Function to return the square root of


# a number using Newtons method
def squareRoot(n, l) :

# Assuming the sqrt of n as n only


x = n

# To count the number of iterations


count = 0

while (1) :
count += 1

# Calculate more closed x


root = 0.5 * (x + (n / x))

# Check for closeness


if (abs(root - x) < l) :
break

# Update root
x = root

return root

# Driver code
if __name__ == "__main__" :

3 of 6 11/28/2021, 5:30 PM
chapter04 - 飞桨AI Studio - 人工智能学习实训社区 https://aistudio.baidu.com/aistudio/projectdetail/3140925

Projects Datasets Courses Competitions Certification More Forum PaddlePaddle 中 | En

for i in range(0, len(string)):


if(string[i] != ' '):
count = count + 1;

#Displays the total number of characters present in the given string


print("Total number of characters in a string: " + str(count));

11. Output the first 20 entries of Fibonacci Sequence:1,1,2,3,5,8,…with 4 entries per line

In [ ] # Program to display the Fibonacci sequence up to n-th term

nterms = int(input("How many terms? "))

# first two terms


n1, n2 = 0, 1
count = 0

# check if the number of terms is valid


if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")

4 of 6 11/28/2021, 5:30 PM
chapter04 - 飞桨AI Studio - 人工智能学习实训社区 https://aistudio.baidu.com/aistudio/projectdetail/3140925

Projects Datasets Courses Competitions Certification More Forum PaddlePaddle 中 | En

case 5:

5 of 6 11/28/2021, 5:30 PM
chapter04 - 飞桨AI Studio - 人工智能学习实训社区 https://aistudio.baidu.com/aistudio/projectdetail/3140925

Projects Datasets Courses Competitions Certification More Forum PaddlePaddle 中 | En

About AI Studio Resources Education Zone Contact Us

AI Studio is a one-stop online development


User Guide Introduction Email: aistudio@baidu.com
platform based on PaddlePaddle, Baidu self-
developed deep learning framework.
FAQ Document QQ: 580959619

View   Application

PaddlePaddle Service on
WeChat

Baidu Products: PaddlePaddle | PaddlePaddle Github | Baidu Developer Center | ABC Institute | Baidu Institute of Technology | Baidu Xiaolvyun | Dianshi | AI Studio Terms |

© Baidu Terms

6 of 6 11/28/2021, 5:30 PM

You might also like