OverviewScheduleResourcesAssignmentsHome

CSC 122: Computer Science II, Fall 2006

HasCl Exercises

Write the following functions. If appropriate you may reuse functions that you have already written on this sheet to write other functions on this sheet.

  1. Write a HasCl function sumUp that takes two parameters (both integers). The function should return the sum of all the numbers between the two parameters (inclusive). You cannot make any assumptions about the order of the parameters. For example, sumUp( 5, 3 ) should return 12.
  2. Write a HasCl function called count that takes two parameters (a list and an item). The function should return the number of times that the given item occurs in the list. For example, count( [1, 3, 2, 3], 3 ) should return 2.
  3. Write a HasCl function called remove that takes two parameters (a list and an item). The function should return the list that results from removing the first copy of the item from the list (all other copies of the item should remain). If the item is not in the list, return the list unchanged. For example, remove( [2, 4, 6, 4, 8, 10], 4 ) should return [2, 6, 4, 8, 10].
  4. Write a HasCl function called insert that takes two parameters (a list and a number). You may assume that the list consists of 0 or more numbers that are in ascending order. The function should return the list that results from inserting the number into the list in the appropriate place to keep the list in order. For example, insert( [2, 4, 6, 8], 5 ) should return [2, 4, 5, 6, 8].
  5. Write a HasCl function called insertSort that takes one parameter (a list of numbers). The function should return the list that results from sorting the list using insertion sort. Note: the insert function should be useful here.
  6. Write a HasCl function called declist that takes one parameter (a list of numbers). The function should return a list of numbers with each number one less that the corresponding number in the original list. For example, declist( [1, 2, 3, 4] ) should return [0, 1, 2, 3].
  7. Write a HasCl function called position that takes as parameters an integer and a list of numbers. The function will return the position of the integer in the list, where the first position is 1. For example, position( 3, [1, 3, 4, 5] ) should return 2. You may assume that the integer occurs in the list.
  8. Challenge: Modify problem 7 so that it returns the position 0 if the integer is not in the list.
OverviewScheduleResourcesAssignmentsHome

Valid HTML 4.01!Valid CSS!DePauw University, Computer Science Department, Fall 2006
Maintained by Brian Howard (bhoward@depauw.edu). Last updated