## Python Coding Work

## Python Coding Work

- Write the
**definition**of a function twice, that receives an int parameter and returns an int that is**twice**the value of the parameter. - Write the definition of a function square which recieves a parameter containing an integer value and returns the square of the value of the parameter.
- 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 int greater than or equal to zero) in a particular time interval. The second is the length of the time interval in seconds (an int greater than zero). The function returns the typing speed of that person in words per minute (a float). - Write a
**function**min that has**three**str parameters and returns the**smallest**. (Smallest in the sense of coming first alphabetically, not in the sense of “shortest”.) - 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__

Note: is_prime takes an integer as a parameter and returns True if and only if that integer is prime.

- Assume the availability of a function is_prime. Assume a
__variable__n has been associated with positive integer. Write the statements needed to find out how many prime numbers (starting with 2 and going in increasing order with successively higher primes [2,3,5,7,11,13,…]) can be added before exceeding n. Associate this number with the__variable__ - Write the definition of a function oneLess which recieves a parameter containing an integer value and returns an integer whose value is one less than the value of the parameter.

- [Functions >> functions and if statements] Write the definition of a function powerTo which recieves two parameters, a double and an integer. If the second parameter is positive, the function returns the value of the first parameter raised to the power of the second. Otherwise, the function returns 0.
- Write the definition of a function absoluteValue that recieves a parameter containing an integer value and returns the absolute value of that parameter.

- Define a function called isPositive that takes a parameter containing an integer value and returns True if the paramter is positive or False if the parameter is negative or 0.

- Define a function called isSenior that takes a parameter containing an integer value and returns True if the parameter is greater than or equal to 65, and Falseotherwise.

Assume that max2 is a **function** that expects two int parameters and returns the value of the larger one.

Also assume that four __variables__, population1, population2, population3, and population4 have already been defined and associated with int values.

Write an **expression** (not a statement!) whose value is the largest of population1, population2, population3, and population4 by calling max2.

- Given a file object named output, associate it with a file named yearsummary.txt by opening the file for appending

- Given that corpdata is a file object used for reading data and that there is no more data to be read, write the necessary code to complete your use of this object.

- Using the file object input, write code that read an integer from a file called rawdata into a
__variable__datum (make sure you assign an integer value to datum). Open the file at the beginning of your code, and close it at the end.

- A file named numbers.txt contains an unknown number of lines, each consisting of a single positive integer. Write some code that reads through the file and stores the largest number read in a
__variable__named maxvalue.

- A file named data.txt contains an unknown number of lines, each consisting of a single integer. Write some code that creates two files, dataplus.txt and dataminus.txt, and copies all the lines of data1.txt that have positive integers to dataplus.txt, and all the lines of data1.txt that have negative integers to dataminus.txt. Zeros are not copied anywhere.

- Two files named numbers1.txt and numbers2.txt both have an unknown number of lines, each line consisting of a single positive integer. Write some code that reads a line from one file and then a line from the other file. The two integers are multiplied together and their product is added to a
__variable__called scalar_product which should be initialized to zero.

Your code should stop when it detects end of file in either file that it is reading.

For example, if the sequence of integers in one file was “9 7 5 18 13 2 22 16” and “4 7 8 2” in the other file, your code would compute:

4*9 + 7*7 + 8*5 + 2*18

and thus store 161 into scalar_product.

- Two
__variables__, num_boys and num_girls, hold the number of boys and girls that have registered for an elementary school. The__variable__budget holds the number of dollars that have been allocated to the school for the school year. Write code that prints out the per-student budget (dollar spent per student). If a division by zero error takes place, just print out the word “unavailable”.