CS101 Assignment1 PDF
CS101 Assignment1 PDF
"#$# &''()*+,*-#
./, .0-,1 23 45-67,8 ##19:;+
Problem 1: Fibonacci Series (30 points)
Write a program that prints the first n terms of the Fibonacci series.
Fibonacci Numbers are : 0 1 1 2 3 5 8 13 21 34
Hint: Every Fibonacci number is the sum of the previous two Fibonacci number (eg., 21 = 8+13, or 34 = 13+21)
Sample run of your program:
Hint: To print numbers besides each other you should use print functions with an extra argument called end. For example to print
the numbers 1 and 2 next to each other, you can use the following code:
print(1, end = )
print(2, end = )
Problem 2: Occurrence of Max Numbers (30 points)
Write a program that reads integers, finds the largest of them, and counts its occurrences. Assume that the input ends with number
0. Suppose that you entered 3 5 2 5 5 5 0; the program finds that the largest is 5 and occurrence count for 5 is 4.
Sample run of your program:
Please enter n: 9
0 1 1 2 3 5 8 13 21 34
Please enter n: 0
0
Enter a number (0 to quit) 3
Enter a number (0 to quit) 5
Enter a number (0 to quit) 2
Enter a number (0 to quit) 5
Enter a number (0 to quit) 5
Enter a number (0 to quit) 5
Enter a number (0 to quit) 0
The largest number is 5
The occurence count of the largest number is 4
Problem 3: Finding the square root of a number (40 points)
In this homework you are asked to write a program which finds an approximate value for the square root of a number with a given
error. You can assume that the error is !"
!!
! !!!!!!".
Lets consider the following example: Assume the entered number is 650, that means the difference between your approximate
solution and actual solution has to be no more than 10
-5
.
We can find the square root of a number as follows:
! ! !"#
The square root of 650 is equivalent to finding the roots of ! ! ! !
!
! !"#. The roots of ! ! can be estimated iteratively
!
!!!
! !
!
!
!
!
!
! !"#
!!
!
You should start with an initial guess (!
!
). Lets guess that the square root of 650 equals !
!
! !"
!
!
! !
!
!
!
!
!
! !"#
!!
!
! !" !
!"
!
! !"#
!"
! !"!!"
!
!
! !
!
!
!
!
!
! !"#
!!
!
! !"!!" !
!"!!"
!
! !"#
!"!!
! !"!!"!#!$
!
!
! !
!
!
!
!
!
! !"#
!!
!
! !"!!"!#!$ !
!"!!"!#!$
!
! !"#
!"!!""#!$
! !"!!"#$%%
!
!
! !
!
!
!
!
!
! !"#
!!
!
! !"!!"#$%% !
!"!!"#$%%
!
! !"#
! ! !"!!"#$%%
! !"!!"#$"%
We stop here because the difference between !
!
and !
!
is less than !"
!!
!
!
! !
!
! !"
!!
Without loss of generality you should iterate until the following condition holds
!
!!!
! !
!
! !"
!!
Finally you should print !
!!!
Note: You are not allowed to use math.sqrt(x) and abs(x) built-in functions.
Sample run of your program:
Please enter a positive number 650
Square root of 650 is 25.495098
Important Notes:
You should submit your homeworks through unilica. If you submit your homework by sending email either to your
instructor or TA, you will get 50% of your total grade.
You will lose 10% for each day they are late and assignments will not be accepted after two days.
We will use software that compares programs to check for similar assignments. All similar assignments will
automatically get zero!
Your assignments will be graded not only for correctness but also for style (5% of your grade). For this assignment,
you will probably need to comment only a few lines.
Here are the guidelines:
Formatting style
1. Use Python style conventions for your function and variable names. This means that you should use lowercase
letters with underscores (_) to improve readability. For example, the variable bag_type is good but bagType is
not.
2. Choose sensible names for your variables. For example, num_seconds is more readable then ns or x.
3. Do not use tab character to indent code. Instead, use four spaces for each indentation level.
4. Put a blank space before and after every operator. For example, the first line is good but the second line is
not:
num_seconds 3 > num_start and num_start != 0
num_seconds-3>num_start and num_start!=0
5. Each line must be less than 80 characters long including spaces. You should break up long lines using \.
6. Include comments (with #) if the code is not obvious.
Comments
1. Use comments to explain tricky spots in the code.
2. Use comments to explain the meaning of a variable, if it is not clear.
3. Use comments to summarize chunks of code. For example, if 5 lines of code as a whole accomplish some
task, it can be helpful to have a short comment summarizing that task.
4. Do not write comments that simply rephrase a line of code in English. For example "Increase n by one" does
not add anything that "n = n + 1" doesn't already say.
Do not write a comment for every line of code. That's too much. Instead, use the guidelines above to decide when a
comment is appropriate
You should rename your files as p1_firstname_lastname_section.py for problem 1. Similarly for problem 2 your file
name should be p2_firstname_lastname_section.py. 5% will be deducted for each incorrect file name. For example
if your name is Fatih Senel and your section is 3A your filenames will be:
p1_Fatih_Senel_3A.py
p2_Fatih_Senel_3A.py
p3_Fatih_Senel_3A.py