Skip to content

Instantly share code, notes, and snippets.

@kulicuu
Last active June 5, 2018 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kulicuu/57bedf018832093b32375063ff705a8e to your computer and use it in GitHub Desktop.
Save kulicuu/57bedf018832093b32375063ff705a8e to your computer and use it in GitHub Desktop.
string sorte
# Word sort:
# I had an interview assessment test which had this requirement but I didn't realise it was permitted to use JS API tools like LocaleCompare or something. I started a word sort function but didn't finish it. This is for that, a basic word/string sorting function.
c = console.log.bind console
color = require 'bash-color'
_ = require 'lodash'
fp = require 'lodash/fp'
x3 = [
"Aerosmith"
"MeatPuppets"
"BadCompany"
"FrankBlack"
"Fishbone"
"BadReligion"
"Epsilon"
"Aeroplane" # Aerosmith duplex
"MeatDuplex"
]
score_str = ({ str }) ->
str = str.toLowerCase()
_.reduce str, (acc, char, idx) ->
x99 = switch char
when 'a'
1
when 'b'
2
when 'c'
3
when 'd'
4
when 'e'
5
when 'f'
6
when 'g'
7
when 'h'
8
when 'i'
9
when 'j'
10
when 'k'
11
when 'l'
12
when 'm'
13
when 'n'
14
when 'o'
15
when 'p'
16
when 'q'
17
when 'r'
18
when 's'
19
when 't'
20
when 'u'
21
when 'v'
22
when 'w'
23
when 'x'
24
when 'y'
25
when 'z'
26
else
null
unless x99 is null
acc.push x99
acc
, []
find_duplicate_intervals_in_scored_rayy = ({ scored_rayy, digit }) ->
# c 'scored-Rayy medial', scored_rayy, '\n'
duplicate_interval_rayy = _.reduce scored_rayy, (acc, str_arq, idx) ->
c 'str_raq', str_arq
c 'digit', digit
score_at_digit = str_arq.score[digit]
broken = false
kdx = idx
collection = [ idx ]
while (not broken) and (++kdx < scored_rayy.length)
c scored_rayy[kdx].score[digit], score_at_digit
if scored_rayy[kdx].score[digit] is score_at_digit
c '8llasr.cheusatoehusatoheus'
collection.push kdx
else
broken = true
c collection.length
if collection.length > 1
acc.push
head: idx
tail: collection.pop()
acc
, []
c 'duplicate_interval_rayy', duplicate_interval_rayy
c scored_rayy
duplicate_interval_rayy
quicksort_scored_rayy = ({ scored_rayy, digit }) ->
len = scored_rayy.length
if (len is 1) or (len is 0) then return scored_rayy
rand_idx = ( Math.floor ( Math.random() * len ) )
[ pivot ] = scored_rayy.splice rand_idx, 1 # TODO Preempt cas which fails due to no char at that digit.
less_rayy = []
more_rayy = []
for str_arq, idx in scored_rayy
num = str_arq.score[digit] # TODO preempt case which fails due to no char at that digit
# c 'digit', digit
# c 'num', num
# c 'word', str_arq.str
if num < pivot.score[digit]
less_rayy.push str_arq
else
more_rayy.push str_arq
less_sorted = quicksort_scored_rayy
scored_rayy: less_rayy
digit: digit
more_sorted = quicksort_scored_rayy
scored_rayy: more_rayy
digit: digit
medially_sorted = less_sorted.concat pivot, more_sorted
dup_rayys = find_duplicate_intervals_in_scored_rayy
scored_rayy: medially_sorted
digit: digit
_.map dup_rayys, (interval, idx) ->
{ head, tail } = interval
former = medially_sorted.slice 0, head
middle = quicksort_scored_rayy
scored_rayy: medially_sorted.slice head, tail + 1
digit: digit + 1
latter = medially_sorted.slice tail + 1
medially_sorted = former.concat middle, latter
medially_sorted
str_sort = ({ str_rayy }) ->
scored_rayy = _.reduce str_rayy, (acc, str, idx) ->
acc.push
score: score_str { str }
str: str
acc
, []
# c scored_rayy
c '\n'
x99 = quicksort_scored_rayy
scored_rayy: scored_rayy
digit: 0
c '\n'
c '3838', x99
str_sort
str_rayy: x3
@kulicuu
Copy link
Author

kulicuu commented Jun 5, 2018

Word sort based on quicksort, not complete.
(was an interview timed test question, though I could have used the LocalCompare function for the test, it got me trying to write a from-scratch string sorter based on quicksort.

@kulicuu
Copy link
Author

kulicuu commented Jun 5, 2018

It basically works now, but should be able to handle edge cases like space and special characters, and what to do if run out of letters to break ties, in which case would use the shorter string, but I haven't implemented stuff like that yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment