Created
July 15, 2016 18:05
-
-
Save mickelsonm/46be98e69c2b9f05a89a74eff140a853 to your computer and use it in GitHub Desktop.
String sort tex
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
package main | |
import ( | |
"fmt" | |
"sort" | |
) | |
const ( | |
//sort the characters in the following string: | |
charsText = "abcdefghijklmnopqrstuvwxyz_" | |
//by the number of occurances in the following text (descending): | |
searchText = `epqiiqwdiwgyka_vsqtsujeqqicnhyivo_sigwasmkwgsih_akl_gtnkhgikgveidpmt | |
qybpxpnnpbxkwpisgjmdzgh_ojysbtsnsvxvuhguocp_qc_vouxqmg_cetlpmounxnvg | |
ldcpem_jodnmklgonocekdkjwkdoilajk_nxujykigsolengqmnqofpseqaamvpsooga | |
spyhoojennefwvljpvsqtgnceg_hsowqvycjkuxdtfbxfloewkphmvkftjlsasvwid_u | |
qcsgn_ypiqjytygiwyziqdjpxgpuunymadnclpdlmmulitsnqlwciotbmyfuummjynne | |
slnit_lpykdafkpydzkntbud_gigjgmu_uqjjmdzpwteodjpuzndxaqmsjdjjamnwoes | |
ajcffkaaoilpyydlkyxauagfcjbabapax_ndlgtpwnud_jpnkiokviqjhyopmjtgtbyo | |
iyfbjdhknimlah_cxfzwspqoscffiyvabtjjuc_liaqbcuomuytdqfy_xaixiiqqdpds | |
uuimzh_ywwcmodxhfxjplyixotjkeawauxltekptuieekpbokbanumffatbtiacnywhw | |
iqxebnosninpzfjmatvnyuspyeu_ziapvogconld_cxfcytkcp_bvsppz_dw_ndlpkhf | |
zdlxbo_vaflmailjvccgsuclyhojganjqxzmqflpze_hqhlul_ybaagtiuokbzaxhmec | |
olsptiexvvmhbdoelgmcffulcebhlyzd_m_qxkbfvnxykdudpxefsm_aqpqtnhxvswht | |
owqnbm_mgejjpyumm_mqbkiuulanbmzllmuqlfftmcxtybmijfuwaknefhekwgujpjqg | |
leu_sjtbszotcygiclkwcbmnvgsoqaqqkkgeaslhvfbtlgpnxgpzxp_vyjinlwwfbvtn | |
twogmnpxghabpxxgzlyirrrrrbbcrrrnbjpcrrrqykhrrrscarrrdnlxrrrrtudrrrr_ | |
ntrbyrqlddbycypcccqongpgexhnabavrmebeofrxsnrilprveetxaranjyfmrisrewp | |
r_y_lgsrsedbn_rfrieusemhpfa_plkifjipvwaqvnenrrrzybsrbeurbhfrvrrzghr_ | |
zpgiyrrrqsnnrrrbhvdrrrqkpdrraqvkeueszfpkj_fm_claw_oetbgurbdocb_rsnzr | |
cyvrvnrvaurbscimurtbriikrfdjlizribdjwkror_gnlzmshwccqcx_huaafbvituxo | |
ru_hohxwrrrhnbttrrriyyirrrnibricrxftrrrrvqvrrrrhjorehroldibsmquelwvy | |
jebkolbbnauompgqdhlbnsfbbdiudoeibwstdg_acsazhtgfufidogmyvtya_dfwihto | |
elucbtlcbaijlcuhfvhesgluiwttsdnqqshnoqumccyqtko_zh_fii_wlsspysdqdpad | |
fvfewlsojavmuaixyxpw_xcwxuatceosdqgmsbbagjmmblouvnywmqqakmmtuasfovol | |
_ogksdukwp_fkxuh_vfhuhfyfvvfqhqxecxsoctcqgpianhtnkbqlltwyhxotfksoewm | |
elxobjgwlyfaeoxsfohhguidoftbsainwovvglynsgjixon_nvuwflsfbca_xnnesvco | |
mceh_gigjxpllckcooagidcpbqxtnejlnlsccocuvcvge_fvjjbyqdkjceia_mkcvbzl | |
zwlxbdjihvpmdcvmssuvktwiqbeivtieol_bu_huumzmlxx_kd_vksmohgzl_fxwfdue | |
lqgfkgzxciwmuduozfbaxstxkwegescggkpxfpeenhb_whqhethcateqdvnxhpt__bja | |
_uiyxchmfkblmdwtyp_ktontmufw_isdflelsbgjizxvqbciuadfxxjaqbluofkgkkkh | |
jbvohisfla_cspbmuezqohnyijyimwgdeszutgnaoagbhku_wwdtylbbiyvbpoumgyid | |
w_xwg_fkogabccip_wouclnjcgdpwwxxvvvwkmmbgfeactbcksxqovqthtjfjghijwwh | |
ydfieyssbjtfqgqyjnmwfpesljmwapvbptucadontbobnspch_i_dxheklulncdsdnic | |
bnjjjedkaokw_ahcolvbcnmqtoakonpgzjufqlnn_uve_uumaufjasfvfcv_cbcuk_hd | |
zigkahchzfqjphjwcbjwmozyodhu_tsqtafwidgmc_snhhkleyvmzdtawdodzfmekuee | |
mnshz_xz` | |
//Now take the sorted string, and drop all the characters after | |
//(and including) the _. The remaining word is the answer. | |
) | |
//TextPiece is a piece of the text | |
type TextPiece struct { | |
Text string | |
Count int | |
} | |
//TextPieces is a slice of TextPiece | |
type TextPieces []TextPiece | |
//Sets up sorting for text pieces | |
//Default sorting will be based on the count and in descending order | |
func (s TextPieces) Len() int { return len(s) } | |
func (s TextPieces) Swap(i, j int) { s[i], s[j] = s[j], s[i] } | |
func (s TextPieces) Less(i, j int) bool { return s[i].Count > s[j].Count } | |
func main() { | |
//constructs a piece store/map for keeping track of parts of the text | |
pieceStore := make(map[string]TextPiece) | |
for _, c := range charsText { | |
char := string(c) | |
pieceStore[char] = TextPiece{ | |
Text: char, | |
} | |
} | |
//analyzes the text...counting number of occurances in the text | |
for _, t := range searchText { | |
char := string(t) | |
if piece, found := pieceStore[char]; found { | |
piece.Count++ | |
pieceStore[char] = piece | |
} | |
} | |
//sorts the text pieces in descending order of occurances | |
var pieces TextPieces | |
for _, p := range pieceStore { | |
pieces = append(pieces, p) | |
} | |
sort.Sort(pieces) | |
//reconstructs the pieces to figure out what the answer is | |
answer := "" | |
for _, p := range pieces { | |
//drops all pieces after and including _ | |
if p.Text == "_" { | |
break | |
} | |
answer += p.Text | |
} | |
fmt.Printf("Answer is: %s\n", answer) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment