The following code is readable and I have required a variation of it in a program before:
sorted([ord(c) for c in
set('letters in this sentence') & set('and this one')], reverse=True)
Doing it in Java would be a chore now.The following code is readable and I have required a variation of it in a program before:
sorted([ord(c) for c in
set('letters in this sentence') & set('and this one')], reverse=True)
Doing it in Java would be a chore now. (use 'clojure.set)
(reverse (sort (map int
(intersection (set "letters in this sentence")
(set "and this one")))))
Edit: Take two... (use 'clojure.set)
(->> (intersection (set "letters in this sentence")
(set "and this one"))
(map int) sort reverse)
(experienced lispers, feel free to make this more idiomatic) (require ['clojure.set :as 's])
(-> (for [c (s/intersection (set "letters in this sentence")
(set "and this one"))] (int c))
sort
reverse)(Set("letters in this sentence":_* ) & Set("and this one":_* )).toSeq.sorted.reverse
Or you, can take advantage of Scala's rich choice of collections.
import scala.collection.immutable.TreeSet
import scala.math.Ordering._
TreeSet("letters in this sentence":_* )(Char.reverse) & Set("and this one":_* ) toSeq
The machinery Scala has in place to make the second example work is quite impressive, and I was pleased to see that it worked. Even though the intersection method returns a brand new set, using implicit arguments it correctly creates a set of the correct type (TreeSet) with the correct ordering function (Char.reverse), without any duplication of code in the standard library (like overriding '&' in every sub-class).
("letters in this sentence" + "and this one").chars.sort.reverse
I can just as easily write ("letters in this sentence" + "and this one").toSeq.sorted.reverse in scala. You could tag the end of your ruby statement with .uniq and you would be fine. Alternatively, you could wrap the above scala statement in TreeSet(). These solutions process things in a significantly different way than was done by the OP, though.
I'm just learning, but I think this performs it correctly with sets.
Scala isn't as succinct, of course, but it gets much closer!
1. I love python.
2. The OP was being a troll.
3. People need to be more honest about themselves and their favorite pet languages. I can write a sorted set in assembler too, if I wanted. However, there's been several times that I've wanted a sorted set data type while coding in Python but found that none was easily available. I hate the, "you can write your own!" counterpoint, because it is essentially meaningless unless you can prove that writing your own is as trivial as importing someone else's implementation.
By the way, in case anybody was wondering, here is a (relatively inefficient) way of making a sorted set in python:
from random import choice
choice(list(myset))
(I'm guessing you'd want to do it without having to convert it to a list first?)