Created
November 17, 2015 05:07
-
-
Save peterood/153649a6d1be78e72918 to your computer and use it in GitHub Desktop.
sort text by character frequency to find word
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Application problem for https://www.fogcreek.com/Jobs/SupportEngineer/ | |
var text = "epqiiqwdiwgyka_vsqtsujeqqicnhyivo_sigwasmkwgsih_akl_gtnkhgikgveidpmtqybpxpnnpbxkwpisgjmdzgh_ojysbtsnsvxvuhguocp_qc_vouxqmg_cetlpmounxnvgldcpem_jodnmklgonocekdkjwkdoilajk_nxujykigsolengqmnqofpseqaamvpsoogaspyhoojennefwvljpvsqtgnceg_hsowqvycjkuxdtfbxfloewkphmvkftjlsasvwid_uqcsgn_ypiqjytygiwyziqdjpxgpuunymadnclpdlmmulitsnqlwciotbmyfuummjynneslnit_lpykdafkpydzkntbud_gigjgmu_uqjjmdzpwteodjpuzndxaqmsjdjjamnwoesajcffkaaoilpyydlkyxauagfcjbabapax_ndlgtpwnud_jpnkiokviqjhyopmjtgtbyoiyfbjdhknimlah_cxfzwspqoscffiyvabtjjuc_liaqbcuomuytdqfy_xaixiiqqdpdsuuimzh_ywwcmodxhfxjplyixotjkeawauxltekptuieekpbokbanumffatbtiacnywhwiqxebnosninpzfjmatvnyuspyeu_ziapvogconld_cxfcytkcp_bvsppz_dw_ndlpkhfzdlxbo_vaflmailjvccgsuclyhojganjqxzmqflpze_hqhlul_ybaagtiuokbzaxhmecolsptiexvvmhbdoelgmcffulcebhlyzd_m_qxkbfvnxykdudpxefsm_aqpqtnhxvswhtowqnbm_mgejjpyumm_mqbkiuulanbmzllmuqlfftmcxtybmijfuwaknefhekwgujpjqgleu_sjtbszotcygiclkwcbmnvgsoqaqqkkgeaslhvfbtlgpnxgpzxp_vyjinlwwfbvtntwogmnpxghabpxxgzlyirrrrrbbcrrrnbjpcrrrqykhrrrscarrrdnlxrrrrtudrrrr_ntrbyrqlddbycypcccqongpgexhnabavrmebeofrxsnrilprveetxaranjyfmrisrewpr_y_lgsrsedbn_rfrieusemhpfa_plkifjipvwaqvnenrrrzybsrbeurbhfrvrrzghr_zpgiyrrrqsnnrrrbhvdrrrqkpdrraqvkeueszfpkj_fm_claw_oetbgurbdocb_rsnzrcyvrvnrvaurbscimurtbriikrfdjlizribdjwkror_gnlzmshwccqcx_huaafbvituxoru_hohxwrrrhnbttrrriyyirrrnibricrxftrrrrvqvrrrrhjorehroldibsmquelwvyjebkolbbnauompgqdhlbnsfbbdiudoeibwstdg_acsazhtgfufidogmyvtya_dfwihtoelucbtlcbaijlcuhfvhesgluiwttsdnqqshnoqumccyqtko_zh_fii_wlsspysdqdpadfvfewlsojavmuaixyxpw_xcwxuatceosdqgmsbbagjmmblouvnywmqqakmmtuasfovol_ogksdukwp_fkxuh_vfhuhfyfvvfqhqxecxsoctcqgpianhtnkbqlltwyhxotfksoewmelxobjgwlyfaeoxsfohhguidoftbsainwovvglynsgjixon_nvuwflsfbca_xnnesvcomceh_gigjxpllckcooagidcpbqxtnejlnlsccocuvcvge_fvjjbyqdkjceia_mkcvbzlzwlxbdjihvpmdcvmssuvktwiqbeivtieol_bu_huumzmlxx_kd_vksmohgzl_fxwfduelqgfkgzxciwmuduozfbaxstxkwegescggkpxfpeenhb_whqhethcateqdvnxhpt__bja_uiyxchmfkblmdwtyp_ktontmufw_isdflelsbgjizxvqbciuadfxxjaqbluofkgkkkhjbvohisfla_cspbmuezqohnyijyimwgdeszutgnaoagbhku_wwdtylbbiyvbpoumgyidw_xwg_fkogabccip_wouclnjcgdpwwxxvvvwkmmbgfeactbcksxqovqthtjfjghijwwhydfieyssbjtfqgqyjnmwfpesljmwapvbptucadontbobnspch_i_dxheklulncdsdnicbnjjjedkaokw_ahcolvbcnmqtoakonpgzjufqlnn_uve_uumaufjasfvfcv_cbcuk_hdzigkahchzfqjphjwcbjwmozyodhu_tsqtafwidgmc_snhhkleyvmzdtawdodzfmekueemnshz_xz"; | |
// Sort scrambled text | |
var sortedText = text.split("").sort().join(""); | |
// Initialize object to store frequency of characters in text | |
var frequency = { | |
_: 0, | |
a: 0, | |
b: 0, | |
c: 0, | |
d: 0, | |
e: 0, | |
f: 0, | |
g: 0, | |
h: 0, | |
i: 0, | |
j: 0, | |
k: 0, | |
l: 0, | |
m: 0, | |
n: 0, | |
o: 0, | |
p: 0, | |
q: 0, | |
r: 0, | |
s: 0, | |
t: 0, | |
u: 0, | |
v: 0, | |
w: 0, | |
x: 0, | |
y: 0, | |
z: 0 | |
} | |
var j = 0; | |
// Loop through the object's keys to update values with character frequency | |
for (i in frequency) { | |
// Calculate frequency by determining distance from current character to the next | |
frequency[i] = sortedText.search(Object.keys(frequency)[j + 1]) - sortedText.search(Object.keys(frequency)[j]); | |
// If the last character, subtract its position from text's length | |
if (i === "z") { | |
frequency[i] = sortedText.length - sortedText.search(Object.keys(frequency)[j]); | |
} | |
j++; | |
} | |
// Convert frequency object to an array to sort | |
var sortable = []; | |
for (var character in frequency){ | |
sortable.push([character, frequency[character]]) | |
} | |
sortable.sort(function(a, b){ | |
return b[1] - a[1]; | |
}) | |
// Take characters from sorted array to form word | |
var sortedWord = []; | |
for (var i = 0; i < sortable.length; i++) { | |
sortedWord.push(sortable[i][0]); | |
} | |
// Display word preceding underscore | |
var underscorePosition = sortedWord.join("").search("_"); | |
console.log(sortedWord.join("").substring(0, underscorePosition)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment