We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 1
a Exercises ™
5 Wetea method called sorandenovebupt cates tut accept alist of integers sis parameter and rearanges
the lists elements into sorted ascending ode, as well as removing ll duplicate values from thelist. For example,
thelist (7, 4, -9, 4, 15) 8) 27, 7, 11, ~5, 32, -9, 9) would become (5, —§ ay
15, 27, 32) ater call10 your method. Use a Set as part of you scltio.
6 Wite« method countunique tat sceps lis of integers sa parameter and returns the numberof unqueintege
‘ales in thelist. Use sets auxiliary storage to help you solve his problem. Fo example if list contains the values
13, 7, 3, 2, 2, 3) 7, 2, 25, 15), yourmetbed should tum 5, The empty Kst contains O unique valves.
a mapls (bare, baz=2, foo"3, sunblesd),
rmap2: (Imearth, 2ewind, Jealr, fire)
mapis (£ivee105, foure10¢, ones102, six-106, three=103, two=102),
‘mp2: (99=un0, 10iedos, 103*trea, 105=quatro)
eo mapls (2942, bed, 087, de15, enll, £924, 9*7),
rmap2s (I#four, 3eacore, Sand, 7=seven, Severs, 11-290)
22, Modify the YordCount. program ¢o thet it prints the most frequently eceurring words sorted by number of occur=
‘ences. Todo this, write code atthe end ofthe program to create a reverse map from counis to words that is based on
the original map. Assume that no two Words of interest occur the exact same number of times,
7, Wete a method count Cosson that accepts two ist of integer as parameters and returns the numberof unique integers
tat oscur in both ists: Use one or more set as storage to help you solve this problem. For example, if one list zonains
the valves (3, 7, 3, ty 2, 3, 7, 2, 15, 15] and the other list contains the values (5, 25, 2, Ly 7,
25, 36}, your method should tum ¢ because the elements 2, 2,7, and 15 oveurin both lists.
8, Write « method maxtength that accepts a set of strings as a pamneter and that retums the length of the longest
string inthe lst. If your method is passed an empty set, it should retum 0.
9, Write a method hasodd that accepts a set of integers as a parameter and returns true ifthe set contains atleast one
dd integer and fate otherwise. I passed the empty set, your method should retum fae.
. Create a HashSet and add « group of names including some names repested rvice, and prin thelist of names ia the 10, Write a method renovesventengeh that accepts a set of strings ns a parameter and that removes al of the strings
set, demonstrating thatthe duplicate names are not present. Then, eat the set as e TreeSet and display the list of a ea
‘nares sorted alphabetically, 11, Write a method called sysnatreseeDi ference that accepis two Sets as parameters and returns anew Sat con-
taining ther symmetsic set difference (thai, the set of elements conned in either ofthe two sets but not inboth). For
‘example the symmetic difference between the sets (1, 4, 7, 9] and (2, 4, 5, 6, 7)is(1, 2, 5, 6, 9).
12. Write a method containa3 that accepts alist of strings asa parameter and returns tue if any single string occurs
a least 3 times inthe list, and Fane otherwise, Use a map as auxiliary storage.
2, Write « program o cieate Couciete Map clases that generate a frequency count of words passed from the command
line. A HashMap i initially used for data storage. Then, the map is converted to a TreeMp to display the key list
sorted alphabetically.
13, Write a method istnique that secepts « map whose keys and values are strings asa parameter and returns true if
‘no two keys map tothe same valve (and £20 if any two or more keys do map othe same value) For exxmpl, if
the map contains te folowing key/value pairs, your method would return true: (Marty=Stepp, StuartsReges,
JessicaeMiller, AnandseCanp, HaleParkina). But calling it onthe following map would retumn fase,
‘because of two mappings for Perkins and Reges: (KendrickePerkins, Stuartexages, Jasaicanéille:
BrucesReges, Mlal=Perkins).
14, Write e method intersect that accepts wo maps whose keys are strings and whose values are integers as parameters
{3, Write a method called ranovernRange that accepts four parameters: aLinkedList, anelement valve, a xarting
{ndex, and an ending index. The method's behavior is to remove all ecurrences of the given element that appear in
the lst between the stating index (inclusive) and the ending index (exclusive). Other values and oceurTences ofthe
‘given value that sppear outside the given index range are not affected,
For example, for te Hist (0, 0, 2, 0 4, 0, 6, 0, 8 0, 10, 0, 12, 0, 14, 0, 16}, acallof ‘and returns a new map containing only the key/value pairs that exist in both of the parauneter meps. In oder for @
removernRange(list, 0, 5, 13) should produce thelist '0, 0, 2, 0, 4, 6, @, 10, 12, 0, 14, 9, 16]. key/value pair tobe included in your resl, not only do both maps need to contain a rapping for that kay, bul they need
[Notice thatthe zeros located at indexes berween 5 inclusive and 13 exelutive inthe original lst (before ary modi 1 map io the same value For example, ifthe ewo maps passed are (Janet+87, Loyen~62, Whiteker=i6,
cations were made) have beea removed. Alysea=i00, Stefaniee60, Jef£=00, Kine52, Sylvined5) and {Logan=é2, KineS2, whitaker=52,
Jeft~ss, stefaniors0, Brianeé0, Lisa=83, sylviae87), your method would rum the following new map
(te order ofthe key/value pairs does not matt): {Logans62, Stefanie=€0, Jeff=88, Kim-52).
15, Weite a method maxcccurrences that sccepts a list of integers as parameter and returns the numberof times the
‘most Frequently occuring integer (the “mode") occurs in the list. Solve this problem using a map as auxiliary storage.
4, Write a method called partition that accepts a list of integers and an integer valve sits parameter, and reamanges
(arttions) the lst 0 that ll he elerents with values less than & occur before all elements with values greater than E.
“The exact onder of the elements is unimportant, 50 long es ll elements less than & appear before all elements grester then nfs ue eee
1. Forexample, fo the linked Hist 18, 1, 6, 12, -3, 4, @, 21, 2, 30, —2. 9], one acceptable ordering of : é
the lis afer call of part‘tton(1iat, 5) would be [-1, 1, 2, 4, -3, 12, 8, 21, 6, 30, 15, 91. You ee aa ae a ao ce
oe Sie ine saacleaey no two keys map tothe same value. For example, Hartys206-9024, Bawking=123-4567, Smithe949-0504,