Docsity

CS 1301 Extra Credit Problem Set Solutions, Assignments of Computer Science

Toggle sidebar
bg1
CS 1301 Extra Credit Problem Set
Due: Wed April 29th before 6pm. (NO Late Turn-In's accepted!)
Because this extra credit problem set is worth test points, you MAY NOT look at other people's code!
You are allowed to talk with other students about how they solved the problems, and you may tell other
students how to solve the problems, but you MAY NOT show other people your code or look at other
people's code! If you get help from others (or help others) you should include their names in your
collaboration statement at the top of the file.
Each correctly solved problem is worth a test point. This extra credit problem set will allow you to gain
up to 11 test points! This could raise your grade on an Exam around 10%.
Each problem will require you to write a function. Download the ec1.py file and write your functions at
the top of the file, using the specified function name and parameter(s). When you want to test your
functions, you can call the test() function that has been provided at the bottom of the file. The test()
function will call each of your functions (if you have named them correctly!) and tell you if they pass a
few simple tests. NOTE: If your function passes the included tests, it is likely to be correct, but TA's
may find other problem when they read your code. Just because a function passes all the tests does not
guarantee that you got it correct (but the likelihood is high!)
1. Write a function countJs() that accepts one parameter which it may assume is a string. This
function should return an integer which is the count of how many “J”'s (capital letter j) it finds
in the string.
2. Write a function maximum() that accepts one parameter which it may assume is a list. The list
may have any elements upon which the < (less-than) operator is defined. Your function should
return the “maximum” element of the list (as defined by the < operator. If the list is empty,
your function should return None (of None-Type), which is easy to do by using a return
statement with nothing behind it.
3. Write a function isPrime() that takes one parameter (you can assume this parameter will be in
the form of a positive integer) and checks to see if it is prime. (A prime number is a positive
integer that has exactly two positive integer factors, 1 and itself.) If the number is prime, your
function should return True, if not, it should return False. [We will not test this function with
any numbers larger than 10,000, so speed isn't terribly important.]
pf3

Partial preview of the text

Download CS 1301 Extra Credit Problem Set Solutions and more Assignments Computer Science in PDF only on Docsity!

CS 1301 Extra Credit Problem Set

Due: Wed April 29

th

before 6pm. (NO Late TurnIn's accepted!)

Because this extra credit problem set is worth test points, you MAY NOT look at other people's code! You are allowed to talk with other students about how they solved the problems, and you may tell other students how to solve the problems, but you MAY NOT show other people your code or look at other people's code! If you get help from others (or help others) you should include their names in your collaboration statement at the top of the file. Each correctly solved problem is worth a test point. This extra credit problem set will allow you to gain up to 11 test points! This could raise your grade on an Exam around 10%. Each problem will require you to write a function. Download the ec1.py file and write your functions at the top of the file, using the specified function name and parameter(s). When you want to test your functions, you can call the test() function that has been provided at the bottom of the file. The test() function will call each of your functions (if you have named them correctly!) and tell you if they pass a few simple tests. NOTE: If your function passes the included tests, it is likely to be correct, but TA's may find other problem when they read your code. Just because a function passes all the tests does not guarantee that you got it correct (but the likelihood is high!)

  1. Write a function countJs() that accepts one parameter which it may assume is a string. This function should return an integer which is the count of how many “J”'s (capital letter j) it finds in the string.
  2. Write a function maximum() that accepts one parameter which it may assume is a list. The list may have any elements upon which the < (lessthan) operator is defined. Your function should return the “maximum” element of the list (as defined by the < operator. If the list is empty, your function should return None (of NoneType), which is easy to do by using a return statement with nothing behind it.
  3. Write a function isPrime() that takes one parameter (you can assume this parameter will be in the form of a positive integer) and checks to see if it is prime. (A prime number is a positive integer that has exactly two positive integer factors, 1 and itself.) If the number is prime, your function should return True, if not, it should return False. [We will not test this function with any numbers larger than 10,000, so speed isn't terribly important.]
  1. Write a function changeGrade() that accepts a single parameter (a string). Your function should look for the following letters in the string, and return a new string with the appropriate substitutions made: If you find a: Replace it with a: F D D C C B B A
  2. Write a function decimalToBinary() that takes one parameter (you can assume this parameter will be in the form of an integer). Your function should assume the parameter represents a decimal number, and return a string made up of 0's and 1's representing the binary representation of that number.
  3. Write a function rotateText(textString, numToRotate) that accepts a paraemter containing a string and “rotates” all letters (AZ and az) by the specified number. By “rotate” we mean the following: If the numToRotate is 2, an “A” should be replaced by a “C”, and a “B” should be replaced by a “D”, and so on. If numToRotate is 5 an “a” would be replaced with an “e”. After the string is rotated, the function should return the newly created string. Any character that is not a letter (such as numbers, whitespace or punctuation marks) should be unchanged. Note: Capital letters should be rotated into capital letters, and lower case letters should be rotated into lower case letters. Alphabets loop, so a “z” would convert to a “b” if numToRotate is 2. Hints: The chr(num) function converts a number to a character. The ord('C') function converts a character to a number. The letters AZ range from 65 to 90 and az range from 97 to
    1. Interesting question: What happens if you apply the function twice, rotating 13 each time?
  4. Write a function blocksInPyramidHeight( height) that takes the integer height of a (2D) pyramid and returns the number of blocks in the Pyramid. Blocks in a Pyramid are layered over the join between two lower blocks, so a Pyramid of height 1 has 1 block, a Pyramid of height 2 has 3 blocks, a Pyramid of height 3 has 6 blocks, and so on. Hint: Each level has one more block than the last.
  5. Write a function binaryToDecimal(binaryString) that takes one parameter made up of a STRING of zero's and ones (representing a positive/unsigned binary number). Your function should assume the parameter represents a decimal number, and return the corresponding decimal integer.
  6. Write a function blocksInPyramidWidth( maxWidth) that takes one parameter representing the number of blocks at the base of the pyramid, and returns the number of blocks (total) in the