COSC 1336 - MPL Ch 4 and 5 (Python)
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.)
def fibonacci(n): if n ==0: return 0 elif n == 1: return 1 else: return fibonacci(n-1) + fibonacci(n-2)
Write a loop that reads strings from standard input, where the string is either "duck" or "goose". The loop terminates when "goose" is read in. After the loop, your code should print out the number of "duck" strings that were read.
duck_count = 0 text =input() while text != "goose": text = input() duck_count +=1 print(duck_count)
In mathematics, the notation n! represents the factorial of the nonnegative integer n. The factorial of n is the product of all the nonnegative integers from 1 to n.
factorial = 1 n = int(input('Enter a nonnegative integer:')) while n>0: factorial = factorial*n n = n-1 print(factorial)
At one college, the tuition for a full-time student is $8,000 per semester. It has been announced that the tuition will increase by 3 percent each year for the next 5 years. Write a program with a loop that displays the projected semester tuition amount for the next 5 years. The program should print out the result.. .....
i = 0 tuition = 8000.0 tuition = tuition*.03 + tuition print('In ', i+1, ' year, the tuition will be $', tuition,'.', sep='') for i in range(1,5): tuition = tuition*1.03 print('In ', i+1, ' years, the tuition will be $', tuition,'.' , sep='')
Assume there is a variable, h already associated with a positive integer value. Write the code necessary to compute the sum of the perfect squares whose value is less than h, starting with 1. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively).) Associate the sum you compute with the variable q.
i = 1 q = 0 while i**2 < h: q = q + i**2 i +=1
You have a unique ID number, which is represented by the variable id, containing a string of numbers. Write a program that continuously takes strings to standard input. If the string is not your ID number, print "This is not your ID number." If it is, print "This is your ID number: " followed by the number, and terminate the loop.
n = input() while n != id: print("This is not your ID number.") n = input() print("This is your ID number:", n)
Assume there is a variable, h already associated with a positive integer value. Write the code necessary to count the number of perfect squares whose value is less than h, starting with 1. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively).) Assign the sum you compute to a variable q.
q = 0 i = 1 while i**2 < h: q += 1 i +=1
You're a swimmer, and you want to compare all of your race times to find the fastest one. Write a program that continuously takes race times as doubles from standard input, until the input is "no more races," at which point it should print out the time of your fastest race.
NOTE: Our instructor forbids BREAK & lists aren't taught until Ch 7. list1 = [] while True: race_time = input("") if race_time == "no more races": break list1.append(float(race_time)) print(min(list1))
Given a positive integer n, assign True to is_prime if n has no factors other than 1 and itself. (Remember, m is a factor of n if m divides n evenly.)
NOTE: Our instructor forbids BREAK. is_prime = True for i in range(2,n): if n%i == 0: is_prime = False break else: is_prime = True
Associate True with the variable is_ascending if the list numbers is in ascending order (that is, if each element of the list is greater than or equal to the previous element in the list). Otherwise, associate False with is_ascending
NOTE: This is now not taught until Chapter 7. is_ascending = True for i in range(2, len(numbers)): if numbers[i]<numbers[i-1]: is_ascending = False break else: is_ascending = True
Define a function is_prime that receives an integer argument and returns true if the argument is a prime number and otherwise returns false. (An integer is prime if it is greater than 1 and cannot be divided evenly [with no remainder] other than by itself and one. For example, 15 is not prime because it can be divided by 3 or 5 with no remainder. 13 is prime because only 1 and 13 divide it with no remainder.) This function may be written with a for loop, a while loop or using recursion.
def is_prime(num): i = 2 if num <2: return False while i < num: if num%i == 0: return False else: i +=1 return True
Assume there are two variables, k and m, each already associated with a positive integer value and further assume that k's value is smaller than m's. Write the code necessary to compute the number of perfect squares between k and m. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively).) Associate the number you compute with the variable q.
i = 1 q = 0 while i**2 <m: if i**2 <k: i += 1 else: i += 1 q +=1
Write a program that creates a pattern like the one below. Let the user input a nonnegative integer to determine the number of lines of the pattern. (note: they want spaces where they show dots) ↵ #·#↵ #··#↵ #···#↵ #····#↵
n = int(input('Enter number of lines for pattern:')) for row in range(n): if row ==0: print('##') else: print('#', end='') for col in range(row): print(' ', end='') print('#')
In this exercise, use the following variables: i,lo, hi, and result. Assume that lo and hi each are associated with an int and that result refers to 0. Write a while loop that adds the integers from lo up through hi (inclusive), and associates the sum with result. Your code should not change the values associated with lo and hi. Also, just use these variables: i,lo, hi, and result.
result = 0 i = lo while i < hi+1: result = result + i i +=1
You want to know your grade in Computer Science, so write a program that continuously takes grades between 0 and 100 to standard input until you input "stop", at which point it should print your average to standard output.
sum = 0 counter = 0 grades = input() while grades != "stop": sum = int(grades) + sum counter +=1 grades = input() avg = sum/counter print(avg)
You work for a bakery that sells two items: muffins and cupcakes. The number of muffins and cupcakes in your shop at any given time is stored in the variables muffins and cupcakes, which have been defined for you. Write a program that takes strings from standard input indicating what your customers are buying ("muffin" for a muffin, "cupcake" for a cupcake). If they buy a muffin, decrease muffins by one, and if they buy a cupcake, decrease cupcakes by 1. If there is no more of that baked good left, print ("Out of stock"). Once you are done selling, input "0", and have the program print out the number of muffins and cupcakes remaining, in the form "muffins: 9 cupcakes: 3" (if there were 9 muffins and 3 cupcakes left, for example).
to_buy=input() while to_buy != "0": if to_buy=="muffin": if muffins>0: muffins-=1 else: print("Out of stock") if to_buy=="cupcake": if cupcakes>0: cupcakes-=1 else: print("Out of stock") to_buy=input() print("muffins:", muffins, "cupcakes:", cupcakes)
Assume the availability of a function is_prime. Assume a variable n has been associated with positive integer. Write the statements needed to compute the sum of the first n prime numbers. The sum should be associated with the variable total. Note: is_prime takes an integer as a parameter and returns True if and only if that integer is prime.
x =2 k =0 total = 0 while k <n: if (is_prime(x)): k +=1 total +=x x +=1 else: x +=1