Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

University of Science and Technology

Communications & Information Engineering Program


CSCI 101: Introduction to Computer Science
Functions (Modular Programming)

1. What is wrong with the following function? Suggest how to fix it.
The function is supposed to calculate and return the sum of items of a list x.

def myfun1(x)
x = input(“Enter List x:”).split()
s=0
for i in range(1,11):
s = s+x[i]
return y

2. What will be the return of foo1(5) given the following function definitions?
def foo1(x)
a=foo2(x*2)
return a

def foo2(x)
y= x+5
return y

3.
a. The following function takes an angle in degrees and returns angle type
(zero, acute, right, obtuse, straight, reflex, or complete ). Complete the
function code

def angleType( A ) #A is from 0 to 360


if A==0:
return ‘zero’
elif A<90:
return ‘acute’
elif …………….:
return ‘right’
…………………. #complete the code to cover all cases
………………….
University of Science and Technology
Communications & Information Engineering Program
CSCI 101: Introduction to Computer Science
Functions (Modular Programming)

b. Write a function called GeneralAngleType that takes an angle B and


returns its type as follows:
i. if B < 0 the function returns “negative”
ii. if 0 <= B <= 360, the function returns the corresponding type by
calling function angleType (from part a above)
iii. if B > 360, calculate the corresponding angle by dropping all
revolutions (all 360s) and find the remainder first, then return the
angle type. For example, if B = 725, so by dropping 720 (2*360),
the remainder is 5 and the function should return ‘acute’

4. Write a program that calls a function in the main script to reverse a string. Your
function is in the same script as the main script.
Hint: We did this problem before, but here we need to make a function that
reads a given string from a file and returns the reversed one, then saves the
new string in a new file.
Example:
Firstly, you must have 2 files, one to read from, and the other to write your
result in.
Then, do not forget to read from the first file!😶
Your string that is already written the first file is ‘1234abcd’
The reversed string is ‘dcba4321’
Lastly, do not forget to write the reversed string in the second file! 😉
5. Write a function that will receive as an input argument a temperature in
degrees Fahrenheit, and will return the temperature in both degrees Celsius
and Kelvin. The conversion factors are C = (F – 32) * 5/9 and K = C + 273.15.
Write a script (i.e. which is the scratch file as we know in Pycharm) to use the
developed function. The used function is in a separate script other than the
main script, you should import it first.
University of Science and Technology
Communications & Information Engineering Program
CSCI 101: Introduction to Computer Science
Functions (Modular Programming)

6. A vector can be represented by its rectangular coordinates x and y or by its


polar coordinates r and theta. For positive values of x and y, the conversions
from rectangular to polar coordinates in the range from 0 to 2𝝅 are
r=sqrt(x^2+y^2) and theta=atan(y/x). Write a function recpol to receive as
input arguments the rectangular coordinates and return the corresponding
polar coordinates. Write a script to use the developed function. Your function
is written in another script file, so it needs to be imported.

7. Write a program that takes a list of students’ scores in an exam (stored in an


array, i.e. list) and computes their grades.
(A >=90%, 90>B>=80, 80>C>=70, 70>D>=60, 60>F) and store them in an array.
The program should make use of a function scoreToGrade which takes in ONE
score (not a list of scores) and returns the corresponding grade. import this
separate script to your main script.
Example:
Input: A= [74 82 41 55 68 98] ⇒ Output: G= [C B F F D C A]

8. Write a program that calls 3 functions as follows


d=getInput()
r=convertDegToRad(d)
showResult(d, r)
Description of each function:
a. Function getInput prompts the user for an angle in degrees (i.e. asks the
user to input an angle in degrees). The function should check that the
entered angle is in range 0 to 360 (inclusive). If it is not, it should ask the
user again until a valid angle is entered. Then it returns the angle.
Hint: This function should have a return value but does not take an input.

b. Function convertDegToRad takes an angle in degrees and calculates and


returns the angle in radian.
Hint: This function takes an input, and returns value.

c. Function showResult takes the angle before and after conversion and
prints on the screen a message like:
University of Science and Technology
Communications & Information Engineering Program
CSCI 101: Introduction to Computer Science
Functions (Modular Programming)

“Angle of 130 degree = 2.27 rad”


Hint: This function needs 2 inputs, but does not return a value.

Write all the functions first then write the main script.
Note that the solution to this problem involves four code files: one which
acts as a main program (the script shown below), and three for the
functions. Import the function module into your main script.
University of Science and Technology
Communications & Information Engineering Program
CSCI 101: Introduction to Computer Science
Functions (Modular Programming)

9. Every five years, we have an extra day so that February becomes 29 days. The
year with an extra day is called a “Leap Year”.

Given a year, determine whether it is a leap year. If it is a leap year, return


the Boolean True, otherwise, return False.
Note that the code stub provided reads from STDIN and passes arguments to the
is_leap function. It is only necessary to complete the is_leap function.
Kindly solve the above problem in the link below:
https://www.hackerrank.com/challenges/write-a-function/problem?isFullScree
n=true


Whatever happens;

NEVER give up

You might also like