can someone explain how people get this gud? im doing codewars and the top "clever" solutions just seem impossible to come up with. i mean how does someone even understand something like pic related? (it checks if a string is an isogram)
how do i /into/ this level? is it actually possible or do you just have to be an autist? >inb4 javascript
Looks like regex to me, or am I missing something here?
Joseph Morgan
It's just regex.
Tyler Clark
>i know regex OP!!!! yes i DO!! AND... ITS NOT HARD AT ALL!!!! xDDD >4 posts in 3 seconds
kill yourselves, retarded little nerds
Blake Brooks
All this is doing is taking the input string (str) and testing it against a regex that looks for a single word character that appears at any point between it and the rest of the input in a case-insensitive manner. If it's found, it returns the opposite of that.
Jacob Collins
>being this mad at not knowing regex
Nathaniel Evans
This is a terrible solution. Regex with backreferences possibly lead to exponential runtime. This can easily be done by return new Set(str.split('')).size === s;
which not only runs faster, but is more easily understood.
Ryan Martinez
>didn't know shit from week 2 of CS101 >get buttmad when people point it out Ok
John Hernandez
>can someone explain how people get this gud? experience
>top "clever" solutions not really useful outside coding challenges
real world code should be easy to read and modify by anybody
Lincoln Myers
i know regex and im not OP you retarded fucking nigger
im just pointing out the joy in a nerds soul when something he gets used to gets mentioned as "hard". Nerds are disgusting creatures and should get slaughtered to death.
Aiden Edwards
cheeky pattern matching, probably no more efficient than keeping tracking of seen letters with a hashtable under the hood though
Jonathan White
Just read the Wikipedia page on regular expressions.
It's not complicated, but it does take some practice to get used to it.
Camden Mitchell
It's a regex. A finite state machine. A subset of turing machines. You're literally worse than a fucking turing machine.
Jason Peterson
Regex with backreferences cannot be implemented with a finite state machine. Kys idiot.
David Martinez
What is "s" here?
Andrew Sanchez
You definitely don’t if you’re that triggered to post this. I’m not a full time dev and don’t find example anything special. Kid clearly is a student and nerds are saying “invest 3 hours and you’ll understand this too”.
Adam Torres
function isIsogram(s) { return new Set(s.split('')).size === s; }
Joshua Lewis
no they dont idiotic subhuman filth
they are saying "ive memorized useless dumb shit used in production code SOLELY for email verification and nothing else and thats why im smart!!!"
yeah fuck this function FUCK(s) { return new Set(s.split('')).size === s.length; }
Juan Brooks
Still returns false for "Helo"
Brody Moore
Oh you’re a front end “engineer” lol got it hey man you’re smart too congrats on maxing out at 80k a year that’s enough to buy a jet ski!
Blake Rivera
That particular language is regular though, since the backreferenced segment is of bounded length.
Adam Bennett
see
Wyatt Martin
Ok but now it's not case insensitive like in OP's image
David Harris
meant for
David King
It's seriously not a difficult regex if you think about the problem
isogram is a word with 1 repeated letter, so you do a check to see if any letter has the same letter later on in the word
if it does, it isn't an isogram
(\w) is a letter captured due to () .* means 0 or more letters \1 means the first captured group lastly ! means not
the i at the end btw means case-insentive afaik
Xavier Nguyen
Please tell us what you do for a living so we can all have a good chuckle over a 6 pack of Monsters
Hudson Gray
function g(s) { return new Set(s.toLowerCase().split('')).size === s.length; }
Dominic Cook
>*OP asks question* >*people answer* >*assblasted virgin barges into the thread to complain about people knowing regex* >*gets called out* >"k-k-kill yourself!" log out
Brayden Reed
I largely agree but the regular expression method does more: it checks for word characters and compensates for case.
Those extra features could easily be added to your method though.
Benjamin Torres
developer advocate for game evangelists at a small start up
Bentley Gray
\w is a word character that includes numbers and underscores. .* means 0 or more characters, not specifically letters
Daniel Moore
nothing really
i shit out REST apis and iphone apps like a pajeet
i dont give a flying fuck about programming, im in it for the money and im making more than you stupid neet faggot nigger
David Peterson
> im in it for the money
sad af what do you do in your free time/hoobies?
Brandon Howard
My bad, I knew but for some reason in my head, assumed it was all letters
Logan Ross
Ok but now it's not called isIsogram so it can't be swapped out with OP's code
Andrew Moore
unironically this is the obvious way to do it
Jacob Lewis
>if i call him a virgin theyll never guess that im the virgin!!
theres no log out function on Jow Forums idiotic dweeby moron
Josiah Ward
You probably have it down mentally already. I'm just being pedantic because I've been bitten by my own regex before
Levi Mitchell
couldn't care less
Luke Clark
Good job writing four posts with examples of unusable code
Logan Jackson
Sounds like a fulfilling existence. Why are you posting on a technology board again?
And I doubt it lol I live in SF I make more than your entire zip code bruddah
Dylan Jones
living life
unlike you
enjoy writing monad macros and jerking off to tranny porn for the rest of your life fucking freak
Carter Gonzalez
i'm not the same you guy you're replying too
>living life i bet
Chase Myers
wouldnt something like this work function isogram(str) { return str[0] == str[strlen(str)-1]; }
Anthony Bailey
Are you too scared to smoke weed so smoke that weird legal stuff and have empty beer cans on the shelf of your parents basement
Oliver Russell
So to recap, it says:
Find any "word character" (letter, number or underscore) which gets repeated anywhere down the string. (So "Fuck!!!!!" is not a match but "fuck off!" is) I do not care about case (so "Bob" is a match). Return true if no such match was found, else return false.
Cameron White
>mad lisp/julia/haskell virgin incels projecting their shitty lives on me
swift chads dont have time for virgins.
im gonna fuck my gf now, incels. Take care.
Joshua Nguyen
lel, get rekt
Kevin Barnes
function isIsogram(s) { return new Set(s.split('')).size !== s.length; }
function getRandomString(vocab, length, isogram = true) { let auxVocab = vocab.slice(); let picked = []; if (isogram) { for (let j = 0; j < length-1; j++) { picked.push(auxVocab[Math.floor(Math.random() * auxVocab.length)]); } picked.push(picked[0]); } else { for (let j = 0; j < length; j++) { picked.push(auxVocab.splice(Math.floor(Math.random() * auxVocab.length), 1)[0]); } } return picked; }
function test(vocab, length) { for (let i = 0; i < 100000; ++i) { if (isIsogram(getRandomString(vocab, length, false).join(''))) { console.error("False positive."); } } for (let i = 0; i < 100000; ++i) { if (!isIsogram(getRandomString(vocab, length, true).join(''))) { console.error("False negative."); } } }
test('abcdefghijklmnopqrstuvwxyz'.split(''), 10);
For testing purposes. Terribly inefficient but w/e, sue me
Oliver Bell
sour fucking grapes
Adam Morgan
yeah pretty much
Cooper Lewis
Properly handles unicode: function isIsogram(s) { var map, str, c, i, l, e, h; if (typeof s !== "string") return false; if ((l = s.length) < 2) return true; c = (str = s.toLowerCase()).codePointAt(); if ((h = c > 0xFFFF) && l < 4) return true; (map = {})[c] = true, e = l - 2, i = h ? 1 : 0; do { if (map[c = str.codePointAt(++i)]) return false; if (c > 0xFFFF) if (i === e) return true; else ++i; map[c] = true; } while (i < l); return true; }
Jose Rogers
OP here. i had never heard of regex. thanks for pointin it out frens. here was my solution btw function isIsogram(str){ const newStr = str.toLowerCase(); const arr = newStr.split(""); let letters = []; for (let i = 0; i < arr.length; i++) { if (letters.indexOf(arr[i]) !== -1) { return false; } letters.push(arr[i]); } return true; }
after i saw the solution in the OP pic, i was just left feeling like a faggot for writing ~15 lines in what could have been done in 3. based on these replies, regex isnt actually any more efficient than my code? its just syntactic sugar?
Cooper Fisher
No. You have to check all characters, not just the first and last.
ie: "Isogram" is an isogram. (each letter is unique) "No isogram" is no isogram. (`o` occurs twice)
Zachary Russell
That's a normal feeling. I've used C for 5 years, did a programming challenge in Python after 2 months practice, and saw my code being written in 3 times less lines, cause of what is actually sensical language constructs like lambdas, rather than the usual unreadable 1-liners
Nolan Butler
kek saw this a few weeks ago
Jonathan Stewart
There are only two good solutions for this. 1. Make a hash set of characters you've already seen. bail and return false if something is already in the set. This is O(n) best case, might degrade to something like O(n log n) if the hash map implementation sucks at choosing a good size.
2. Make an array of booleans, for each character, set use the character value as an index for the array, set that index to true, returning false if it was already true. This is O(n) guaranteed but worse for memory efficiency.
James Thomas
The regex does essentially the same thing, yes. It's probably faster due to optimizations, but there are also more efficient ways than your code. ("indexOf" is very slow)
Even if you choose not to use regular expressions in your code it's still a valuable thing to learn. Some command line tools (like grep) can use regular expressions. And some text editors (like Sublime text) can do a find-and-replace based on regular expressions. Both can be extremely useful sometimes and save you shitloads of work.
David Nguyen
>chads >developing iOS apps natively
You almost had me
Zachary Gomez
regex is read only.
Caleb Parker
OBVIOUSLY I MEANT WRITE ONLY
Adam Hernandez
Regex expressions are usually a very efficient solution when used correctly. In OP image backtracking can create issues. For short words it probably is more efficient but it can bite you in the ass if you drop a large one. user's solution is pretty nice as it leverages Set objects which naturally drop any duplicate values. I am not JS dev but this seems to me the best way someone can do it.
Colton Sullivan
This is actually faster: function isIsogram(str) { return new Set(str.toLowerCase()).size === str.length; } isIsogram with Set x 1,059,095 ops/sec ±0.44% (88 runs sampled) isIsogram x 666,490 ops/sec ±1.04% (94 runs sampled) Fastest is isIsogram with Set but it obviously does not work in environments where Set is not available
David Morgan
>handles unicode: Which raises the question: How does one check for Unicode word characters using regular expressions?
Jaxon Thomas
all these pajeets creating sets and wasting memory. I'd do sort + adjacent_find and not touch the heap at all.
Cameron Cox
All the chars you know can be represented in Unicode
David Edwards
>I'd do sort wut u doin lad
Jason Perry
>environments where Set is not available damn
Leo Price
You must allocate new memory one way or the other because strings are immutable in JS
Are there dynamic arrays? Copy the string, sort and adjacent find. Creating a set is still more expensive.
Mason White
>I am not JS dev but this seems to me the best way someone can do it.
I think Set() is pretty efficient in JS. But with large strings that solution does end up making a large Set which I suspect will be slower than checking for duplicates sooner.
Maybe process the string in chunks, returning as soon as one chunk contains a duplicate character (the likelihood of that should be pretty high if the chunks are long enough) And only if none of the chunks contain any duplicate characters combine the sets into larger and larger sets.
Zachary Phillips
isSetIsogram x 1,046,974 ops/sec ±0.51% (92 runs sampled) isIsogram x 655,609 ops/sec ±1.13% (91 runs sampled) isIsogram with sort and find x 509,137 ops/sec ±1.25% (95 runs sampled) isIsogram with arrays, maps and somes x 178,202 ops/sec ±0.22% (96 runs sampled)
Fastest is isSetIsogram
That's pretty horrible tbhfam
Something like this? function isSortIsogram(str) { const chars = [...str.toLowerCase()]; chars.sort(); const len = chars.length - 1; let i = 0; while (i < len) if (chars[i] === chars[++i]) return false; return true; } Well see above and you'll see the results
Nicholas Sanders
>(\w).*\1
Lemme explain it to you "(\w)" is to match any word-character. which is the same as a-z, A-Z, and 0-9.
".*" is to match an amount(zero or more) characters of any type. The "." means "any character"(except for newline) and the "*" means "Zero or more"
"\1" is a back-reference, that references the whatever matched the "(\w)" from earlier
So basically, this full regex expression only matches strings that have repeated characters. So it won't match "fou" but it will match "foo" because it will match "o" for "(\w)" and it will find another "o" immediately after using the \1.
So first it detects if there are any characters that repeat, which is the opposite of an isogram. Then it inverses the rest result. So it will first find if there are any repeating characters, and then invert this result to find out if it is an isogram.
Use this tool to test out your regex expression as it explains your regex expression in plain english
I mean I want to differentiate between Unicode characters that may occur in words, and crap like currency symbols, mathematical symbols, arrows, emoji's, etc.
Could be for a name field where I'd allow Chinese names but not nigger names like Ty$
Matthew Garcia
Actually, made a mistake there by going out of bounds of the array at the end function isSortIsogram(str) { const chars = [...str.toLowerCase()]; const len = chars.length; if (len < 2) return true; chars.sort(); const end = chars.length - 2; let i = 0, c = chars[i]; do if (c === (c = chars[++i])) return false; while (i < end); return true; } Didn't change much regarding performance tho
Grayson Jackson
>retarded opinions not based in fact or reasonable argument >r*ddit spacing Please stop LARP'ing on Jow Forums, no one finds you interesting or funny and you can't get upboats here. Leave you disgusting piece of shit
Grayson Rodriguez
Interesting, what input sizes where you testing? Given that the JS reference for sort doesn't even specify complexity, I'm not that surprised.
So anons how will you do in your language? Posting the Master Foo way. isIsogram () { test `echo $1 | grep -o . | sort -f | uniq -id` && echo 'false' || echo 'true' }
Luke Campbell
Why so angry, user? Kindly please back up your statements with reasonable arguments so we can make fun of you more sir, thank you
Jack Thompson
You will either waste memory or speed. You cannot have both.
James Nguyen
what notepad ++ theme
Nathaniel Williams
>does not work in environments where Set is not available Isn't Set a part of the language? How cannot be available?
Hudson Gray
Are you benchmarking with a single input? Wouldn't that be prone to caching?
Mason Jones
If you using IE8.
Blake Wilson
Nobody does this
James Nelson
Who uses Internet Explorer at all?
Asher Phillips
Enterprise companies.
Luis James
Waher
String literals get interned anyway, doesn't change anything really and we are measuring the functions' runtime anyway
Christian Thomas
fuck me these solutions are terrible 1. If string length > 256, return false 2. Else, return len(set(str)) == len(str) Early exit will likely slow you down for small inputs (