Skip to content

Commit 462cbf1

Browse files
Add files via upload
1 parent cd6f6aa commit 462cbf1

38 files changed

+2049
-0
lines changed

week_1/chapter_21/makeProjectFS

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
# given a project name as argument
4+
# to this executable script,
5+
# bash instructs machine to make
6+
# a project filesystem for the project
7+
# in the project directory in one's
8+
# home directory.
9+
# if the project directory doesn't exist,
10+
# the machine will create it.
11+
12+
# initialization
13+
PROJPATH=$HOME/project
14+
PROJ=$1
15+
16+
mkdir -p "$PROJPATH"/"$PROJ"/data/raw
17+
mkdir -p "$PROJPATH"/"$PROJ"/data/processed
18+
19+
20+
mkdir -p "$PROJPATH"/"$PROJ"/figure/exploratory
21+
mkdir -p "$PROJPATH"/"$PROJ"/figure/final
22+
23+
24+
mkdir -p "$PROJPATH"/"$PROJ"/script/raw
25+
mkdir -p "$PROJPATH"/"$PROJ"/script/final
26+
27+
28+
mkdir -p "$PROJPATH"/"$PROJ"/text/readme
29+
mkdir -p "$PROJPATH"/"$PROJ"/text/text_of_analysis

week_1/chapter_23/getTop10IPHits

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
# initialization
4+
WEBLOG=$1
5+
6+
# given a path to a web log, returns the top 10 ip browsers
7+
# with the most hits within the time span covered by the web log,
8+
# as well as the hit count for each.
9+
gawk '{print $1}' "$WEBLOG"|# print out only column 1 of each record in weblog
10+
sort |# make identical ip addrs contiguous in output
11+
uniq -c |# for each group of contiguous ip addrs, print single instance plus count
12+
sort -n |# numerically sort, by default in ascending order
13+
tail

week_11_1/bash_interactive_script.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
3+
# an infinite loop. in this example, only the user's input of an empty string
4+
# --- essentially, hitting RETURN instead of entering a project name when
5+
# prompted --- will cause the program counter to leave the while loop.
6+
7+
while true; do # two statements on the same line are separated by a semicolon
8+
# here, "do" marks the start of the while loop's block of code,
9+
# while "done" below marks the end of it.
10+
11+
# at the top of the loop, we prompt the user for input from the command line
12+
# and assign the input string to the variable PROJ
13+
# -p means "prompt"
14+
# "read without -r will mangle backslashes." ---shellcheck
15+
read -pr "please give a project name: " PROJ
16+
17+
# next, we test whether the user's input satisfies the condition
18+
# to drop out of the infinite loop ---
19+
# in other words, has the user hit ENTER or RETURN,
20+
# (which generates the empty string),
21+
# instead of entering a project name?
22+
if [[ "$PROJ" == "" ]]
23+
then
24+
break # if the value of PROJ is the empty string, break out of while loop
25+
fi
26+
27+
# otherwise, the user has entered a project name.
28+
# here, we tell the bash interpreter to make a directory
29+
# in the working directory with the project name as its name
30+
mkdir "$PROJ"
31+
32+
done # end of while loop
33+
34+
echo "and now we're out of the loop!"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
DICT=/usr/share/dict/american-english
4+
5+
# extended grep, case-insensitive matches
6+
grep -Ei "^[BEHILOS]*$" $DICT
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# lines 4-7 to "linearize the sequences"
2+
# from https://www.biostars.org/p/17680/ by Frédéric Mahé
3+
# Creative Commons License
4+
zcat All-Unigene.fa.zip |# read fasta recordset in zipped file into memory
5+
sed -E -e 's/(^>.*$)/#\1#/' |# use egrep; enclose 1st line in each record with hash marks
6+
tr -d "\n" |# delete newline characters
7+
tr "#" "\n" |# replace hash symbols with newline characters
8+
sed -e '/^$/d' |# delete empty lines
9+
tr -d "\n" |# delete newline characters --- fasta record separator (>) is in place
10+
tr '>' "\n" |# replace > with newline characters
11+
sed -e 's/%/% /' |# insert a space after the percent sign. space is field separator.
12+
tr -s ' ' |# replace each sequence of a repeated character with a single occurrence.
13+
sed -e '/^$/d' # delete empty lines
14+

week_11_1/get_web_client_hits

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
http=$1
4+
5+
wget -O - $http | cat | gawk '{print $1}'| sort | uniq -c | sort -n | tail -1

week_11_1/makeProjFS

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
PROJPATH=$HOME/project
4+
PROJ=$1
5+
6+
mkdir -p "$PROJPATH"/"$PROJ"/data/raw
7+
mkdir -p "$PROJPATH"/"$PROJ"/data/processed
8+
9+
mkdir -p "$PROJPATH"/"$PROJ"/figure/exploratory
10+
mkdir -p "$PROJPATH"/"$PROJ"/figure/final
11+
12+
mkdir -p "$PROJPATH"/"$PROJ"/script/raw
13+
mkdir -p "$PROJPATH"/"$PROJ"/script/final
14+
15+
mkdir -p "$PROJPATH"/"$PROJ"/text/readme
16+
mkdir -p "$PROJPATH"/"$PROJ"/text/text_of_analysis

week_11_1/say_hello_world

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
echo "hello, world!"

week_11_1/spellchecker

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
# initialization
4+
DICT=/usr/share/dict/american-english
5+
MYDICT=~/my_dict
6+
7+
# to do: create my_dict is it doesn't exist
8+
9+
tr -d '[:punct:]' | # delete punctuation
10+
tr -s ' ' '\n' | # replacing spaces with newline characters
11+
sort | # alphabetically sort the words in the input string
12+
uniq| # remove multiple instances of the same word
13+
egrep -xvf $DICT | # compare entire lines, return complement, run against dictionary file
14+
tr '[:upper:]' '[:lower:]' | # replace all uppercase letters with lowercase equiv.
15+
egrep -xvf $DICT | # remove common words that are uppercased in input string
16+
egrep -xvf $MYDICT # remove entries in our personal dictionary file

week_11_3/dec_frac2bin_frac.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
''' chapter 42, Algorithm: Converting Positive Decimal Fractions Into Binary Fractions
2+
'''
3+
4+
def dec_frac2bin_frac(dec_frac: float, num_bin_digits: int = 10) -> str:
5+
''' Given a positive fractional float as input,
6+
returns its base-2 equivalent as a string.
7+
A second argument is the number of digits
8+
in the binary fraction. The default is 10
9+
digits.
10+
--------
11+
examples
12+
--------
13+
>>>dec_frac2bin_frac(.75)
14+
'0.1100000000'
15+
>>>dec_frac2bin_frac(.1, 20)
16+
'0.00011001100110011001'
17+
'''
18+
# garbage filters
19+
assert isinstance(dec_frac, float), \
20+
"dec_frac must be a positive fraction."
21+
# if dec_frac is a positive fraction,
22+
# the subtraction below yields zero.
23+
# we assert dec_frac must be a positive fraction
24+
# so we coerce that zero to a bool (False),
25+
# and negate it to True.
26+
# Other differences evaluate as False.
27+
assert not dec_frac - (dec_frac % 1), \
28+
"dec_frac must be a positive fraction."
29+
30+
# implementation of algorithm
31+
# initialization
32+
running_sum, bin_frac = 0, '0.'
33+
34+
# iterate through the positions of
35+
# the binary fraction. for each position,
36+
# calculate its base-10 value, then test
37+
# whether or not it contributes to the value
38+
# of the decimal fraction.
39+
for position in range(1, num_bin_digits+1):
40+
pos_value = 2**-(position)
41+
if (running_sum + pos_value) > dec_frac:
42+
# pos_value does NOT contribute
43+
# to decimal fraction
44+
bin_frac = bin_frac + '0'
45+
else:
46+
# pos_value contributes
47+
# to decimal fraction
48+
bin_frac = bin_frac + '1'
49+
# update running_sum
50+
running_sum = running_sum + pos_value
51+
52+
return bin_frac

0 commit comments

Comments
 (0)