Randomness and Array Manipulation

Anonymous
timer Asked: Feb 2nd, 2014

Question description

Question 1 - Randomness and array manipulations

You are a promising junior programmer in a video game business. You are part of a team working on an online gaming system for a first-person shooter game. In the game, players can register to an arena to play together. Sometimes, they play solo; sometimes, they play in teams.

A1_Q1.pdeYou have been asked to implement a set of functionalities in Processing (!) to be able to shuffle and make subgroups of players to play in teams.

Each player is identified by its username (a String object).

`Start from this code: A1_Q1_Source.zip. The sketch declares empty methods that you need to implement (called "stubs"). It also contains a list of usernames on which it performs a series of tests. You can see the correct expected result of these tests here. If your functions are implemented correctly, you should get similar results. Note that the results of the last test may slightly vary since it involves randomness.`
A1_Q1.pde

1a - Randomize list

Implement the following method without using ArrayList (ie. using only arrays).

`/** * This method takes a list of usernames and randomizes it. * @param userNames the list of usernames * @return a new array containing the randomized list of usernames */String[] createShuffledList(String[] userNames) {}`

Important:

• There should be no repetitions in the usernames (unless there was a username twice in the list sent as a parameter).
• The returned list should contain all and only the items from the original list. This means that it will have the same length (among other things).
• The list of usernames sent as a parameter should not be modified (ie. be aware that you are passing by reference).

1b - Grouping

Implement the following method without using ArrayList (ie. using only arrays).

`/** * This method takes a list of usernames and separates them in different groups. * @param userNames the list of usernames * @param nGroups the number of groups * @return an array of groups of usernames */String[][] createGroups(String[] userNames, int nGroups) {}`

Example:

`String[] userNames = loadStrings("usernames.txt"); // you can define your own file with usernames for testing purposesStringp[][] groups = createGroups(list, 3);`

will return an array "groups" where groups[i][j] contains the j-th username of the i-th group. Another way to say it is that groups[i] is an array of String (ie. String[]) containing the usernames of its group.

Important:

• If nGroups is smaller than 1, it should be considered equal to 1 (ie. one group with everyone in it).
• if nGroups is larger than the number of usernames in the list, it should be considered equal to the number of usernames (ie. one person per "group").
• If the list has length 0 or is null, the method should return null.
• The returned array length is equal to the number of groups ie. groups.length == nGroups.
• There should be no repetitions of usernames anywhere in the returned array and all usernames should be there.
• When the number of usernames sent as a parameter cannot be perfecly divided by nGroups, you will have to return a mix of groups of different sizes. When that happens:
• You need to make sure that the difference in size between two groups does not exceed 1. For example, if you call createGroups(list, 3), the result should be an array of groups where each group contains either 2 or 3 usernames.
• The bigger groups should be at the beginning of the returned array; the smaller groups at the end.
• Groups are formed sequentially, in the order of the elements found in userNames. So for instance, userNames[0] should appear in the first group and userNames[length-1] should always be in the last group.

1c - Randomized groups

Add an optional boolean to the last method: when true, the usernames will be randomized instead of being chosen sequentially.

`/** * This method takes a list of usernames and separates them in different groups. * @param userNames the list of usernames * @param nGroups the number of groups * @param randomize if true, the groups will contain shuffled usernames; if false, the usernames will be added sequentially to the groups * @return an array of groups of usernames */String[][] createGroups(String[] userNames, int nGroups, boolean randomize) {}`

(Top Tutor) Studypool Tutor
School: UT Austin
Studypool has helped 1,244,100 students
flag Report DMCA

Brown University

1271 Tutors

California Institute of Technology

2131 Tutors

Carnegie Mellon University

982 Tutors

Columbia University

1256 Tutors

Dartmouth University

2113 Tutors

Emory University

2279 Tutors

Harvard University

599 Tutors

Massachusetts Institute of Technology

2319 Tutors

New York University

1645 Tutors

Notre Dam University

1911 Tutors

Oklahoma University

2122 Tutors

Pennsylvania State University

932 Tutors

Princeton University

1211 Tutors

Stanford University

983 Tutors

University of California

1282 Tutors

Oxford University

123 Tutors

Yale University

2325 Tutors