05-03 function definition

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Write the definition of a function absoluteValue that recieves a parameter containing an integer value and returns the absolute value of that parameter.

Solution 1 def absoluteValue(x): [Tab Key]if x < 0: [Tab Key][Tab Key]x *= -1 [Tab Key]return x Solution 2 def absoluteValue(x): [Tab Key]if x < 0: [Tab Key][Tab Key]return -1*x [Tab Key]else: [Tab Key][Tab Key]return x

Fibonacci numbers are a sequence of integers, starting with 1, where the value of each number is the sum of the two previous numbers, e.g. 1, 1, 2, 3, 5, 8, etc. Write a function called fibonacci that takes a parameter, n, which contains an integer value, and have it return the nth Fibonacci number. (There are two ways to do this: one with recursion, and one without.)

Solution 1 def fibonacci(n): [Tab Key]nex = 1 [Tab Key]prev = 0 [Tab Key]for i in range(1, n+1): [Tab Key][Tab Key]temp = nex [Tab Key][Tab Key]nex = nex + prev [Tab Key][Tab Key]prev = temp [Tab Key]return prev Solution 2 def fibonacci(n): [Tab Key]if n == 0: [Tab Key][Tab Key]return 0 [Tab Key]elif n == 1: [Tab Key][Tab Key]return 1 [Tab Key]else: [Tab Key][Tab Key]return fibonacci(n-1)+fibonacci(n-2)

Write the definition of a function half which recieves a variable containing an integer as a parameter, and returns another variable containing an integer, whose value is closest to half that of the parameter. (Use integer division!)

Solution 1 def half(x): [Tab Key]return int(x/2) Solution 2 def half(x): [Tab Key]return x//2

Write the definition of a function square which receives a parameter containing an integer value and returns the square of the value of the parameter.

Solution 1 def square(x): [Tab Key]return x**2 Solution 2 def square(x): [Tab Key]return x*x

Write the definition of a function add which receives two parameters containing integer values and returns their sum.

def add(x, y): [Tab Key]return x + y

Define a function called min that takes two parameters containing integer values and returns the smaller integer of the two. If they have equal value, return either one.

def min(x, y): [Tab Key]if x < y: [Tab Key][Tab Key]return x [Tab Key]else: [Tab Key][Tab Key]return y

Write the definition of a function oneLess which receives a parameter containing an integer value and returns an integer whose value is one less than the value of the parameter.

def oneLess(x): [Tab Key]return x - 1

Write the definition of a function oneMore which receives a parameter containing an integer value and returns an integer that is one more than the value of the parameter.

def oneMore(x): [Tab Key]return x + 1

Write a definition of the function printDottedLine, which has no parameters and doesn't return anything. The function prints to standard output a single line consisting of 5 periods (".").

def printDottedLine(): [Tab Key]print(".....")

Write the definition of a function printgrade, which takes one parameter containing a string value and returns nothing. The function prints "Grade: " followed by the string parameter.

def printgrade(grade): [Tab Key]print("Grade:", grade)

Define a function called signOf that takes a parameter containing an integer value and returns a 1 if the parameter is positive, 0 if the parameter is 0, and -1 if the parameter is negative.

def signOf(x): [Tab Key]if x>0: [Tab Key][Tab Key]return 1 [Tab Key]elif x == 0: [Tab Key][Tab Key]return 0 [Tab Key]else: [Tab Key][Tab Key]return -1

Write the definition of a function twice, that receives an integer parameter and returns an integer that is twice the value of the parameter.

def twice(var): return var*2

Write the definition of a function typing_speed, that receives two parameters. The first is the number of words that a person has typed (an integer greater than or equal to zero) in a particular time interval. The second is the length of the time interval in seconds (an integer greater than zero). The function returns the typing speed of that person in words per minute (a float).

def typing_speed(w, t): [Tab Key]return float(w / (t / 60) )


Set pelajaran terkait

Unit 23 - Portfolio Performance Measures

View Set

Principles of Marketing Exam 1 Chap. 1-7

View Set

Principles of Emergency Nursing final

View Set

INFO-I308 Oracle 12c Book: Chapter 11 Quiz- Group functions

View Set

Chapter 4: Repetition Structures

View Set