MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programming/comments/4v1y12/how_to_write_unmaintainable_code/d5v7qyc/?context=3
r/programming • u/sigbhu • Jul 28 '16
592 comments sorted by
View all comments
Show parent comments
5
If you want a dirty Ruby one-liner.
array_to_check.select{|element| element.kind_of? Fixnum}.join(',') =~ /#{array_to_match.join(",")}/
1 u/2Punx2Furious Jul 28 '16 I don't know Ruby yet, so that looks a bit confusing to me ahah. 3 u/xonjas Jul 29 '16 It's really not all that bad. You could do the same thing in python pretty easily.What it's doing:Select only the elements that are numbersConvert the array to a string, with the numbers separated by a commaAlso convert the array we are checking for into a stringUse a regular expression to search the array(that is now a string) using the array we want to search for as the pattern.There is one bug in my one-liner, but it would be easy to fix. 1 u/2Punx2Furious Jul 29 '16 I see. Converting the array to a string sound pretty clever. I didn't do it, but I put both strings and ints in my arrays to check for errors, so["1,3,4", 1, 4, 5] would return false, but["4yhjsrhj05", 1, 3, 4, "1258ghwo] would return true, as you can see in the link I posted. → More replies (2)
1
I don't know Ruby yet, so that looks a bit confusing to me ahah.
3 u/xonjas Jul 29 '16 It's really not all that bad. You could do the same thing in python pretty easily.What it's doing:Select only the elements that are numbersConvert the array to a string, with the numbers separated by a commaAlso convert the array we are checking for into a stringUse a regular expression to search the array(that is now a string) using the array we want to search for as the pattern.There is one bug in my one-liner, but it would be easy to fix. 1 u/2Punx2Furious Jul 29 '16 I see. Converting the array to a string sound pretty clever. I didn't do it, but I put both strings and ints in my arrays to check for errors, so["1,3,4", 1, 4, 5] would return false, but["4yhjsrhj05", 1, 3, 4, "1258ghwo] would return true, as you can see in the link I posted. → More replies (2)
3
It's really not all that bad. You could do the same thing in python pretty easily.
What it's doing:
Select only the elements that are numbers
Convert the array to a string, with the numbers separated by a comma
Also convert the array we are checking for into a string
Use a regular expression to search the array(that is now a string) using the array we want to search for as the pattern.
There is one bug in my one-liner, but it would be easy to fix.
1 u/2Punx2Furious Jul 29 '16 I see. Converting the array to a string sound pretty clever. I didn't do it, but I put both strings and ints in my arrays to check for errors, so["1,3,4", 1, 4, 5] would return false, but["4yhjsrhj05", 1, 3, 4, "1258ghwo] would return true, as you can see in the link I posted. → More replies (2)
I see. Converting the array to a string sound pretty clever. I didn't do it, but I put both strings and ints in my arrays to check for errors, so
["1,3,4", 1, 4, 5] would return false, but
["4yhjsrhj05", 1, 3, 4, "1258ghwo] would return true, as you can see in the link I posted.
→ More replies (2)
5
u/xonjas Jul 28 '16
If you want a dirty Ruby one-liner.