d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Crappy Holiday Code
Add Reply New Topic New Poll
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Dec 26 2014 01:01am
Code

try {
InputStream is = new FileInputStream(f);

in = new Scanner(is);

while (in.hasNextLine()) {
String tmp = in.nextLine();
listOne.add(tmp);
choices.put(i, tmp);
i++;
}
} catch (IOException ex) {
ex.printStackTrace();
}

for(String s: listOne) {
flag = true;
while (flag) {
i = rand.nextInt(10) + 1;
if (s != choices.get(i) && bitmap[i] == 0) {
listTwo.put(i, s);
bitmap[i] = 1;
flag = false;
} else {
continue;
}
}
}


So I wrote some spaghetti which I used to randomize signatures in a secret santa contest.

I am guessing there would have been an easier way to do it.

Basically, you have a list of n names (I hardcoded 10 because me a year from now can go fuck himself)

You must match each name with one of the other names in the list that isn't itself.

However, you cannot give the same sig to multiple people

for example

{a, b, c}

a --> b
b --> c
c --> a

but you cannot do like

a --> b
c --> b
b --> a


To solve the first problem, I just used Random()

for the 2nd problem, I had to use a bitmap (I had used one before for an OS assignment and it worked wonders!)

I feel like I declared too many variables/structures and that there is more than likely a better solution.

I hacked that shit out in like 20 mins (because I suck at java file I/O)

anyways MERRY XMAS and don't laugh too hard

This post was edited by Eep on Dec 26 2014 01:01am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 26 2014 08:32am
Take a moment to think about what you're doing. Think some more before answering my question: why are you forcibly matching it up?












SPOILER:








You don't need to forcibly match it up. Just randomize a single array. Then iterate over it, each 2 being a pair.








PS: Dumbledore dies

This post was edited by carteblanche on Dec 26 2014 08:39am
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Dec 26 2014 01:24pm
Code
irb(main):010:0> names = ['abduct', 'eep', 'killg0re', 'miko', 'sheepie', 'bob']
=> ["abduct", "eep", "killg0re", "miko", "sheepie", "bob"]
irb(main):011:0> names.shuffle.each_slice(2).to_a
=> [["eep", "sheepie"], ["bob", "killg0re"], ["miko", "abduct"]]
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Dec 26 2014 02:17pm
Quote (carteblanche @ Dec 26 2014 09:32am)
Take a moment to think about what you're doing. Think some more before answering my question: why are you forcibly matching it up?












SPOILER:








You don't need to forcibly match it up. Just randomize a single array. Then iterate over it, each 2 being a pair.








PS: Dumbledore dies


that does make more sense

I guess the way I always thought of the problem led me to think I had to do it one way.

I figured there was a solution that was in-place, but couldn't quite figure it out :o



Quote (killg0re @ Dec 26 2014 02:24pm)
Code
irb(main):010:0> names = ['abduct', 'eep', 'killg0re', 'miko', 'sheepie', 'bob']
=> ["abduct", "eep", "killg0re", "miko", "sheepie", "bob"]
irb(main):011:0> names.shuffle.each_slice(2).to_a
=> [["eep", "sheepie"], ["bob", "killg0re"], ["miko", "abduct"]]


ahhhh
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 26 2014 02:24pm
Quote (Eep @ Dec 26 2014 03:17pm)
that does make more sense

I guess the way I always thought of the problem led me to think I had to do it one way.

I figured there was a solution that was in-place, but couldn't quite figure it out :o





ahhhh


I should rip off Willy Hendriks and write a book: Code First, Think Later: Sense and Nonsense in Improving Your Coding.

/edit: now that i think of it, you can just read his book. it's about chess, but the ideas still apply

This post was edited by carteblanche on Dec 26 2014 02:38pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Dec 26 2014 09:28pm
A circular array would do the trick nicely. Index I and I+1 are paired. The last index in the array is paired with index 0.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll