Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Timeline for Shortest Unique Substring

Current License: CC BY-SA 4.0

26 events
when toggle format what by license comment
Jun 17, 2020 at 9:04 history edited Community Bot
Commonmark migration
S Nov 24, 2018 at 19:45 history edited mbomb007 CC BY-SA 4.0
Use better table and syntax highlighting
S Nov 24, 2018 at 19:45 history suggested bb216b3acfd8f72cbc8f899d4d6963 CC BY-SA 4.0
Use better table and syntax highlighting
Nov 24, 2018 at 16:00 review Suggested edits
S Nov 24, 2018 at 19:45
Apr 29, 2016 at 21:30 comment added Taylor Lopez @Blue nah, it happens. It always surprises me how those bytes add up, especially when I find some way to optimize something, and it turns out to be longer. lol
Apr 29, 2016 at 21:22 comment added Blue @iAmMortos the first one works because it is equivalent to what you are doing now. As for the other 2, yeah, I tested and realized. Sorry
Apr 29, 2016 at 20:53 comment added Taylor Lopez @Blue regarding the first suggestion: but then I would have to increment the second n later on too, which would result in no improvement. Regarding the second suggestion: the range aliasing results in no improvement, and the len aliasing actually adds two bytes. If I called the functions more often, then it would have helped. That's probably why @Dennis used the aliasing for enumerate since it's a longer word lol. It actually had a positive effect.
Apr 29, 2016 at 20:48 comment added Blue Then a couple more by doing lambda a,b,r=range,l=len:..., then call r() and l() instead of range() and len(). This might actually beat Dennis's answer
Apr 29, 2016 at 20:44 comment added Blue You can save 2 more bytes by doing a[m:m+n+1]for n in range(len(a))...
Apr 29, 2016 at 19:42 history edited Taylor Lopez CC BY-SA 3.0
added language type meta tag
Apr 29, 2016 at 18:27 history edited Taylor Lopez CC BY-SA 3.0
shorter
Apr 29, 2016 at 18:20 comment added Taylor Lopez Aha! Much better. Man, code golfing takes a certain kind of problem solving for sure. It's like a completely different thought process.
Apr 29, 2016 at 18:15 comment added NonlinearFruit I was just making stuff up for a short example, for your case it would be [a[m:m+n] for m in range(...) for n in range(...) if a[m:m+n]not in b]. This would give an array of possible answers, so return the 0th one unless it is empty, then return ''
Apr 29, 2016 at 17:59 comment added Taylor Lopez @NonlinearFruit Maybe I'm not understanding your suggestion. In the code in the Edit section of my post, I tried doing that, but it ended up being a byte longer. Is your example the suggested change? Because I'm not understanding the even-check at the end. Or adding n and m together.
Apr 29, 2016 at 17:38 comment added NonlinearFruit You can save a couple bytes (maybe make it a one-liner) if you compact your for loops and if statement. For example, [ n+m for m in range(...) for n in range(...) if n%2==0]
Apr 29, 2016 at 16:39 history edited Taylor Lopez CC BY-SA 3.0
saved 4 bytes
Apr 29, 2016 at 16:38 comment added Taylor Lopez That works. I hate double-checking substrings I've already checked for the sake of a few bytes, but the challenge is for shortest, so... Thanks!
Apr 29, 2016 at 16:34 comment added user81655 Sorry, I'm not familiar with Python and I assumed the ranges were inclusive. In that case try range(1,len(a)+1) and range(len(a)).
Apr 29, 2016 at 16:28 history edited Taylor Lopez CC BY-SA 3.0
correction to count. reinstated older, shorter code.
Apr 29, 2016 at 16:22 comment added Taylor Lopez No, with range(1,len(a)), the 4th test cast fails because it won't try the full string; it'll only go to the length of the string - 1. And with range(len(a)-1), the 1st test case fails returning 'cd' instead of just 'd'. There may be something there, though.
Apr 29, 2016 at 16:17 comment added user81655 range(1,len(a)) and range(len(a)-1) should work shouldn't it? Also I think using a tab character for the two space indent would save a byte.
Apr 29, 2016 at 16:17 history edited Taylor Lopez CC BY-SA 3.0
used list comprehension instead
Apr 29, 2016 at 16:07 comment added Taylor Lopez @CᴏɴᴏʀO'Bʀɪᴇɴ Can't do it quite that way. If I assign range to r in the line above, it actually adds a byte. Good idea, though. There's probably a shorter way to iterate through the substrings.
Apr 29, 2016 at 16:03 comment added Conor O'Brien I don't know much python, but maybe you can do (r=range)(1,len(a)+1) then use r?
Apr 29, 2016 at 15:57 history edited Taylor Lopez CC BY-SA 3.0
Added test cases
Apr 29, 2016 at 15:50 history answered Taylor Lopez CC BY-SA 3.0
toggle format

AltStyle によって変換されたページ (->オリジナル) /