# split each piece of record using character '"', then $6 is $http_user_agent
# More information: https://en.wikipedia.org/wiki/User_agent # User agent tool: https://useragent.buyaocha.com # It should be declared that Edge has a UA ends with `Edg/version`, instead of `Edge/version`, this conclusion can be obtained by `access.csv` and test using Edge. # Here, the method of classifying the explorers using the special matching, like unique identifier, although it may not always be accurate.
cat access.csv | awk -F '"''{ print $6 }' | awk '{ if ( $0 ~ /Edg/ ) { print "#Microsoft Edge"; # Here the character `#` denotes the separator between the count and the name. Otherwize, the processing of split may be difficult in next steps. } else if ( $0 ~ /Firefox/ ) { print "#Firefox"; } else if ( $0 ~ /Chromium/ ) { print "#Chromium"; } else if ( $0 ~ /Chrome/ || $0 ~ /CriOS/ ) { print "#Chrome"; } else if ( $0 ~ /Safari/ ) { print "#Safari"; } else if ( $0 ~ /Android/ && $0 ~ /WebKit/ ) { print "#WebKit"; } else if ( $0 ~ /MSIE/ || $0 ~ /Trident/ ) { print "#MSIE"; } else { printf "#%s\n", $0; } }' | sort | uniq -c | awk -F '#'' BEGIN { totCnt = 0; print "Counting explorers" print "Percent/% Explorer Name"; } { cnt[NR] = $1; totCnt += cnt[NR]; name[NR] = $2; } END { for (i = 1; i <= NR; ++i) { printf "%8.3f%% %-20s\n", 100 * cnt[i] / totCnt, name[i] } print ""; }'
cat access.csv | awk -F '"''{ print $6 }' | awk '{ st = index($0, "("); en = index($0, ")"); # Get the information from the first pair of bracket if (st && en) { info = substr($0, st+1, en-st-1); } else{ info = $0; } # print info; if ( info ~ /iPhone/ ) { print "#iOS"; # Here the character `#` denotes the separator between the count and the name. Otherwize, the processing of split may be difficult in next steps. } else if ( info ~ /iPad/ ) { print "#iPadOS"; } else if ( info ~ /Mac OS/ ) { print "#macOS"; } else if ( info ~ /Windows NT/ ) { print "#Windows"; } else if ( info ~ /Android/ ) { print "#Android"; } else if ( info ~ /Linux/ ) { print "#Linux"; } else if ( info ~ /OpenBSD/ ) { print "#openBSD"; } else { printf "#%s\n", info; } }' | sort | uniq -c | awk -F '#'' BEGIN { totCnt = 0; print "Counting systems" print "Percent/% System Name"; } { cnt[NR] = $1; totCnt += cnt[NR]; name[NR] = $2; } END { for (i = 1; i <= NR; ++i) { printf "%8.3f%% %-20s\n", 100 * cnt[i] / totCnt, name[i] } print ""; }'
Assignment 2 (League of Legends, Pandas and matplotlib) - 25%
Assignment 3 (GreenHub again, all the covered materials) - 40%
One test at the end. - 20%
20220330 Week 6 (T2)
python basis recap
20220408 Week 7 (T3)
numpy
array: broadcasting
ex1: Build a function that receives two arrays as arguments and, solely based on their shape, outputs which one of them has more elements. These arrays do not need to have the same number of dimensions.
1 2 3 4 5 6 7 8 9 10 11
# from functools import reduce
defcompareSize(a,b): # sizeA = reduce(lambda x,y:x*y, a.shape) sizeA = np.array(a.shape).prod() # sizeB = reduce(lambda x,y:x*y, b.shape) sizeB = np.array(b.shape).prod() if sizeA > sizeB: return a else: return b
ex2: Write a function that takes as input a 2- dimensional NumPy array and transposes it without using the T attribute nor the transpose() function.
1 2 3 4 5 6 7
deftranspose(a): shapes = a.shape b = np.zeros((shapes[1],shapes[0])) for i inrange(shapes[0]): for j inrange(shapes[1]): b[j,i] = a[i,j] return b