r/dailyprogrammer • • Jul 27 '12
[7/27/2012] Challenge #82 [easy] (Substring list)
Write a function that takes a number n as an argument and returns (or outputs) every possible unique substrings (not counting "") of the first n letters of the alphabet (in any order you like). For example, substrings(5) could be:
a
ab
abc
abcd
abcde
b
bc
bcd
bcde
c
cd
cde
d
de
eBONUS 1: Find a way to quickly determine how many strings this function returns for a given input. If the alphabet were 500 letters long, how much possible substrings would it have?
BONUS 2: Modify your function to take a string as an argument. Make sure all substrings in your output are still unique (i.e., there are two "l" substrings in "hello", but you should output only one).
4
u/5outh 1 0 Jul 27 '12 edited Jul 27 '12
Answer with both bonuses:
Some output:
As a side note, this will find all sub-lists of any type of data that can have equality to another element. For example,
bonus2 [1, 2, 3]will output[[1,2,3],[2,3],[3],[1,2],[2],[1]].Edit: completely pointfree
4
u/mudkip1123 0 0 Aug 09 '12
Python:
2
u/sch1zo Jul 27 '12 edited Jul 27 '12
python(no bonus)
edited because i'm an idiot: thanks bobbo_.
output
2
u/MagikarpSalesman 0 0 Jul 27 '12 edited Jul 27 '12
Only Bonus 1. Nothing else. In C#.
EDIT: Wow, I'm dumb. I'll just leave it as is. It doesn't need to be this complicated.
2
u/mktange Jul 27 '12
Python with both bonuses:
2
u/sleepingsquirrel Jul 28 '12
Haskell
2
u/Wium Jul 28 '12
Java, with both bonuses:
Output: 'abcde' substrings: a ab abc abcd abcde b bc bcd bcde c cd cde d de e 'abcde' has 15 substrings. 'Hello' substrings: H He Hel Hell Hello e el ell ello l ll llo lo o 'Hello' has 14 substrings.
3
u/[deleted] Jul 27 '12 edited Jul 28 '12
C with both bonuses
Edit: Fixed the issue with the bit shift because I am bad at using TDD for these challenges and I should feel bad.
1
u/krawcrates Jul 27 '12
Javascript with bonus 2, uses a hash to keep everything unique:
One caveat is it does expect an array for a "string", like
var string = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];because I left out astrToArrfunction for brevity.1
u/prophile Jul 27 '12
Python:
1
u/wlabfuvm Jul 27 '12
Python (with Bonus 1):
1
u/SwimmingPastaDevil 0 0 Jul 27 '12 edited Jul 27 '12
Should work for the main problem as well as Bonus 1 and 2.
Output for
subs('hello',4):1
u/[deleted] Jul 27 '12
Java, including both bonuses (Bonus 1 works only with the standard alphabet "abcd...."). Recursion is a funny thing.
Input:
Output:
Input:
Output:
Input:
Output:
1
u/ander1dw Jul 27 '12
Java (with bonus solutions):
Usage:
Output:
1
u/lawlrng 0 1 Jul 27 '12
Includes both bonuses. =)
Output:
1
u/taylorfausak Jul 27 '12
Python, with both bonuses.
1
u/[deleted] Jul 28 '12
C++:
1
u/ripter Jul 29 '12
In Javascript:
Outputs:
1
u/semicolondash Jul 30 '12 edited Jul 30 '12
Trying this in Scala, did both bonuses.
Calculates substringSize(500) almost instantly.
1
u/[deleted] Jul 31 '12 edited Jul 31 '12
Python with both bonuses.
1
u/taion809 Aug 01 '12
php solution with bonus 1 and 2, critique on improvements welcome.
1
u/cdelahousse Aug 02 '12
C Programming language. I'm more of a JS guy, so I'll do it in that language next ...
Actually, I just reread the problem. I did every subset, not substring... Woops.
1
u/cdelahousse Aug 02 '12
Javascript. Both bonuspoints
1
u/Ledrug 0 2 Aug 03 '12
Bonus 2 only, without storing all generated strings:
Running: (input string must be all lower case letters)
1
u/swarage 0 0 Aug 04 '12 edited Aug 19 '12
this code is a bit convoluted, as it smashes substrings of the same length into one string, but still theoretically works
1
u/mathiasbynens 0 0 Aug 10 '12
JavaScript solution with both bonuses:
Example output:
1
u/stgcoder Aug 21 '12
python:
1
u/itsthatguy42 Oct 29 '12
3 months ago? ...I'll still submit code because I am excited about my solution :P Java with both bonuses while abusing char and int casting:
output: a ab abc abcd abcde b bc bcd bcde c cd cde d de e An input with 5 letters has 15 substrings. An input with 500 letters has 125250 substrings. h he hel hell hello e el ell ello l ll llo lo o The string 'hello' has 14 substrings.