COMS 104 Final Exam Iowa State

Ace your homework & exams now with Quizwiz!

name= "Fall Semester" what does print name [10:2] give?

"" (an empty string)

find the output of the loop numberList= [1, 2, 3, 4, 5] i=0 while i< len(numberList): print numberList[i] i += 1

1 2 3 4 5

find the output of the loop numberList= [1, 2, 3, 4, 5] i=0 while i< len(numberList): print numberList[i] i += 2

1 3 5

find the output of the loop numberList= [1, 2, 3, 4, 5] a=5 while 1<= a <= 10: if a%2==0: a += 3 print a else: a -=1 print a

4 7 6 9 8 11

Explain the difference between 42 + 2 and "42" + "2". What is the + operator doing in each case?

42 + 2 will give an output of 44 because the + operator is adding the two integers together. "42" + "2" will have an output of 422 because the plus operator is adding the two strings together, called string concatenation.

find the output of the loop numberList= [1, 2, 3, 4, 5] i= len(numberList)-1 while i>=0: print numberList[i] i -=1

5 4 3 2 1 (i is 4 and the index for the string goes up to 4 because it starts at 0!!!!)

What is the difference between a string variable and a list with same letters? For example, s = "Steve" listS= ["S" , "t" , "e" , "v" , "e" ] What is the difference between s and listS?

The difference is that s can't be changed because strings are immutable. listS can have different values set for each part of it because it is not a string. For example, s[0]="K" would not be allowed, but listS[0]="K" would be allowed.

name= "Fall Semester" what does print name[ :10]

Fall Semest

name= "Fall Semester" what does print name[:]

Fall Semester

name= "Fall Semester" what does print name[5] give?

S

What is the benefit of writing a function? (2 things)

They can be reused and they help break down problems into manageable pieces.

What is meant by "Strings are immutable"?

They can't be changed once they are defined

what does range(1, 10, -1)

[ ] imposible

what does range(5) give?

[0,1,2,3,4]

what does range(1, 10, 3) give

[1, 4, 7]

what does range(10, 3, -2) give

[10, 8, 6, 4]

what does range(2, 5) give

[2,3,4]

what does append() do? give an example

adds to the list and is used in a loop wordlist.append(word) filterList.append(word)

Write a function "a_word_list" which allows the user to enter words that starts with letter "a" or "A" and store them in a list. If user enter a word starts with any other letter, then the function stop taking words and will display the set of words it already stored.

def a_word_list(word): wordlist= [ ] word= raw_input("Enter a word: ") while word.lower().startswith("a"): wordlist.append(word) word= raw_input("Enter a word: ") print wordlist

A number which does not have any divisors other than 1 and itself is called a Primenumber. Write a Python function called "checkPrime" which takes an integer as the input parameter and return True if the number is a Prime and return False otherwise.

def checkPrime(num): for i in range (2, n): if num%i==0: return false else: return True

Three numbers a, b and c are called a Pythagorean triple if a2 + b2 = c2. An example is the triple 3, 4 and 5, since 9 + 16 = 25. Write a Python function called "check_pythagorean" that takes three input parameters a, b and c and return True if a, b, c is a Pythagorean triple. Otherwise, return False.

def check_pythagorean(a,b,c): l=a m=b n=c if l**2+m**2==n**2: return "True" else: return "False" l=input("Enter a number: ") m=input("Enter a number: ") n=input("Enter a number: ") print check_pythagorean(l,m,n)

Write a function "filter_long_words" that takes a list of words and an integer n as the input parameters and prints the list of words that are longer than n.

def filter_long_words(list, n): filerList= [ ] for word in list: if len(word)> n: filterList.append(word) print filterList

Write a function called "nFactorial"that computes and return the "n factorial" for any number n > 0.

def nFactorial(num): factorial=1 if n>0: for i in range(1, n+1): factorial= factorial*i return factorial

Write a function "overlapping" that takes two lists as the parameters and returns True if they have at least one member in common, False otherwise.

def overlapping(list1, list2): for i in list1: for h in list2: if i==h: return True else: return False return False

A palindrome is a word that reads same both forward and backward. For example, the word "rotor" or the sentence "rats live on no evil star". If a string has zero or one character it is a palindrome. If it has two or more characters, test to see if the first character matches the final character and so on. If any pair of characters is not equal, then it is not a palindrome. Write a function called "test_palindrome" which takes a string as the input parameter and return True if the string is a palindrome or False otherwise.

def test_palindrome(phrase): word=phrase if len(word)==0 or len(word)==1: return True i=0 j= len(phrase)-1 while i< len(phrase)/2: if phrase[i] != phrase[j]: return False i +=1 j -=1 return True

Suppose you have a bank account that earns 5% interest per year. Write a Python function called "time_to_double_balance" that takes the initial account balance as the input parameter and calculates the number of years it would take for the account balance to be double the initial balance.

def time_to_double_balance(initial_balance): balance= input("Enter your initial balance: ") years=0 double_balance= balance*2 while balance< double_balance: years +=1 interest= balance*0.05 balance= balance+interest print years

myString= "Iowa State" get the number of letters in the complete string

len(myString)

Rewrite the following for loop Python statements using while loops. listN= [1, 2, 3, 4, 5] for i in listN: print i

listN= [ 1, 2, 3, 4, 5] index= 0 while index< len(listN): i= listN[index] print i index += 1

name= "Fall Semester" what does print name[2:10] give?

ll Semest

myString= "Iowa State" get the number of times "a" appears

myString.count("a")

myString= "Iowa State" check if substring "tat" exists or not

myString.find("tat")

myString= "Iowa State" convert to all lowercase

myString.lower()

myString= "Iowa State" get a list that contains the two words of myString as two sepearte elements

myString.split()

Give range that would result from [0, 1, 2, 3]

range(0, 4) or range(4)

Give range that would result from [0, 2, 4, 6, 8]

range(0, 9, 2)

Give range that would result from [5, 6, 7, 8, 9]

range(5, 10)`

Give range that would result from [9, 6, 3]

range(9, 2, -3)

Rewrite the following for loop Python statements using while loops. s="Steve" for i in range(0, 5, 2): print s[i] (output is S e e because the range has it go every other)

s= "Steve" index= 0 while i< len(s): print s[i] index += 2


Related study sets

CFA 48: Overview of Equity Securities FIN3013

View Set

Research Methods Study Questions

View Set