Requesting feedback for a few-liner solution


  • 0
    V

    I have a few-liner solution using map. It works for me. However, when I submit, the site reports failure

    Input:
    a
    
    Output:
    gramoches - 49\nsambucus - 47\ncaodaist - 47\ntestata - 45\nglacis - 44\nunprimmed - 39\nnoncirculation - 37\nwrongly - 36\nheteronereis - 35\ngutterling - 35\nlimacon - 34\nchassis - 33\ntongman - 31\naftercrop - 31\nacetamidin - 30\nblackband - 29\ncutely - 28\nunhad - 26\nuncaptiously - 26\nrespersive - 25\nunturreted - 23\nlapped - 23\nsquelch - 22\ninterfere - 21\nkyphoscoliosis - 20\nbetulinol - 18\narthropyosis - 18\nhimyaric - 16\nrazorless - 15\ndisheart - 15\nukiyoye - 13\nrepermit - 13\na - 13\nclothbound - 12\ncan - 12\ntracked - 11\ntrevor - 10\nhydrogalvanic - 10\ncarpological - 10\nterrifiedly - 9\npilotship - 9\nkhanda - 9\ncamball - 9\nunnew - 8\nmuilla - 7\nmorphologist - 7\nhypoplankton - 7\ncanner - 7\nacolhuan - 6\nstitchlike - 5\nobeisant - 5\ncorroboree - 5\nunepistolary - 4\nthe - 4\nophthalmist - 4\ndotterel - 4\ncans - 4\nas - 4\nsun - 3\nmany - 3\nlinguata - 3\nis - 3\nc - 3\nb - 3\nvery - 2\nunaugmented - 2\nsunset - 2\nsunny - 2\nn - 2\nif - 2\nglyphic - 2\nuncaptiouslyntestata - 1\nukiyoyenlapped - 1\ntrevornophthalmist - 1\ntrevornhypoplankton - 1\ntrevorncamball - 1\nthenthe - 1\nterrifiedlynophthalmist - 1\nterrifiedlyncorroboree - 1\nsambucusngramoches - 1\nnwrongly - 1\nnhimyaric - 1\nnaftercrop - 1\nmorphologistnsambucus - 1\nlooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong - 1\nleetcode - 1\nkhandantrevorncamball - 1\nkhandankhanda - 1\nintravasation - 1\nhow - 1\ngutterlingnglacis - 1\nglyphicncorroboreencamball - 1\nglacisncaodaist - 1\ndisheartntestata - 1\nday - 1\ncansna - 1\ncanna - 1\ncamballnunnew - 1\nbatrachoidndeferential - 1\nbatrachoid - 1\narthropyosisnnoncirculation - 1\nana - 1\naa - 1
    
    Expected:
    a 1
    

    I know the solution is verbose but can you help me spot the bug in the script? Not sure if output makes sense.

    freq.sh

    declare -A freq
    
    fields=($(cat $input | awk -F: '{$1=$1} 1'))
    
    while read field; do
    	freq[$field]=$((freq[$field] + 1))
    done <<<"$(printf "%s\n" "${fields[@]}")"
    
    for word in "${!freq[@]}"; do
    	echo "$word - ${freq["$word"]}";
    done | sort -rn -k3
    

    and you could use as follows: cat input.txt | ./freq.sh


  • 1
    C
    1. The judging system will not run cat input.txt | ./freq.sh for you. You need to make sure you can get the result from your code directly.
    2. The output of your code is in form of a - 1\nb - 2, but no - is needed, it should be a 1\nb 2. Pls read the description carefully.
    3. You did not sort the result.

    I think that's all.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.