id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

frew@mclean.com disco > combinations
prev  |  next  |  chance

We define the function combinations(things, k) to be a list of all the subsets of exactly k elements of things. You can generate subsets by counting up in binary. If the binary number has exactly k 1's then this is a subset to add to the list.


combinations(["A", "B", "C"], 2) → ["AB", "AC", "BC"]
combinations(["A", "B", "C"], 3) → ["ABC"]
combinations(["A", "B", "C", "D", "E", "F"], 5) → ["ABCDE", "ABCDF", "ABCEF", "ABDEF", "ACDEF", "BCDEF"]

...Save, Compile, Run (ctrl-enter)

public List combinations(ArrayList<String> things, int k) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 200 Post-solution available

Copyright Nick Parlante 2017 - privacy