MIME Type

题目链接 MIME TYPE

思路:

  • 读取 file extension 和 MIME type(校验长度), 创建 Hash Tables.
  • 读取 file name(校验长度)
    • 若 file name 不包含"."则输出 UNKNOWN.
    • 若 file name 包含"."则输出 file name 后缀值在 Hash Tables 中对应的 value 值, 若无则输出 UNKNOWN.

知识点:

  • declare -A tmp : 定义关联数组(Hash Tables)
  • ${tmp^^} : 将 $tmp 中的小写字母全部转大写, 参考 man bash
  • ${##*.} : 获取 $tmp 中最右侧"."之后的字符串, 参考 man bash
  • ${tmp:-demo} : 若 $tmp 未定义或为 null 值, 则使用 demo 替换 $tmp, 否则为 $tmp, 参考 man bash

Bash 代码如下:

# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.

# N: Number of elements which make up the association table.
read -r N
# Q: Number Q of file names to be analyzed.
read -r Q
#定义关联数组(Hash Tables) ext_mts
declare -A ext_mts
for (( i=0; i<$N; i++ )); do
    # EXT: file extension
    # MT: MIME type.
    read -r EXT MT
	#校验长度
    if [[ ${#EXT} -gt 10 ]] || [[ ${#MT} -gt 50 ]];then
        continue
    fi
    #EXT 转大写加盐(避免 key 值为空)作为 key 值, MT 作为 value 值存储于 ext_mts 中
    ext_mts["${EXT^^}a"]="$MT"
done
for (( i=0; i<$Q; i++ )); do
    # FNAME: One file name per line.
    read -r FNAME
    if [[ ${FNAME} -gt 250 ]];then
    	continue
    fi
    #通过截取 FNAME 后缀(FNAME 中最后一个 '.' 右侧的字符)与 FNAME 本身作等式比较, 判断 FNAME 是否包含 '.', 等式成立则不包含
    if [[ "${FNAME##*.}" == "$FNAME" ]]; then
        ext_mt=""
    else
        ext_mt=${FNAME##*.}
    fi
    #获取 ext_mts 中 key 值为 FNAME 后缀加 'a' 对应的 value 值, 如不存在或为 null 则输出 UNKNOWN, 否则输出 value值
    echo ${ext_mts[${ext_mt^^}a]:-UNKNOWN}
done

# Write an action using echo
# To debug: echo "Debug messages..." >&2

# For each of the Q filenames, display on a line the corresponding MIME type. If there is no corresponding type, then display UNKNOWN.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值