首页 > 技术文章 > LeetCode Bash练习

orchidzjl 2017-05-04 11:02 原文

195. Tenth Line

#!/bin/bash
i=0
cat file.txt | while read line
do
    #echo $line
    if [ $i -eq 9 ]
        then echo $line
    fi
    
    let i=i+1
done
    

 194. Transpose File

# Read from the file file.txt and print its transposed content to stdout.
#########################################################################
# File Name: 194.sh
# Author: atrp
# mail: scau_zjl@163.com
# Created Time: Thu 04 May 2017 09:02:39 AM EDT
#########################################################################
#!/bin/bash
i=1
function getLen() {
    num_column=$#
}
read line < file.txt
getLen $line
while [ 0 -lt 1 ]
do
    column=`cut -d' ' -f$i ./file.txt`
    echo ${column}
    if [ $i -eq ${num_column} ]
    then
        break
    fi
    let i++
done

 

193. Valid Phone Numbers

#########################################################################
# File Name: 193.sh
# Author: atrp
# mail: scau_zjl@163.com
# Created Time: Wed 03 May 2017 11:08:51 PM EDT
#########################################################################
#!/bin/bash
grep -P "^\(\d{3}\)\s\d{3}-\d{4}$|^\d{3}-\d{3}-\d{4}$" file.txt

 

192. Word Frequency

###########################################################################
# File Name: 192.sh
# Author: atrp
# mail: scau_zjl@163.com
# Created Time: Thu 04 May 2017 06:38:21 AM EDT
#########################################################################
#!/bin/bash
declare -A map1=()
function calc() {
    #echo $@
    for i in $@
    do 
        let map1["$i"]++
        #echo ${map1["$i"]}
    done
}
function out() {
    times=0
    while [ $times -lt ${#map1[@]} ]
    do
        max=0
        id=0
        for i in ${!map1[@]}
        do
            if [ ${max} -lt ${map1[$i]} ]
            then
                max=${map1[$i]}
                id=$i
            fi
        done
        echo "${id} ${max}"
        map1[${id}]=0

        let times++
    done
}
while read line
do
    #echo $line
    calc $line
done < words.txt

out

 

推荐阅读