This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Wrj-te a returns returns O-based following IL, then the Iist. method lastlndexOf that takes an integer n as a parameter and that the index of the last occurrence of n in a list of integers and that -1 i-f n does not appear in the List. We are using the normalindexing scheme. For example, tf a variable called lj-st stores the sequence of values: L8, cal-l: IastlndexOf (18) of the last occurrence of l8 in 2,'7, 19, 39, 19, 40] shoul-d return the list. If Iist. 6 because that is the index the caII had instead been: (3) IastlndexOf -1 because 3 does not appear in t'he l_ist The method would return not make any assumptions about how many elements are in the list not cal-l any other methods of the class problem. to solve this Write a method numDuplicates returns that list Because the list sorted of integers. will the duplicates be grouped together. list stores this sequence of values: You may You may the number of duplicates in a will be in sorted order, aLl- of For example, if a variabLe call-ed [1, then the
Iist. r, L, 3, 3, 6, g, 15, 15, 23, 23, 23, 40, 40] call-:
numDuplicates () I, of 1 duplicate of 3, 1 duplicate of 15, 2 you may assume that 40) . Remember that 7 (Z duplicates of shoul-d return of 23 and 1 duplicate duplicates is in sorted order. the list returns Write a method maxAdjacentPairSum that For example , Lf values in a list of integers. sequence of values: the following [ 1, then the 18, call: () 2, 7, 18, 39, 12) the maximum sum of adjacent a variable call-ed I j-st stores 1i st . maxAd j acentPairSum return 51 because it is the Iargest should sum that can be obtained with (18 + 39) . values in this list adjacent Your method should consider only pairs. For example, if t he list adjacent instead had stored: [1, 18, 2, 7, 18, 39, 12, 40, 32, -9, .103, -131 on the method should return 94 because the maximum sum that then a cafl can from adjacent values in the l-ist is 94 (-9 + 103) . be obtained Your method 0 if the list fewer than two values. shouLd return contains Write a method stutter that doubl-es the size of the list by replacing every wi_t_!_ wo gf qhat l4!eger. inteqer il the llst t F o r qlgryp1e, _ _!!_!he I i st when the method is call-ed: stores the followingr sequence of integers lL,
It should lL, 8, 19, 4,
the 8, r'71
following ]-9, 4, sequence 4, 17, Lll of integers after stutter is cal-l-ed: store L, 8, 19, ...
View Full Document
This note was uploaded on 11/03/2009 for the course CSE 143 taught by Professor Sr during the Spring '08 term at University of Washington.
- Spring '08